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:
- Starting VSCode through the Developer Command Prompt for VS 2022 using:
%comspec% /k "C:Program FilesMicrosoft Visual Studio2022CommunityCommon7ToolsVsDevCmd.bat"
- 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
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):
Locate and copy x64
cl.exe
's location, usually at:C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC[version]binHostx64x64
Press Win+R to open Run dialog and type
sysdm.cpl
to get System PropertiesGo to
Advanced
TabClick on
Environment Variables
In
System variables
section locatePath
variable and double click itOn the new window that pops up click
New
and addcl.exe
's locationClick
OK
Now we still have to add the
VARS
that are setup when you runDeveloper Command Prompt for VS YYYY
:Open
x64 Native Tools Command Prompt for VS YYYY
. It has to bex64
or else you'll get alibrary machine type 'x86' conflicts with target machine type 'x64'
error when compiling.cd
to your preferred directory as we are going to create a fileRun
set > envvars.txt
On this new file
envvars.txt
find these 3 lines:a)
INCLUDE=C:...
b)
LIB=C:...
c) and
LIBPATH=C:...
and delete everything else.
Now there are 2 ways to add these
VARS
to theSystem variables
manually or trough a one time*.bat
file. I chose the*.bat
file way: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 spacec) 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)We should have something like:
Rename
envvars.txt
toenvvars.bat
Execute as Administrator
Restart computer
Done.
After restart you should be able to launch VS Code however and just compile and debug. (ASSUMING YOUR
TASKS.JSON
ANDLAUNCH.JSON
are setup properly)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:
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 fileIf you then create shortcut to this bat file and put Explorer.exe you can even pin it to your taskbar.