cmake 不考虑-pthread

cmake does not consider -pthread

我正在尝试使用 gmock/gtest 对我的程序进行测试; Linux/Ubuntu; KDevelop/CMake。从 link 错误消息我得出结论,部分 gtest 包缺少 pthread 支持。

/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'

我也看了

googletest: how to setup?

直接用g++,一切正常。因此,由于我使用的是 KDevelop/CMake,我怀疑是我的代码还是 CMake。

在我的 CMakeLists.txt 中,我使用

add_definitions( -pthread  -m64)

但是,我在 Makefile 中看不到任何效果。 我是否遗漏了 CMakeLists.txt 中的某些内容,或者 CMake 没有考虑上面的行?

我的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})

我是否误解了 add_definitions 应该在这种情况下起作用?

阅读后 How do I force cmake to include "-pthread" option during compilation? 我的问题看起来真的很重复。然而,

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})

仍然给出警告'Could NOT find Threads'。我试图在 Ubuntu 软件中心搜索 "threads",但没有结果。之后,我安装了 libghc-threads-dev。但是,当使用

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)

我一直收到 'Could NOT find Threads' 作为错误。当简单的 Makefile 产生我期望的结果时,我应该怎么做才能满足 find_package,为什么我会遇到这个问题?

PS: 我的主文件:

#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;
}

我在自己的项目中集成了 Google 测试框架,这个 CMake 文件可以正常工作。

# 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 警告 "Could NOT find Threads" 与错误有关: CMake failing to detect pthreads due to warnings

在忙碌的时间里,我成功地编译了我的超复杂测试程序,使用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();
}

可以完成任务的CMakeLists.txt文件是

# 
# 
# 
# 
# https://meekrosoft.wordpress.com/2009/10/04/testing-c-code-with-the-googletest-framework/
# 
# 
# 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)

我想强调一些事情:一些有价值的链接和

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

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

我想其余的都可以管理。这可能是一个很好的起点。

您只需 link 使用 pthread:

target_link_libraries(BUILD_ARTIFACT pthread)

BUILD_ARTIFACT 可以是您的项目或库名称。您应该在定义工件后添加此行。

例如,如果您的构建工件是 ThreadTest:

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