如何通过conan使用gtest



我目前正在学习柯南。作为一个实践项目,我想创建一个带有单元测试的库(我很清楚,使用conan进行gtest并不是最有用的事情,但这只是一个例子(。

我的项目目录:

  • 构建
    • bin
      • 阵列测试
  • src
    • 阵列.hpp
  • 测试
    • array_tests.cpp
  • conanfile.txt
  • CMakeLists.txt

我的conanfile.txt

[requires]
gtest/1.10.0
[generators]
cmake

我的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)

我的array_tests.cpp:

#include <gtest/gtest.h>
#include <iostream>
TEST(Array, test){
std::cout << "g" << std::endl;
EXPECT_EQ(1, 1);
}

来自build:

./bin/array_tests

执行array_tests 时出错

munmap_chunk(): invalid pointer
Aborted (core dumped)

来自build:

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

输出:

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

有什么提示为什么这不起作用吗?

我通过target_link_libraries添加gtest_main没有帮助。

入门文档中有一条警告:

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  #

当首次初始化默认配置文件时,您可能也会看到此警告。

默认值是libstdc++,因为它也与旧发行版广泛兼容。您需要使用将默认值更改为libstdc++11

$ conan profile update settings.compiler.libcxx=libstdc++11 default

对此似乎有两种不同的解决方案。

首先,您可以将conan设置为使用libstdc++11。

这个问题也在这里得到了回答:安装了柯南的GTest:未定义的参考

对我来说,即使gtest conan包得到了no_main=False,我也必须手动将main.cpp文件添加到我的测试文件夹中才能使其工作。

main.cpp文件的内容:

#include <gtest/gtest.h>
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

最新更新