我目前正在学习有关柯南的知识。作为一个实践项目,我想创建一个包含单元测试的库(我很清楚,将conan用于gtest并不是最有用的事情,但这只是一个例子)。
我的项目目录:
我的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并没有帮助。
答案 0 :(得分:2)
对此似乎有两种不同的解决方案。
您可以将柯南设置为使用libstdc ++ 11。
答案 1 :(得分:2)
Getting Started文档中有一个警告:
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
答案 2 :(得分:0)
对我来说,即使 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();
}