(bright music)
Frank: Welcome back.
Now that we've got Visual Studio Code installed
and the extensions installed,
what we're going to do is create a folder structure
so that we can create different projects
all within the same root folder.
That's just gonna make it very efficient for us to work.
So I've already created a folder and let me show it to you.
It's right on my desktop
and I created this folder called SectionX.
The idea being if you're working on section one
or two or three of the course,
you can create your own section one,
section two and section three folders.
So this could represent any one of them.
It's an empty folder, there's nothing in there right now.
So what I want to do is, I want to open that folder
in Visual Studio Code.
So I can choose open folder or open
or I could go to file open.
So I'm just gonna click open folder
and go to my desktop and select that SectionX folder
and I'm going to open it.
So now it's gonna open up in Xcode
and you can see right here, there's the folder,
there's nothing inside of it.
Let me just make this window a little bit wider
so we can see it a little bit better.
So the idea is now that I want
to create sub-folders in there
and each one of those sub-folders corresponds
to a different project.
So the way we do that is,
we can click this icon right here,
that's the new folder icon,
and I just clicked it and I'm gonna type Proj1,
that's the name of that folder
which is also going to be the name of my first project.
And then within that, I want to select
this icon right here, new file.
And that's going to be main.cpp.
That is going to be my C++ program.
That's what we're going to build and run.
So this font is a little bit small,
'cause I just installed this
so let me just increase it a little bit.
And the way I did that was just COMMAND +.
All right, so let's write
a really, really simple C++ program here
and then we will configure those extensions
so that they can build and run it.
So let's start with something,
pound include <iostream.
That allows us to do input and output.
We're gonna say using namespace std
and now we're gonna create the main function.
If this looks foreign to you
and you've just started the course,
don't worry, that's perfectly normal.
Just follow along, type along.
The whole point of these lectures
is to make sure we've got the environment set up correctly.
All the syntax and everything you're seeing
is going to be explained in the course.
So at this point, what we're gonna say is,
we're gonna say, cout << "Hello from Proj1" <<,
and we'll put an end line at the end to give us a new line.
And we return zero from main
to indicate that everything's good.
And I just saved that with COMMAND + S.
First thing we're going to do,
is we're gonna set up IntelliSense.
IntelliSense is a little program
that runs alongside Visual Studio Code
that watches what we're typing
and it tries to give us suggestions
and it shows us when we have errors
and it's very, very handy.
So let's set that up.
And the way we do that, we come up to view
and then COMMAND + PALLET.
We can also use SHIFT + COMMAND + P.
And that's going to give us a bunch of options here.
And if you just type C++ at the top,
what we're looking for is this guy right here,
C/C++ Edit Configurations (UI).
We need to make a couple of changes to that.
So I'm gonna open that up
and I'm gonna come right down here
to where it says compiler path.
And if I bring that back a little bit
so I can see this little arrow down here,
I wanna change that to G++.
So I want you to choose G++ right there.
That's the compiler that's going to be used
and then I'm gonna scroll down just a little bit more
until I get to the C++ standard
and there I want to select C++ 17.
That's it, that's all we need to do.
And now if you save this
and I'm saving right now with COMMAND + S.
Now notice what's happened here over on the left-hand side,
a folder was automatically created called .vscode
and inside that folder there's a file called
c_cpp_properties.json.
That file contains the configuration information
for what we just did. So now what we need to do,
is we need to set up a build task
that will execute and build our program.
And the first thing we have to do in order to do that,
we have to select that main.cpp file.
This is very important.
Every time that we need to build something
in Visual Studio Code,
we need to select that main file that we're trying to build.
So we need to define that build task.
So we can come up to terminal
and select configure default build task
and I'll select that and then it's gonna ask us
what build system are you using, what compiler are we using?
Well, we're using G++.
You can see it right here.
I'm gonna select that.
Now it's created a new file,
you can see it over here on the left. It's called tasks.json
and it contains configuration information
for how Visual Studio Code is gonna build our project.
Now we need to make a few changes to this file.
Right here on line nine, you'll see this,
on, sorry, on line eight, you'll see this args section.
These are the arguments that are sent to the compiler
and we're gonna add a couple.
So right after line nine, right after the -g option,
we're gonna put in -Wall with a capital W,
lowercase A-L-L.
That tells the compiler to generate all warnings.
Then we're going to add one more, -std=c++17,
and that's -std=c++17.
That tells the compiler to use the C++ 17 standard.
And there's one more thing we need to do,
is we're going to change this line 12 right here.
We're gonna change that file to this name, fileDirname.
So what I'm gonna do is, I'm just gonna copy that
and I'm gonna paste it right in here
and then right after that curly,
that closed curly brace, I'm just gonna do /*.cpp.
That tells the compiler to compile
all the C++ files into folder.
By default, Visual Studio Code will only compile one file.
We only have one there now,
but we'll have more than one later in the course,
so we wanna be sure that we're compiling all of them.
That's it, those are the changes we need to make.
At this point we can save,
I just saved with COMMAND + S and we can close this down.
Now we're ready to build our program.
So remember what I told you,
the first thing you need to do,
is select the main.cpp file that you want to build.
There may be and there will be many, many projects in here.
We'll have Proj1, Proj2.
Each one of them will have its own main.cpp,
so we have to be sure that we select
the one that we want to build
and then we're gonna come up to terminal and run build task.
You can also do SHIFT + COMMAND + B.
When we do that, the compiler will build the executable file
that we want to run.
So I'm gonna click that now
and you can see the output right here.
Notice we're using the G++ compiler.
There's that -g -Wall -std=c++17 we entered.
There's the *cpp we entered
and it's building main in Proj1 folder.
That's exactly what we want.
Build finished successfully.
So if you notice, a file was created over here, right there.
That main file.
That is our executable file, that's the file we can run.
That's the file that's gonna say "Hello from Proj1"
when we execute it.
How do you execute it?
Real simple, I'm just gonna close this here
to gimme a little bit more room
and I am gonna right click on that
and then say open in integrated terminal.
And it's just opened that folder for me.
And if I type ls, it'll give me a listing
of the files in that folder and you can see,
you can see main, you can see main.cpp
and you also see this main symbol file.
That's something that the compiler generates.
So if we want to run this program, we need to run main.
We do ./main.
That dot slash is real important.
So always use dot forward slash
and then the name of the executable.
And when I press ENTER,
we should see "Hello from Proj1".
And there it is, that's exactly what we expected.
Pretty cool, pretty easy.
Now, suppose I want to create another project
right under here.
If I click on that folder icon right there
to create a new folder, it won't do what I want.
What it'll do is, it'll create that new folder
underneath Proj1.
That's not what I want.
I want Proj2 to be at the same level as Proj1.
Intuition says, come on up here and click it, right?
But that doesn't really work.
So it's not too intuitive,
but the way that you do it is you can select Proj1
and then just hit ESCAPE.
So you see, I just pressed the ESCAPE key
and I got this blue box.
Now I can click on our folder
and create a new project called Proj2
and it's going to be at the same level as Proj1.
Now I can create my main.cpp file in here
and I can write another program
and I'm actually not gonna write it,
I'm just gonna copy this program since it's almost the same.
So I'm just gonna copy this program from here
right into here, and we'll just make one change.
We'll say Proj2.
That's pretty easy.
Now if we run this, remember we have to build it first.
We have to select which one we want to build.
Well, we wanna build Proj2.
So make sure you select the main in Proj2.
We don't wanna select the main in Proj1.
We've already done that one.
So I'm gonna select that
and then I'm gonna build.
So I'm gonna go to terminal and select run the build task.
And we're done, you can see build finished successfully
and Proj2's main was created.
And there it is right here.
So if I wanna execute that,
I can just open in the integrated terminal.
We can do ls again if we want.
There it is and we do ./main
and it's gonna say "Hello from Proj2".
Pretty easy.
We can modify this program slightly
to get input from the user.
Let's do that.
Let's say we have an integer
and we'll call that integer fav_number, just like that.
And then we'll keep this "Hello from Proj2" in there,
but let's also ask the user to enter their favorite number.
So we're gonna say, "Enter your favorite number".
Then we're gonna read that number
into the favorite number variable
and now we're going to display
and now we're going to display your favorite number
is the number they just entered.
And we'll add a new line at the end.
That's it, so we've just modified that program slightly.
If we execute main now,
the old program is going to run,
because we haven't built the new one yet, right?
These updates are not in this main executable yet.
That's why we need to build it.
So we're gonna select our main.cpp
and I'm going to select terminal.
Run the build task.
We have a successful build.
Now I can come over here to my main right, click on it,
open it in the terminal and we could just run it.
Hello from Proj2.
Now it's asking me to enter my favorite number.
Well, my favorite number let's say is 34.
I'll hit ENTER.
It says your favorite number is 34.
Simple as that.
We can create as many projects as we like.
Once our tasks.json
and our cpp_properties.json files are configured,
then we can create as many as we want.
We don't have to touch those again.
They'll apply to all the sub-projects.
So in the next video, I'll show you how to use the debugger
and how to configure the debugger
so that we can walk through our code one line at a time
and inspect our variables and their values
as we execute the code.
It's a very, very handy tool
that we can use to help us program.
I'll see you in the next video.