Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to `pthread_getspecific' when using cmake for googletest

I am following this book.

I have a GoogleTest installation and I built the libraries:

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

I have a CMakeLists.txt file:

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)

I then try to build my source files:

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...

I read from here that I have to use static library instead of dynamic library. So, I am able to build successfully using manual building:

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

My question is, how do I correct CMakeLists.txt so that building succeeds? That is, so that libgtest.a is used instead of libgtest.so

like image 859
hermit.crab Avatar asked Jan 07 '18 13:01

hermit.crab


1 Answers

There is a problem at link-time.

It is only an assumption: did you try this?

target_link_libraries(test gmock gtest pthread)

instead of your version:

target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)
like image 127
Picaud Vincent Avatar answered Oct 21 '22 15:10

Picaud Vincent