makefile writing template

makefile writing template

When compiling code on Linux, in order to simplify the handwriting of g++ commands during each compilation and use makefile files to reduce the workload, a simple makefile template is recorded here. You can simply replace the path and name when using it.

#编译工具
CC=g++ 
#编译选项
CFLAGES=-O2 -Wall -std=c++0x

 #头文件路径
INCLUDE_PATH=-I../include

#库文件路径
LIB_PATH=-L./ -l(动态库名称,需要手动替换或删除这段) -lrt -lpthread -ldl -lm 

#obj文件,遍历当前目录下所有cpp,生成.o
OBJ=$(patsubst %.cpp,%.o,$(wildcard *.cpp))
#obj文件,遍历当前目录下所有c,生成.o
OBJC=$(patsubst %.c,%.o,$(wildcard *.c))

#生成的二进制文件名称
Target := demo

# $@表示目标,这里是$(Target)
# $^表示代表所有的依赖对象,这里是$(OBJ) $(OBJC)
$(Target) : $(OBJ) $(OBJC)
	$(CC) -o $@ $^ $(CFLAGES) $(INCLUDE_PATH) $(LIB_PATH)

#OBJ生成规则
%.o : %.c
	$(CC) -c $(CFLAGES) $(INCLUDE_PATH) $< -o $@
%.o : %.cpp
	$(CC) -c $(CFLAGES) $(INCLUDE_PATH) $< -o $@

#make clean命令清除
.PHONY clean:
	rm -rf $(OBJ) $(OBJC) $(Target)

Guess you like

Origin blog.csdn.net/zuolj/article/details/124692472