conan経由でインストールされたgtestを使用しようとしましたが、定義されていない参照リンカーエラーが発生しました。この質問は、多かれ少なかれ、this stackoverflow questionまで続きます。しかし、私は提供された例が単純だと思う。私はgcc 6.3を使って最新のarch linux x64をコンパイルしています。GTanとConanがインストールされています:未定義の参照
C++バージョンのミスマッチがありますか?それとも、問題を解決する方法が他にありますか?
Directoryツリー:
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:
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/[email protected]/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
が、私はそれに答えることを約あった、あなたは秒速かったです!実際のところ、conanはデフォルトの '' libstdC++ ''を使用しています。より広い互換性を提供するので、 ''/.conan/conan.conf''で確認できます。 http://blog.conan.io/2016/03/22/From-CMake-syntax-to-libstdc++ABI-incompatibiliy-migrations-are-always-hard.htmlを読むことができます。 TL:DR: '' libstdC++ ''を使うと、ほとんどのディストリビューション(gcc> 5.1でもlibstdC++(11ではなく) –
drodri