c++ - gtest - 对 `testing::InitGoogleTest(int*, char**)' 的 undefined reference

标签 c++ makefile googletest

我正在尝试创建 makefile 并编译 gtest 的简单示例,但出现错误:

g++ main.o -o exampleOutput main.o: In function main': main.cpp:(.text+0x1e): undefined reference to testing::InitGoogleTest(int*, char**)' collect2: error: ld returned 1 exit status make: *** [output] Error 1

这是 main.cpp:

#include <iostream>
#include "gtest/gtest.h"

using namespace std;

int main(int argc, char **argv)
{
    cout << "This is test" << endl;
    testing::InitGoogleTest(&argc, argv);
    return 0;
}

这是生成文件:

INCLUDE = -I/usr/include/
LIBPATH = -L/usr/lib/

output: main.o 
    g++ main.o  -o exampleOutput

main.o: main.cpp
    g++ -c main.cpp $(INCLUDE)  $(LIBPATH) -lgtest -lgtest_main  -pthread

(Gtest 的)头文件位于 /usr/include/gtest 并且 lib 文件位于 /usr/lib

我做错了什么?

谢谢。

最佳答案

-lgtest-lgtest_main 参数应该在链接 exampleOutput 时传递,而不是在编译 main.o 时传递。它与您评论中的命令一起使用的原因是该命令一次性执行这两个步骤,而您的 makefile 则不是。

makefile 也是不正确的,因为目标仅命名为 output 而命令实际生成 exampleOutput,因此即使不需要时也会始终执行此命令,因为它期望的名为 output 的文件实际上永远不会生成...

关于c++ - gtest - 对 `testing::InitGoogleTest(int*, char**)' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49044872/

相关文章:

c++ - 未初始化的变量会发生什么? C++

c++ - 正确实现类层次结构

makefile - Makefile 中的变量=@value@

linux - 带有静态和共享库的 Makefile

c++ - 如何从 Init 强制 gtest 将报告写入 xml

c++ - vector 作为 HashMap 中的键值对

c++ 用于每个字符串 vector

makefile - 如何在Mac终端中检查GNU make的版本?

c++ - 聚合对象使用 googlemock 调用的模拟函数

c++ - 谷歌单元测试默认设置