Lesson 11

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


Multiple Makefiles. Software installation.


Compilation and installation of software from its source code usually involves 4 steps: download and untar the archive, configuration, compilation, and installation. Multiple Makefiles may be involved if the software source is complex.

Exercise
  • Download the tar archive and extract the files:
    wget http://linuxcourse.rutgers.edu/lessons/application_compilation/downloads/Project.tgz
    tar -zxvf Project.tgz
    
    Check out the directory tree of Project and the files contained there:
    apt-get install tree
    tree Project
    
    You should notice two Makefiles: one in the root of directory Project and the other in subdirectory src1.
  • Configuration:
    cd Project 
    ./configure
    
    This sets the environment for the Makefiles.
  • Compilation:
    make
    
    The command make above processes Makefile in directrory Project:
    SRC = src1
    
    all:
            (cd $(SRC); $(MAKE) )
    
    clean:
            (cd $(SRC); $(MAKE) clean ) 
    
    install: 
            (cd $(SRC); $(MAKE) install ) 
    
    uninstall:
            (cd $(SRC); $(MAKE) uninstall )
    
    This Makefile contains phony targets, envoking make in subdirectory src1, which compiles the source codes, *.c, builds the library, the application binary and places them into subdirectories lib and bin.
    Makefile in directory Project/src1:
    SLIB = libScalar_Product.a
    APP=app.x
    CC = gcc
    CFLAGS = -O3
    LIBPATH = ../lib
    INCPATH = ../include
    BIN = ../bin
    INSTPATH = /usr/local
    OBJS = main.o
    
    $(APP): main.o $(SLIB) 
    	$(CC) $(CFLAGS) -o $@ main.o -L$(LIBPATH) -lScalar_Product
    	-cp $@ $(BIN) 
    
    .c.o: 
    	@echo "Compiling" $< 
    	$(CC) -c $< -I$(INCPATH)
    
    $(SLIB): Scalar_Product.o
    	ar -cru $@ $<
    	ranlib $@ 
    	-cp $@ $(LIBPATH)
    
    $(OBJS): $(INCPATH)/Scalar_Product.h 
    
    clean:
    	-rm -f *.o $(LIBPATH)/*.a *.a $(BIN)/$(APP) $(APP) 
    
    install: 
    	@cp -p $(BIN)/$(APP) $(INSTPATH)/bin ;\
    	chown root:root $(INSTPATH)/bin/$(APP) ;\
    	cp -p $(LIBPATH)/$(SLIB) $(INSTPATH)/lib ;\
    	chown root:root $(INSTPATH)/lib/$(SLIB) ;\
    	echo "Install $(APP) and $(SLIB) into $(INSTPATH)" 
              
    uninstall:
    	-@rm -f $(INSTPATH)/bin/$(APP)
    	-@rm -f $(INSTPATH)/lib/$(SLIB)
    	@echo "Removed $(APP) and $(SLIB) from $(INSTPATH)"
    

    After the compilation procedure completes, there will be binary, object files, sources, in the library in the subdirectoies of Project:
    Directory
    Files in the directory
    Project/
    Makefile, configure
    Project/src1
    Makefile, main.c, Scalar_Product.c, main.o, Scalar_Product.o, app.x, libScalar_Product.a
    Project/bin
    app.x
    Project/include
    Scalar_Product.h
    Project/lib
    libScalar_Product.a

  • Installation of the binaries into the directories accessible to all users:
    sudo make install
    
    This would copy file app.x into the conventional directory for software binaries, /usr/local/bin and set it executable for all users on the system.
  • Try running command
    app.x
    
    It should fail with error message app.x: command not found.
  • The PATH environment variable needs to be modified to include /usr/local/bin:
    export PATH=$PATH:/usr/local/bin
    
    The above can be placed either into individual users's .bashrc or /etc/profile for all users. The application now should run find by command
    app.x
    
  • The installation also copies the library into /usr/local/lib.
  • If the compilation and installation of the code involved shared object libraries, *.so, and MAN pages, they would be placed into /usr/local/lib and /usr/local/man, accordingly. Access to the MAN pages and shared libraries can be set either through the environment variables
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
    export MANPATH=$MANPATH:/usr/local/man
    
    or configuration files /etc/ld.so.conf and /etc/man.config



  • Take me to the Course Website