Lesson 10

Dates: 4/10/2019
Application compilation and installation on Linux
Linux System Administration


Compilation Procedure


  • Suppose one needs to compile a single source C program, code.c, into an executable binary, code.x
    gcc -o code.x code.c
    

  • This implies the following implicit compilation steps that also can be done by parts:
    1 Preprocessing
    gcc   -E   code.c   -o   code.i
    #define, #ifdef, #include are processed
    2 Compiling
    gcc   -S   code.i   -o   code.s
    Assembly file, code.s
    3 Asembling
    gcc   -c   code.s   -o   code.o
    Creates object file in the machine language
    4 Linking
    gcc     code.o   -o   code.x
    Code is linked with the other .o object files and libraries into an executable binary

  • If the source code consists of several files and needs libraries to be linked with, the compilation procedure can be done either in one step:
    gcc code.c extra_1.c extra_2.c -o code.x -lextra
    
    or multiple steps:
    gcc -c code.c
    gcc -c extra_1.c
    gcc -c extra_2.c
    gcc -o code.x code.o extra_1.o extra_2.o -lextra
    
    If the header files *.h and the libraries *.a are located in the directories different from the source *.c files, for example ../include and ../lib, their location needs to be specified as follows:
    gcc  -o code.x code.o extra_1.o extra_2.o -I../include -L../lib -lextra
    

  • Exercise
    Download code.c
    wget http://linuxcourse.rutgers.edu/lessons/application_compilation/downloads/code.c
    
    Look at the content of the source file:
    less code.c
    
    Compile the source file in the four stages - Preprocessing, Compiling, Assembling, and Linking:
    gcc -E code.c -o  code.i
    gcc -S code.i -o  code.s
    gcc -c code.s -o  code.o
    gcc    code.o -o  code.x -lm
    
    Check the file type of the compilation products:
    file code.i
    file code.s
    file code.o
    file code.x
    
    Browse the content of the two ASCII files, code.i and code.s
    less code.i
    less code.s
    
    Run the executable, code.x:
    ./code.x
    



  • Take me to the Course Website