6

我尝试使用通过柯南安装的 gtest,但最终出现未定义的引用链接器错误。这个问题或多或少是这个 stackoverflow question的后续问题。但我认为提供的示例很简单。我使用 gcc 6.3 在最新的 arch linux x64 下编译。

C++ 版本会不会有一些不匹配?或者您对如何解决问题有任何其他想法?

我将在下面提供我的源代码:

目录树:

tree
.
├── CMakeLists.txt
├── conanfile.txt
└── main.cpp

主.cpp:

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

class TestFixture : public ::testing::Test {
protected:
    void SetUp(){
    std::cout << "SetUp()" << std::endl;
    }

    void TearDown(){
    std::cout << "TearDown()" << std::endl;
    }
};



TEST_F (TestFixture, shouldCompile) {
    std::cout << "shouldCompile" << std::endl;
    ASSERT_TRUE(true); // works, maybe optimized out?
    ASSERT_TRUE("hi" == "hallo"); // undefined reference

}

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

CMakeLists.txt:

project(ConanGtestExample)
cmake_minimum_required(VERSION 2.8.12)

set(CMAKE_CXX_STANDARD 11)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

# Necessary to compile gtest
# - dependencies and final build
#   need to be compiled with same
#   build type. Otherwise linker
#   error will occure.
set(CMAKE_BUILD_TYPE Release)

add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})

conanfile.txt:

[requires]
gtest/1.7.0@lasote/stable

[generators]
cmake

我尝试使用以下命令构建项目:

mkdir build
cd build
conan install -s build_type=Release .. --build=missing
cmake .. -DCMAKE_BUILD_TYPE=Release 
cmake --build .

未定义的参考输出:

Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `TestFixture_shouldCompile_Test::TestBody()':
main.cpp:(.text+0x99): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:95: bin/main] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
4

2 回答 2

9

我找到了我的问题的答案:

libstdc++问题是,即使我的编译器(gcc 6.3)默认使用 ,柯南也会默认下载/编译 gtest 二进制文件libstdc++11。因此 和之间存在不匹配libstdc++libstdc++11

要解决此问题,您必须明确告诉柯南使用 libstdc++11 进行编译:

conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11
于 2017-02-10T16:10:25.907 回答
3

我最终不得不添加self.options['gtest'].shared = True项目conanfile.py来解决这个问题。以前,由于某些与 Windows 相关的原因变得无关紧要,它被设置为 false。

如果像我一样看到默认设置已经libstdc++11如此,因此更改conan installargs 还不够,请尝试更改为 gtest/gmock 的共享库。

于 2018-03-29T19:23:41.467 回答