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
main function. It contains a reference to the external
hello, but the corresponding memory address is left
undefined in the object file at this stage (it will be filled in later
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
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
statements in 'main.c' and 'hello_fn.c'.