skip to Main Content

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 FilesMicrosoft Visual Studio2022CommunityCommon7ToolsVsDevCmd.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.

2

Answers


  1. Chosen as BEST ANSWER

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

    Steps (for x64):

    1. Locate and copy x64 cl.exe's location, usually at:
      C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC[version]binHostx64x64

    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 trough 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)


  2. 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 FilesMicrosoft Visual Studio2022CommunityCommon7ToolsVsDevCmd.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search