下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:72Please respect copyright.PENANATbknjhnukQ
all: $(TARGET)
这个总的target由多个.o文件link而成:72Please respect copyright.PENANAnvWKQWrH1N
$(TARGET) : $(OBJECTS)72Please respect copyright.PENANAeF2uPTCmq8
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)72Please respect copyright.PENANATc7aLVYHmk
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:72Please respect copyright.PENANALJEDtAd6iw
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h72Please respect copyright.PENANAL0uE44NCco
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc72Please respect copyright.PENANA4l8tDlpZlH
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g72Please respect copyright.PENANA4ec4g2lsxX
72Please respect copyright.PENANAQbq4QYxOEZ
TARGET = buda72Please respect copyright.PENANAFYow56GIHA
OBJ_DIR = ./debug/obj72Please respect copyright.PENANAwNdP7Tgb6t
SRC_DIR = ./src72Please respect copyright.PENANAAKSknB4U42
H_DIR = ./includes72Please respect copyright.PENANASdMUMqX2Tz
72Please respect copyright.PENANAyfVI3wxoD2
SRC = $(wildcard $(SRC_DIR)/*.cpp)72Please respect copyright.PENANApoiXfPy04j
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))72Please respect copyright.PENANASZ8Om2svcc
72Please respect copyright.PENANAwHPuJtIs31
VPATH = $(SRC_DIR)72Please respect copyright.PENANAAmN3pn00T0
vpath %.o $(OBJ_DIR)72Please respect copyright.PENANAZTqS3actqB
72Please respect copyright.PENANAmqb3YBRwwd
all: $(TARGET)72Please respect copyright.PENANA6crgPmugEi
72Please respect copyright.PENANAGHMtJwpYai
$(TARGET) : $(OBJECTS)72Please respect copyright.PENANAyhwCUG3OvX
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)72Please respect copyright.PENANAZ6eGbafxKV
echo "make succeed"72Please respect copyright.PENANAEzoJKBh5MJ
72Please respect copyright.PENANAyWGiwWJnoG
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h72Please respect copyright.PENANAsrDFaT1k2Y
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:72Please respect copyright.PENANADGLGzaBqAG
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:72Please respect copyright.PENANArKKcIyBv7j
make clean72Please respect copyright.PENANAD5f34E0Zv5
make72Please respect copyright.PENANACmqdrzYZWP
72Please respect copyright.PENANAh67apD62gB
72Please respect copyright.PENANABadM4SVgnp
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507672Please respect copyright.PENANAKXmxgUrI27
https://blog.csdn.net/momodosky/article/details/11985798072Please respect copyright.PENANAIAbAiAc0pe
https://makefiletutorial.com/72Please respect copyright.PENANAr4a5cpBw1e
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]72Please respect copyright.PENANA6jIeagSEDh
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。72Please respect copyright.PENANAIngveSMKgy
72Please respect copyright.PENANAYEt91BDO56