c++ 使用GoogleTest库编译程序失败,该库由Conan安装

wb1gzix0  于 2022-12-24  发布在  Go
关注(0)|答案(2)|浏览(157)

我尝试使用通过Conan包管理器安装的GTest,但是得到了一个承保错误。
main.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})

conanfile.txt

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

[generators]
cmake

build.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,编译program而不测试,运行program后,得到以下输出:

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

58wvjzkj1#

我解决了这个问题:
柯南的CMake Package 帮助了我。
查看此处:https://github.com/conan-io/cmake-conan

h9vpoimq

h9vpoimq2#

有一种更新的方法可以使用conan将GTest添加到CMake中。
首先,使用以下生成器(more info):

[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)

相关问题