【问题标题】:Makefile cannot find include pathMakefile 找不到包含路径
【发布时间】:2015-09-02 15:17:06
【问题描述】:

我的项目子目录中的 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 之间存在奇怪的差距,该错误的第一行对我来说相当奇怪。由于我的项目很快就会变得很大,我该如何解决这个问题?

【问题讨论】:

    标签: c++ makefile


    【解决方案1】:

    如果在 -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 完成。

    【讨论】:

    • 这就是我所拥有的。 jsoncpp.cpp 已经有#include<json/json.h>。这就是为什么我的INCLUDE 只有-Iinclude/
    • 好的,所以你在 main/ 编译 tools/json.cpp 并且只有 -Iinclude 而不是 -Itools/include ?
    • 工具/的 Makefile 中没有。我也尝试将它放在 main/ 的 Makefile 中,但它没有用。
    • 所以我尝试在主目录makefile中使用-Itools/include将其更改为#include <json/json.h>,但它仍然给我那个错误。
    • 这些 Makefiles 当前有效吗?我不明白是怎么回事:$(CC) -c jsoncpp.cpp $(INCLUDE) 变成了g++ -c -o tools/jsoncpp.o tools/json.cpp 它一定是完全不同的动作
    【解决方案2】:

    #include <json/json.h>

    gcc Include Syntax

    #include <file> 这个变种用于系统头文件。它在系统的标准列表中搜索名为 file 的文件 目录。您可以使用 -I 将目录添加到此列表中 选项(请参阅调用)。

    #include "file" 这个变种用于你自己程序的头文件。它首先在目录中搜索名为 file 的文件 包含当前文件,然后在引用目录中,然后 用于 .您可以将目录添加到 带有 -iquote 选项的引用目录列表。

    【讨论】:

    • 这不是我写的文件,我是从 github 下载的。 [github.com/open-source-parsers/jsoncpp]
    • 如果你从主目录调用make,你必须添加-Itools/include以便预处理器可以找到tools/include/json/json.h文件
    • 正如 KIIV 答案下的 cmets 中所述,我尝试过但没有成功。
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2016-06-26
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多