繁体   English   中英

Makefile找不到.hpp

[英]Makefile doesn't find .hpp

我的项目中有2个目录,一个名为Builds,具有Makefile和一个测试程序(test-P0-consola.cpp),另一个名为P0的目录,其中包含我使用的类,cadena(字符串)和fecha(日期) )。

test-P0-consola.cpp包括它们两者,但是Make找不到它们。

CPP = g++
CPPFLAGS = -std=c++14 -g -Wall -pedantic

VPATH = ../P0:.:..

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} -o $@.ex $^

test-P0-consola.o: test-P0-consola.cpp fecha.hpp cadena.hpp
    ${CPP} -c ${CPPFLAGS} $< -o $@

fecha.o: fecha.hpp
cadena.o: cadena.hpp

尝试编译test-P0-consola.o时,它将引发致命错误“ cadena.hpp文件或目录不存在”,但是当我强制其编译cadena或fecha时,它会发现这些错误。 我正在使用GCC和Ubuntu。

..
├── Builds
│   ├── makefile.mak
│   └── test-P0-consola.cpp
├── P0
│   ├── cadena.cpp
│   ├── cadena.hpp
│   ├── fecha.cpp
│   └── fecha.hpp

编辑

错误:

g++ -std=c++14 -g -Wall -pedantic -c test-P0-consola.cpp
test-P0-consola.cpp:7:21: fatal error: fecha.hpp: There is no file or directory

compilation terminated.

makefile.mak:9: Failure in the instructions for the objective 'test-P0-consola.o'

make: *** [test-P0-consola.o] Error 1

仔细查看您的错误:

test-P0-consola.cpp:7:21: fatal error: fecha.hpp: There is no file or directory

您可能有类似以下内容:

// test-P0-consola.cpp
#include "fetcha.hpp"

但是fetcha.hpp不在该目录中,因此无法找到它。 您需要更改直接包含文件的方式(通过#include "../P0/fetcha.hpp" ),或者更改构建规则以传递附加的包含路径(通过-I../P0 )。


注意:我不确定有添加的理由. VPATH 那是隐性的。

注意2:这是一个坏主意:

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} -o $@.ex $^
                          ~~~~~

不要撒谎。 运行配方的结果应为目标文件(PHONY目标除外)。 此处的配方应为-o $@ 如果需要.ex后缀,则应将目标更改为test-consola.ex 如果您仍然希望将规则命名为test-consola ,则需要:

test-consola : test-consola.ex
test-consola : .PHONY

您应将编译器要使用的.hpp文件的include路径放入makefile中。 您应该使用-Ipath编译器指令,其中path是包含文件的路径。

请参阅` Makefile:如何正确包含头文件及其目录?

如何在Makefile中定义几个包含路径

就像是:

CPP = g++
CPPFLAGS = -std=c++14 -g -Wall -pedantic
INC = -Iyourincludebasepath/P0

VPATH = ../P0:.:..

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} ${INC} -o $@.ex $^

test-P0-consola.o: test-P0-consola.cpp fecha.hpp cadena.hpp
    ${CPP} -c ${CPPFLAGS} ${INC} $< -o $@

fecha.o: fecha.hpp
cadena.o: cadena.hpp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM