Cmake:错误地使用ld.exe



我想在Visual Studio 2022上运行一个Cmake项目。当我选择"Windows调试"时;要运行,才能运行成功。但是当我选择"Windows发布"时,出现了一个错误:

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 CMakeFilescmTC_ac8bc.dirtestCCompiler.c.obj
[2/2] Linking C executable cmTC_ac8bc.exe
FAILED: cmTC_ac8bc.exe 
cmd.exe /C "cd . && "D:Program FilesMicrosoft Visual Studio2022CommunityCommon7IDECommonExtensionsMicrosoftCMakeCMakebincmake.exe" -E vs_link_exe --intdir=CMakeFilescmTC_ac8bc.dir --rc=rc --mt="D:Windows Kits10bin10.0.22000.0x64mt.exe" --manifests  -- D:MinGW64binld.exe /nologo CMakeFilescmTC_ac8bc.dirtestCCompiler.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:Testoutbuildwindows-releaseCMakeFilesCMakeScratchTryCompile-byw8tr && C:WindowsSystem32WindowsPowerShellv1.0powershell.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:MinGW64binld.exe /nologo CMakeFilescmTC_ac8bc.dirtestCCompiler.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:CMakeFilescmTC_ac8bc.dir/intermediate.manifest CMakeFilescmTC_ac8bc.dir/manifest.res" failed (exit code 1) with the following output:
D:MinGW64binld.exe: cannot find /nologo: No such file or directory
D:MinGW64binld.exe: cannot find /out:cmTC_ac8bc.exe: No such file or directory
D:MinGW64binld.exe: cannot find /implib:cmTC_ac8bc.lib: No such file or directory
D:MinGW64binld.exe: cannot find /pdb:cmTC_ac8bc.pdb: No such file or directory
D:MinGW64binld.exe: cannot find /version:0.0: No such file or directory
D:MinGW64binld.exe: cannot find /debug: No such file or directory
D:MinGW64binld.exe: cannot find /INCREMENTAL: No such file or directory
D:MinGW64binld.exe: cannot find /subsystem:console: No such file or directory
D:MinGW64binld.exe: cannot find kernel32.lib: No such file or directory
D:MinGW64binld.exe: cannot find user32.lib: No such file or directory
D:MinGW64binld.exe: cannot find gdi32.lib: No such file or directory
D:MinGW64binld.exe: cannot find winspool.lib: No such file or directory
D:MinGW64binld.exe: cannot find shell32.lib: No such file or directory
D:MinGW64binld.exe: cannot find ole32.lib: No such file or directory
D:MinGW64binld.exe: cannot find oleaut32.lib: No such file or directory
D:MinGW64binld.exe: cannot find uuid.lib: No such file or directory
D:MinGW64binld.exe: cannot find comdlg32.lib: No such file or directory
D:MinGW64binld.exe: cannot find advapi32.lib: No such file or directory
D:MinGW64binld.exe: cannot find /MANIFEST: No such file or directory
D:MinGW64binld.exe: cannot find /MANIFESTFILE:CMakeFilescmTC_ac8bc.dir/intermediate.manifest: Invalid argument
CMakeFilescmTC_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.

D:MinGW64是g++的路径,但我使用的是MSVC。

我认为MSVC的cl.exe可能错误地使用了GNU的ld.exe

这是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"
}
]
}

我试着从环境变量PATH中删除g++的路径,但是没有任何效果。

我也试图修改CMakeLists.txt文件,但我无法找出错误在哪里。

修改CMakePresets.json中的configurePresets[0].cacheVariables:

"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe",
"CMAKE_C_LINK_EXECUTABLE": "link.exe",
"CMAKE_CXX_LINK_EXECUTABLE": "link.exe"
}

则可以成功运行。

非常感谢汉斯·帕桑特!

最新更新