【问题标题】:undefined reference to `pthread_getspecific' when using cmake for googletest将 cmake 用于 googletest 时对“pthread_getspecific”的未定义引用
【发布时间】:2018-06-16 16:20:05
【问题描述】:

我正在关注这个book

我安装了 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...

我从here 读到我必须使用静态库而不是动态库。因此,我可以使用手动构建成功构建:

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

【问题讨论】:

    标签: c++ cmake googletest


    【解决方案1】:

    链接时出现问题。

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

    target_link_libraries(test gmock gtest pthread)
    

    而不是您的版本:

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

    【讨论】:

    • @Tsyvarev 好的,感谢您的反馈,我认为图书馆订单可能不是一个好的。 gtest 需要 pthread 并且链接器抱怨某些 pthread 符号未定义,我想确定“gtest,然后是 pthread”的顺序。
    • @picaud-vincent,更改图书馆顺序对我有用。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2017-05-18
    • 2012-10-07
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多