下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:67Please respect copyright.PENANAtHr5oB0Dca
all: $(TARGET)
这个总的target由多个.o文件link而成:67Please respect copyright.PENANAUQfCU6fxYR
$(TARGET) : $(OBJECTS)67Please respect copyright.PENANAwGpNRrM2IV
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)67Please respect copyright.PENANAygcekpkyUS
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:67Please respect copyright.PENANAOS3ZYFKMJG
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h67Please respect copyright.PENANAISVyF7FuqM
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc67Please respect copyright.PENANAOJXIXSfUaf
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g67Please respect copyright.PENANA6nZzKyYOlg
67Please respect copyright.PENANAWrBVR4t1WS
TARGET = buda67Please respect copyright.PENANAuX0vdrVWr8
OBJ_DIR = ./debug/obj67Please respect copyright.PENANAhFF7Ooa7Nh
SRC_DIR = ./src67Please respect copyright.PENANAVRUWfFpLVf
H_DIR = ./includes67Please respect copyright.PENANAwnPgoahEYH
67Please respect copyright.PENANAOFfqPZ5YSU
SRC = $(wildcard $(SRC_DIR)/*.cpp)67Please respect copyright.PENANA9OC4y5Q0QA
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))67Please respect copyright.PENANAr3Swdgl1cP
67Please respect copyright.PENANA6iDSfbGz24
VPATH = $(SRC_DIR)67Please respect copyright.PENANAcX0JiQQ1h1
vpath %.o $(OBJ_DIR)67Please respect copyright.PENANAvAbwIsmTBb
67Please respect copyright.PENANAkjbpx6C6t9
all: $(TARGET)67Please respect copyright.PENANAdk84VMUodJ
67Please respect copyright.PENANAPFkaojXf2e
$(TARGET) : $(OBJECTS)67Please respect copyright.PENANApGBLAup3Nn
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)67Please respect copyright.PENANAJDwDZiBy53
echo "make succeed"67Please respect copyright.PENANAicppYqsoz0
67Please respect copyright.PENANAODkGflEUNZ
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h67Please respect copyright.PENANA3SHEEKv1of
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:67Please respect copyright.PENANAXJC3WwukbS
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:67Please respect copyright.PENANAdiuQHNJh4w
make clean67Please respect copyright.PENANAkYEqDGqP4f
make67Please respect copyright.PENANAhvi3RgrzwD
67Please respect copyright.PENANAFsWvyFPVDF
67Please respect copyright.PENANAiBe1Hi4VUA
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507667Please respect copyright.PENANAU5EkqLGiAs
https://blog.csdn.net/momodosky/article/details/11985798067Please respect copyright.PENANAMfpiUd9ZCK
https://makefiletutorial.com/67Please respect copyright.PENANAPS17YsjcZR
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.com67Please respect copyright.PENANASPWGpqjW4r
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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)以便拉您进群。67Please respect copyright.PENANAMVWlK6RfWU
67Please respect copyright.PENANAyo3ELG8NsH