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

vcpkg清单模式下CMake报错:路径包含非法字符

问题描述

已设置环境变量VCPKG_ROOT=C:\vcpkg,使用vcpkg清单模式时,配置CMakeUserPresets.json无论通过环境变量引用还是直接填写绝对路径指定CMAKE_TOOLCHAIN_FILE,都触发「路径中存在非法字符」错误。

配置文件内容

{
  "version": 2,
  "configurePresets": [
    {
      "name": "default",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
        //"CMAKE_TOOLCHAIN_FILE": "c:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
      }
    }
  ]
}

CMake报错信息

1> CMake generation started for configuration: 'default'.
1> 路径中存在非法字符。
路径中存在非法字符。
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.Combine(String path1, String path2)
   at libcmake.RelativePathStringComparer.GetCanonicalPath(String path)
   at libcmake.RelativePathStringComparer.Equals(String x, String y)
   at libcmake.CMakeDaemonDriver.HasToolchainChanged(CMakeCommandLine cmdLine, String currentToolchain, List`1 messages, IEqualityComparer`1 comparer)
   at libcmake.CMakeDaemonDriver.<DeleteCacheIfConsistencyCheckFailsAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at libcmake.CMakeListsParser.<SetAndCheckToolchainAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at libcmake.CMakeListsParser.<ParseImplAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at libcmake.CMakeListsParser.<ParseAsync>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.CMake.Project.ExternalBuildProject.ProjectProvider.<ParseProjectAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.CMake.Project.ExternalBuildProject.ProjectProvider.<ParseProjectAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.ParseCache.<ParseAsyncImpl>d__34.MoveNext()
解决方法

1. 移除JSON中的注释(核心问题)

JSON语法不支持//单行注释,配置文件中注释的行会被解析为非法字符,导致路径解析失败。修改后的配置文件如下:

{
  "version": 2,
  "configurePresets": [
    {
      "name": "default",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
      }
    }
  ]
}

如需保留注释内容,建议移到JSON文件外部,CMake仅支持标准JSON语法。

2. 验证环境变量有效性

  • 打开命令提示符,输入echo %VCPKG_ROOT%,确认输出为C:\vcpkg,且路径无空格、特殊字符。
  • 重启Visual Studio,确保环境变量已被正确加载。

3. 检查路径转义(直接填写绝对路径时)

使用绝对路径时,JSON中需用双反斜杠转义,确保写法正确:

"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"

4. 清理CMake缓存

删除build目录下的所有缓存文件,重新触发CMake生成,避免旧缓存干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 09:57:34