Lesson 11

Dates: 4/10/2014
Application compilation and installation on Linux
Linux for Engineering and IT Applications


Exercises: code compilation and the libraries


  • Single code compilation.
    By following the instructions below, download and compile a C code, single.c, that computes the scalar product of two vectors:
    mkdir single_code
    cd single_code
    wget http://linuxcourse.rutgers.edu/lessons/application_compilation/downloads/single.c
    gcc -o app.x single.c
    ./app.x
    
    When prompted for the input parameters, type in the dimension of the vectors and the value of their components, for example:
    Vector dimension n=2
    Input 2 coordinate x: 3 5
    Input 2 coordinate y: 1 6
    

  • Compile the code with a static library.
    This time, the original single.c file has been devided into 3 separate modules: main.c, Scalar_Product.h, and Scalar_Product.c.
    Download the archive and untar it into a new directory:
    cd 
    wget http://linuxcourse.rutgers.edu/lessons/application_compilation/downloads/source_code.tgz
    mkdir code_static
    cp source_code.tgz code_static
    cd code_static
    tar -zxvf source_code.tgz 
    
    Build the static library:
    gcc -c Scalar_Product.c
    ar -cru libScalar_Product.a Scalar_Product.o
    ranlib libScalar_Product.a
    
    Verify the content of library libScalar_Product.a:
    ar -t libScalar_Product.a 
    
    Compile the main code and link with the library:
    gcc -c main.c
    gcc -o app.x main.o -L. -lScalar_Product
    
    Make sure the compiled code runs fine:
    ./app.x
    

  • Compile the code with a shared library.
    Create a new directory and untar the source code in it:
    cd .. 
    mkdir code_so
    cp source_code.tgz code_so
    cd code_so
    tar -zxvf source_code.tgz
    
    Build the shared library:
    gcc -fPIC -c Scalar_Product.c
    gcc -shared Scalar_Product.o -o libScalar_Product.so
    
    Compile the main code and link with the shared library:
    gcc -c main.c
    gcc -o app.x main.o -L. -lScalar_Product
    
    Verify that app.x executable requires libScalar_Product.so library:
    ldd app.x
    
    It should show that the shared library is not found, libScalar_Product.so => not found. Try running the code
    ./app.x
    
    It should fail to run.
    Set LD_LIBRARY_PATH environment variable to include the current working directory:
    export LD_LIBRARY_PATH=./
    
    Check if the loader finds the library and try running the code again:
    ldd app.x
    ./app.x
    



  • Take me to the Course Website