我正在尝试创建makefile并编译gtest
的简单示例,但我得到了错误:
g main.o -o示例output main.o:在函数
main': main.cpp:(.text+0x1e): undefined reference to
testing :: initgoogletest(int*,char **('collect2:错误:ld返回1 退出状态制造:*** [输出]错误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;
}
这是makefile:
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
参数应在链接示例输出时传递,而不是编译main.o时。它在您的评论中与命令一起使用的原因是,该命令正在一次执行这两个步骤,而您的makefile却不是。
makefile也不正确,因为目标仅命名output
,而该命令实际上产生exampleOutput
,因此即使不需要,该命令也将始终执行,因为该命令的名为output
的文件将永远不会实际是生产...