Visual stdio 2022 解决 Cmake 项目报错问题

折腾了好几个小时终于搞明白了

最开始的问题是我想导入一个vcpkg下载的C依赖,然后Cmake直接提示报错找不到cl

The CMAKE_C_COMPILER:

cl.exe

is not a full path and was not found in the PATH.

Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.

这个是比较常见的环境变量问题,不知道是不是我的安装版本有些差别
网上其他人的解决方案都是.\vs2022\VC\Tools\MSVC\14.40.33807\bin就有cl.exe而我需要一路找到.\vs2022\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64

总之导入后就能解决第一个问题,然后会出现一个新的问题

CMakeFiles\cmTC_43e20.dir/manifest.res: file not recognized: file format not recognized

ninja: build stopped: subcommand failed.

CMake will not be able to correctly generate this project.

这个问题的根本原因是CMakePresets.json 设置了 Ninja 作为生成器,但它与 MSVC (cl.exe) 冲突
在CMakePersets.json中把 configurePresets下的 generator 改成自己合适的版本就行 例如

{
    "name": "windows-base",
    "hidden": true,
    "generator": "Visual Studio 17 2022",
    "binaryDir": "${sourceDir}/out/build/${presetName}",
    "installDir": "${sourceDir}/out/install/${presetName}",
    "cacheVariables": {
        "CMAKE_C_COMPILER": "cl.exe",
        "CMAKE_CXX_COMPILER": "cl.exe"
    },
    "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
    }
}

最后清除Cmake缓存就可以正常工作了
比较奇怪的是这些工作按理来说都是IDE自动完成的,感觉是电脑里面装了太多东西相互破坏了一些环境设置

posted @ 2025-03-18 12:23  DmLeaves  阅读(1033)  评论(0)    收藏  举报