public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Functions in gcc
@ 2001-11-09  9:03 Abdullah M.A Hussein
  2001-11-09  9:56 ` Rupert Wood
  0 siblings, 1 reply; 2+ messages in thread
From: Abdullah M.A Hussein @ 2001-11-09  9:03 UTC (permalink / raw)
  To: Gcc

Dear Sir

Is it possible to use a function in a C program and compile it using the
gcc compiler? If yes then how can I do that? The idea is that I want to
define a new function and use it in a main program. Can I use the
following function:

mpf_t   xyz (  mpf_t a, mpf_t b )
{
   mpf_t c;   mpf_init ( c );
   mpf_set ( c, a, b );
   return c;
}

Then in the main program do I write

   mpf_set ( w, xyz( u, v ) );

How to initialize the arguments of a function?

Is there a function for calculating the exponential value (that is e to
the power of x)?


Thanks and regards

Abdullah Hussein
ahussein@sharjah.ac.ae




^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Functions in gcc
  2001-11-09  9:03 Functions in gcc Abdullah M.A Hussein
@ 2001-11-09  9:56 ` Rupert Wood
  0 siblings, 0 replies; 2+ messages in thread
From: Rupert Wood @ 2001-11-09  9:56 UTC (permalink / raw)
  To: 'Abdullah M.A Hussein'; +Cc: gcc-help

Abdullah M.A Hussein wrote:

(I think there are some communication problems here. I'm not a linguist
myself so I don't dare criticise your English, but it isn't clear
exactly what you're asking.)

> Is it possible to use a function in a C program and compile it using
> the gcc compiler? If yes then how can I do that? The idea is that I
> want to define a new function and use it in a main program.

If you mean does GCC support C functions, then the answer is yes. GCC
implements the majority of the ANSI C standards.

My best guess is that you are really asking:

    Can I define functions in two separate C source files and link
    them together to make a single program?

and the answer to that is also yes. For example, hello.c

    #include <stdio.h>

    const char* world(void);

    int main(void)
    {
        printf("Hello, %s!\n", world());
        return 0;
    }

and world.c:

    const char* world(void)
    {
        return "world";
    }

Here hello.c uses the world() function in world.c. Note that the world()
function is declared in hello.c before it is used.

To compile this, either build all in one go:

    gcc hello.c world.c -o helloworld

This will compile each file separately and then link them to a
'helloworld' executable.

Alternatively, you may compile each file separately and then link them
together:

    gcc -c hello.c
    gcc -c world.c
    gcc hello.o world.o -o helloworld

Or a mixture of both:

    gcc -c hello.c
    gcc world.c hello.o -o helloworld

Note that by default, the 'compile only' flag, '-c', will generate an
object file with the same file prefix but with suffix 'o' instead of
'c'. You may override this with '-o' (for 'output') in the same way as
specifying the final executable name.

The advantage of compiling each separately is that you do not have to
rebuild all files if you only change one. If you have large,
compilicated projects then you may wish to investigate 'make' to help
automate compilation and linking of multiple files.

> Can I use the following function:
> 
> mpf_t   xyz (  mpf_t a, mpf_t b )
> {
>    mpf_t c;   mpf_init ( c );
>    mpf_set ( c, a, b );
>    return c;
> }

Yes, provided that you include the declarations of 'mpf_t', 'mpf_init'
and 'mpf_set' first in the file (or #include a header file which does).
If these are non-trivial types, then you may prefer to pass the data as
pointers. You may also wish to consider using C++ if you are using
complex types.

> Then in the main program do I write
> 
>    mpf_set ( w, xyz( u, v ) );

Yes, provided you have first included the line:

    mpf_t xyz (  mpf_t a, mpf_t b );

to declare the function, or #included a header file which contains this
declaration.

> How to initialize the arguments of a function?

I don't understand, unless you're asking how to declare the function -
how to specify the argument and return value types. ('initialize the
arguments' means something else in C++ which doesn't apply to C.) To
declare a function, you simply state the first line of the function
definition but terminate the line with a ';' rather than provide a
function body with '{' and '}'.

> Is there a function for calculating the exponential value 
> (that is e to the power of x)?

If you're using floating point, then you could use the 'pow' function
(although this is a C library issue rather than a compiler issue.) In a
POSIX environment, you do this by adding:

    #include <math.h>

to the top of your source and adding '-lm' to the link command line:

    gcc my_source_using_pow.c -o pow_program -lm

to link in the standard maths library. Try 'man pow'.

Hope that helps,
Rup.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-11-20  9:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-09  9:03 Functions in gcc Abdullah M.A Hussein
2001-11-09  9:56 ` Rupert Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).