簡體   English   中英

與柯南一起安裝的GTest:未定義的引用

[英]GTest installed with Conan: undefined reference

我嘗試使用通過conan安裝的gtest,但最終導致未定義的引用鏈接器錯誤。 這個問題或多或少是對這個stackoverflow問題的一個跟進。 但我認為提供的例子很簡單。 我使用gcc 6.3在最新的arch linux x64下編譯。

可能有一些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/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

我找到了一個問題的答案:

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

要解決此問題,您必須明確告訴conan使用libstdc ++ 11進行編譯:

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

我最終不得不在項目的conanfile.py添加self.options['gtest'].shared = True來解決這個問題。 以前,由於某些與Windows無關的原因,它被設置為false。

嘗試更改為gtest / gmock的共享庫,如果像我一樣,您看到默認設置已經是libstdc++11那么更改conan install args是不夠的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM