下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:47Please respect copyright.PENANAX9v1QeMRje
all: $(TARGET)
这个总的target由多个.o文件link而成:47Please respect copyright.PENANAJ4A17e2F5B
$(TARGET) : $(OBJECTS)47Please respect copyright.PENANACTs6LgnHio
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)47Please respect copyright.PENANASuDCLAIwwf
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:47Please respect copyright.PENANAtIhJTUDgz3
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h47Please respect copyright.PENANAhjsOIgQC7e
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc47Please respect copyright.PENANAtB2Q1LAuua
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g47Please respect copyright.PENANAJ0uzC18ISE
47Please respect copyright.PENANAUydLB4OEXt
TARGET = buda47Please respect copyright.PENANAvWVveUx45w
OBJ_DIR = ./debug/obj47Please respect copyright.PENANAveXZwLwuWv
SRC_DIR = ./src47Please respect copyright.PENANAadBOKPlKSC
H_DIR = ./includes47Please respect copyright.PENANA4G1Qa5Fkb4
47Please respect copyright.PENANAj7Bbv3pJeu
SRC = $(wildcard $(SRC_DIR)/*.cpp)47Please respect copyright.PENANACZLy3KLyct
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))47Please respect copyright.PENANAOrS4dkxOF3
47Please respect copyright.PENANAhbJxdzVSwb
VPATH = $(SRC_DIR)47Please respect copyright.PENANAl8APOZ1c4g
vpath %.o $(OBJ_DIR)47Please respect copyright.PENANABcQeREMuXi
47Please respect copyright.PENANAqHUuXUekvY
all: $(TARGET)47Please respect copyright.PENANAmY0RoawAKh
47Please respect copyright.PENANA2jYPOAo1KB
$(TARGET) : $(OBJECTS)47Please respect copyright.PENANAOKCQgsWf4l
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)47Please respect copyright.PENANAgrHJboBAKr
echo "make succeed"47Please respect copyright.PENANAkVgtnpQFX5
47Please respect copyright.PENANAIzmcSNo2Tu
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h47Please respect copyright.PENANA7fkyK3bmKF
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:47Please respect copyright.PENANARc4OCG3dKS
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:47Please respect copyright.PENANAuRczZRvJ8z
make clean47Please respect copyright.PENANAAESGMVmRFd
make47Please respect copyright.PENANAszmmw6EDSo
47Please respect copyright.PENANAKtlJOjG3in
47Please respect copyright.PENANAshhLLsVNxj
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507647Please respect copyright.PENANACaQS7SmtoM
https://blog.csdn.net/momodosky/article/details/11985798047Please respect copyright.PENANATuDcNnFc62
https://makefiletutorial.com/47Please respect copyright.PENANA2Bjjlkh6h9
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.com47Please respect copyright.PENANAVUJ2zOJNbG
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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)以便拉您进群。47Please respect copyright.PENANA8hErSNNBRT
47Please respect copyright.PENANApEuWU1k55c