下面这个makefile非常简单,能满足基本的项目需要,首先是一个总的target:107Please respect copyright.PENANAWGj8M73sRk
all: $(TARGET)
这个总的target由多个.o文件link而成:107Please respect copyright.PENANAKMkxVPB5Jp
$(TARGET) : $(OBJECTS)107Please respect copyright.PENANAkPSLISdCU7
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)107Please respect copyright.PENANAghOTIWKB4U
echo "make succeed"
每个.o文件对应一个同名的.cpp文件,加上全局的头文件,这里有3个全局的头文件,如果不加头文件,那么当头文件被修改时,.cpp不会被重新编译成.o,因此一定要加:107Please respect copyright.PENANAETvmUzOYdL
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h107Please respect copyright.PENANA1Ppo2FW52S
$(CC) -c -g $< -o $(OBJ_DIR)/$@
这样makefile就做好了,完整的内容如下:
----------makefile------------------
CC = gcc107Please respect copyright.PENANA5k0tBU82bF
CFLAGS = -lpthread -lm -lssl -lcrypto -lzip -g107Please respect copyright.PENANAmrOsTuruQz
107Please respect copyright.PENANAVqbGKCwki9
TARGET = buda107Please respect copyright.PENANA9XrWDX1ssf
OBJ_DIR = ./debug/obj107Please respect copyright.PENANAMpuMpNCuhD
SRC_DIR = ./src107Please respect copyright.PENANAEE5wHpyIJ4
H_DIR = ./includes107Please respect copyright.PENANA33dUVEzPw8
107Please respect copyright.PENANAYaBHE3TRSX
SRC = $(wildcard $(SRC_DIR)/*.cpp)107Please respect copyright.PENANABhcP1npCg5
OBJECTS = $(patsubst $(SRC_DIR)/%.cpp, %.o, $(SRC))107Please respect copyright.PENANA8GcQ0PtLWP
107Please respect copyright.PENANAu4wc19o3h1
VPATH = $(SRC_DIR)107Please respect copyright.PENANAso7byZ9RSq
vpath %.o $(OBJ_DIR)107Please respect copyright.PENANAJ7IZ7i4bZz
107Please respect copyright.PENANAkZSEp9XQ5j
all: $(TARGET)107Please respect copyright.PENANAb4jyArhi58
107Please respect copyright.PENANAkto2ZuOJfB
$(TARGET) : $(OBJECTS)107Please respect copyright.PENANAv2adq1Fa1a
$(CC) -o $(TARGET) $(addprefix $(OBJ_DIR)/, $(OBJECTS)) $(CFLAGS)107Please respect copyright.PENANAeVuTb4hIyO
echo "make succeed"107Please respect copyright.PENANAYTqhN6lKbE
107Please respect copyright.PENANANCzO96OI1R
%.o : %.cpp $(H_DIR)/01.system.h $(H_DIR)/02.01.macros.h $(H_DIR)/02.types.h107Please respect copyright.PENANAx2ZjEm1uei
$(CC) -c -g $< -o $(OBJ_DIR)/$@
.PHONY : clean
clean:107Please respect copyright.PENANAEOxIeRTdjc
rm -rf ./debug/$(TARGET) $(TARGET) $(OBJ_DIR)/*.o ./*.o
rebuild:107Please respect copyright.PENANAqFpDoul4eu
make clean107Please respect copyright.PENANAdJ6z0TtnZp
make107Please respect copyright.PENANASqKXlhDAgR
107Please respect copyright.PENANA5ZDBmwNs9b
107Please respect copyright.PENANAZ63P9ymXn7
------------------------------------
Section X. Thanks
https://blog.csdn.net/qq_38113006/article/details/112055076107Please respect copyright.PENANApTQ7UcIbcw
https://blog.csdn.net/momodosky/article/details/119857980107Please respect copyright.PENANAVSIANouauq
https://makefiletutorial.com/107Please respect copyright.PENANAJHSW9kdBY0
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]107Please respect copyright.PENANALyCoOC94o8
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。107Please respect copyright.PENANA0zsFoUfcB4
107Please respect copyright.PENANAJlzCg9CAJx