UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

14.7.  Compiling and linking step by step

So far, we have applied a compilation process of one step. However it can be divided into two steps compiling and linking. The compiling step creates the assembly code and converts it into binary file .o what is known as the object code. To execute this step you must use -c option in the compilation command.

$ gcc -c filename.c

Usually a C program is made up of several .c files. Then to execute the compilation step you will need to compile each .c file separately resulting an .o file by each compiled file. Take each .c file obtainded in the previous section and apply them the compilation step, then verify that you have obtained an .o by each compiled file.

Now you are ready to generate an executable program. Execute the linking step that links your program with other programs that contain functions that your program uses. To create the executable program you must use the gcc compiler but adding -o option and providing the object files resulting of compilation step

$ gcc -o program filename1.o filename2.o filename3.o

Execute the program and verify the results. Compiling the programs separately is sueful e.g., when changes have been made in a .c file you only will need compile this file and then you will need link it with the others .o files. Try to include a change into one of your .c files then compile the file with -c option. Link the .o file with the other object files by compiling with -o option and execute the program. Do you consider useful this option? Do you apply it to complex programs?