(bright ascending music)
(air whooshing)
(keyboard typing)
-: [Frank Mitropoulos] Welcome back.
Now that we've got Visual Studio Code installed
and the C++ extension installed,
we can now start creating C++ projects
that we can build and run within Visual Studio Code.
So, first thing we need to do is we need to create a folder.
I'm going to create the folder, not within VSCode,
but I'm gonna create it right on my Desktop.
So I'm just gonna come over here to my Desktop
and I'm gonna create a New Folder,
and I'm just gonna call it "SectionX."
The idea being that whatever section you're working on,
you can create a Section5, Section6, Section7 folder,
and so forth.
So this could represent any folder,
we'll create it, and I'll close this up.
Then, we want to go into Visual Studio Code
and open that folder we just created.
We can click the blue button here if this is open,
we can also hit Control+O, Control+K, and so forth.
Or we can come up here to File, Open Folder right there.
So I'll just do that, and I'm going to select the folder
I just created on my Desktop, "SectionX,"
and I'll press Okay.
And now, you can see that that folder has been opened.
It's right here, "SectionX.'
There's nothing in it right now, it's just an empty folder.
And this Getting Started page,
I'm just gonna close that to get it outta the way.
Perfect.
So now, what we need to do
is we need to create a folder within this folder.
This is the top-level folder.
So we can do that by selecting that icon right there,
which is New Folder.
And I'm just gonna call this "Project1."
So this will be my "Project1" folder,
the idea being that we can create multiple project folders
within this "SectionX" folder.
Now once we do that, we need to create a C++ program
that we'll be able to run,
and we wanna do that within "Project1."
So we wanna be sure that "Project1" is selected,
and I'm gonna press this icon right here for New File.
And I want to create "main.cpp," that's our C++ program.
When I select that, it opens up on this side here.
And what I want to do here
is I just wanna write a really simple C++ program,
and I'm going to do "#include."
A lotta this won't make sense to you
if you're just starting the course.
That's okay, just follow along, type along,
and this will all be explained as the course progresses.
Then we're gonna say, "using namespace standard."
And now, what we're going to do
is we're gonna create the main function.
So we'll say, "int main,"
and we'll just do a display statement,
something really simple.
We'll just say "cout," and, "Hello from 'Project1.'"
And we'll put a new line at the end.
And finally, we'll "return 0."
As I said,
the syntax will be explained as the course progresses.
Now, I'm gonna save that.
So I'm just gonna come to here, File, Save, or Control+S.
We can't run it quite yet,
we've got to do a little bit of configuration
which is gonna help us build and run this project.
So there's two things we need to do.
First, we need to configure IntelliSense.
That's the piece of software that runs in Visual Studio Code
that kinda watches you as you type,
and it's tryna help you with ideas, and suggestions,
and finding mistakes as you go.
So we could go to View, Command Palette.
And then what we wanted to do, right in here,
we wanted to just type "C++,"
and find this Edit Configurations UI.
C++, Edit Configurations UI.
It might be somewhere down here.
In this case, it showed up right up here for me.
So I just wanna select that,
and we're gonna make a couple of small changes to this.
First of all, go to the "compiler path,"
and be sure that you don't have GCC, but we want G++
We want G++.
We wanna use a C++ compiler, not the "C" compiler.
So you wanna be sure that that's there.
And then we wanna come down to the "C++ standard,"
and we want to use "C++17."
There we go.
That's it.
So now, we can go File, Save that file,
and we can close that up 'cause we're done with that.
Now, you'll notice what happened over here on the left side,
a ".vscode" folder was created.
And that folder contains a file
called "cpp_properties.json,"
and that file contains the configuration information
for IntelliSense, what we just set up.
Perfect.
So the next step is to create a default build task.
That's what's going to compile the code and build it.
So in order to do that, we want to come to Terminal,
and select Configure Default Build Task,
right here at the bottom.
And at this point, it's asking you,
"Hey, what compiler are you using?"
Be sure to select the G++ option,
not CPP or anything else, just G++.
You may have several different versions, like I do,
just select G++, and it will use the most recent version.
So I'll do that.
And now, I get this file that pops up.
We need to make a few small changes to this file
so we can configure the build.
I'm coming here to line nine in the "args" section.
And I just wanna add, right after the "-g,"
I wanna add minus or dash capital "W," "all,"
and the "all" is in lowercase, followed by a comma.
That tells the build to display all warnings.
Then we need to do "-std=c++17,"
with a comma at the end again.
That tells the build system to use the C++17 standard.
And we've got one more small change right here.
We want to change line 12 from "file" to "fileDirname."
So what I wanna do is I want to copy that
and paste it right in here, just like that.
So now, we've got "fileDirname."
We still have the dollar sign and the curlies,
but we want to change "file" to "fileDirname."
So the next thing we want to do
is we want to add, "/*.cpp" right after that closed curly.
This tells the build system
to build all the C++ files in that project.
By default, Visual Studio Code, when it comes configured,
it only does the one file.
So as we build projects that get bigger,
we're gonna have multiple C++ files.
So, that's really important that we do that.
So I'm gonna Control+S to save,
and I'm going to close that down.
And now, we're ready to build and run this program.
So, what do we do first?
It's very important that we select the C++ file
that we want to build within the right project.
It's possible that we'll have multiple projects,
and every project will have its own C++ file.
That has to be selected
because the build system in Visual Studio Code
will build the active file,
which is the one that's been selected.
So once we have that selected, we can come up to Terminal,
and Run Build Task or Control+Shift+B.
And what'll happen is right down here,
you can see the output of the build,
it says, "Build finished successfully."
You can also see our G++ compiler being executed
with those options that we typed into our test JSON.
And you can also see that it's using the "Project1" folder,
and it's compiling all the "*.cpp" files.
So it's doing exactly what we told it to do.
So now, we have an executable that we can run,
and that executable is called "main,"
and it's right here in the project.
Before we only had the one source file,
now we've got the executable file.
So, how do we run that?
Lemme close this up and just make a little bit more room.
We can right click on that file,
and select Open an Integrated Terminal.
That will open up a terminal window down here,
and notice it's in the "Project1" folder.
That's exactly where we want to be.
And if you type "ls,"
that will list the files in that directory.
And you can see there's my "main.cpp,"
and there's my main executable, it's green.
So if I want to run this program,
all I need to do is type "./main."
Really important that you use the "./" so it can find it.
And when we press Enter,
we expect this to display, "Hello from 'Project1.'"
So, let's do it.
And that's exactly what we get, "Hello from 'Project1.'"
So there you go.
This shows you how to set this up
so that it can build and run C++ projects.
Right now, we only have the one project, "Project1,"
so let's create another project.
We need to create a new folder.
The problem is if we create a new folder right now,
it's going to create it underneath "Project1."
That's not what I want.
I want "Project2" to be at the same level as "Project1."
And it's not really intuitive as to how you would do that.
You would think, "You select this one,"
but then the folders go away.
So the way to do it is just select "Project1,"
and then press Escape.
And when you press Escape, notice what happens,
you get this blue bounding box here goin' around.
Now when you create the folder,
you can type in "Project2," or whatever you like,
and it's at the same level as "Project1."
That's exactly how you would do that.
We can create as many project folders as we need.
Now in "Project2," I need to create my C++ file,
so I'll select New File, and type in "main.cpp."
And what I wanna put in here is...
Let's put in the same thing that we had here.
I'm just gonna copy and paste this,
so that I don't have to type it all in again.
So I'm just gonna select it all, and I'm going to Copy,
and I'm gonna come back to "Project2's" main,
and Paste it in here.
There we go.
Lemme close this window up down here,
give us a little bit more room.
And all I'm gonna do here is say, "Hello from 'Project2.'"
And I'll Control+S to save this.
So now if I want to build this project, "Project2,"
I have to select that.
If you select this or this, and try to do a build,
you're gonna get all kinds of weird things happening
because it doesn't know what to do.
So we wanna select the C++ file within "Project2,"
and again we wanna go to Terminal
and we want to Run the Build Task.
And notice, we get exactly the same thing,
"Build finished successfully,"
except you can see it's working on "Project2" now.
And we created an executable file in "Project2,"
"Project1" was not affected.
So if we want to run this file, we can right click on it,
open it in the integrated terminal.
Notice we're in "Project2" folder now,
and we can just do "./main."
And we expect now to see, "Hello from 'Project2,'"
and that's exactly what we get.
So, that's it.
That shows you how to create folder structures,
create C++ files, build and run the active tasks.
We can add as many projects as we like inside this folder,
this "SectionX" folder.
And it's a real handy way to keep yourself organized.
In the next video, what we'll do
is I'll show you how to set up Visual Studio Code
so that we can debug C++ projects.
Debugging is really important,
and helps us find errors and understand our code better.
And the idea with the debugger
is that we can execute our code one line at a time,
and we can jump around between pieces of code,
and we can actually see the values of our variables
as we're executing the code.
So it's a very, very powerful tool to have in our tool belt.
So, I'll see you in the next video and we'll do that.