3

I use cmake to build my project and conan to install Google Test as dependency:

conanfile.txt

[requires]
gtest/1.7.0@lasote/stable

[generators]
cmake

[imports]
bin, *.dll -> ./build/bin
lib, *.dylib* -> ./build/bin

CMakeLists.txt

PROJECT(MyTestingExample)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

INCLUDE(conanbuildinfo.cmake)
CONAN_BASIC_SETUP()

ADD_EXECUTABLE(my_test test/my_test.cpp)
TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS})

test/my_test.cpp

#include <gtest/gtest.h>
#include <string>

TEST(MyTest, foobar) {
    std::string foo("foobar");
    std::string bar("foobar");
    ASSERT_STREQ(foo.c_str(), bar.c_str()); // working
    EXPECT_FALSE(false); // error
}

Build

$ conan install --build=missing
$ mkdir build && cd build
$ cmake .. && cmake --build .

I can use ASSERT_STREQ, but if I use EXPECT_FALSE I get an unexpected error:

my_test.cpp:(.text+0x1e1): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status

What's wrong with my configuration?

maiermic
  • 4,764
  • 6
  • 38
  • 77
  • In which OS and compiler (with version) are you running? The ``conaninfo.txt`` and ``CONAN_LIBS`` content might be useful too. – drodri Dec 31 '16 at 12:55
  • 1
    It could be a ``libcxx`` mismatch, from the error output it seems that you should be using ``libcxx=libstdc++11``. If in linux (in general, not multi-configuration envs), you should use ``cmake .. -DCMAKE_BUILD_TYPE=Release``, assuming that the conan install used the default setting ``-s build_type=Release`` – drodri Dec 31 '16 at 12:58
  • Thank you very much, `-DCMAKE_BUILD_TYPE=Release` fixes this issue. – maiermic Dec 31 '16 at 13:55
  • Good! Adding it as an answer then – drodri Dec 31 '16 at 14:56

2 Answers2

7

The issue is that you are installing conan dependencies using the default settings (which is build type Release):

$ conan install --build=missing
# equivalent to
$ conan install -s build_type=Release ... --build=missing

The default settings can be seen in your conan.conf file

Then, you are using cmake in a nix system with the default build type which is Debug, which is a single-conf environment (opposed to multi-configuration Debug/Release environments, as Visual Studio), so when you are doing:

$ cmake .. && cmake --build .
# equivalent to
$ cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build .

The incompatibility of Debug/Release build leads to that unresolved issue. So the solution would be to use the same build type that matches your installed dependencies:

$ cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build .

If using multi-configuration environments like Visual Studio, the correct way would be:

$ cmake .. && cmake --build . --config Release
drodri
  • 5,157
  • 15
  • 21
  • Thank you for your answer, it also works for me for the particular example of @maiermic but using different asserts still shows undefined reference to me (eg. `ASSERT_TRUE("hi" == "hallo");)`. Did you also tried different assert types? – erikzenker Feb 06 '17 at 15:38
  • 1
    This is weird, one assertion working but not the other. Maybe deserves its own question with detailed output and files, or maybe submitting issue to the package author repo: https://github.com/lasote/conan-gtest (if it is the package you are using) – drodri Feb 07 '17 at 08:59
  • I started a follow up question http://stackoverflow.com/questions/42162014/gtest-installed-with-conan-undefined-reference and also opened an issue on the repo of lasote https://github.com/lasote/conan-gtest/issues/20 – erikzenker Feb 10 '17 at 14:41
  • This helped me to narrow down the issue, thanks. Another option is to change `build_type=Debug` in `~/.conan/profiles/default`; you may need to completely wipe the `data` folder and your `build` directory too. – Marco Massenzio Mar 16 '20 at 05:10
1

On my side, I get this issue because being on Redhat 7 then with an old libstdc++. Too old for conan default packages binaries/libraries.

I have fixed that by rebuilding gtest with '--build gtest' arg.