How to permanently configure VSCode to use cl.exe without launching it from the Developer Command Prompt?
Asked Answered
M

2

2

So compiling C on VS Code on Windows is an absolute nightmare.

The only way I am able to "force" VSCode to compile and debug my C/C++ programs is by either:

  1. Starting VSCode through the Developer Command Prompt for VS 2022 using:
    %comspec% /k "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
    
  2. Or having a .bat file set up, as this similar StackOverflow question suggests.

While these approaches work, they feel like workarounds and are not ideal. What I want is to make cl.exe work "natively" within VSCode without needing to rely on the Developer Command Prompt or any scripts.

Is there a way to set up VSCode so that cl.exe can be used for compiling and debugging C/C++ code without these manual steps every time? I'm looking for a more permanent solution to set up the environment correctly for cl.exe on Windows.

Myoglobin answered 8/9 at 3:36 Comment(2)
related: https://mcmap.net/q/1957266/-error-with-vscode-cpptools-quot-cl-exe-build-and-debug-is-only-usable-when-vs-code-is-run-from-the-developer-command-prompt-for-vs-quot-duplicateBrogan
@UsmanMehmood how does that answer this question? it looks to me like you're grabbing a tangential opportunity to promote your own work. leaves a bad taste in my mouth.Brogan
M
3

As I was writing this question I finally found the solution, so I decided to share it here. I tried to make it beginner friendly (win10 - x64).

Steps (for x64):

  1. Locate and copy x64 cl.exe's location, usually at:
    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\[version]\bin\Hostx64\x64

  2. Press Win+R to open Run dialog and type sysdm.cpl to get System Properties

  3. Go to Advanced Tab

  4. Click on Environment Variables

  5. In System variables section locate Path variable and double click it

  6. On the new window that pops up click New and add cl.exe's location

  7. Click OK

Now we still have to add the VARS that are setup when you run Developer Command Prompt for VS YYYY:

  1. Open x64 Native Tools Command Prompt for VS YYYY. It has to be x64 or else you'll get a library machine type 'x86' conflicts with target machine type 'x64' error when compiling.

  2. cd to your preferred directory as we are going to create a file

  3. Run set > envvars.txt

  4. On this new file envvars.txt find these 3 lines:

    a) INCLUDE=C:\...
    b) LIB=C:\...
    c) and LIBPATH=C:\...

  5. and delete everything else.

Now there are 2 ways to add these VARS to the System variables manually or through a one-time *.bat file. I chose the *.bat file way:

  1. We are going to turn envvars.txt into this *.bat file by:

    a) Preceding each of the vars by setx (permanently setting environment variables)

    b) Replacing the = with a space

    c) involving the paths with quotation marks ""

    d) adding /M after the paths on each line (ensures that the variables are set for the system and not just the current user)

  2. We should have something like:

    setx INCLUDE "C:\Program Files\..." /M
    setx LIB "C:\Program Files\..." /M
    setx LIBPATH "C:\Program Files\..." /M
    
  3. Rename envvars.txt to envvars.bat

  4. Execute as Administrator

  5. Restart computer

Done.

After restart you should be able to launch VS Code however and just compile and debug. (ASSUMING YOUR TASKS.JSON AND LAUNCH.JSON are setup properly)

Myoglobin answered 8/9 at 3:36 Comment(0)
V
1

I did find a workaround that is quite comfortable to use. You can create bat file that launches the vs code through Developer Command prompt. First you create bat file containing this:

@echo off 
REM (your ardess to VsDevCmd.bat)
call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
start code "C:[path to desired folder]"

This will automatically start vs code in mode that has access to cl.exe If you don´t want the leftover command prompt you can automatically close it only by putting taskkill /F /IM "cmd.exe" /T at the end of the VsDevCmd.bat file If you then create shortcut to this bat file and put Explorer.exe you can even pin it to your taskbar.

Vigorous answered 16/9 at 17:18 Comment(1)
try extension available on elevateprogramming.blogspot.com/2022/04/…Cammack

© 2022 - 2024 — McMap. All rights reserved.