繁体   English   中英

无法使用柯南安装的 GoogleTest 库编译程序

[英]Failed to compile programm with GoogleTest library, installed by Conan

我尝试使用通过 Conan 包管理器安装的 GTest,但遇到了包销错误。

主.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})

柯南文件.txt

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

[generators]
cmake

构建.sh

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

我正在构建项目,通过 usung build.sh并收到以下消息:

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

但是如果我从main.cpp中删除TEST ,编译程序而不测试,运行程序后,得到以下输出:

[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

我解决了这个问题:

柯南的 CMake 包装器帮助了我。

看这里: https://github.com/conan-io/cmake-conan

有一种更新的方法可以使用柯南将 GTest 添加到 CMake。

首先,使用以下生成器( 更多信息):

[requires]
gtest/cci.20210126

[generators]
CMakeDeps
CMakeToolchain

然后,在所需的CMakeLists.txt中,使用find_package(GTest)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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM