1

I'm trying to run a Cmake project on Visual Studio 2022. When I select "Windows Debug" to run, it can run successfully. But when I selected "Windows Release", an error occurred:

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
D:\MinGW64\bin\ld.exe: cannot find /implib:cmTC_ac8bc.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /pdb:cmTC_ac8bc.pdb: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /version:0.0: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /debug: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /INCREMENTAL: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /subsystem:console: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find kernel32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find user32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find gdi32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find winspool.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find shell32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find ole32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find oleaut32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find uuid.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find comdlg32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find advapi32.lib: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /MANIFEST: No such file or directory
D:\MinGW64\bin\ld.exe: cannot find /MANIFESTFILE:CMakeFiles\cmTC_ac8bc.dir/intermediate.manifest: Invalid argument
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.

D:\MinGW64 is a path for g++, but I'm using MSVC.

I think MSVC's cl.exe might incorrectly use GNU's ld.exe.

This is the 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"
        }
    ]
}

I tried removing the path of g++ from the environment variable PATH, but it didn't have any effect.

I also tried to modify the CMakeLists.txt file, but I couldn't figure out where the error was.

hjl2011
  • 31
  • 4
  • are you actually use VS or are you using VS Code? – phuclv Apr 04 '23 at 10:49
  • I am using VS2022. – hjl2011 Apr 04 '23 at 10:50
  • 2
    Quote: `-- D:\MinGW64\bin\ld.exe`. Hard to judge what happened to the missing option or cmake, but clearly it is cmake and not msvc which is the problem. How to use a custom linker is [noted here](https://stackoverflow.com/questions/1867745/cmake-use-a-custom-linker) – Hans Passant Apr 04 '23 at 12:30

1 Answers1

2

Modify the configurePresets[0].cacheVariables in the CMakePresets.json:

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

Then it could run successfully.

Thanks @Hans Passant a lot!

hjl2011
  • 31
  • 4