与柯南一起安装的 gtest:对 `testing::internal::GetBoolAssertionFailureMessage` 的未定义引用

gtest installed with conan: undefined reference to `testing::internal::GetBoolAssertionFailureMessage`

我使用 cmake to build my project and conan to install 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

我的配置有什么问题?

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

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

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

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

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

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

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

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

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