Hey there, today I'm going to show you how to target the Windows Subsystem for Linux 2 from
Visual Studio Code now if you've never installed WSL 2 before first thing you want to do is go to
aka.ms/wsl2-install. This will give you all the necessary information you need
to enable the features in your operating system, to go
and download your favorite Linux distribution from the Microsoft store,
everything you need to get set up. I have already gone through all of these instructions so I've got
Debian installed alongside, you know, of course the most important applications like cowsay
and apt and, you know, everything which you're used to from just running a Debian system normally.
Now if I want to target this from VS Code, what I want to do is go ahead and go to the extensions
and find this Remote WSL extension. If I go and open this up, I have already installed this but
you'll be able to install it just using these these buttons here, just like any other
extension and it will give you a bunch of information on getting started with targeting WSL.
Once we've installed this one way we can open up a connection is this remote window button.
This gives us a bunch of options, we can pick our distribution if we have multiple ones
installed, which I do, I have ubuntu installed in WSL 1 and Debian in WSL 2, so I can go ahead and open
a folder this will just in a second, there we go, get a file picker. Like I said I got Debian
and Ubuntu and the whole file system is just mounted inside Windows so i can
go and have a look. I'm going to go ahead and open my expected implementation
and we can give a try at building it and testing it and making sure IntelliSense works.
Everything you might be used to. Another way of getting a connection into WSL is
just opening up our Debian here and saying `code` and then the folder you want to open
and then this will go ahead and open up Visual Studio Code inside Windows and connect into WSL.
Which is what's happening right now: it's starting up the server inside Debian and then
connecting to it and then this is going to take a little while to load up because it's
doing a whole lot of stuff, it's starting up that server and connecting to it it's then
also gonna have to get all of our extensions, like I'm using CMake for for this project here,
it's gonna have to configure CMake. Configuring CMake for this project will take a little bit of
time because that pulls down some some files like Catch2 from the internet, but in a minute we're
gonna, there we go we just got our, syntax highlighting which is nice, and this is going to
activate all our extensions so this will be when it'll say, hey we want to, this is a CMake project
and we have the CMake extension installed so we're going to there we go set up CMake, find any local
kits, which are tool chains essentially, like different versions of gcc or Clang or whatever
and now this is gonna go and configure our project so CMake will be you know looking at our CMakeLists.txt
and pulling down any necessary files you can see this is running inside Linux, this is not
giving us anything in our Windows file system, this is all inside Debian. So it's going to configure,
when it does configure then we should have all the ability to build our project, to run the tests,
to get IntelliSense, everything you might expect. So this is just going to configure for a second...
There we go, that's it configured and generated, and now if I hit f7 then this will build
my tests for me inside WSL. Starting build, running CMake build...
You can see this is saying to do it on six threads and we're gonna get our tests
built and then we'll be able to run them from within Visual Studio Code. Building, linking
and done. So now if I hit ctrl shift p, I can use CMake run tests to run the tests
and there's a whole bunch of different test files.
There we go, it realized it didn't have to do any work and ran the tests and they passed! And if we go
ahead and go here we should be able to see our IntelliSense completion. There we go, we've got and_then and
assign and has_value it's just like you would if you were if you were writing our code natively.
Then when we're finished, if we want to go back to do native development we can just hit that
button again and close the remote connection, and then we'll be back to developing inside Windows.
So that's everything you need to get started with targeting WSL 2
From Visual Studio Code, please do check out Pure Virtual C++ on the 3rd of
May for more C++ content, we're going to have more things about CMake,
C++20 features, a whole lot of great stuff so I'll hopefully see you there.
Bye!