1

I tried to use GTest installed through Conan package manager, but get an underwritten error.

main.cpp:

#include <gtest/gtest.h>

TEST(tests, t_first)
{
    ASSERT_EQ(1,1);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8) 
project(ranges-v3 LANGUAGES CXX)  

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})

conanfile.txt

[requires]
range-v3/0.9.1
fmt/6.2.0
gtest/1.10.0

[generators]
cmake

build.sh

mkdir build
cd build
conan install ..
cmake ..
make
cp -f bin/main ../main

I am building project, by usung build.sh and get the following message:

Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++
compiler.version=7
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

conanfile.txt: Installing package
Requirements
    fmt/6.2.0 from 'conan-center' - Cache
    gtest/1.10.0 from 'conan-center' - Cache
    range-v3/0.9.1 from 'conan-center' - Cache
Packages
    fmt/6.2.0:d351525cc53ebe68279edf1978846402420066e7 - Cache
    gtest/1.10.0:4a8c5b4cd3b4d45b83fff85d53160ea02ae5fa2d - Cache
    range-v3/0.9.1:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache

Installing (downloading, building) binaries...
fmt/6.2.0: Already installed!
gtest/1.10.0: Already installed!
range-v3/0.9.1: Already installed!
conanfile.txt: Generator cmake created conanbuildinfo.cmake
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo
-- Conan: Adjusting output directories
-- Conan: Using cmake global configuration
-- Conan: Adjusting default RPATHs Conan policies
-- Conan: Adjusting language standard
-- Current conanbuildinfo.cmake directory: /home/bogdasar/Documents/C++_Programming/HuffmanCode/build
-- Conan: Compiler GCC>=5, checking major version 7
-- Conan: Checking correct version: 7
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bogdasar/Documents/C++_Programming/HuffmanCode/build
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
main.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x87): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:94: recipe for target 'bin/main' failed
make[2]: *** [bin/main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

But if I delete TEST from main.cpp, compile programm without test, after running programm, get follow output:

[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Bogdasar
  • 181
  • 1
  • 1
  • 9
  • 1
    "I tried to use GTest installed through Conan package manager" - Are you sure you have no GTest installed **system-wide**? The error seems to indicate **incompatibility** between GTest headers and libraries. Like described in that [answer](https://stackoverflow.com/a/36643372/3440745) for the question about similar error message. – Tsyvarev Apr 21 '20 at 13:28

2 Answers2

1

There is a newer way of adding GTest to CMake with conan.

First, using the following generators (more info):

[requires]
gtest/cci.20210126

[generators]
CMakeDeps
CMakeToolchain

And then, in the required CMakeLists.txt it's enough with the lines find_package(GTest) and target_link_libraries(${PROJECT_NAME} gtest::gtest):

cmake_minimum_required(VERSION 3.15)
project(gtest_project CXX)

find_package(GTest)

add_executable(${PROJECT_NAME} src/main.cpp)

target_link_libraries(${PROJECT_NAME} gtest::gtest)
Tarod
  • 6,732
  • 5
  • 44
  • 50
0

I solved the problem:

CMake wrapper for Conan helped me.

Look here: https://github.com/conan-io/cmake-conan

Bogdasar
  • 181
  • 1
  • 1
  • 9