compiling a multi source C program
compiling a multi-source C program
multi-source = multi file
having all the source in a single file is limiting :
- As the file grows, compilation time tends to grow, and for each little change, the whole program has to be re-compiled.
- It is very hard, if not impossible, that several people will work on the same project together in this manner.
- Managing your code becomes harder. Backing out erroneous changes becomes nearly impossible.
The solution is to split the source code into multiple files, each containing a set of closely-related functions (or, in C++, all the source code for a single class).
There are two possible ways to compile a multi-source C program.
1. use a single command line to compile all the files.
cc main.c wmlib.c somelib.c -o wmtst
the compiler will compile each of the given files separately, and then link them all together to one executable file named .
Two comments :
- If we define a function (or a variable) in one file, and try to access them from a second file, we need to declare them as external symbols in that second file.
This is done using the C"extern"keyword. - The order of presenting the source files on the command line may be altered.
The compiler (actually, the linker) will know how to take the relevant code from each file into the final program
2. compile and link seperatly
The problem with this way of compilation is that if we make a change in one of the source files, all of them will be re-compiled when we run the compiler again.
In order to overcome this limitation, we could divide the compilation process into two phases - compiling, and linking. Lets first see how this is done, and then explain:
gcc -c main.cc
gcc -c wmlib.c
gcc -c somelib.c
gcc main.o wmlib.o somelib.o -o wmtst
using the “-c” flag that tells the compiler only to create an object file, and not to generate a final executable file just yet.
“object file” : same filenames, but with a “.o” suffix.
The object file contains the code for the source file in machine language, but with some unresolved symbols.
For example, the “main.o” file refers to a symbol named “func_a”, which is a function defined in file “wmlib.c”.
link the 3 object files into one program.
The linker (which is invoked by the compiler now) takes all the symbols from the 3 object files, and links them together - it makes sure that when “func_a” is invoked from the code in object file “main.o”, the function code in object file “a.o” gets executed. Further more, the linker also links the standard C library into the program, in this case, to resolve the “printf” symbol properly.
3. re-compile partially after
after a change in wmlib.c
gcc -c wmlib.c
gcc main.o wmlib.o somelib.o -o wmtst
(Much) faster ; normally the link phase is much faster than the compilation phase. This is especially true when doing optimizations, since that step is done before linking.
in a case of having few tens of files each containing a few hundred lines of source-code, the time saving is significant; not to mention even larger projects.