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 going to 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.
So first thing we're going to do is we're going to create that folder.
Then what we're going to do is we're going to 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.
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 going to minimize this for a minute and I'm going to go to my desktop and I'm going to create
a folder here and I'm just going to call this section X.
Let's say you can organize your folders however you like.
Section one, section two, section three of the course.
Section X is what I'm going to 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 going to create subfolders 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 want to 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, select the desktop.
So now what I'll do is I select the desktop and I'll select section X and I'll select the folder down
here.
And now what happens is Visual Studio code is opened up the section x 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 going to do is I'm going to close this down right here just so we don't clutter anything
up.
And the first thing I want to do is I'm going to create a sub folder within this section, x 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 going to call it Project one.
We can create multiple projects and I'll show you how to do that a little bit.
And then within Project one.
So we want to be sure that Project one is selected, we're going to create a new file and that new file
is main DHCP.
That's our main C++ program.
This is that little test program that we're going to write.
So I can just select that and we're going to get this message down here.
Do you want to use the insiders 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, or at least candidates and so forth.
I'm just going to say no, I want to use the stable version.
And now what we're going to do here is we're going to write a really simple C++ program so that we can
configure it to run.
So let me 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 going to say using namespace C CD and now we're going to create an INT Main, that's our main
function.
And then we're going to say here is C out hello from Project one.
Just so we know that if that displays, our program is running correctly.
All right.
As I said, all this will be explained.
So don't worry about it.
If you don't understand the syntax, that's that's perfectly normal.
Okay.
Now, so we've got this program that we want to run and it is in Project one folder, which is in the
section x route 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 CP 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 going to do here is we're just going to type C plus plus and somewhere in here you're
going to 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 going to try to help us figure out what errors we might have and so forth.
So I'll show you how to do that.
We just select that and we're going to come right into here, IntelliSense configurations and most of
these we're not going to mess with this compiler right here.
Notice that it's already figured out that I'm using the GW 64 compiler.
That GCC, though, is not the one we want.
We want the GCC plus plus that guy right there.
We may have other compilers on the system as well, but this is the one I want to use the GW.
So make sure that that's c g plus plus.
Now GCC TCC is the C compiler.
Someone else scroll down just a little bit more and then right here for the C++ standard, you can use
14 or 17.
I'm just going to use C++ 17.
That tells IntelliSense to use that standard when it's looking through the code.
That's it.
So you can press control s to save.
And our IntelliSense now is configured.
You'll notice what happened over here on the left side, a dot vs code folder was created and if you
expand that, it created a CP Properties JSON file.
That's a configuration file that has all that stuff that we just said.
So we're almost done.
Not quite.
What we need to do now is we need to configure a build test 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 going to 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 min GW 64 G Plus Plus.
This guy right here, don't use this.
That's a preprocessor.
Don't use this.
That's the C compiler.
Make sure we're using some kind of G plus plus or C++ something in there.
If you're using Min GW 64, this is what it's going to be.
It might be installed in a different place, but you're going to see the min GW 60 for the g plus plus.
That's what I'm going to 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 going
to add a few command line switches.
One is dash capital W, a LX lowercase followed by a comma that tells the C++ compiler to generate warnings,
all warnings that it can.
Then I'm going to press enter and again I'm going to use some quote marks and I would say dash speed
equals C plus plus 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 going to replace file with file directory name.
So I'm just going to 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 going to do a slash
slash start.
CP 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 out of 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 and Lincoln.
So I'm going to come up to terminal and I'm going to 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 Finish successfully
and you'll also see that w a LL 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 star CP that we changed.
So basically it's compiling everything in that project one folder and creating a file called Main XY.
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 one directory and that's
exactly where Main EFC is.
So if I type LHS, that just gives us a file listing and we can see that we've got our main CP and R
may not EFC.
So what we want to do now is we want to run the main XY program so we can do a dot slash, may dot XY
and press enter.
And there it is.
Hello from Project one.
Simple as that.
So notice what we did.
We built the program.
And by the way, these configurations that we've done, we're done with that.
We don't have to do that again.
We can create new projects.
We can create new FTP files within those projects.
These configurations will apply to all of them.
So let me show you how to do that.
Let me close this down.
Let's create another project, project two.
So if I click over here and I select that guy now, it's going to create the folder within Project one.
That's not what I want.
I want Project two to be at the same level as Project one.
And at first you're thinking, how do we how do I do that?
I know, I remember.
I couldn't figure it out.
The way you do it is when you press this, you click Main CP, 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 going to call it Project two.
And notice now that Project one and Project two are at the same level, which is what we want.
So I'm just going to copy Project one, so I'm just going to copy and paste and I'm going to come over
here to Project two and I'm going to create a marked HTTP just like we did before, and I'm just going
to copy that.
So I don't have to type it all in again.
I'm just going to say hello from Project two.
That's it.
So now what do we want to do?
We want to run Project two.
So remember, it has to be the active file.
So you want to make sure that you are clicking here.
Right.
And then what we're going to do is we're going to go to terminal control shift B or 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 t c right over here where my mouse is.
And it created another main gun file.
If I want to run that, I can right click on it.
Open an integrated terminal and do a dot slash main.
See hello from Project two.
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 to right here, I could always say, let's see it num.
And then we're going to say something like See out, enter your favorite number
and then we'll read that from the user into NUM and then we'll just display see out your favorite number
is what they just entered, right?
No, we will 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 XY.
Let me show you how that works.
It'll just go run the previous one, right?
Because I haven't built it again.
So I have to come up to terminal.
Build active.
There we go.
Now, I can run this mainly because it's been rebuilt and I can run that.
And it's going to say, enter your favorite number and I want to enter 100.
Your favorite number is 100.
And I could have put a space right in here or something like that and a colon just to prompt the 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 going to do is it's going to 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 given us problems or we're trying
to find an error and I'll show you how to do that in the next video.