Lesson 11

Dates: 6/25/2014
Application compilation and installation on Linux
Linux System Administration


Makefile with suffix rules


  • When there is need to compile many *.c codes into *.o binaries, the suffix rule can be implemented to avoid many target lines in the Makefile.
  • Makefile with the suffix rule:
    SLIB = libScalar_Product.a
    APP=app.x
    CC = gcc
    CFLAGS = -O3
    LIBPATH = .
    INSTPATH = /usr/local
    
    
    $(APP): main.o $(SLIB) 
            $(CC) $(CFLAGS) -o $@ main.o -L$(LIBPATH) -lScalar_Product
    
    .c.o: 
            @echo "Compiling" $< 
            $(CC) -c $<
    
    $(SLIB): Scalar_Product.o
            ar -cru $@ $<
            ranlib $@ 
    
    clean:
            -rm -f *.o *.a $(APP) 
    
    install: 
            @cp -p $(APP) $(INSTPATH)/bin ;\
            chown root:root $(INSTPATH)/bin/$(APP) ;\
            cp -p $(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)"
    

  • Special symbols:
    $@ name of current target.
    $< name of current dependency.
    $* name of current dependency without extension.
    $?  list of dependencies changed more recently than current target.




  • Take me to the Course Website