Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gtest via conan

I'm currently learning about Conan. As a practice projcet I would like to create a library with unit tests (I am well aware that using conan for gtest is not the most usefull thing to do, but this is just an example).

My project directory:

  • build
    • bin
      • array_tests
  • src
    • Array.hpp
  • tests
    • array_tests.cpp
  • conanfile.txt
  • CMakeLists.txt

My conanfile.txt

[requires]
gtest/1.10.0

[generators]
cmake

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(ralib)
enable_testing()

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

add_library(ralib INTERFACE)

add_executable(array_tests tests/array_tests.cpp)
target_link_libraries(array_tests ${CONAN_LIBS})


add_test(NAME array_tests WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin COMMAND array_tests)

My array_tests.cpp:

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

TEST(Array, test){
    std::cout << "g" << std::endl;
    EXPECT_EQ(1, 1);
}

From build:

./bin/array_tests

Error on executing array_tests

munmap_chunk(): invalid pointer
Aborted (core dumped)

From build:

conan install .. && cmake .. && cmake --build . -- -j8 && ctest

Output:

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
    gtest/1.10.0 from 'conan-center' - Cache
Packages
    gtest/1.10.0:4a8c5b4cd3b4d45b83fff85d53160ea02ae5fa2d - Cache

Installing (downloading, building) binaries...
gtest/1.10.0: 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/domane/dev/dlr/ravis/ralib/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/domane/dev/dlr/ravis/ralib/build
Scanning dependencies of target array_tests
[ 50%] Building CXX object CMakeFiles/array_tests.dir/tests/array_tests.cpp.o
[100%] Linking CXX executable bin/array_tests
[100%] Built target array_tests
Test project /home/domane/dev/dlr/ravis/ralib/build
    Start 1: array_tests
1/1 Test #1: array_tests ......................***Exception: Child aborted  0.02 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.02 sec

The following tests FAILED:
          1 - array_tests (Child aborted)
Errors while running CTest

Any hints why this does not work ?

I adding gtest_main via target_link_libraries did not help.

like image 221
A. Osterthun Avatar asked Dec 18 '22 13:12

A. Osterthun


1 Answers

There is a warning in the Getting Started docs:

Important

If you are using GCC compiler >= 5.1, Conan will set the compiler.libcxx to the old ABI for backwards compatibility. You can change this with the following commands:

$ conan profile new default --detect  # Generates default profile detecting GCC and sets old ABI
$ conan profile update settings.compiler.libcxx=libstdc++11 default  #

You will probably see this warning too when the default profile is first initialized.

The default is libstdc++ because it is the one that is widely compatible with older distros as well. You need to change your default to libstdc++11 with:

$ conan profile update settings.compiler.libcxx=libstdc++11 default
like image 124
drodri Avatar answered Jan 02 '23 17:01

drodri