Functions in gcc

Rupert Wood
Fri Nov 9 09:56:00 GMT 2001

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

    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

> 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,

More information about the Gcc-help mailing list