You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tool_requires(cmake)构建Conan依赖GTest时提示cmake未找到

Conan依赖构建问题:Test依赖GTest无法使用tool_requires的CMake

问题描述

项目根目录的conanfile.py内容如下:

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout


class APL(ConanFile):
    name    = "***"
    version = "1.0.0"

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    generators = "VirtualBuildEnv"

    def build_requirements(self):
        self.tool_requires("cmake/3.22.6")
        self.test_requires("gtest/1.16.0")

    def configure(self):
        return
    def layout(self):
        cmake_layout(self)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()

        tc = CMakeToolchain(self)
        tc.cache_variables["CONAN_PROJECT_VERSION"] = self.version
        tc.cache_variables["CONAN_PROJECT_NAME"] = self.name
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

执行命令:

conan install . --build missing

构建GTest时出现错误,提示:

'cmake' is not recognized as an internal or external command,
operable program or batch file.

系统未全局安装CMake,期望通过self.tool_requires("cmake/3.22.6")提供CMake,但未生效。

完整失败日志及环境信息

执行日志

conan install . -b missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Debug
compiler=gcc
compiler.cppstd=23
compiler.libcxx=libstdc++
compiler.version=14
os=Windows

Profile build:
[settings]
arch=x86_64
build_type=Debug
compiler=gcc
compiler.cppstd=23
compiler.libcxx=libstdc++
compiler.version=14
os=Windows


======== Computing dependency graph ========
Graph root
    conanfile.py (alarm-point-lib/1.0.0): G:\cpp\alarm-point-library\conanfile.py
Test requirements
    gtest/1.16.0#4fd8d9d80636ea9504de6a0ec1ab8686 - Cache
Build requirements
    cmake/3.22.6#32cced101c6df0fab43e8d00bd2483eb - Cache

======== Computing necessary packages ========
Connecting to remote 'conancenter' anonymously
Test requirements
    gtest/1.16.0#4fd8d9d80636ea9504de6a0ec1ab8686:36b58f8adc45de83a03da2d614722cbcac25bc82 - Build
Build requirements
    cmake/3.22.6#32cced101c6df0fab43e8d00bd2483eb:522dcea5982a3f8a5b624c16477e47195da2f84f#a9d024f459972755e5a815b775408fff - Cache

======== Installing packages ========
cmake/3.22.6: Already installed! (1 of 2)
cmake/3.22.6: Appending PATH environment variable: A:\Conan\p\cmake1927748c2604b\p\bin

-------- Installing package gtest/1.16.0 (2 of 2) --------
gtest/1.16.0: Building from source
gtest/1.16.0: Package gtest/1.16.0:36b58f8adc45de83a03da2d614722cbcac25bc82
gtest/1.16.0: Copying sources to build folder
gtest/1.16.0: Building your package in A:\Conan\p\b\gtest74bcb8da0522a\b
gtest/1.16.0: Calling generate()
gtest/1.16.0: Generators folder: A:\Conan\p\b\gtest74bcb8da0522a\b\build\Debug\generators
gtest/1.16.0: CMakeToolchain generated: conan_toolchain.cmake
gtest/1.16.0: CMakeToolchain generated: A:\Conan\p\b\gtest74bcb8da0522a\b\build\Debug\generators\CMakePresets.json
gtest/1.16.0: CMakeToolchain generated: A:\Conan\p\b\gtest74bcb8da0522a\b\src\CMakeUserPresets.json
gtest/1.16.0: Generating aggregated env files
gtest/1.16.0: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
gtest/1.16.0: Calling build()
gtest/1.16.0: apply_conandata_patches(): No patches defined in conandata
gtest/1.16.0: Running CMake.configure()
gtest/1.16.0: RUN: cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="A:/Conan/p/b/gtest74bcb8da0522a/p" -DCMAKE_SH="CMAKE_SH-NOTFOUND" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Debug" "A:/Conan/p/b/gtest74bcb8da0522a/b/src"
'cmake' is not recognized as an internal or external command,
operable program or batch file.

gtest/1.16.0: ERROR:
Package '36b58f8adc45de83a03da2d614722cbcac25bc82' build failed
gtest/1.16.0: WARN: Build folder A:\Conan\p\b\gtest74bcb8da0522a\b\build\Debug
ERROR: gtest/1.16.0: Error in build() method, line 140
        cmake.configure()
        ConanException: Error 1 while executing

环境信息

  • CLion 2025.1
  • Windows 10
  • MinGW构建工具

问题原因

self.tool_requires("cmake/3.22.6")默认仅作用于host上下文,而gtest/1.16.0作为test_require,其构建过程属于build上下文的任务,因此构建GTest时无法找到host上下文的CMake依赖。

修复方案

方案1:修改conanfile.py,指定CMake为build上下文依赖

在build_requirements方法中,为tool_requires添加build=True参数,将CMake关联到build上下文:

def build_requirements(self):
    # 为build上下文声明CMake依赖,确保构建GTest时能调用到
    self.tool_requires("cmake/3.22.6", build=True)
    self.test_requires("gtest/1.16.0")

方案2:在Build Profile中全局配置build_requires

编辑你的build profile文件,添加[build_requires]配置:

[build_requires]
cmake/3.22.6

修改后重新执行conan install . --build missing即可正常构建GTest。

内容的提问来源于stack exchange,提问作者DoktorovAV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 21:05:17