Linux下Makefile文件

Linux下Makefile文件
生成动态库(*.so)Makefile文件#Lib information
CURDIR =$(shell pwd)
LIBNAME :=libmyso.so
OUTDIR :=$(CURDIR)
OUTFILE :=$(OUTDIR)/$(LIBNAME)

SRC =node.cpp
OBJS =$(SRC:.cpp=.o)

#Complier information
CC = g++
INCPATH :=
LIBPATH :=
LIBS :=
COMOPT := -O2

all:$(OBJS)
    $(CC) -fPIC -shared -o $(OUTFILE) $(OBJS)
    
%.o:%.cpp
    $(CC) -c $(COMOPT) $(INCPATH) -o $@ $<

clean:
    rm -rf $(CURDIR)/*.o


生成静态库(*.a)Makefile文件#lib information
CURDIR =$(shell pwd)
LIBNAME :=libmylib.a
OUTDIR :=$(CURDIR)
OUTFILE :=$(OUTDIR)/$(LIBNAME)

SRC = Display.cpp
OBJS =$(SRC:.cpp=.o)

#complier information
CC=g++
INCPATH :=
LIBPATH :=
LIBS :=
COMOPT := -O2

all:$(OBJS)
    ar -r $(OUTFILE) $(OBJS)
    
%.o:%.cpp
    $(CC) -c $(COMOPT) $(INCPATH) -o $@ $<

clean:
    rm -rf $(CURDIR)/*.o


比较通用的Makefile文件#CPP Files Directory
vpath %.cpp ./../   
vpath %.cpp ./../Student

#Head Files Directory    
vpath %.h ./../Student/

#Get Makefile Current Directory
CURDIR =$(shell pwd)

SRC =Main.cpp \
     Student.cpp
     
SRC_OBJ =$(SRC:.cpp=.o)
CCFLAGS =-O2 -g -DUT_4_16
CXX =g++
CPPFLAGS =

#Load Static Library
LIBDIR =-L $(CURDIR)/../StaticLib  -lmylib $(CURDIR)/../DynamicLib/libmyso.so
#Load Library Head Files
INCDIR =-I $(CURDIR)/../StaticLib  -I $(CURDIR)/../DynamicLib

.PHONY :all
all :edit run

.PHONY :edit
edit: $(SRC_OBJ)
$(SRC_OBJ):%.o :%.cpp
    $(CXX) $(INCDIR) -c $(CCFLAGS) $(CPPFLAGS) $< -o $@
   
.PHONY :run
run: $(SRC_OBJ)
    $(CXX) $(INCDIR) $(SRC_OBJ) $(LIBDIR) -o Main
   
.PHONY :clean
clean:
    rm $(SRC_OBJ)

最简单的Makefile文件#假设只有一个文件Main.cpp生成Makefile文件
Main:Main.o
    g++ -o $@ $^
%.o:%.cpp %.h
    g++ -c $@ $>

不需要手工添加各个文件的依赖关系,使用vpath自动搜索文件!
测试工程:
http://www.cppblog.com/Files/bujiwu/Linux.rar


你可能感兴趣的:(Linux下Makefile文件)