use*_*206 4 c++ makefile googletest
我正在尝试创建 makefile 并编译简单的示例,gtest但出现错误:
g++ main.o -o exampleOutput main.o:在功能
main': main.cpp:(.text+0x1e): undefined reference to测试中::InitGoogleTest(int*, char**)'collect2:错误:ld返回1退出状态make:***[输出]错误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;
}
Run Code Online (Sandbox Code Playgroud)
这是生成文件:
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
Run Code Online (Sandbox Code Playgroud)
(Gtest 的)头文件位于/usr/include/gtest
,lib 文件位于/usr/lib.
我究竟做错了什么?
谢谢。
-lgtest和参数-lgtest_main应该在链接 exampleOutput 时传递,而不是在编译 main.o 时传递。它与您评论中的命令一起使用的原因是该命令一次性执行这两个步骤,而您的 makefile 则不然。
makefile 也是不正确的,因为仅output在命令实际生成时才命名目标exampleOutput,因此即使不需要该命令,也始终会执行该命令,因为output它所期望的命名文件永远不会实际生成...