gpt4 book ai didi

c++ - 安装 GTest 和 GMock

转载 作者:行者123 更新时间:2023-11-30 05:03:38 26 4
gpt4 key购买 nike

我已经在我的 Ubuntu 17.04 机器上安装了 GTest 和 GMock。大致安装如下;

sudo apt-get 安装 libgtest-devcmake CMakeLists.txt 文件制作

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

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

sudo apt-get install -y google-mock

然后像上面那样cmake和make像以前一样链接库。

我创建了以下 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)

并且有两个源文件

主要.cpp

#include "gmock/gmock.h"

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

声音测试.cpp

class Soundex
{
};

#include "gmock/gmock.h"

TEST(SoundexEncoding, RetainsSoleLetterOfOneLetterWord)
{
Soundex soundex;
}

(我正在阅读 Jeff Langr 的 Modern C++ Programming with Test Driven Development 一书)

我在我的 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 的新手,对 make 很生疏。虽然这个输出让我觉得我没有正确构建 GTest 或 GMock。任何人都可以请指点。 (没有双关语意)

谢谢

最佳答案

传递给链接器的库的顺序很重要。当链接器看到库文件名时,默认情况下 (*) 它只从该库中提取对象,这些对象解析它知道此时需要的一个或多个 undefined symbol 。

因此,如果库 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 将导致它重复循环一组库,直到它们都不能用于解析整个循环的任何更多符号.但大多数时候,只需列出您需要的库就足够了,这样使用其他库的库就会排在列表的前面。

关于c++ - 安装 GTest 和 GMock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49340073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com