如何将gtest与cmake和conan链接?

问题描述 投票:0回答:1

我正在尝试在我的项目中使用 gtest。我正在使用 conan 2.x 安装 gtest。这是我的conanfile.txt:

[requires]
gtest/1.15.0

[generators]
CMakeDeps
CMakeToolchain

然后我正在做 “conan install . --output-folder=build --build=missing” 以获得 gtest 依赖项。输出:

======== Computing dependency graph ========
Graph root
    conanfile.txt: D:\Work\probemanager\components\sl-common-utils\conanfile.txt
Requirements
    gtest/1.15.0#9eb07f548819215e766e41399858454f - Cache

======== Computing necessary packages ========
Requirements
    gtest/1.15.0#9eb07f548819215e766e41399858454f:e8881c31d7715e9ffccf19b87014654db155beed#d82fd97b1021341bb14d4ddd4f68739d - Cache

======== Installing packages ========
gtest/1.15.0: Already installed! (1 of 1)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: gtest/1.15.0

======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to D:\Work\probemanager\components\sl-common-utils
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(GTest)
    target_link_libraries(... gtest::gtest)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: CMakeToolchain: Preset 'conan-default' added to CMakePresets.json.
    (cmake>=3.23) cmake --preset conan-default
    (cmake<3.23) cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake  -DCMAKE_POLICY_DEFAULT_CMP0091=NEW
conanfile.txt: CMakeToolchain generated: D:\Work\probemanager\components\sl-common-utils\CMakePresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully

我的目录树:

▒▒▒▒build
▒▒▒▒include
    ▒▒▒▒Thread.h
▒▒▒▒src
    ▒▒▒▒Thread.cpp
▒▒▒▒tests
    ▒▒▒▒TestThread.cpp
    ▒▒▒▒CMAkeLists.txt
▒▒▒▒CMakeLists.txt
▒▒▒▒conanfile.txt

来自根目录的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0091 NEW)

project(SLCommonUtils)

option(SL_WITH_TESTS "Build SL with tests" OFF)

file(GLOB SRCS "src/*.cpp" "include/*.h")

add_library(${PROJECT_NAME} STATIC ${SRCS})

target_include_directories(${PROJECT_NAME} PRIVATE include)

if (SL_WITH_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif(SL_WITH_TESTS)

还有我的测试目录中的 CMakeLists:

find_package(GTest REQUIRED COMPONENTS gtest_main)

add_executable(ThreadTest ThreadTest.cpp)
target_link_libraries(ThreadTest PRIVATE SLCommonUtils gtest::gtest)

add_test(NAME ThreadTest COMMAND ThreadTest)

因此,我使用命令构建此项目:cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DSL_WITH_TESTS=On。输出:

-- Using Conan toolchain: D:/Work/probemanager/components/sl-common-utils/build/conan_toolchain.cmake
-- Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143
-- Conan toolchain: Setting CMAKE_MSVC_RUNTIME_LIBRARY=$<$<CONFIG:Release>:MultiThreadedDLL>
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045.
-- Conan: Component target declared 'GTest::gtest'
-- Conan: Component target declared 'GTest::gtest_main'
-- Conan: Component target declared 'GTest::gmock'
-- Conan: Component target declared 'GTest::gmock_main'
-- Conan: Target declared 'gtest::gtest'
-- Configuring done (0.0s)
-- Generating done (0.1s)
-- Build files have been written to: D:/Work/probemanager/components/sl-common-utils/build

在此构建之后,我将构建目录并在 Visual Studio 中打开 .sln 文件。我这样编写示例测试:

#include "Thread.h" 

#include "gtest/gtest.h"

TEST(ThreadTest, ExampleTest) {
    EXPECT_EQ(1, 1);
}

为什么 Visual Studio 找不到 gtest 包含文件?也许我在 CMakeLists 中犯了一些错误?

编译输出:

Assembly started...
1>------ Assembly started: Project: ThreadTest, Configuration: Debug x64 ------
1>ThreadTest.cpp
1>D:\Work\probemanager\components\sl-common-utils\tests\ThreadTest.cpp(3,10): fatal error C1083: Cannot open file inclusion: gtest/gtest.h: No such file or directory,
1>The “ThreadTest.vcxproj” project build was terminated with an error.
c++ cmake conan
1个回答
0
投票

在 conan github repo issues 中你可以找到类似的问题:https://github.com/conan-io/conan/issues/16721

我认为你的问题是由于 conan 和 Visual Studio 使用不同的默认设置而出现的。

Conan 默认使用

Release
构建模式,而 Visual Studio 默认使用
Debug
构建模式。尝试在 IDE 中选择
Release
构建模式,看看它是否有效。您还可以尝试在
Debug
构建模式下使用 conan。

© www.soinside.com 2019 - 2024. All rights reserved.