2017-02-10 71 views
3

我試圖使用通過柯南安裝的gtest,但結束了未定義的參考鏈接器錯誤。這個問題或多或少是跟進this stackoverflow question。但我認爲提供的例子很簡單。我使用gcc 6.3編譯了最新的arch linux x64。GTest與柯南一起安裝:未定義的參考

會有一些C + +版本的錯位?還是你有任何其他想法來解決這個問題?

我會提供以下我的源代碼:

目錄樹:

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 

回答

3

我發現一個回答我的問題:

問題是,柯南下載/編譯gtest二進制文件即使我的編譯器(gcc 6.3)默認使用 libstdc++11,默認情況下爲也是libstdc++。因此,在libstdc++libstdc++11之間存在錯配

要解決這個問題,你必須明確的告訴柯南用的libstdC++ 11編譯:

conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11 
+1

我剛想回答這個問題,你快了秒!事實是,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++'',你可以在大多數發行版甚至更老的發行版上運行bin,甚至gcc> 5.1,它們都帶有libstdC++(不是11) – drodri