Next: , Up: Compiling files independently   [Contents][Index]


2.4.1 Creating object files from source files

The command-line option -c is used to compile a source file to an object file. For example, the following command will compile the source file main.c to an object file:

$ gcc -Wall -c main.c

This produces an object file main.o containing the machine code for the main function. It contains a reference to the external function hello, but the corresponding memory address is left undefined in the object file at this stage (it will be filled in later by linking).

The corresponding command for compiling the hello function in the source file hello_fn.c is:

$ gcc -Wall -c hello_fn.c

This produces the object file hello_fn.o.

Note that there is no need to use the option -o to specify the name of the output file in this case. When compiling with -c the compiler automatically creates an object file whose name is the same as the source file, but with .o instead of the original extension.

There is no need to put the header file hello.h on the command line, since it is automatically included by the #include statements in main.c and hello_fn.c.