下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:65Please respect copyright.PENANAt1GYaVkGJN
all: $(TARGET)
这个总的target由多个.o文件link而成:65Please respect copyright.PENANAfZvqHLOehn
$(TARGET) : $(OBJECTS)65Please respect copyright.PENANApf7kYPq37l
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)65Please respect copyright.PENANARAdt4Q5WXx
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:65Please respect copyright.PENANAnkQcMNDplL
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h65Please respect copyright.PENANA0WpTMEfZEe
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc65Please respect copyright.PENANAkDsbTbIjNF
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g65Please respect copyright.PENANACWxWxzKxzw
65Please respect copyright.PENANA0RT1pzG4vr
TARGET = buda65Please respect copyright.PENANA3B2dQCiVoK
OBJ_DIR = ./debug/obj65Please respect copyright.PENANAqg4hWV68oP
SRC_DIR = ./src65Please respect copyright.PENANAylRerLEind
H_DIR = ./includes65Please respect copyright.PENANAswXlRvzKL9
65Please respect copyright.PENANAcEpx8S0OVq
SRC = $(wildcard $(SRC_DIR)/*.cpp)65Please respect copyright.PENANAgtGI2iYKFO
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))65Please respect copyright.PENANAUKy2zE54J4
65Please respect copyright.PENANAiLMRIaYk3n
VPATH = $(SRC_DIR)65Please respect copyright.PENANAobUBtpXEwp
vpath %.o $(OBJ_DIR)65Please respect copyright.PENANANi22qCanyZ
65Please respect copyright.PENANAR9WleWjoIJ
all: $(TARGET)65Please respect copyright.PENANAKj32MwWf8O
65Please respect copyright.PENANAUv5lIkoY3q
$(TARGET) : $(OBJECTS)65Please respect copyright.PENANAJBFUnHgMCn
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)65Please respect copyright.PENANANYdGxajUsd
echo "make succeed"65Please respect copyright.PENANAsXSuRrWIyL
65Please respect copyright.PENANAujHdyuvrrt
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h65Please respect copyright.PENANAijGhXIJyRC
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:65Please respect copyright.PENANAajbXrr9qW1
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:65Please respect copyright.PENANAM2G4N2Am0H
make clean65Please respect copyright.PENANAGhfUUBoqoX
make65Please respect copyright.PENANAAifzT6XGTT
65Please respect copyright.PENANA0D9h5B22P9
65Please respect copyright.PENANAD3za2yjmX2
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507665Please respect copyright.PENANAuzzgWdoZOs
https://blog.csdn.net/momodosky/article/details/11985798065Please respect copyright.PENANAFyI7O1y4Nz
https://makefiletutorial.com/65Please respect copyright.PENANA4TZaUzGZDO
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]65Please respect copyright.PENANA0aGG01TUuy
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。65Please respect copyright.PENANAlvQVgJo1fG
65Please respect copyright.PENANALmGg2rYBUH