cmake不考虑-pthread

时间:2015-07-23 07:14:28

标签: c++ cmake pthreads googletest

我正在尝试使用gmock / gtest为我的程序创建一个测试平台; Linux的/ Ubuntu的; KDevelop的/ CMake的。从链接错误消息我得出结论,部分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 ++,一切正常。所以,既然我是Kincvelop / 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})

仍然会发出警告'找不到线程'。我试图搜索Ubuntu软件中心的“线程”,没有结果。之后,我安装了libghc-threads-dev。但是,使用时

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)

我一直收到'找不到线程',因为错误。当简单的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;
}

3 个答案:

答案 0 :(得分:3)

忙碌了几个小时后,我成功编译了我的超复杂测试程序,使用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文件是

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

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

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

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

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

答案 1 :(得分:1)

我在自己的项目中集成了Google Test框架,这个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警告&#34;无法找到线程&#34;与bug有关: CMake failing to detect pthreads due to warnings

答案 2 :(得分:1)

您需要做的就是链接到pthread

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

BUILD_ARTIFACT可以是您的项目或库名称

P.S在定义ARTIFACT后添加此行

P.S.S您的构建工件是ThreadTest

$info = ['abc'=>'xyz', '123'=>'456', 'vowels'=>'aeiou'];  //complete data array
$keys = ['abc','123'];  //list of keys I'm interested in

$out=array_intersect_key($info,array_flip($keys));

print_r($out);