8

After setting up VS Code, installing the build tools and going through the tutorial here: https://code.visualstudio.com/docs/cpp/config-msvc

Visual Studio Code is unable to find the cl.exe to compile C++.

I replaced the path from the tutorial with the correct one on my hard drive (cl.exe is there).

// My Config
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

// The tutorial build-task
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/Zi",
                "/Fe:",
                "helloworld.exe",
                "helloworld.cpp"
            ],
            "group":  {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

When running the build task this error shows, although the compilerPath is correct (the cl.exe is there) and helloworld.cpp exists as well. Running everything as administrator didn't help.

cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
At line:1 char:1
+ cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (cl.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
Krck
  • 187
  • 1
  • 1
  • 8

3 Answers3

9

The 'c_cpp_properties.json' file only configures IntelliSense in the C/C++ extension, so the compilerPath option does not help with building.

Make sure you are launching VS Code from the Developer Command Prompt. This will set the necessary environment variables, including the location of 'cl.exe'.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
  • Is there a way to configure this from the JSON files? Or do I need to open VS Code each time from the VS Dev command prompt? – Michele Iafrancesco Feb 26 '20 at 12:42
  • 1
    @Michele The docs state that "to use MSVC [...], you must run from a Developer Command Prompt", and this seems like the most reliable way of doing it. If you *really* want to try and avoid this though, it is possible to [use the developer tools in an existing command window](https://learn.microsoft.com/cpp/build/building-on-the-command-line#use-the-developer-tools-in-an-existing-command-window), and even to [set `env` variables for debugging and tasks](https://techbrij.com/visual-studio-code-tasks-debugging). Otherwise, it may be best to write this up as a separate question. – Callum Watkins Feb 26 '20 at 21:44
  • Thanks, the first option worked! I was able to call the `vcvarsall.bat` from the command prompt and then run the compiler just fine! – Michele Iafrancesco Feb 27 '20 at 13:27
2

In the documentation of Visual Studio Code you can see a solution to this issue. In the section of "C++ > Microsoft C++ on Windows > Troubleshooting" they explain that you need to open your projects from the Developer Command Promp

As an example:

cd projects/yourproject
code .

I haven't found other way of doing it.

  • This is the best way if you want to use the cl.exe compiler. remember, it's not a command prompt (cmd). it is the visual studio developer command promt. – basjak Jan 26 '21 at 14:46
0

My first answer had used hardcoded paths, but this is better. You only need to pay attention to the paths since the docs use 2019 etc. The VsDevCmd.bat sets up all the env vars you'll need and it works easily within VSCode. It also gives you compiler errors which my previous solution with the hardcoded cl.exe paths did not for some reason. Run VS Code outside the Developer Command Prompt

NPatch
  • 66
  • 6