makefile:在 -I 之后给出的路径,但找不到头文件

makefile: path given after -I but header file not found

本文关键字:路径 找不到 文件 之后 makefile      更新时间:2023-10-16

我正在尝试使用谷歌测试框架:https://github.com/google/googletest/tree/master/googletest。

我有 4 个文件:

阶乘.cpp:

#include "factorial.h"
int factorial(int n) { [some code here] }

法科试验.h:

int factorial(int n);

test_factorial.cpp

#include "gtest/gtest.h"
#include "factorial.h"
[some tests here]

gtest_main.cpp:

#include <stdio.h>
#include "gtest/gtest.h"
GTEST_API_ int main(int argc, char **argv) {
  printf("Running main() from gtest_main.ccn");
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

我还有一个制作文件,其中包含(除其他外(:

INCLUDES = -I/home/my_username/Documents/gtest/googletest/googletest/include
[...]
$(MAIN): $(OBJS)
        $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

在终端中写入make后,我得到:

test_factorial.cpp:1:25: fatal error: gtest/gtest.h: No such file or directory
compilation terminated.
makefile:27: recipe for target 'test_factorial.o' failed

问题出在哪里?

在googletest的自述文件中,他们说:

g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a 
    -o your_test

所以这里是-isystem而不是-I但我在 -isystem 方面也有问题。

您已将包含添加到链接命令中,但未添加到编译命令中。 此规则:

$(MAIN): $(OBJS)
        $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

讲述如何从目标文件链接主程序。 此规则不用于编译目标文件:假设您的[...]没有选择编译规则,则您使用的是内置编译器规则,该规则对INCLUDES变量一无所知。

如果您在收到该错误时向我们展示编译命令打印test_factorial.cpp那么很明显该标志丢失了。

如果您不组成自己的变量来保存这些标志,而是使用 CPPFLAGS 变量,这是 C 预处理器标志(如 -I(的标准变量,它将正常工作。

CPPFLAGS = -I/home/my_username/Documents/gtest/googletest/googletest/include

它可能只是工作。