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