[英]Is there automated way to detect which .cpp source files I should be compiling (depending on #include *.h)
[英]GNU make compiling all .cpp files to .o and include .h files
所以我有一個看起來像這樣的文件夾
OS
Makefile
/include
head.h
/src
main.cpp
head.cpp
/objects
我如何使用Makefile將所有.cpp文件編譯到objects文件夾,然后將包含.h的所有.o文件編譯到我的實際cpp程序中。我目前有:
CPP_FILES := $(wildcard src/*.cpp)
H_FILES := $(wildcard include/*.h)
OBJ :=$(wildcard objects/*.o)
LIBS := -Wall
CC := -std=c++14
接下來我要輸入什么以將所有.cpp文件制作為.o並使用.h進行編譯。謝謝您的時間
您可以為此使用模式規則 (以前稱為“后綴規則”)。 使用GNU make時,假設Makefile
位於頂層目錄中,則可以編寫以下代碼來將src/
所有.cpp
文件編譯為objects/
.o
文件:
objects/%.o: src/%.cpp
$(CXX) -c $(CFLAGS) $< -o $@
在GNU make語法中, $<
表示依賴關系(在這種情況下為.cpp文件), $@
表示目標(目標文件)。
.h
文件不需要編譯,只需將正確的包含路徑設置為CFLAGS變量包含的字符串的一部分即可:
CFLAGS += -I./include
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.