gpt4 book ai didi

c++ - Makefile 找不到包含路径

转载 作者:搜寻专家 更新时间:2023-10-31 02:20:43 24 4
gpt4 key购买 nike

我的项目的子目录中的 Makefile 仅在从我的项目的主 Makefile 中使用时才会看到包含路径。我对 Makefile 没有太多经验,而且我阅读的很多内容都非常困惑。这是我的目录的布局(自从我刚开始这个项目以来我所拥有的):

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)必须为编译设置。请参阅:make manual

主要问题是Main: main.o $(DEP) - DEP 中的文件必须已经存在,否则它将使用隐式规则。稍后 cd tools/&& make 完成。

关于c++ - Makefile 找不到包含路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com