A. Osterthun
A. Osterthun

Reputation: 185

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:

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.

Upvotes: 4

Views: 10714

Answers (3)

Tudax
Tudax

Reputation: 1

For me even if gtest conan package got no_main=False , i had to manually add a main.cpp file to my test folder to make it work.

The content of the main.cpp file :

#include <gtest/gtest.h>

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

Upvotes: 0

drodri
drodri

Reputation: 5962

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

Upvotes: 9

A. Osterthun
A. Osterthun

Reputation: 185

There seem to be two different solutions to this.

For one you can set conan to use libstdc++11.

This question was also answered here: GTest installed with Conan: undefined reference

Upvotes: 3

Related Questions