(upbeat music)
-: Welcome back, in this video,
what I'm going to do is I'm going to build
and run a sample C++ program using Visual Studio Code.
First thing we need to do is we need to create a folder.
That folder is going to hold our projects.
We're gonna have more than one project,
which makes life a lot simpler.
Most of the ways that you see Visual Studio Code set up
on the web is basically just one project,
and one project at a time.
And that's okay, but it becomes really tedious
for a course like this where we've got a lot
of little projects and we're learning a lot as we go.
It's much more efficient just to create one folder
that holds a bunch of projects
for each section of the course.
Okay, so first thing we're gonna do
is we're gonna create that folder.
Then what we're gonna do
is we're gonna write a really small C++ program
that we'll be able to test easily.
Then we'll configure Visual Studio Code
to build and run that.
Okay, so first order of business is to create that folder.
Now, I don't create the folder within Visual Studio Code,
I just do it right in the file system.
So I'm just gonna minimize this for a minute,
and I'm gonna go to my desktop
and I'm gonna create a folder here.
And I'm just gonna call this SectionX, let's say.
Now you can organize your folders however you like.
Section 1, Section 2, Section 3 of the course.
SectionX is what I'm gonna use
to represent any section that you might be working on.
So that's it, it's just an empty folder,
there's nothing in it.
We're gonna create sub-folders within it.
Each sub-folder will be a different project,
but we'll do that part in Visual Studio Code.
So let me come back to Visual Studio Code.
And now what I wanna do is I want to open the folder.
I can do it right here.
I can also use these command keys
and I can also come up to here
where it says File, Open Folder.
I'll select the desktop.
So now what I'll do is I'll select the desktop,
and I'll select SectionX,
and I'll select the folder down here.
And now what happens is Visual Studio Code has opened
up the SectionX folder.
There's nothing in it, it's just an empty folder right now.
And we can create new files and open files and so forth.
Now what I'm gonna do is I'm gonna close this
down right here, just so we don't clutter anything up.
And the first thing I'm gonna do
is I'm gonna create a sub folder
within this SectionX folder.
And we can do that by clicking that icon right there,
New Folder, this creates a new file,
this creates a new folder.
So that's what we're going to do,
and I'm gonna call it Project 1.
We can create multiple projects,
and I'll show you how to do that in a little bit.
And then within Project 1,
so we wanna be sure that Project 1 is selected,
we're gonna create a new file,
and that new file is main.cpp.
That's our main C++ program.
This is that little test program that we're going to write.
So I could just select that,
and we're gonna get this message down here,
do you want to use the insider's version.
That allows you to use new features in Visual Studio Code
that maybe aren't ready for prime time yet,
they're still being worked out,
release candidates and so forth.
I'm just gonna say no, I wanna use the stable version.
And now what we're gonna do here
is we're gonna write a really simple C++ program
so that we can configure it to run.
So lemme show you how to do that.
A lot of this you won't know yet,
especially if you're just starting out the course.
But just type along, follow along.
And all this will be explained during the course.
So I'm gonna say using namespace std.
And now we're gonna create an int main,
that's our main function.
And then all we're gonna say in here
is cout, hello from Project 1,
just so we know that if that displays,
our program is running correctly.
Alright, as I said, all this will be explained,
so don't worry about it if you don't understand the syntax,
that's perfectly normal.
Okay, now, so we've got this program that we want to run,
and it is in Project 1 folder,
which is in the SectionX root folder, perfect.
It's very important now that we always select the file
that we want to run.
We may have multiple projects with multiple main.cpp files.
Visual Studio Code runs the active one,
which is the one that's selected.
So it's really important that we select it.
Now before we run it, we need to do some configuration.
So we can come up here to View, Command Palette,
or press Control + Shift + P.
And what we're gonna do here
is we're just gonna type C++.
And somewhere in here you're gonna get
an Edit Configurations UI.
That's what we need to do.
That's going to configure
the Visual Studio Code IntelliSense,
which is a real nice feature, as we type our code,
it's gonna try to help us figure out, you know,
what errors we might have and so forth.
So I'll show you how to do that, we just select that,
and we're gonna come right into here,
IntelliSense configurations.
And most of these we're not gonna mess with.
This compiler right here,
notice that it's already figured out
that I'm using the mingw64 compiler.
That gcc though is not the one we want.
We want the g++, that guy right there.
We may have other compilers on the system as well,
but this is the one I want to use, mingw.
So make sure that that's g++, not gcc,
gcc is the C compiler.
So I'm gonna scroll down just a little bit more.
And then right here, for the C++ standard,
you can use 14 or 17.
I'm just gonna use c++17.
That tells IntelliSense to use that standard
when it's looking through the code, that's it.
So you can press Ctrl + S to Save,
and our IntelliSense now is configured.
You'll notice what happened over here on the left side,
A .vs code folder was created, and if you expand that,
it created a c_cpp.properties.json file.
That's a configuration file that has all that stuff
that we just set.
Okay, so we're almost done, not quite.
What we need to do now is we need to configure a build task,
and I'll show you how that works.
You can come up here to Terminal,
and Configure Default Build Task right here.
Select that.
Now it's gonna ask us what compiler do you want to use?
And we may get a lot of different compilers,
depending on what's installed on your system.
It's important that you use mingw64 g++.
This guy right here, don't use this,
that's a pre-processor.
Don't use this, that's the C compiler.
Make sure we're using some kind of g++ or c++,
something in there.
If you're using mingw64, this is what it's gonna be.
It might be installed in a different place,
but you're gonna see the mingw64 and the g++.
That's what I'm gonna select.
Okay, so notice now what happened,
it created a tasks.json configuration file.
So we need to make a few changes here.
Right here on line nine-
right after line nine,
I'm gonna add a few command line switches.
One is -W, all, lowercase,
followed by a comma, that tells the C++ compiler
to generate warnings, all warnings that it can.
Then I'm gonna press Enter, and again,
I'm gonna use some quote marks,
and I'm gonna say -std=c++17,
and a comma at the end,
that tells the build to use the c++17 compiler.
Now there's one more thing we need to do
right here where it says file directory name,
we want to copy that, and put it right in here.
So we're gonna replace file with file directory name.
Okay, so I'm just gonna paste it right in there
so I don't have any typos.
And then, right at the end of that,
right between the curly and the quote,
I'm just gonna do a \\*.cpp.
And that tells the build system to compile all the C++ files
that it sees in the project.
One of the problems with Visual Studio Code
as it comes configured,
it only builds the one single C++ file.
So when students write larger programs
that have multiple C++ files, they have errors,
because they can't build multiple C++ files.
So that's it, that takes care of this.
I'm going to save this and I'm going to get outta here.
So now I'm right back to here.
So what I want to do next is I want
to build this program, that means compile it then link it.
So I'm gonna come up to Terminal,
and I'm gonna say Run Build Task,
Control + Shift + B.
Now when I do that, we'll get some stuff going on down here,
and notice it says build finished successfully.
And it'll also see that Wall
and the standard c++17.
All those compiler flags that we put
in the tests.json file,
this is where they're being used.
There's the *cpp that we changed.
So basically it's compiling everything
in that Project 1 folder,
and creating a file called main.exe.
That's our executable file,
that's the one we can run.
And you can see it right over here.
There it is.
Now what we can do is we can run that executable.
So I'm going to right click on that executable,
and open it in the integrated terminal.
And you can see that it's opened up a command prompt
right in that Project 1 directory.
And that's exactly where main.exe is.
So if I type ls, that just gives us a file listing,
and we can see that we've got our main.cpp
and our main.exe.
So what we want to do now is we want
to run the main.exe program
so we can do a .\main.exe and press Enter.
And there it is, "Hello from Project 1."
Simple as that.
So notice what we did,
we built the program, and by the way,
these configurations that we've done are,
we're done with that, we don't have to do that again.
We can create new projects,
we can create new cpp files within those projects.
These configurations will apply to all of them.
So lemme show you how to do that.
Let me close this down,
and let's create another project, Project 2.
So if I click over here and I select that guy,
now it's gonna create the folder within Project 1.
That's not what I want.
I want Project 2 to be at the same level as Project 1.
And at first you're thinking, how do I do that?
I know, I remember I couldn't figure it out.
The way you do it is, and when you press this,
you click main.cpp for example, and you press escape,
and notice this blue box that just showed up.
It's not really intuitive
that you need to press escape there,
but that's what works.
Now we can come over here and create a new folder,
and we're gonna call it Project 2.
And notice now that Project 1
and Project 2 are at the same level,
which is what we want.
So I'm just gonna copy Project 1.
So I'm just gonna do a Copy and Paste
and I'm gonna come over here to Project 2,
and I'm gonna create a main.cpp, just like we did before.
And I'm just gonna copy that
so I don't have to type it all in again.
I'm just gonna say, "Hello from Project 2."
That's it, so now what do we wanna do?
We wanna run Project 2.
So remember it has to be the active file.
So you wanna make sure that you are clicking here, right?
And then what we're gonna do is we're gonna go to Terminal,
Control + Shift + B, or Run Build Task.
I tend to just do Control + Shift + B,
but I'll do it this way here,
so you can see exactly what I'm doing.
So Run Build Task,
it is going to compile and build Project 2.
You can see right over here where my mouse is,
and it created another main.exe file.
If I wanna run that, I can right click on it,.
Open in the Integrated Terminal, and do a .\main.exe,
"Hello from Project 2."
So this is how we can create as many projects as we want.
It's really easy and it's really convenient,
because sometimes when we're learning,
we're trying different projects out.
So that's pretty much it.
We can also, when we run the program,
we can grab input from the user.
So if I wanted to change this Project 2 right here,
I could always say, let's see,
int num, and then we're gonna say something like,
cout Enter your favorite number,
and then we'll read that from the user into num.
And then we'll just display cout "Your favorite number is,"
what they just entered, right?
Num, and we'll put a new line at the end.
So this program now has input and output.
And remember, I can't just run it again,
because I have to rebuild it.
If I run main.exe, let me show you how that works.
It'll just,
it'll run the previous one, right,
'cause I haven't built it again.
So I have to come up to Terminal,
Build Active, there we go.
Now I can run this main.exe,
because it's been rebuilt, and I can run that.
And it's gonna say "Enter your favorite number."
and I'm gonna enter a 100.
"Your favorite number is 100." okay?
And I could have put a space right in here
or something like that and a colon,
just to prompt a user in a little prettier way.
So that's it, this is how we can build
and run our C++ programs.
In the next video, what we'll do
is we'll create one more configuration file
called launch.json.
And what that's gonna do is it's gonna allow us
to use the debugger so that we can walk through code
and debug code, especially code that's really tricky
or it's giving us problems
where we're trying to find an error.
And I'll show you how to do that in the next video.