[英]Makefile cannot find include path
我的項目子目錄中的 Makefile 僅在從我的項目的主 Makefile 中使用時才看到包含路徑。 我對 Makefiles 沒有太多經驗,而且我閱讀的很多內容都令人困惑。 這是我的目錄的布局(從我剛開始這個項目到現在為止):
main/
Inventory/
Item.h
Item.cpp
Makefile
tools/
include/
json/
json.h
jsoncpp.cpp
Makefile
main.cpp
Makefile
這是主目錄中的Makefile:
INCLUDE = -IInventory/
CC = g++
DEP = tools/jsoncpp.o Inventory/Item.o
Main: main.o $(DEP)
cd tools/ && make
cd Inventory/ && make
$(CC) -o Main main.o $(DEP) $(INCLUDE)
main.o main.cpp
$(CC) -c main.cpp $(INCLUDE)
這是工具目錄中的Makefile:
INCLUDE = -Iinclude/
CC = g++
jsoncpp.o: jsoncpp.cpp
$(CC) -c jsoncpp.cpp $(INCLUDE)
當我從 tools/ 調用 make 時,它工作得很好。 但是當我從主目錄調用 make 時,我收到此錯誤:
g++ -c -o tools/jsoncpp.o tools/json.cpp
tools/jsoncpp.cpp:76:23: fatal error: json/json.h: No such file or directory
#include "json/json.h"
^
compilation terminated.
現在我部分認為它無法找到包含目錄的任何原因,但由於 g++ 和 -c 之間的奇怪差距,該錯誤中的第一行對我來說相當奇怪。 由於我的項目很快就會變得很大,我該如何解決這個問題?
如果它在 -I 指令中,它應該是#include <json/json.h>
否則#include "include/json/json.h"
編輯:包含目錄取自當前目錄,所以在 main/ 你必須使用-Itools/include
解決方案:使用了隱式規則,因此必須為編譯設置正確的變量CXXFLAGS+=$(INCLUDE)
。 參見: 制作手冊
主要問題是Main: main.o $(DEP)
- DEP 中的文件必須已經存在,否則它將使用隱式規則。 之后 cd tools/ && make 完成。
#include <json/json.h>
<- 預處理器將搜索所有包含路徑
請參閱gcc 包含語法:
#include <file>
此變體用於系統頭文件。 它在系統目錄的標准列表中搜索名為 file 的文件。 您可以使用 -I 選項將目錄添加到此列表中(請參閱調用)。
#include "file"
此變體用於您自己程序的頭文件。 它首先在包含當前文件的目錄中搜索名為 file 的文件,然后在引用目錄中搜索,然后在用於 . 您可以使用 -iquote 選項將目錄添加到引用目錄列表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.