下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:33Please respect copyright.PENANAWuTYvmcZxE
all: $(TARGET)
这个总的target由多个.o文件link而成:33Please respect copyright.PENANAWXq6jdpTH5
$(TARGET) : $(OBJECTS)33Please respect copyright.PENANAgyEyB4Tfmv
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)33Please respect copyright.PENANAmQPIJJhfiN
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:33Please respect copyright.PENANABfRumL74DU
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h33Please respect copyright.PENANA29Iyrd0CKh
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc33Please respect copyright.PENANAjjOQKq3dcY
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g33Please respect copyright.PENANA0SENIjjSVM
33Please respect copyright.PENANAwhnDwPii4K
TARGET = buda33Please respect copyright.PENANAd6dDdvehPj
OBJ_DIR = ./debug/obj33Please respect copyright.PENANA94Wbf3DgsD
SRC_DIR = ./src33Please respect copyright.PENANA4Mz01Wp5ww
H_DIR = ./includes33Please respect copyright.PENANA6trUoVjZwX
33Please respect copyright.PENANAwnN3pI5Xvx
SRC = $(wildcard $(SRC_DIR)/*.cpp)33Please respect copyright.PENANAcC73n00nHS
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))33Please respect copyright.PENANAlsdt0ZupkZ
33Please respect copyright.PENANADie6NKa4Nd
VPATH = $(SRC_DIR)33Please respect copyright.PENANAMCy963cJkM
vpath %.o $(OBJ_DIR)33Please respect copyright.PENANADi3gyPhllA
33Please respect copyright.PENANANJ70YPduxi
all: $(TARGET)33Please respect copyright.PENANA5UeO0ZGwu8
33Please respect copyright.PENANAF3xvy5CaKO
$(TARGET) : $(OBJECTS)33Please respect copyright.PENANAie7sFfC84J
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)33Please respect copyright.PENANAPvPKDrSdAG
echo "make succeed"33Please respect copyright.PENANAGMfEF0es16
33Please respect copyright.PENANAy03xWcsHil
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h33Please respect copyright.PENANACx3Qcw1bS9
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:33Please respect copyright.PENANArAlMDRJ2rm
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:33Please respect copyright.PENANADiuq4Gl6by
make clean33Please respect copyright.PENANAtnxcsT4zsC
make33Please respect copyright.PENANA7j4XS54KtE
33Please respect copyright.PENANAN1dlqdjPO8
33Please respect copyright.PENANAbakXAaKSBS
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507633Please respect copyright.PENANA3zXPEuTuXc
https://blog.csdn.net/momodosky/article/details/11985798033Please respect copyright.PENANAibXWpBJfXZ
https://makefiletutorial.com/33Please respect copyright.PENANAIxAs6SZB5v
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: si.jinmin@gmail.com33Please respect copyright.PENANAFvoYlSIwV6
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: si.jinmin@gmail.com
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。33Please respect copyright.PENANANYY9L9QNbj
33Please respect copyright.PENANAvtp1xqNTzr