下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:36Please respect copyright.PENANAH6MT3lN4KO
all: $(TARGET)
这个总的target由多个.o文件link而成:36Please respect copyright.PENANAt2bF8pyQTw
$(TARGET) : $(OBJECTS)36Please respect copyright.PENANAv9Pi4xPOBb
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)36Please respect copyright.PENANAzeL0emT2Ma
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:36Please respect copyright.PENANAQbOPQheTUw
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h36Please respect copyright.PENANABCd56cS8xy
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc36Please respect copyright.PENANA1oWWOc3JrF
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g36Please respect copyright.PENANAApmJDcjmLe
36Please respect copyright.PENANASUkjb1yJaX
TARGET = buda36Please respect copyright.PENANAbyJGIL3Ndt
OBJ_DIR = ./debug/obj36Please respect copyright.PENANAiyxsZDNUXo
SRC_DIR = ./src36Please respect copyright.PENANAUl090LuQda
H_DIR = ./includes36Please respect copyright.PENANAIKd6tu0xxG
36Please respect copyright.PENANAXvFdTdeLRi
SRC = $(wildcard $(SRC_DIR)/*.cpp)36Please respect copyright.PENANAqxrp1M3Jvt
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))36Please respect copyright.PENANAFbVBy3SnqC
36Please respect copyright.PENANAtxXIkL7u3v
VPATH = $(SRC_DIR)36Please respect copyright.PENANAwPiwWEUqGj
vpath %.o $(OBJ_DIR)36Please respect copyright.PENANA24JgoTZrew
36Please respect copyright.PENANAVhC1zTMXC4
all: $(TARGET)36Please respect copyright.PENANANCo26ZxuVI
36Please respect copyright.PENANAAQfjv2glcP
$(TARGET) : $(OBJECTS)36Please respect copyright.PENANAGTPOUBUxUH
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)36Please respect copyright.PENANAvBqye1PZDG
echo "make succeed"36Please respect copyright.PENANATKRJt7y7lm
36Please respect copyright.PENANAZRw5wF1XLv
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h36Please respect copyright.PENANA0vChGCh74M
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:36Please respect copyright.PENANAq428iuXlGk
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:36Please respect copyright.PENANAZT8jLoGh9b
make clean36Please respect copyright.PENANACMCMh197Br
make36Please respect copyright.PENANAXFClD0Czn5
36Please respect copyright.PENANA3N2yiUmhOC
36Please respect copyright.PENANAx2M3Xen52Z
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/11205507636Please respect copyright.PENANA3qnigqBbCd
https://blog.csdn.net/momodosky/article/details/11985798036Please respect copyright.PENANAMmmTiRlXlR
https://makefiletutorial.com/36Please respect copyright.PENANAATwMzqiZNU
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.com36Please respect copyright.PENANAEy1fAfQTQy
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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)以便拉您进群。36Please respect copyright.PENANAkOeeuHTAhO
36Please respect copyright.PENANAr4SJdQYwib