Wednesday, February 4, 2026

Debug C++ with WSL 2 Distributions and Visual Studio 2022

hey everyone my name is erica sweet i
work on microsoft c
plus plus team and i'll be joined in a
few minutes by craig lowen
who works on wsl or the windows
subsystem for linux
and we've teamed up today to show you
the new wsl2 toolset for c
plus plus development in visual studio
2022
which lets you build and debug c plus
code
on wsl 2 from visual studio without ever
adding an ssh connection
so the first thing you'll need to do is
set up your wsl distro
to ensure you have all the required
build tools installed
so you'll need a c plus compiler c make
an underlying build tool like make or
ninja rsync
zip and gdp and since i'm using ubuntu
i can install these all using the system
package manager
i'm now ready to open my c plus plus
code in visual studio 2022
i'm using a cmake project which is our
recommendation for c
plus plus cross platform development and
my source files are located in the
windows
file system thanks to bullet physics for
creating bullet 3 which is the cross
platform 3d physics library that we'll
be using in this demo
i'm using visual studio's new cmake
presets integration
which will activate automatically
anytime you have a cmgpresets.json file
at the root of the project across the
menu bar you'll see that i have three
drop downs and the drop down on the left
is my active target system
so i can build and debug the same
project on windows
remote systems and on wsl from the same
instance of visual studio
which makes this a great option for your
c plus cross platform development
and when you select wsl 2 visual studio
will automatically use the new wsl2
toolset
for configuration and build when you're
targeting wsl
you'll have a native intellisense
experience so for example
if i were to go to document on this
header file
you'll see that visual studio is
automatically copying my system headers
from the linux file system
over to windows for a native
intellisense experience
and with that i'm going to pass it on
over to craig to build and debug this
project on wsl wsl2
thanks erica so over here on my machine
and literally all we have to do to get
started
is hit f5 from here everything is
compiled and built
inside of linux using the windows
subsystem for linux and we use
gdb to connect to visual studio for a
full debugging session
so our app started and you'll actually
notice that this is a linux based
gui app which wsol now has support for
in the windows insider preview channel
you can learn more about that at dot ms
wslg
what really gives this away is the linux
based tiling here at the top so you can
see that these
this chrome tiling is uh gtk based
indicating that we are
actually running this directly on linux
in a linux instance
so i can go ahead and interact with my
app i'm going to drag this robot around
and hit d
to trigger a breakpoint and i can use
all of my favorite visual studio
debugging techniques directly in here
i can take a look at my call stack i can
take a look at these
environment variables and what their
actual variables are
using my regular workflows that i would
use inside of visual studio
this is hugely exciting to unlock the
potential to use your favorite
tools at using visual studio to debug
run and develop
your c plus based apps inside of linux
as well even though you're using a
windows based machine
and exciting enough if we go ahead and
close this just as eric mentioned
i can go ahead and use my local machine
and target this back
to windows just by clicking that drop
down button so what's actually happening
behind the scenes to make this possible
well if i go ahead and hit build you
will see that i
have a line here saying starting copying
files to a remote machine
what we're actually doing is we're
creating a twin of your copies that are
stored on your windows drive
inside of the windows subsystem for
linux in your linux file system
the reason for this is that wsl 2 runs a
lot faster when your files are in the
linux file system
it can actually be up to 3 to 20 times
faster than wsl1
on top of that you get the added bonus
of wcl2 which includes a full linux
kernel
and 100 system call compatibility
letting you do fun things like
run linux gui apps for example and so
we can pop over to my terminal window
here
and you can see that i have this project
open inside of the dot vs folder
in my home folder on linux this is what
is being copied over using rsync so only
incremental changes
are basically transferred as you develop
if you want to learn more about this
project you can take a look
at the full blog post explaining these
changes in the links in the description
below
as well as links to the full code for
this repository
um morse presets wsl docs and the wsl
repository
as well where you can find any technical
issues or feedback that you might have
thank you so much for tuning in

No comments:

Post a Comment