我正在尝试创建 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/