下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:185Please respect copyright.PENANAJDZUfKfcOH
all: $(TARGET)
这个总的target由多个.o文件link而成:185Please respect copyright.PENANAnWd8L7LiWF
$(TARGET) : $(OBJECTS)185Please respect copyright.PENANAhvMkipPPPR
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)185Please respect copyright.PENANA0gFYrCZqAo
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:185Please respect copyright.PENANAGTo9fbLqxY
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h185Please respect copyright.PENANAoR4bwB96rr
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc185Please respect copyright.PENANAFfj3imysu0
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g185Please respect copyright.PENANAlZ6ofBt7P6
185Please respect copyright.PENANAKrCmXT02p7
TARGET = buda185Please respect copyright.PENANAv2Mxcxbsbd
OBJ_DIR = ./debug/obj185Please respect copyright.PENANABWwtphHDRi
SRC_DIR = ./src185Please respect copyright.PENANAVhdlwaRaZl
H_DIR = ./includes185Please respect copyright.PENANAYARWMm0srS
185Please respect copyright.PENANAcs5CaqX14z
SRC = $(wildcard $(SRC_DIR)/*.cpp)185Please respect copyright.PENANAJfgn7WPfKp
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))185Please respect copyright.PENANApa4cZ2UIgt
185Please respect copyright.PENANAgkPvQ3IfyS
VPATH = $(SRC_DIR)185Please respect copyright.PENANA0hPiXEufZo
vpath %.o $(OBJ_DIR)185Please respect copyright.PENANASfuIa36O9j
185Please respect copyright.PENANAfj7Dx3N2v3
all: $(TARGET)185Please respect copyright.PENANAK0p0wa7TM0
185Please respect copyright.PENANATGh1ud5DhS
$(TARGET) : $(OBJECTS)185Please respect copyright.PENANAQU5HMk4qHg
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)185Please respect copyright.PENANAeSLhRTxkyv
echo "make succeed"185Please respect copyright.PENANA78ifr7jLPw
185Please respect copyright.PENANAN6sH0fYxrZ
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h185Please respect copyright.PENANABcX1A8wgxz
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:185Please respect copyright.PENANAMy46dpYy80
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:185Please respect copyright.PENANAgCMgkUzzjD
make clean185Please respect copyright.PENANAm9Ok4h49Sx
make185Please respect copyright.PENANA47KtSjIEOD
185Please respect copyright.PENANAG8gS1dI2TE
185Please respect copyright.PENANAcgrMlVaTqI
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/112055076185Please respect copyright.PENANARKUwXL7tnX
https://blog.csdn.net/momodosky/article/details/119857980185Please respect copyright.PENANAZbTwIAGJgW
https://makefiletutorial.com/185Please respect copyright.PENANAEdwaUTgpKA
https://makefiletutorial.com/#makefile-cookbook
Section Y. Contacts Me
If you found any errors or have any suggestions for this article, please let me know, my wechat: si_jinmin, my email: [email protected]185Please respect copyright.PENANANwYA06hr8b
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。185Please respect copyright.PENANAjsr7AY8Hcr
185Please respect copyright.PENANAgM16pVSk7L