【问题标题】:Linking errors GoogleMock with my C++project under linux在 linux 下将错误 GoogleMock 与我的 C++ 项目链接起来
【发布时间】:2018-10-22 16:58:32
【问题描述】:

我想在我的项目中使用 GMock。首先我编译了 GMock 和 GTest。我的操作顺序(我使用 Linux):

  1. git clone googlemock 和 googletest
  2. 进入 GIT/googletest/googlemock/build-aux/ 目录运行 cmake .. 然后制作
  3. 结果我得到:libgmock.a 和 libgmock_main.a

My project 具有以下结构:buildincsrclib 目录。在 build 目录中,我运行 cmake .. 和 make。在 inc 目录中,我放置了来自 GIT/googletest/googlemock/include/ 和 GIT/googletest/googletest/include/ 的所有标头。在 lib 目录中仅放置 libgmock.a。在 src - 我的项目的来源。

我的CMakeLists.txt 包含:

project(blockchain)

cmake_minimum_required(VERSION 2.6)

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB CPPS "*.cpp")

include_directories("../inc/")
link_directories("../lib/")

add_definitions(-Wall -O2 -std=c++11)
add_executable(${PROJECT_NAME} ${CPPS})
target_link_libraries(blockchain gmock)

install(TARGETS ${PROJECT_NAME} DESTINATION bin)

当我尝试创建我的项目时(在构建目录中输入 cmake .. && make)我得到链接错误:

CMakeFiles/blockchain.dir/main.cpp.o: 在函数 main': main.cpp:(.text.startup+0x13): 未定义引用 totesting::UnitTest::GetInstance()' main.cpp:(.text.startup+0x1b): 未定义引用 testing::UnitTest::Run()' ../lib/libgmock.a(gmock-all.cc.o): 在 functiontesting::internal::scoped_ptr

::reset(std::__cxx11::basic_stringstream) [克隆 .part.81] [克隆 .constprop.367]': gmock-all.cc:(.text+0x21a): 未定义引用 testing::internal::IsTrue(bool)' ../lib/libgmock.a(gmock-all.cc.o): In functiontesting::internal::MutexBase::AssertHeld() const [克隆 .constprop.368]': gmock-all.cc:(.text+0x274): undefined reference to testing::internal::IsTrue(bool)' gmock-all.cc:(.text+0x2b2): undefined 参考 totesting::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, 字符常量,整数)' gmock-all.cc:(.text+0x2fd): 未定义引用 testing::internal::GTestLog::~GTestLog()' gmock-all.cc:(.text+0x312): 未定义对testing::internal::GTestLog::~GTestLog()'的引用 ../lib/libgmock.a(gmock-all.cc.o): 在函数测试中::internal::Expect(bool, char const*, int, std::__cxx11::basic_string, std::allocator > const&) [克隆 .part.51]': gmock-all.cc:(.text+0x910): 未定义的引用 totesting::Message::Message()' gmock-all.cc:(.text+0x926): 未定义引用 testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' gmock-all.cc:(.text+0x931): 未定义 参考 totesting::internal::AssertHelper::operator=(testing::Message const&) 常量' gmock-all.cc:(.text+0x939): undefined reference to testing::internal::AssertHelper::~AssertHelper()' gmock-all.cc:(.text+0x94b): 未定义的引用 totesting::internal::IsTrue(bool)'

还有很多其他链接错误。我做错了什么?

【问题讨论】:

  • 为什么要将头文件和编译库从 gtest/gmock 目录移动到项目中的某些目录?根据gtest docs“稳健而灵活的方法是将gtest直接构建为该项目的一部分”。

标签: c++ c++11 linker googletest


【解决方案1】:

gmock 依赖于gtest。您添加了前者,但没有添加后者。

【讨论】:

  • 这不是问题。您只能与 gmock 目标链接,它会起作用。在一台机器上,我什至曾经在libc 中遇到过双重释放问题,因为它同时链接到gmockgtest。我不记得究竟是什么导致了测试关闭时崩溃,但它与静态数据初始化有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
相关资源
最近更新 更多