katang katang - 2 months ago 27
C++ Question

cmake does not consider -pthread

I am trying to make a testbench to my program using gmock/gtest; Linux/Ubuntu; KDevelop/CMake. From the link error message I conclude that part of the gtest package is missing pthread support.

/home/projects/cpp/gmock/gtest/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x16): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x2b): undefined reference to `pthread_key_delete'


I also read

googletest: how to setup?

Using g++ directly, everything works. So, since I am usinc KDevelop/CMake, I suspect either my code or CMake.

In my CMakeLists.txt I do use

add_definitions( -pthread -m64)


However, I do not see any effect in the Makefile.
Am I missing something from my CMakeLists.txt, or CMake does not consider the line above?

My CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
add_definitions( -Dpthread )

project(ThreadTest)

INCLUDE_DIRECTORIES(gmock/gtest/include)

set ( GTEST_LIBS libgtest.a )
link_directories( ~/projects/cpp/gmock/gtest)

add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_LIBS})


Do I misunderstand, that add_definitions should work in this situation?

After reading
How do I force cmake to include "-pthread" option during compilation?
my question really looks like duplicate. However,

cmake_minimum_required(VERSION 2.8)
add_definitions( -Dpthread )

project(ThreadTest)

INCLUDE_DIRECTORIES(gmock/gtest/include)

find_package( Threads )
set ( GTEST_LIBS libgtest.a )
link_directories( ~/projects/cpp/gmock/gtest)

add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})


still gives the warning 'Could NOT find Threads'. I tried to search Ubuntu software center for "threads", with no result. After that, I installed libghc-threads-dev. However, when using

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)


I keep receiving 'Could NOT find Threads', as error. What shall I do to satisfy find_package, and why do I have this problem, when the simple Makefile produces what I expect?

PS: my main file:

#include "gmock/gtest/include/gtest/gtest.h"

TEST(blahTest, blah1) {
EXPECT_EQ(1, 1);
}

int main (int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);

int returnValue;

returnValue = RUN_ALL_TESTS();

return returnValue;
}

Answer Source

I integrated Google Test framework in my own project and this CMake file is working.

# Google C++ Testing Framework
# https://code.google.com/p/googletest/
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

if(WIN32)
    # GTest is static compiled by default
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()

enable_testing()
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_BOTH_LIBRARIES})

CMake warning "Could NOT find Threads" is related to a bug: CMake failing to detect pthreads due to warnings