【问题标题】:gtest installed with conan: undefined reference to `testing::internal::GetBoolAssertionFailureMessage`与柯南一起安装的 gtest:未定义对 `testing::internal::GetBoolAssertionFailureMessage` 的引用
【发布时间】:2016-12-31 12:16:21
【问题描述】:

我使用cmake 构建我的项目并使用conan 安装Google Test 作为依赖项:

conanfile.txt

[requires]
gtest/1.7.0@lasote/stable

[generators]
cmake

[imports]
bin, *.dll -> ./build/bin
lib, *.dylib* -> ./build/bin

CMakeLists.txt

PROJECT(MyTestingExample)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

INCLUDE(conanbuildinfo.cmake)
CONAN_BASIC_SETUP()

ADD_EXECUTABLE(my_test test/my_test.cpp)
TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS})

test/my_test.cpp

#include <gtest/gtest.h>
#include <string>

TEST(MyTest, foobar) {
    std::string foo("foobar");
    std::string bar("foobar");
    ASSERT_STREQ(foo.c_str(), bar.c_str()); // working
    EXPECT_FALSE(false); // error
}

构建

$ conan install --build=missing
$ mkdir build && cd build
$ cmake .. && cmake --build .

我可以使用ASSERT_STREQ,但如果我使用EXPECT_FALSE,我会收到意外错误:

my_test.cpp:(.text+0x1e1): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status

我的配置有什么问题?

【问题讨论】:

  • 您在哪个操作系统和编译器(带有版本)中运行? conaninfo.txtCONAN_LIBS 内容也可能有用。
  • 可能是libcxx 不匹配,从错误输出看来您应该使用libcxx=libstdc++11。如果在linux中(一般来说,不是多配置环境),你应该使用cmake .. -DCMAKE_BUILD_TYPE=Release,假设conan安装使用默认设置-s build_type=Release
  • 非常感谢,-DCMAKE_BUILD_TYPE=Release 解决了这个问题。
  • 好!然后将其添加为答案

标签: c++ unit-testing googletest conan


【解决方案1】:

问题是您正在使用默认设置(构建类型 Release)安装柯南依赖项:

$ conan install --build=missing
# equivalent to
$ conan install -s build_type=Release ... --build=missing

默认设置可以在你的conan.conf文件中看到

然后,您在 nix 系统中使用 cmake,默认构建类型为 Debug,这是一个单一配置环境(与多配置调试/发布环境相反,如 Visual Studio ),所以当你在做的时候:

$ cmake .. && cmake --build .
# equivalent to
$ cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build .

Debug/Release build 的不兼容导致了这个未解决的问题。所以解决方案是使用与您安装的依赖项匹配的相同构建类型:

$ cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build .

如果使用像 Visual Studio 这样的多配置环境,正确的方法是:

$ cmake .. && cmake --build . --config Release

【讨论】:

  • 感谢您的回答,对于@maiermic 的特定示例,它也适用于我,但使用不同的断言仍然显示对我的未定义引用(例如ASSERT_TRUE("hi" == "hallo");)。您是否也尝试过不同的断言类型?
  • 这很奇怪,一个断言有效,但另一个无效。也许应该有自己的问题,详细的输出和文件,或者可能将问题提交给包作者 repo:github.com/lasote/conan-gtest(如果它是你正在使用的包)
  • 我发起了一个后续问题stackoverflow.com/questions/42162014/… 并在lasote 的回购github.com/lasote/conan-gtest/issues/20 上打开了一个问题
  • 这帮助我缩小了问题的范围,谢谢。另一种选择是将build_type=Debug更改为~/.conan/profiles/default;您可能还需要彻底清除 data 文件夹和您的 build 目录。
猜你喜欢
  • 2017-06-28
  • 2020-11-08
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多