Makefile找不到包含路径 [英] Makefile cannot find include path

查看:660
本文介绍了Makefile找不到包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目子目录中的Makefile仅在从项目主Makefile中使用时看不到包含路径.我在Makefiles上没有太多经验,而且我一直在阅读的很多内容令人困惑.这是目录的布局(这是自我刚开始该项目以来的所有内容):

A Makefile in the subdirectory of my project doesn't see the include path ONLY when it is used from the main Makefile of my project. I don't have much experience with Makefiles and a lot of what I've been reading is pretty confusing. Here is the layout of my directory (with what I have so far since I just started the project):

main/
    Inventory/
        Item.h
        Item.cpp
        Makefile
    tools/
        include/
            json/
                json.h
        jsoncpp.cpp
        Makefile
    main.cpp
    Makefile

这是主目录中的Makefile:

Here is the Makefile in the main directory:

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:

Here is the Makefile in the tools directory:

INCLUDE = -Iinclude/
CC = g++

jsoncpp.o: jsoncpp.cpp
    $(CC) -c jsoncpp.cpp $(INCLUDE)

当我从tools/调用make时,效果很好.但是,当我从主目录调用make时,会出现此错误:

When I call make from the tools/, it works just fine. But when I call make from the main directory I get this error:

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.

现在,我部分地相信无论出于何种原因都无法找到include目录,但是由于g ++和-c之间的怪异间隙,该错误的第一行对我来说是很奇怪的.由于我的项目很快就会变得很大,因此该如何解决呢?

Now I partially believe that it can't find the include directory for whatever reason, but the first line in that error is fairly odd to me because of that weird gap between g++ and -c. Since my project will soon get pretty big, how can I fix this?

推荐答案

如果在-I指令中,则应为#include <json/json.h>,否则为#include "include/json/json.h"

If it's in -I directive, it should be #include <json/json.h> otherwise #include "include/json/json.h"

包含目录取自当前目录,因此在main/中,您必须使用-Itools/include

Include directory is taken from current directory, so in main/ you have to use -Itools/include

解决方案:使用了隐式规则,因此必须设置正确的变量CXXFLAGS+=$(INCLUDE)进行编译.请参阅:制作手册

Solution: Implicit rules were used so correct variable CXXFLAGS+=$(INCLUDE) must be set for compilation. See: make manual

主要问题是Main: main.o $(DEP)-DEP中的文件必须已经存在,否则将使用隐式规则.之后的CD工具/&&完成.

And the main problem is Main: main.o $(DEP) - files in DEP must exist already otherwise it'll use implicit rules. Later after that cd tools/ && make is done.

这篇关于Makefile找不到包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆