简体   繁体   中英

How to configure the VS Code shell like the developer command prompt?

I'm trying to set up a task for compiling my C++ code in Visual Studio Code. I can't get it to work... but the command it spits out works perfectly fine when I just open the Developer Command Prompt and paste it in.

I've managed to narrow down this problem to the correct environment variables not being set in the shell that VS Code is using (as evidenced by running echo %INCLUDE% just returning %INCLUDE% ).

Now I don't exactly know how the Developer Command Prompt differs from a normal Powershell terminal as used by VS Code, so I wouldn't know exactly how to configure it (aside from running vcvarsall.bat ), but even if I could, every time I open a new terminal in VS Code the environment variables have reset themselves again.

In essence, the solutions to this problem that I can see are:

  1. Run vcvarsall.bat before every build task.

    Unfortunately I'm not familiar enough to know how to execute multiple commands in a row using a tasks.json configuration file.

  2. Configure the shell that VS Code uses to be like the Developer Command Prompt by default.

    Unfortunately, I have no clue where to even start with this. I can easily set the shell used to either cmd or PowerShell, but not to the Developer Command Prompt, and neither can I find where to configure its environment variables, nor do I know what the full effects of vcvarsall.bat are so I know which variables to set.

If there's a simpler way of achieving what I'm after, I'd be very happy to hear it. Though regardless, what it comes down to is that I want to know how to configure VS Code in a way that allows me to compile my code from inside the IDE.

I found an answer after a bunch more poking around. My setup (in tasks.json ) is now as follows:

"command": "&",
"args": [
  "'D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvars32.bat';",
  "cl.exe",
  "/Zi",
  "/EHsc",
  "/Fe:",
  "${fileDirname}\\${fileBasenameNoExtension}.exe",
  "${file}"
]

with & you can execute a path that contains spaces. This leads to my vcvars32.bat (Location may be different for other people) which sets up the correct variables without having to enter them manually. ; lets you execute multiple commands one after the other in PowerShell.

The accepted answer did not work for me but was helpful in finding a solution that did work.

Using the task type "process" and then settting the command to the batch file called by the Developer Command Prompt launches the windows command prompt directly (instead of invoking it via powershell).

The first argument is then "&" which means that the compiler will be invoked within the developer command prompt context instead of just being passed to the batch file as an argument.

>{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "process",
        "label": "cl.exe build active file",
        "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvars32.bat",
        "args": [
          "&"
          "cl.exe",
          "/Zi",
          "/EHsc",
          "/Fe:",
          "${fileDirname}\\${fileBasenameNoExtension}.exe",
          "${file}"
        ],
        "problemMatcher": ["$msCompile"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

You can create a new task type within your tasks.json this way:

{
  "version": "2.0.0",
  "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\"",
          "&&"
        ]
      }
    }
  },
  "tasks": [
    {
      "type": "shell",
      "label": "cl.exe build active file",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/Fe:",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "${file}"
      ],
      "problemMatcher": ["$msCompile"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Don't forget to change type of task to "shell" .

Note that the build will start with a couple of sec delay.

See here: https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM