0

I follow the step on incorporating-into-an-existing-cmake-project and I can compile and link my test correctly.

But if I install gtest by myself and then I use the installed gtest, I failed the link my test code. I need to add set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") into my CMakeLists.txt and it will work correctly.

So why do I need add the params -pthread if I use the installed gtest lib but I don't need to if I follow the step of incorporating-into-an-existing-cmake-project?

Anybody can help me?

Here is my test code.

CMakeLists.txt

cmake_minimum_required(VERSION 3.5.1)
project(Test C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") # without -pthread, it will failed to link pthread_xxxx
include_directories("/usr/local/include")
link_directories("/usr/local/lib")
find_library(GTEST_LIB NAMES gtest)
add_executable(test gtest_demo.cpp)
message(status "gtest: ${GTEST_LIB}")
target_link_libraries(test ${GTEST_LIB})

gtest_demo.cpp

#include <gtest/gtest.h>
int add(int a, int b) {
    int c = a + b;
    return c;
}

TEST(AddCase, AddOK) {
    EXPECT_EQ(3, add(1,2));
}

using namespace std;
int main(int argc,char* argv[]) {
    testing::InitGoogleTest(&argc,argv); 
    RUN_ALL_TESTS(); 
    return 0;
}

Here is how I install gtest

mkdir googletest/build \
    && cd googletest/build && cmake .. && make -j10 \
    && cp lib/* /usr/local/lib/ \
    && cp -r googletest/googlemock/include/gmock/ /usr/local/include \
    && cp -r googletest/googletest/include/gtest/ /usr/local/include \

this is the error message

[100%] Linking CXX executable test
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x15): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x2a): undefined reference to `pthread_key_delete'
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED2Ev[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED5Ev]+0x15): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED2Ev[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED5Ev]+0x2a): undefined reference to `pthread_key_delete'
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::GetOrCreateValue() const':
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv]+0x16): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv]+0x79): undefined reference to `pthread_setspecific'
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv]+0x16): undefined reference to `pthread_key_create'
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::CreateKey()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE9CreateKeyEv]+0x16): undefined reference to `pthread_key_create'
/usr/local/lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue() const':
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x16): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x79): undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:85: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
GOGO
  • 619
  • 2
  • 7
  • 17
  • 1
    Could you compile with `VERBOSE=1 make` to check the actual command line your compiler is invoked with? – YSC Mar 20 '20 at 09:01
  • 2
    And please, provide **exact error message**, with filenames and other details. – Tsyvarev Mar 20 '20 at 09:08
  • @Tsyvarev I have added it. Please check it. Thank you. – GOGO Mar 20 '20 at 09:53
  • @YSC I use `VERBOSE=1 make`. The actual command line is `/opt/rh/devtoolset-8/root/usr/bin/c++ CMakeFiles/test.dir/gtest_demo.cpp.o -o test -L/usr/local/lib -Wl,-rpath,/usr/local/lib /usr/local/lib/libgtest.a`. No `lpthread` is added. – GOGO Mar 20 '20 at 09:55
  • @YSC And I test way of the [incorporating-into-an-existing-cmake-project](https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project). It is `/opt/rh/devtoolset-8/root/usr/bin/c++ CMakeFiles/test.dir/gtest_demo.cpp.o -o test -L/usr/local/lib -Wl,-rpath,/usr/local/lib lib/libgtest_main.a lib/libgtest.a -lpthread`. `-lpthrad` is added. Do you know how it is added? – GOGO Mar 20 '20 at 10:02
  • 1
    You link with plain **static** `gtest`, so this library doesn't contain references to other libraries (e.g. `pthread`) it depends. Other ways for using `gtest` properly handle dependencies of `gtest`. – Tsyvarev Mar 20 '20 at 10:06

0 Answers0