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

如何在VS Code CMake配置中指定Ninja路径且不修改系统PATH

不修改系统PATH,在VS Code中配置CMake指定Ninja路径的解决方法

问题背景

需要在不修改操作系统PATH环境变量的前提下,通过VS Code的CMake配置指定Ninja路径,但现有配置(CMakeSettings.json、cmake-kits.json、settings.json)均未生效,启动时出现以下错误:

[proc] The command: ninja --version failed with error: Error: spawn ninja ENOENT
[proc] The command: ninja-build --version failed with error: Error: spawn ninja-build ENOENT
[main] Unable to determine what CMake generator to use. Please install or configure a preferred generator, or update settings.json, your Kit configuration or PATH variable. Error: No usable generator found.

错误原因

VS Code的CMake插件在确定生成器前,会直接调用ninja --version验证工具可用性,这一步不会读取你传给CMake的CMAKE_MAKE_PROGRAM参数,因此即使在configureArgs中设置了该参数,插件依然无法找到Ninja。

可行解决方案

方案一:通过cmake-kits.json配置Kit环境

在你的kit配置中添加临时环境变量和CMake设置,既让插件能检测到Ninja,又能确保CMake使用指定的Ninja路径:

[
  {
    "name": "riscv32-esp-elf-gcc",
    "isTrusted": true,
    "compilers": {
      "C": "/ProgramData/esp-idf-tools/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc.exe",
      "CXX": "/ProgramData/esp-idf-tools/tools/riscv32-esp-elf/esp-2022r1-11.2.0/riscv32-esp-elf/bin/riscv32-esp-elf-g++.exe"
    },
    "toolchainFile": "/ProgramData/esp-idf/tools/cmake/toolchain-esp32c3.cmake",
    "preferredGenerator": {
      "name": "Ninja"
    },
    "environment": {
      "PATH": "/ProgramData/esp-idf-tools/tools/ninja/1.10.2;${env:PATH}"
    },
    "cmakeSettings": {
      "CMAKE_MAKE_PROGRAM": "/ProgramData/esp-idf-tools/tools/ninja/1.10.2/ninja.exe"
    }
  }
]

方案二:通过settings.json配置插件环境

在VS Code的settings.json中添加cmake.environment,将Ninja所在目录临时加入插件运行时的PATH:

{
    "cmake.cmakePath": "/ProgramData/esp-idf-tools/tools/cmake/3.24.0/bin/cmake.exe",
    "cmake.configureArgs": [
        "-DCMAKE_MAKE_PROGRAM=/ProgramData/esp-idf-tools/tools/ninja/1.10.2/ninja.exe"
    ],
    "cmake.environment": {
        "PATH": "/ProgramData/esp-idf-tools/tools/ninja/1.10.2;${env:PATH}"
    }
}

注意事项

  • 无需在cmake.buildArgs中重复设置CMAKE_MAKE_PROGRAM,CMake在配置阶段会记住该参数。
  • 两种方案均不会修改系统级PATH,仅对VS Code的CMake插件生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 00:47:44