gpt4 book ai didi

c++ - 将 cmake 用于 googletest 时未定义对 `pthread_getspecific' 的引用

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:20 24 4
gpt4 key购买 nike

我正在关注这个 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

最佳答案

链接时有问题。

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

target_link_libraries(test gmock gtest pthread)

而不是你的版本:

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

关于c++ - 将 cmake 用于 googletest 时未定义对 `pthread_getspecific' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com