Hi, my name is Julia, I'm a program manager on the Visual C++ team at Microsoft and today
I'm going to show you how to build and debug projects with Makefiles in Visual Studio Code.
The first thing I'm going to do is open VS Code from the command line.
Now all of my C++ Makefile projects, those actually are on a separate Linux VM in Azure:
I don't have those on my local machine, which is a Windows Surface book, so I'm
going to first connect VS Code to my Azure VM with all my Makefile projects and make and gdb,
gcc ,all of that installed, and then I'll show you how to build and debug Makefiles in VS Code.
So I'm going to do that using the remote SSH extension
which you can find in the extension marketplace. Looks like this. So I already have this installed,
but if you don't you can go ahead and install it from here, and then once
you install the remote SSH extension you'll see this remote explorer icon on the left.
So we'll click that and from here you can connect to a VM through SSH, you can connect to WSL
or container we're going to use SSH for today's demo and I'm going to connect to this one,
because that is the one that has all of my Makefile projects, which you can see like
ffmpeg, vim, git, gcc and so I can just click this connect to host a new window button.
The cool thing about VS Code's remote SSH experience is that you get a local quality
development experience because I'm running VS Code client on my surface book even though all of my
source code and all the build and debug that's all going to happen on the VM that I'm attaching
to. So let me enter my password. Cool and now you can tell that you are in your remote development
environment by checking out this green rectangle in the bottom left corner which has the IP address
of the remote target. So let's open a folder and let's build and debug ffmpeg today. I love that
tool for editing demo videos, I will probably use it for the demo that you're currently watching.
And enter the password one more time when opening a new folder...
Okay, there we go, now we have our syntax highlighting. So as you can see I have a
Makefile for this project open, and also you can see that Makefile Tools, the extension that
configures builds and debugs Makefile projects, automatically just configured my project for me.
We see configure succeeded in the output. I will show you how to get started with Makefile Tools.
So the first thing you're going to have to do is go to the extensions marketplace and here you can
see which extensions you have installed locally, which you have installed on your remote machine.
Here's a quick tip: if you want to install all the extensions you have locally on your remote machine
you can just press this little cloud icon here and then select the ones that you want to install on
the remote machine. Click OK. That way you don't have to search for them again in the marketplace.
So Makefile Tools, this is the one that I'm using today to configure, build,
and debug my Makefile project. If you've used the CMake Tools extension it's pretty similar
to that, except that it is for projects with Makefiles not with CMakeLists scripts. So this
is a new extension, we announced it a few months ago, it's still in its pilot stages, so your
feedback is incredibly valuable. If you build your C++ projects with Makefiles, please give
this extension a try and give us your feedback by opening an issue on our GitHub repository.
So let's take a look real quick at the settings for the Makefile Tools extension.
So if we go to extension settings, let's go to our workspace,
there are a few settings that are particularly important. So the first that I will show you is
Makefile path. So this is the path to the Makefile of your project. If your Makefile
is in your project root, which mine is you don't need to change anything here: the extension will
automatically detect that. Once the extension detects the Makefile in your project,
that's when it activates so, if the extension is not activating because it's not detecting your
Makefile, you can update your Makefile path here, and then you can run any of the commands from the
extension to activate the extension. The second important setting that I'll talk about is make
path. So this is the path to the make tool. So I have /usr/bin/make, and if I just quickly check
in the terminal where make is installed on this VM, you can see it is /usr/bin/make,
so that's all good, the Makefile Tools extension is set up to find my make executable.
So once you have your make file path set and path to make set, also if your make is in
the path for your machine then you shouldn't have to update the path to the make tool,
but now that you have Makefile path set, make path set, we want to make sure that Makefile Tools is
configuring IntelliSense for our project, that's how we'll get the most accurate IntelliSense.
So the very first time you set up the extension you'll get a little pop-up notification that says
allow Makefile Tools to configure IntelliSense for your project. You're going to want to hit
yes on that. If you accidentally dismiss that notification or you can't remember if you
let it configure IntelliSense or not, you can open the command palette with ctrl shift p and type
change configuration provider. It's that first option with C++ in the beginning,
and then hit Makefile Tools. Cool, so now we have Makefile Tools set up to configure IntelliSense
for our project and at this point if you don't normally pass additional arguments to make from
the command line when you're building your project then you're all set to start configuring, but
if you do then you can specify those arguments in the Makefile configuration setting. So right
here in Makefile configurations you can edit in settings.json and what this is is an array of
objects, configuration objects, and each one has a name property. So ffmpeg configuration,
I cannot type today, and then a make args property, and this is where...
oh missing our quote there, okay, there we go, a makeargs property and this is an array of arguments
to pass to the make command. I don't need to do anything, I don't need to pass any additional
arguments when configuring ffmpeg, so i'm going to go ahead and delete this, because I don't need to
define any custom configurations. But if you have to, this is where you would do it, in settings.json.
So now let's take a look at how we configure build and debug our Makefile projects. So the
Makefile Tools extension provides this icon here on the left with the C++ and the gear.
So if we click on that, now we have this UI where we can select our configuration, our build
target, and our launch target, so if you had defined configurations in that makefile configurations
object that I was just talking about, then those would show up in this list here. If you go to
select a configuration, since I don't have any defined, we're selecting default.
So now let's configure our project.
Configure succeeded, and now let's select our build targets. So
hovering over this pencil icon and clicking it brings up a list of all
of the targets found in your project's Makefile, so I'm going to build ffmpeg
and then hit this build button and this built successfully so
we've built our target, now let's run it, and we're going to select
a launch target. So this launch target, what it does is it creates an object in makefile.launch
configuration in your project's settings.json file and this is where if you need to pass additional
arguments to your target for debugging you can specify those in this binaryArgs array here.
So now that we have our launch target set, and then also if you define more launch configurations here
they will appear in this list when you go to select your launch target.
Let's first just run ffmpeg in the terminal, which we can do using this play button.
So now you can see in the integrated terminal that it ran ffmpeg and we got some information
about what this program is and how to use it. We also have this message that says we can pass
-h as an argument to ffmpeg to get some more verbose helpful information.
So why don't we do that, let's actually add -h
as one of the arguments in binaryArgs. And now let's debug our program. All we have to do is
hit this debug button right here and we'll select the launch configuration that has the -h argument
and this launches our debugger and you can step through the code here and see your local variables,
you can add watch variables, we see the call stack, breakpoints, you can disable and add new
breakpoints from this debugger UI pane on the left, and if we just keep stepping through our program
you'll notice that all of the debugger features that you have access to locally are the same
through the remote SHH extension and through the Makefile Tools extension. So once again you're
getting that local quality development experience regardless of where your source code and build
tools live. Let's continue running the program. And since we ran ffmpeg, passing it that
-h argument, when we check our terminal we should see a bunch of helpful information, which we do.
So that is how you configure, build and debug C++ projects using Makefiles and Visual Studio
Code with our new Makefile Tools extension. make sure to check out all the live C++
sessions at Pure Virtual C++ on May 3rd, there'll be some good stuff there.
ThanksI