使用cmake进行googletest时出现“undefined reference to `pthread_getspecific'”错误

10

我正在学习这本

我已经安装了GoogleTest并编译了库:

kuyu@ub16:~/Downloads/googletest-master$ find . -name *.a
./mybuild/googlemock/libgmock_main.a
./mybuild/googlemock/gtest/libgtest_main.a
./mybuild/googlemock/gtest/libgtest.a
./mybuild/googlemock/libgmock.a

我有一个CMakeLists.txt文件:

kuyu@ub16:~/Downloads/lotdd-code/c2/2$ echo $GMOCK_HOME 
/home/kuyu/Downloads/googletest-master
kuyu@ub16:~/Downloads/lotdd-code/c2/2$ cat CMakeLists.txt
project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)

include_directories($ENV{GMOCK_HOME}/googlemock/include $ENV{GMOCK_HOME}/googletest/include)
link_directories($ENV{GMOCK_HOME}/mybuild/googlemock $ENV{GMOCK_HOME}/mybuild/googlemock/gtest)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")

set(sources 
   main.cpp 
   SoundexTest.cpp)
add_executable(test ${sources})
target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)

我接着尝试构建我的源文件:

kuyu@ub16:~/Downloads/lotdd-code/c2/2$ mkdir build && cd build
kuyu@ub16:~/Downloads/lotdd-code/c2/2/build$ cmake ..
# output omitted for brevity...
kuyu@ub16:~/Downloads/lotdd-code/c2/2/build$ make
Scanning dependencies of target test
[ 33%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/test.dir/SoundexTest.cpp.o
/home/kuyu/Downloads/lotdd-code/c2/2/SoundexTest.cpp: In member function ‘virtual void SoundexEncoding_RetainsSoleLetterOfOneLetterWord_Test::TestBody()’:
/home/kuyu/Downloads/lotdd-code/c2/2/SoundexTest.cpp:7:12: warning: unused variable ‘soundex’ [-Wunused-variable]
    Soundex soundex;
            ^
[100%] Linking CXX executable test
/home/kuyu/Downloads/googletest-master/mybuild/googlemock/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x25): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x3a): undefined reference to `pthread_key_delete'
# output omitted for brevity...

我从这里了解到,我必须使用静态库而不是动态库。因此,我能够成功地使用手动构建编译:

g++ SoundexTest.cpp main.cpp -I/home/kuyu/Downloads/googletest-master/googletest/include -I/home/kuyu/Downloads/googletest-master/googlemock/include /home/kuyu/Downloads/googletest-master/mybuild/googlemock/libgmock.a /home/kuyu/Downloads/googletest-master/mybuild/googlemock/gtest/libgtest.a -pthread

我的问题是,我该如何修改CMakeLists.txt文件,以便构建成功?也就是说,要使用libgtest.a而不是libgtest.so。

1个回答

13

链接时出现了问题。

这只是一个假设:你尝试过这个吗?

target_link_libraries(test gmock gtest pthread)

与你的版本不同:

target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)

1
@Tsyvarev 好的,感谢反馈。我认为可能是库的顺序不正确。gtest 需要 pthread,链接器会抱怨一些 pthread 符号未定义,我想确认一下“gtest,然后 pthread”的顺序是否正确。 - Picaud Vincent
1
@picaud-vincent,调整库的顺序对我起了作用。非常感谢你的帮助。 - hermit.crab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接