制作GTest时出错

som*_*yay 36 linker-errors googletest

我试图在我的Ubuntu机器上设置GTest环境.但在使GTest获取库时,我收到以下错误...

som@som-VPCEH25EN:~/Workspace/CPP/gtest-1.6.0/make$ make
g++ -I../include -g -Wall -Wextra -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
gtest_main.a(gtest-all.o): In function `~ThreadLocal':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1336: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1340: undefined reference to `pthread_key_delete'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1336: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1340: undefined reference to `pthread_key_delete'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::GetOrCreateValue() const':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1372: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1379: undefined reference to `pthread_setspecific'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1365: undefined reference to `pthread_key_create'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::CreateKey()':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1365: undefined reference to `pthread_key_create'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue() const':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1372: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1379: undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status
make: *** [sample1_unittest] Error 1
Run Code Online (Sandbox Code Playgroud)

Fra*_*ser 53

尝试在g ++命令中-lpthread转到after gtest_main.a.

  • @somenath这似乎是你问题的直接答案,因为我们有一个相同的问题......所以请接受它(如果确实是这样). (4认同)

Str*_*bbl 17

我有同样的问题,我-lpthread在Makefile中更改为-pthread.(我放弃了小'L'.)之后编译'make'没有错误.


Ger*_*ncy 5

您是否看过Erik Smistad关于如何在Ubuntu上使用gtest的优秀教程?我刚刚在(K)ubuntu 12.04上尝试过,它运行得很好.简而言之,您需要做的是

sudo apt-get install cmake libgtest-dev
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib
Run Code Online (Sandbox Code Playgroud)

如果您觉得以root身份运行make和cmake会感到不舒服,则必须将/ usr/src/gtest复制到用户可写的目录中.在本教程中,Erik还提供了一个带有CMakeLists.txt的示例测试来构建它.