1
votes

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;
}
3
for CMake add link_libraries(pthread)Alex Punnen

3 Answers

1
votes

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

3
votes

After busy hours, I succeeded to compile my supercomplex test program, using KDevelop/CMake/gtest

#include "gtest-1.7.0/include/gtest/gtest.h"

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

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

    return  RUN_ALL_TESTS();
}

The CMakeLists.txt file that could do the task is

# http://stackoverflow.com/questions/13513905/how-to-properly-setup-googletest-on-linux/13513907#13513907
# http://stackoverflow.com/questions/15193785/how-to-get-cmake-to-recognize-pthread-on-ubuntu
# http://stackoverflow.com/questions/21116622/undefined-reference-to-pthread-key-create-linker-error
# http://stackoverflow.com/questions/1620918/cmake-and-libpthread
# https://meekrosoft.wordpress.com/2009/10/04/testing-c-code-with-the-googletest-framework/
# http://stackoverflow.com/questions/30106608/googletest-cmake-and-make-tests-not-running
# http://stackoverflow.com/questions/13521618/c-project-organisation-with-gtest-cmake-and-doxygen
# http://www.kaizou.org/2014/11/gtest-cmake/
cmake_minimum_required(VERSION 2.8)
project(ThreadTest C CXX)
ADD_SUBDIRECTORY (gtest-1.7.0)
enable_testing()

#set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

if(NOT MSVC)
  set(PThreadLib -pthread)
endif()

add_executable(ThreadTest main_test.cpp) 
target_link_libraries(ThreadTest ${PThreadLib} ${GTEST_LIBRARIES})
#add_test(ThreadTest ThreadTest)

I want to highlight some things: a few valuable links and

project(ThreadTest C CXX) rather than project(ThreadTest)

and

set(PThreadLib -pthread) rather than set(PThreadLib pthread)

I guess the rest can be managed. It could be a good starting point.

1
votes

All you need to do is link with pthread:

target_link_libraries(BUILD_ARTIFACT pthread)

BUILD_ARTIFACT can be your project, or library name. You should add this line after defining your artifact.

For example, if your build artifact is ThreadTest:

add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest pthread)