Read the docs for configuring VS Code for Microsoft C++.
The following excerpt is found under a screenshot showing the following error:
"cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS."
If you get an error trying to build and debug with cl.exe, make sure you have started VS Code from the Developer Command Prompt for Visual Studio using the code .
shortcut.
Each version of Visual Studio Build Tools come with a developer prompt that sets up an environment for using that version's tools. For example, it puts directories in its evironment's PATH
so that they can be easily accessible. Some other tools like VS Code in configurations that use Visual Studio Build Tools may expect to be run from that environment.
There's some good related info in https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#how-to-use-the-command-line-tools :
To work correctly, the tools require several specific environment variables to be set. These variables are used to add the tools to the path, and to set the locations of include files, library files, and SDKs. To make it easy to set these environment variables, the installer creates customized command files, or batch files, during installation. You can run one of these command files to set a specific host and target build architecture, Windows SDK version, and platform toolset. For convenience, the installer also creates shortcuts in your Start menu. The shortcuts open developer command prompt windows by using these command files for specific combinations of host and target. These shortcuts ensure all the required environment variables are set and ready to use.
The required environment variables are specific to your installation and to the build architecture you choose. They also might be changed by product updates or upgrades. This variability is one reason why we recommend you use an installed command prompt shortcut or command file, instead of setting the environment variables yourself.
It can be a pain to do this dance when launching VS Code, but I don't see how this could be improved if you have multiple Visual Studio installations and want to develop in VS Code using a particular one's build tools.
If you don't care to have the entire VS Code process started with an environment from a VS developer prompt, and you only care that a build task you've defined in tasks.json will work, and you are okay with hardcoding a specific developer prompt environment for the workspace, you can follow the instructions in https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt. Read the docs for the full info and caveats, but TL;DR, put something like the following (adjust as needed) at the top-level of your tasks.json:
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
"\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},
There are other tricks you can probably experiment with. See https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#developer_command_file_locations, which gives the locations of the command files that you can use to modify the environment of an existing command prompt instead of using one of the pre-built developer command prompts. For example, writing a script file that just runs one of those command files to set up a chosen developer environment, and then run code
, passing through the rest of the commandline arguments to code
. Then you could pin that script file to some easy-to-reach location, or use it via commandline. For example, see Run VScode from Dev Console through Context Menu.
See also How to permanently configure VSCode to use cl.exe without launching it from the Developer Command Prompt?.
Note that if you're using CMake, the CMake Tools extension has a "kits" feature (see also What are the differences between the scanned-for kits on my Windows machine using the VS Code cmake-tools extension?) and supports CMake presets, both of which have mechanisms for handling this kind of stuff (for presets, see the "architecture" and "toolset" properties of configure presets).
tasks.json
if you have one. – Seven