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

VS2022中CMake Release模式错误:MSVC误用MinGW的ld.exe

问题解决:VS2022 CMake Release模式误调用MinGW ld.exe导致链接失败

问题现象

Visual Studio 2022中CMake项目切换到Windows Release模式时,MSVC的cl.exe错误调用MinGW64的ld.exe,导致链接参数不匹配(MSVC的链接参数被当作ld的文件路径),出现大量"无法找到"的错误,Debug模式可正常运行。已尝试移除环境变量PATH中的MinGW路径、修改CMakeLists.txt,问题未解决。

错误日志

CMake Error at D:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
The C compiler "D:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe" is not able to compile a simple test program.
It fails with the following output:
Change Dir: D:/Test/out/build/windows-release/CMakeFiles/CMakeScratch/TryCompile-byw8tr
Run Build Command(s):D:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe cmTC_ac8bc && [1/2] Building C object CMakeFiles\cmTC_ac8bc.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_ac8bc.exe
FAILED: cmTC_ac8bc.exe 
cmd.exe /C "cd . && "D:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_ac8bc.dir --rc=rc --mt="D:\Windows Kits\10\bin\10.0.22000.0\x64\mt.exe" --manifests  -- D:\MinGW64\bin\ld.exe /nologo CMakeFiles\cmTC_ac8bc.dir\testCCompiler.c.obj  /out:cmTC_ac8bc.exe /implib:cmTC_ac8bc.lib /pdb:cmTC_ac8bc.pdb /version:0.0 /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D D:\Test\out\build\windows-release\CMakeFiles\CMakeScratch\TryCompile-byw8tr && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file D:/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary D:/Test/out/build/windows-release/CMakeFiles/CMakeScratch/TryCompile-byw8tr/cmTC_ac8bc.exe -installedDir D:/vcpkg/installed/x64-windows-static/debug/bin -OutVariable out""
LINK Pass 1: command "D:\MinGW64\bin\ld.exe /nologo CMakeFiles\cmTC_ac8bc.dir\testCCompiler.c.obj /out:cmTC_ac8bc.exe /implib:cmTC_ac8bc.lib /pdb:cmTC_ac8bc.pdb /version:0.0 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_ac8bc.dir/intermediate.manifest CMakeFiles\cmTC_ac8bc.dir/manifest.res" failed (exit code 1) with the following output:
D:\MinGW64\bin\ld.exe: cannot find /nologo: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /out:cmTC_ac8bc.exe: No such file or directory
...(省略重复错误)
CMakeFiles\cmTC_ac8bc.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配置

{
    "version": 3,
    "configurePresets": [
        {
            "name": "windows-base",
            "hidden": true,
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            },
            "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            }
        },
        {
            "name": "linux-base",
            "hidden": true,
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "gcc",
                "CMAKE_CXX_COMPILER": "g++"
            },
            "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Linux"
            },
            "vendor": {
                "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                    "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
                }
            }
        },
        {
            "name": "windows-debug",
            "displayName": "Windows Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "VCPKG_TARGET_TRIPLET": "x64-windows-static"
            }
        },
        {
            "name": "windows-release",
            "displayName": "Windows Release",
            "inherits": "windows-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "VCPKG_TARGET_TRIPLET": "x64-windows-static"
            }
        },
        {
            "name": "linux-debug",
            "displayName": "Linux Debug",
            "inherits": "linux-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "VCPKG_TARGET_TRIPLET": "x64-linux"
            }
        },
        {
            "name": "linux-release",
            "displayName": "Linux Release",
            "inherits": "linux-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "VCPKG_TARGET_TRIPLET": "x64-linux"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "windows-debug",
            "displayName": "Windows Debug",
            "configurePreset": "windows-debug"
        },
        {
            "name": "windows-release",
            "displayName": "Windows Release",
            "configurePreset": "windows-release"
        },
        {
            "name": "linux-debug",
            "displayName": "Linux Debug",
            "configurePreset": "linux-debug"
        },
        {
            "name": "linux-release",
            "displayName": "Linux Release",
            "configurePreset": "linux-release"
        }
    ]
}

解决方案

1. 彻底清理Release构建目录

删除D:/Test/out/build/windows-release整个文件夹,CMake的缓存和临时构建文件可能残留了MinGW的链接器配置,Debug和Release是独立目录,Release目录的缓存未被正确更新。

2. 强制指定MSVC链接器

修改CMakePresets.json中的windows-base配置,在cacheVariables里添加以下内容,强制CMake使用MSVC自带的link.exe而非外部ld:

"CMAKE_LINKER": "link.exe",
"CMAKE_C_LINK_EXECUTABLE": "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>",
"CMAKE_CXX_LINK_EXECUTABLE": "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"

修改后的windows-base片段:

{
    "name": "windows-base",
    "hidden": true,
    "generator": "Ninja",
    "binaryDir": "${sourceDir}/out/build/${presetName}",
    "cacheVariables": {
        "CMAKE_C_COMPILER": "cl.exe",
        "CMAKE_CXX_COMPILER": "cl.exe",
        "CMAKE_LINKER": "link.exe",
        "CMAKE_C_LINK_EXECUTABLE": "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>",
        "CMAKE_CXX_LINK_EXECUTABLE": "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
    },
    "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
    "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
    }
}

3. 验证vcpkg依赖的编译环境

如果项目使用vcpkg,确保x64-windows-static triplet的依赖是用MSVC编译的:

  • 删除D:/vcpkg/installed/x64-windows-static目录
  • 打开VS的x64 Native Tools Command Prompt,重新安装依赖:
    vcpkg install --triplet x64-windows-static [你的项目依赖包名]
    

4. 重启VS并使用纯净MSVC环境

关闭所有VS窗口,重新打开项目时,通过VS的x64 Native Tools Command Prompt启动VS:

devenv D:/Test/你的项目文件.sln

确保环境变量中没有残留MinGW的路径,避免CMake误检测。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:22:11