gpt4 book ai didi

c++ - Makefile 找不到 .hpp

转载 作者:行者123 更新时间:2023-11-27 23:55:30 26 4
gpt4 key购买 nike

我的项目中有 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 时抛出 fatal error “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 $^
~~~~~

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

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

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

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