问题描述
我尝试使用通过柯南安装的gtest,但最终出现未定义的引用链接器错误。这个问题或多或少是对。但是我认为所提供的示例很简单。我使用gcc 6.3在最新的arch linux x64下编译。
I tried to use gtest installed through conan, but ended up with an undefined reference linker error. This question is a more or less a follow up to this stackoverflow question. But I think the provided example was to simple. I compile under up to date arch linux x64, using gcc 6.3.
C ++版本可能会有些不匹配吗?还是您对解决该问题还有其他想法?
我将在下面提供我的源代码:
I will provide my source code in the following:
目录树:
tree
.
├── CMakeLists.txt
├── conanfile.txt
└── main.cpp
main.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:
The 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:
The conanfile.txt:
[requires]
gtest/1.7.0@lasote/stable
[generators]
cmake
我尝试使用以下命令构建项目:
I tried to build the project with the following commands:
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
推荐答案
我找到了我的问题的答案:
I found an answer to my question:
问题是柯南默认使用 libstdc ++
即使我的编译器(gcc 6.3)默认情况下使用
libstdc ++ 11
。因此,在 libstdc ++
和 libstdc ++ 11
之间存在不匹配。
The problem is that conan does download/compile gtest binariesby default with libstdc++
even if my compiler (gcc 6.3) useslibstdc++11
by default. Thus there is a mismatch between libstdc++
and libstdc++11
.
要解决此问题,您必须明确告诉柯南使用libstdc ++ 11进行编译:
To workaround this issue you have to explicit tell conan to compile with libstdc++11:
conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11
这篇关于随柯南安装的GTest:未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!