安装GTest和GMock

时间:2018-03-17 17:54:02

标签: c++ cmake

我已经在我的Ubuntu 17.04盒子上安装了GTest和GMock。从广义上讲,安装如下;

sudo apt-get install libgtest-dev cmake CMakeLists.txt 使

然后sym将/ usr / src中的库creaetd链接到/ usr / lib中。 (即/ usr / lib中的链接指向/ usr / src)

然后我为GMock做了同样的事情

sudo apt-get install -y google-mock

然后cmake并按上述方式制作 图书馆sym像以前一样链接。

我创建了以下CMakeLists.txt

project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)

include_directories(/usr/include)
link_directories(/usr/lib)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")

set(sources
   main.cpp
   SoundexTest.cpp)
add_executable(tddTest ${sources})
target_link_libraries(tddTest pthread)
target_link_libraries(tddTest gmock)
target_link_libraries(tddTest gtest)

并有两个源文件

的main.cpp

#include "gmock/gmock.h"

int main(int argc, char** argv) {
   testing::InitGoogleMock(&argc, argv);
   return RUN_ALL_TESTS();
}

SoundexTest.cpp

class Soundex
{
};

#include "gmock/gmock.h"

TEST(SoundexEncoding, RetainsSoleLetterOfOneLetterWord)
{
        Soundex soundex;
}

(我正在研究Jeff Langr的“现代C ++编程与测试驱动开发”一书)

我在我的CmakeLists.txt文件上运行cmake,该文件运行时没有错误。当我运行make虽然我得到了这个;

[ 33%] Linking CXX executable tddTest
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x24): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x39): undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.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]+0x24): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED2Ev[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED5Ev]+0x39): undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.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]+0x25): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.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]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue() const':
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x25): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::CreateKey()':
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEE9CreateKeyEv]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::GetOrCreateValue() const':
gmock-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv]+0x25): undefined reference to `pthread_getspecific'
gmock-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::~ThreadLocal()':
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEED5Ev]+0x24): undefined reference to `pthread_getspecific'
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEED5Ev]+0x39): undefined reference to `pthread_key_delete'
collect2: error: ld returned 1 exit status
CMakeFiles/tddTest.dir/build.make:120: recipe for target 'tddTest' failed
make[2]: *** [tddTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/tddTest.dir/all' failed
make[1]: *** [CMakeFiles/tddTest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我是cmake的新手,并且非常生疏。这个输出让我觉得我没有正确构建GTest或GMock。请问有人可以提供一些指示。 (没有双关语)

由于

1 个答案:

答案 0 :(得分:0)

传递给链接器的库的顺序很重要。当链接器看到库文件名时,默认情况下(*)它只会从该库中提取解析一个或多个它知道在那时需要的未定义符号的对象。

因此,如果库A依赖于库B但是您将main.o -lB -lA传递给链接器,那么当涉及到-lB时,它将从B中引入一些对象(如果main.o需要它们) ,但是如果A需要来自B的其他符号,它将不会返回并从B中寻找其他对象,因此所需的符号仍未解析。

gmock依赖于gtest,而gmock(也许是gtest?)依赖于pthread,所以对你的库来说更好的顺序是:

target_link_libraries(tddTest gtest)
target_link_libraries(tddTest gmock)
target_link_libraries(tddTest pthread)

(*)某些GNU ld链接器选项会导致不同的行为。特别是,--whole-archive -lsomething --no-whole-archive将强制它从库文件中获取所有内容,无论是否需要。 --start-group -lA -lB -lC --end-group将导致它重复循环遍历一组库,直到它们都不能用于解析整个循环的任何更多符号。但大多数情况下,仅列出您需要的库就足以让使用其他库的库在列表的前面出现。