vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Microsoft C++ configure "Run VS Code outside the Developer Command Prompt" doesn't work

Open anhq-nguyen opened this issue 2 years ago • 13 comments

Does this issue occur when all extensions are disabled?: Yes/No

  • VS Code Version: 1.85.0
  • OS Version: Windows 11 Home, version 23H2 Build 22631.2715

I followed the exact guide in https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt

But my code only works when I start VSC with Developer Command Prompt. When I start VSC from a shortcut, it gives error cl.exe is not recognized.... Even when I modified the task's command. Here's my tasks.json:

{
    "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/2022/BuildTools/Common7/Tools/VsDevCmd.bat\"",
            "&&"
            ]
        }
        }
    },
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostx86/x86/cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

anhq-nguyen avatar Dec 08 '23 21:12 anhq-nguyen

Hi @anhq-nguyen, the error message that cl.exe is not recognized usually implies that the C++ Extension doesn't know the path to the cl.exe compiler. Have you added the path of this compiler under Configurations or directly in your c_cpp_properties.json file?

AlexandraKemperMS avatar Dec 08 '23 21:12 AlexandraKemperMS

I'm having the exact same problem, and @AlexandraKemperMS with respect your reply seems to miss the point a bit.

If the global task property override detailed in the section Run VS Code outside the Developer Command Prompt is run then cl.exe will be on the path and so should be locatable.

image

If VsDevCmd.bat is not correctly run in the build environment, (which is strongly suggested by the fact that even running the build task manually results in the build failing with cl.exe not found), then adding cl.exe to the extension config might make it possible to compile a trivial sample, but that isn't going to solve the bigger problem which is that if VsDevCmd.bat is not run then none of it's other configuration will have been applied including all the Windows SDK and MsBuild settings required to build any non-trivial project.

Neutrino-Sunset avatar Dec 09 '23 15:12 Neutrino-Sunset

It appears that the attempt to escape the spaces in the path to the batch file does not work.

image

Neutrino-Sunset avatar Dec 09 '23 15:12 Neutrino-Sunset

From https://code.visualstudio.com/docs/editor/tasks#_global-tasks

This doesn't seem to be working.

image

Neutrino-Sunset avatar Dec 09 '23 15:12 Neutrino-Sunset

The task override can be fixed using this syntax.

image

But that doesn't solve the problem. Attempting to build still fails.

I've now tried adding the compiler path directly to c_cpp_properties.json, and it still doesn't work.

image

And I've also now hardcoded the compiler path in the extension settings. And that doesn't work either.

image

image

Neutrino-Sunset avatar Dec 09 '23 15:12 Neutrino-Sunset

I've added the path to the compiler to the system path. Now the task at least finds cl.exe but that doesn't help much because even trying to build HelloWorld still fails as the build environment doesn't have any of the paths to the standard headers.

Which is the problem I referred to earlier regarding not running VsDevCmd.bat

image

Neutrino-Sunset avatar Dec 09 '23 16:12 Neutrino-Sunset

What I'm doing for now is creating a batch file in my C++ project directory that loads the dev envronment and then opens VsCode.

image

UPDATE

I improved on this by creating the following batch file at a location available on the system PATH. With this I can invoke codecpp on the commandline with any project or directory specified and it opens that directory with the dev tools installed in the environment. I can also pass any other arguments I want too.

image

Neutrino-Sunset avatar Dec 09 '23 16:12 Neutrino-Sunset

Hi @anhq-nguyen, the error message that cl.exe is not recognized usually implies that the C++ Extension doesn't know the path to the cl.exe compiler. Have you added the path of this compiler under Configurations or directly in your c_cpp_properties.json file?

Yes I did, here's my c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostx86/x86/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

Besides, when I used Run C/C++ file. I got this error: image image

anhq-nguyen avatar Dec 10 '23 00:12 anhq-nguyen

@anhq-nguyen I was having the same issue. Trying to run the program from the top-right run icon (which is added through the C/C++ extension I believe) would fail with the popup: "cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS.". However, running the program through the VSCode Run menu (or using the F5, ctrl+F5 shortcuts) would succeed in compiling and running the program.

I guess this may be an issue with the extension. Not sure if there's another setting somewhere that will fix the issue, but for now this seems like a fine workaround.

Mobious avatar Jan 11 '24 20:01 Mobious

@anhq-nguyen You need to change the task type from cppbuild to shell. But I have the same issue as @Mobious. That is, I can run through the menu, but cannot via the top-right Run button.

SimonYansenZhao avatar Feb 27 '24 14:02 SimonYansenZhao

Launch VsDevCmd.bat for your project directory, and type "code .". Compiler path is: cl.exe

LaunchProject.bat:

@echo off
call "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\VsDevCmd.bat"
code .

jerrysmatrix avatar Jun 02 '24 07:06 jerrysmatrix

Launch VsDevCmd.bat for your project directory, and type "code .". Compiler path is: cl.exe

LaunchProject.bat:

@echo off
call "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\VsDevCmd.bat"
code .

Another method, you need both tasks.json and launch.json under .vscode folder:

# tasks.json, copy from https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt
{
  "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
      }
    }
  ]
}


# launch.json, pay attention to the preLaunchTask name. it's not default value like 'C/C++: cl.exe build active file', you need to modified accroding to your task label  defined in tasks.json, which should be 'cl.exe build active file' in this situation. And you also need to add some symbol to configurations name, such as hyperhen'-', because there is a default hidden configuration name from cpp extension with its default name "C/C++: cl.exe build and debug active file"(no hyperhen)
{
	"version": "0.2.0",
	"configurations": [
	  {
		"name": "-C/C++: cl.exe build and debug active file", // need modification
		"type": "cppvsdbg",
		"request": "launch",
		"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
		"args": [],
		"stopAtEntry": false,
		"cwd": "${workspaceFolder}",
		"environment": [],
		"externalConsole": false,
		"preLaunchTask": "cl.exe build active file" // need modification
	  }
	]
  }

Then both cpp extension play button in the right up corner, and vscode run and play button in the left up corner would work as expect.

gigberg avatar May 12 '25 05:05 gigberg