【问题标题】:Makefile doesn't find .hppMakefile 找不到 .hpp
【发布时间】:2017-08-09 09:31:33
【问题描述】:

我的项目中有 2 个目录,一个名为 Builds,它拥有 Makefile 和一个测试程序 (test-P0-consola.cpp),另一个名为 P0 的目录包含我使用的类 cadena (string) 和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

【问题讨论】:

  • 你能描述一下你正在做的具体错误和具体构建,并在构建开始时打印完整的目录树吗?目前还不清楚错误是什么。
  • 我添加了完整的错误以及目录树。我只是想执行一个测试来检查类是否有错误。如果你在同一个文件夹中编译它们就可以了。

标签: c++ gcc makefile


【解决方案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 $^
                          ~~~~~

不要对 Make 撒谎。运行配方的结果应该是目标文件,除了 PHONY 目标。这里的配方应该是-o $@。如果您想要.ex 后缀,则应将目标更改为test-consola.ex。如果您仍希望将规则命名为 test-consola,您需要:

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

【讨论】:

  • 问题是我没有添加-我,我记下了你的笔记!谢谢
【解决方案2】:

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

见`Makefile: How to correctly include header file and its directory?

How to define several include path in 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

【讨论】:

  • 我以为你不必添加 -I,非常感谢
猜你喜欢
  • 2021-11-20
  • 2018-08-22
  • 1970-01-01
  • 2019-04-24
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2013-10-15
相关资源
最近更新 更多