GTest installed with Conan: undefined reference
Asked Answered
W

2

7

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.

Could there be some missmatch of C++ versions? Or do you have any other idea of how to fix the Problem?

I will provide my source code in the following:

Directory tree:

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();
}

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})

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 .

The undefined reference output:

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
Weimaraner answered 10/2, 2017 at 14:40 Comment(0)
W
10

I found an answer to my question:

The problem is that conan does download/compile gtest binaries by default with libstdc++ even if my compiler (gcc 6.3) uses libstdc++11 by default. Thus there is a mismatch between libstdc++ and 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
Weimaraner answered 10/2, 2017 at 16:10 Comment(3)
I was about to answer it, you were faster by seconds! The fact is that conan uses the default libstdc++, you can check it in your <home>/.conan/conan.conf, because it is the one providing wider compatibility. You might want to read blog.conan.io/2016/03/22/…. TL;DR: with libstdc++ you can make bins that work in most distros, even older ones, that even gcc>5.1, they come with libstdc++ (not 11)Tildatilde
you can also change this in the profile. You can find the profiles in ~/.conan/profiles you can either create a new one, or update the default one. the key value I needed was: compiler.libcxx=libstdc++11.Scirrhus
To set this as the default: conan profile update settings.compiler.libcxx=libstdc++11 defaultIncautious
D
3

I ended up having to add self.options['gtest'].shared = True in the project's conanfile.py to get around this. Previously it was set to false for some windows-related reasons that became non-relevant.

Try changing to shared libraries for gtest/gmock if, like me, you saw that the default settings were already libstdc++11 so changing the conan install args was not sufficient.

Dungeon answered 29/3, 2018 at 19:23 Comment(1)
Configuring self.options['gtest'].shared = True worked in my case, too. Previously, I also configured self.options['gtest'].shared == False, and GCC fails for some projects (ONLY some projects which I believe that I configured the same as other successful projects!) while MSVC and Clang does not have such problems.Hexahydrate

© 2022 - 2024 — McMap. All rights reserved.