error MSB6004: The specified task executable location "C:\WINDOWS\system32\cmd.exe" is invalid.

Hey! I’m running into an issue with CMake on my Windows machine (Visual Studio 2019 generator).

It seems this is specific to my system, but hope you have a clue on how to workaround. Non-cmake generated Visual Studio projects work fine. Before creating this issue, I was googling this error 2 days and didn’t succeed. Thanks for your attention!

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(test)

Output:

-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
CMake Error at CMakeLists.txt:2 (project):
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.

    Build started 26.08.2022 13:32:38.
    Project "C:\projects\test\build\CMakeFiles\3.24.1\VCTargetsPath.vcxproj" on node 1 (default targets).
    InitializeBuildStatus:
      Touching "x64\Debug\VCTargetsPath.tlog\unsuccessfulbuild".
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB6004: The specified task executable location "C:\WINDOWS\system32\cmd.exe" is invalid. [C:\projects\test\build\CMakeFiles\3.24.1\VCTargetsPath.vcxproj]
    Done Building Project "C:\projects\test\build\CMakeFiles\3.24.1\VCTargetsPath.vcxproj" (default targets) -- FAILED.

    Build FAILED.

    "C:\projects\test\build\CMakeFiles\3.24.1\VCTargetsPath.vcxproj" (default target) (1) ->
    (PostBuildEvent target) ->
      C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB6004: The specified task executable location "C:\WINDOWS\system32\cmd.exe" is invalid. [C:\projects\test\build\CMakeFiles\3.24.1\VCTargetsPath.vcxproj]

        0 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:00.41


  Exit code: 1



-- Configuring incomplete, errors occurred!
See also "C:/projects/test/build/CMakeFiles/CMakeOutput.log".
  • Where are you running CMake (i.e. what kind of shell or terminal)?
  • What is the output of msbuild -version from that shell (I know it’s shown in your output above, but humor me)?
  • What is the command line you’re using to run CMake?
  • cmd.exe, windows powershell, vscode terminal. with the same results.
  • cmd.exe: 'msbuild' is not recognized as an internal or external command, operable program or batch file.
  • cmake .. (current directory is <source_dir>/build)

Please run CMake from an empty build directory (i.e. start from scratch). Show the full command you use to run CMake and the full output from that. Also, just to be sure, does C:\WINDOWS\system32\cmd.exe exist on your system (hard to see how it wouldn’t, but I have to ask)?

And one more thing to try, please do all this from a Visual Studio command prompt, not the ordinary one that comes with Windows. It shouldn’t matter if using the Visual Studio generator, but see if that makes any difference (it would make msbuild available as a command, if nothing else).

Thank you for your help!

I found that C:\WINDOWS\SysWOW64\\cmd.exe was missing.

Running sfc /scannow from Windows PowerShell (under Admin) and rebooting the system helped me.

I love how Windows works ha-ha

When you first posted this I was curious so Goolged error MSB6004: The specified task executable location “C:\WINDOWS\system32\cmd.exe”

One of the replies turned up this StackOverflow answer.

However that notes

  • In administrator command prompt, run sfc /scannow

Did you run sfc /scannow as a regular user or was it started with Run as administrator?

1 Like

yes