一 生成动态链接库的模板:
1 ####################### 2 # Makefile 3 ####################### 4 5 # compile and lib parameter 6 CC := g++ 7 LIBS := 8 LDFLAGS := 9 DEFINES := 10 INCLUDE := -I. 11 CFLAGS := 12 CXXFLAGS:= 13 14 # link parameter 15 #LIB := libfunc.so 16 LIB := libfunc.a 17 18 #link 19 $(LIB):func.o 20 $(CC) -shared -o -fPIC -o $@ $^ 21 #compile 22 func.o:thread.c 23 $(CC) -c -fPIC $^ -o $@ 24 25 # clean 26 clean: 27 rm -fr *.o
二 编译可执行程序的模板:
1 ########################################### 2 #Makefile for simple programs 3 ########################################### 4 INC= 5 LIB= -lpthread 6 7 CC=gcc 8 CC_FLAG=-Wall 9 10 PRG=threadtest 11 #OBJ=thread.o CThreadPool.o CThread.o CWorkerThread.o threadpooltest.o 12 OBJ=thread.o 13 14 $(PRG):$(OBJ) 15 $(CC) $(INC) -o $@ $(OBJ) $(LIB) 16 17 .SUFFIXES: .c .o .cpp 18 .cpp.o: 19 $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o 20 21 .PRONY:clean 22 clean: 23 @echo "Removing linked and compiled files......" 24 rm -f $(OBJ) $(PRG)
测试结果:
1 参考文档: https://blog.csdn.net/lzx_bupt/article/details/7988192
2 代码所在路径: https://github.com/DyLanCao/iApply/tree/master/c_example/makefile