How to compile and run a C++ program ?

I love learning new language, and I just thought this would be kind of a fun series. And it gives me an opportunity to kind of cover things that I myself found very difficult in the C plus plus language, and really focus on them in the tutorial. Whereas someone who’s maybe an expert in C plus plus may have kind of forgotten about what beginners find quite difficult in a new language like this. So with that set in this topic here, I’m going to discuss how you can compile and run a C plus plus program.

I’m going to show you how to set up your coding environment, whether you’re on Mac, Linux or windows, and then in future videos, we’ll actually get into the code and then we’ll go all the way up until object oriented programming in C plus plus.

What C++ Is Used For ?

So with that said, let’s dive in. So before I actually get into the install and setup steps, I figured it wouldn’t be a bad idea to discuss what C plus plus is used for and why you may want to learn. So, first of all, C plus plus kind of gets a reputation of being a very difficult programming language to learn. I would argue that it’s not that hard to learn, especially if you haven’t learned another programming language before, and it’s really not that difficult. It’s just different than some other languages like Python or JavaScript.

So don’t be intimidated by that. If you’ve heard something like that before, this is a great language to start with, again, not going to be the absolute easiest one to learn, but definitely a good option. And you’re not going to regret learning C plus plus as your first or maybe even second programming language. Now that said C plus plus is a very popular programming language. It’s used by tens of millions of developers. It’s probably in the top five, maybe in maybe even in the top three of the most popular programming languages out there it’s been around for a really long time. It’s used for a ton of different applications.

It’s used for 3d games, operating systems, compilers, interpreters, embedded systems, desktop applications, websites, database, software. I could go on and list about a million things, super versatile, super fast programming language. It’s very efficient and performance optimized. If you’re writing any kind of code where you really care about the execution time of that code, then C plus plus is a really good choice for whatever that application or code is going to be.

So I could go on and on about why you would want to learn C plus plus, but just trust me, it’s a great language. You’re definitely going to enjoy it, learning it. And it’s super versatile. You can do a ton of stuff with it. So with that said, let’s now dive in to the install slash setup steps.

Text Editor Install

So what I’m going to show you, how to do specifically is how to install a C plus plus compiler and a code editor that you can actually write your code in. So in theory, you can write C plus plus code in anything. You can write it in notepad, you can write it in notepad plus plus you can write it in a, I don’t know what it’s called on windows, like are not windows or a Mac, whatever their default little text editor is. Uh, you can write it in that you don’t need like a dedicated code editor to write C plus plus code in.

However, if you want to get nice syntax highlighting, you want to get auto-complete and some other just nice features to have when you’re writing code, you’re going to want to install a text editor or a IDE. So like something like visual studio code, you don’t know what that is. Don’t worry about it. In this tutorial series, I’m going to be using a text editor called sublime text. This is a really lightweight, really minimalist text editor. I really like it for doing basic tutorials.

And I would recommend if you’re someone who’s just starting out and you don’t kind of know about any other text editors or IDE, just go with the sublime text editor it’s free and you can get it from the link in the description. So just download this sublime text editor, and then open it up and you will now have an environment in which you can write code in.

How to Setup

Now, if you’re on windows, your install steps are going to be a little bit different than if you’re on Mac. So if you’re on Mac windows or Linux, you need a text editor, or you need something to write your code in. So pick whatever that is that you want. I recommend recommend sublime text for this tutorial. And then if you’re on Mac, your setup is done. You actually already have a C plus plus compiler installed on your computer. I’ll show you how to access that in a minute.

If you’re on Linux, same thing, you already have a compiler in your system. However, if you’re on windows, you do not. So you need to install it. So there’s this thing called Ming GW, not really going to get into exactly what this is, but go to the link in the description, download this, run through the, install it, if you see any options, just leave them completely default. The only thing you should change, if you want to is the install location of this program.

Make sure when you install this, you keep track of where you install it. So if you install it on a different drive or something like that, just make sure you know where it is because we are going to need to find the actual location of this on our, uh, on our disk or I guess on our draft. So go ahead, run through this install again, link in the description. Once it’s installed, you need to make sure you remember where you installed it. Okay?

What a Compiler Is

So now that you have this installed, assuming you’re on windows, again, if you’re on Mac, you can kind of fast forward a bit until this has done. What you want to do is you want to find this program from your file Explorer. So open up your file Explorer, uh, go to your drive. I’m going to go to this PC. I’m going to go to my C drive. This is where I installed my program and just go and locate wherever you installed. So it should be by default in your C drive in program files, x86, and then you should scroll down and you should see the Ming, uh, GWS Ming GWW 64.

Now go inside of all these folders that I’m showing you. So go inside of this one, go inside of the Ming GW folder, then go inside of bit. So again, this is specific. If you’re on windows, now, what you want to do is you want to get the path to this bin. Notice here. There’s all kinds of executable files here. These are actually a bunch of different interpreters that you can use for other program languages or sorry, not interpreters compilers. So, uh, you can see G plus, plus, this is one that we’re actually going to use for this topic right here.

Again, you don’t need to worry about all of this, but just find this install location. So if you click on your little bar here, you can copy this install location. So copy it to your clipboard. Now that you have this installed, what you need to do is you need to go and modify your path variable. Now, a path is just kind of a list of all of the locations to different executable files on your machine. Now, the reason we need to add this right here to our path is so that we can actually use the C plus plus compiler from our command line.

You’ll see how this works in a second, but while you’re going to type here and your windows search bar is just path. Now it should pop up this thing that says, edit the system environment variables you want to go to a tablet, looks like this again. You could type in edit the system environment variables, open this up, where you’re going to see as you have all of these different tabs, go to the advanced tab, go to environment variables, and then you should see something that says, Pat. Now when you’re on path, what you’re going to do is you’re going to press edit. You’re probably gonna see a ton of other stuff inside of here. What you want to do now is you want to add the path to this install location. So whatever you just installed, you want to add the path to that.

That’s the path we just copied. So we’re going to press new. And then we’re just going to paste in that path that we have. You can see, I already have mine here, so I’m not actually going to do this, but just go ahead and paste it in. So let me just remove that because I don’t need it twice. So once that is in there, you’re going to press okay. And then you’re going to press okay again. And then if apply is able to be pressed, press apply. It probably won’t be, then you just press, okay. Now what you’ve done is you’ve added the path to that install location.

It’s now any of those files that were in that bin, you can now access them without having to type out their entire path from your command prompt. So now on windows, what you want to do to test that this is working is open up your command prompt and you want to type G plus plus now G plus plus is a C plus plus compiler, which I will discuss in a second. So you type G plus plus, and just press enter. If you got some type of output, even if it says fatal air, that’s totally fine. That is good.

If you see something that says G plus plus is not a recognized command, neat, you need to make sure you follow along with the steps I just showed you. And that you added this to the path, try to restart your command prompt, and then the boot it up again and see if this works, but it should work after you go through those steps. So now you should have your CBOs plus compiler installed, and we’re ready to actually go ahead and start writing some code. So at this point in time, all of the Mac people, you can start listening again. Uh, you will have this G plus plus compiler on your machine by default. So you guys should open up your terminal. You should type G plus plus in here. And you should see if that works.

No again, that just should work. If you’re on macro Linux idle, I don’t know why that wouldn’t work. That should work for you. And then we’re kind of ready to go. So now this is a good point for me to discuss what I mean by compiling. Cause I keep saying this word compiler, you’ve probably heard me say interpreter to a compiler kind of takes the source code that we write and it transcribes it. We’re translates it into a binary executable file. So by default, whenever you write C plus plus code, you can’t just run it back code that is just, you know, plain text on your computer. The computer doesn’t understand that. So what the compiler does is it takes this code. He translates it into machine language or into a binary language that AI that your computer can actually understand and that it can run.

So what’s going to happen is every time we write our code, whenever we want to run it, we’re going to compile the code, translate it into this new file. And then this new file we’ll have our program in a binary format. We will then run that program and there we go, or we’ll run that file that we’ll run our program and we will be good to go. So that is the difference between C plus plus, and some other programming languages say like Python, you wouldn’t have to do something like this. Okay. So hopefully that makes sense. Uh, that is what I mean by compiler.

Our First C++ Program

So now what we’re going to do is we’re going to write our very first C plus plus program. So you’re gonna open up sub-line text on whatever editor, whatever operating system you’re using, or if you’re not using sublime text, open up your editor and create a new file. Now this new file that you create must have a dot CPP file.

Extension CPP stands for C plus plus. So whenever you write your C plus plus code, you’re going to put it in a dot CPP file. Now, if you’re working in a text editor like sublime text, as soon as you start typing, you’ll notice your text is going to be highlighted in different colors. That kind of tells you that you’re getting syntax highlighting in that this editor recognizes that the code that you’re writing is C plus plus code. .

So what we’re going to do is write out really standard hello world program. I’m not really going to explain what I’m writing, just write it out here. This is just the test that our environment is set up. So what we’re going to do is the following we’re going to say include, and then inside of angle brackets, we’re going to say I O stream like that.

Then underneath this line, what we’re going to type is using namespace STD. You might get this auto-complete if you’re working in a, in sublime text, and then we’re going to put a semi-colon at the end of this line, a semi-colon is a Terminator. It tells you that whatever statement you just wrote is now terminated.

Now what these two lines are kind of doing is they’re saying we’re going to include a library that allows us to actually print information to the console. And this namespace is going to give us access to things that are in the standard C plus plus library. So just like pieces of Syntex, again, you don’t have to know what that means. Just trying to give you a bit of information. Now we’re going to create a function. We’re going to say int main, we’re going to put two parentheses like this, and then we’re going to put two curly braces.

And inside of here, we’re going to write the following line. We’re going to write C out. We’re going to write these two things right here. I think these are less than signs. Although I always mix up if they’re less than or greater than, you know, write two of these here. And then you’re going to put inside of quotation marks, make sure they’re double quotation marks. Hello, world, exclamation point semi-colon. And then the next line, you’re going to say return zero. Now, as soon as we get through a few videos here, all of this will make perfect sense.

You will know this like the back of your hand, but for right now, just kind of take for granted the fact that this is going to print something out to the console. And this is our very first program. All this is going to do is print this hello world. So when we run this code, all we’re going to see is that a little bit of textures of the console that says hello world. So go ahead and save that control. S and then what you want to do is you want to compile this file.

Compiling C++ Code

So to compile this file, you need to open up your terminal or your command prompt depending on the operating system you’re on. And you need to navigate to the directory. You’re storing this file in. So I’ve created a folder on my desktop. It’s called C plus plus tutorial inside. I have my file tutorial, one dot CPP. So open up your terminal, open up your command prompt, and you need to use the CD command, which stands for change directory to get into the directory where this file is contained. So a little bit of a shortcut.

If you’re on windows here is if you just go to the directory from your file Explorer, like you normally would. So let’s go to C plus plus tutorial, and then you go here. So this little search bar, and you delete all of this and you type CMV, this will open up the command prompt in this location.

So notice location is where we just work. And then you’re good to go. So if you’re on Mac, you’re probably going to be in some directory that is not your desktop. You’re probably going to be in your user’s home directory, which means you should have something like users and then whatever your name is or something like that.

So what you’re going to do is you’re going to navigate to wherever you’re storing this font. So if it’s on your desktop, for example, you’re gonna type CD desktop, and then that’s going to bring you to your desktop. So let’s actually just go ahead and do this. I ignored the two lines. I just typed. Let’s say, I’m on Mac and I’m in this, this area. I type desktop. I go to my desktop notice. Now the path changes the desktop.

Then I’m going to type CD, and I’m going to go to whatever directory this file is contained in. So maybe you have a folder called C plus plus tutorial. Uh, by the way, you can type the first few characters of whatever you want to go and press tab, and it should auto fill it. So if I do C and then tab and auto-fill C plus plus tutorial, so go ahead and press enter. And now from here, you should be good to go. So just navigate using CD, which stands for change directory to the directory you want to go to.

If you make a mistake and you need to go back type cd.dot, this will bring you back to the parent directory. So we’ll move you back. One notice here, this moves me from sequel, suppositories, back to desktop. Anyways, I have this, uh, command prompt open in this directory. Now what I’m going to do is compile my C plus plus file to do this. I’m going to type G plus, plus this will work on both Mac and windows. I’m going to type hyphen O which I believe stands for out. Then I’m going to type the name of the file that I want to contain the compiled code.

So the name of the file I want to run. I’m going to call this run or I’ll call this like run program or something, call it whatever you want. Doesn’t matter. And then after this, you’re going to put the name of the file you want to compile in this case, the name of my file is tutorial one. So whatever you named your file, you put here and then you press enter. So G plus plus hyphen, Oh, run program. Name of the file that contains the code. You wrote dot CBB, press enter. You’re going to see that you’re gonna get no output. If you get no output. That is good.

Running C++ Code

Now, what you can do is you can type this name right here that contains the kind of output file. So I’m going to say run program and just press enter. When you do that, you should see that hello world pops up on the screen. If you’re on Mac or Linux type of dot slash run program, and that should work. So either of them should probably work, but on Mac and Linux, I’m not sure cause I haven’t done this. Uh, you probably need to type dot slash and that should then run the file for you.

But you can run this as many times as you want. It contains the compiled code. No, just notice something here. If I change this, what I’m kind of printing out to the screen to just say hello and I saved this file. And then I go ahead and I try to run this code again. So I do run program notice it’s still printing. Hello world. The reason for this is I have not recompiled my coat. So what I need to do to recompile my code or to get this program to now be updated with the new code I wrote is I need to compile it again.

So a little shortcut here, if you press the up arrow on your keyboard, this will cycle through the commands you’ve already typed. So since I’m already in the same directory and I just want to compile the same file again, and I want it to go to the same output file. I’ll just rerun this command. So now that I rerun this command, it should recompile my code or compile it again. And now what I can do is just type the name of that program. So we’ll say run program, and now notice it just says hello. So there you go.

That is the basics of how to set up your machine to run C plus plus code. So that is all I had for tutorial one. I apologize. This was a little bit slow. I just want to make sure I go through every single possible edge case and thing that could go wrong. So you guys can follow along and aren’t getting frustrated and stuck at a ton of things. You’ll probably notice with my tutorials. I’m very verbose. I go through everything and I just want to make sure I’m giving you as much information as I can so that this is the only resource you need to go to.

Avatar of Biên tập Viên

About: Biên tập Viên

A guy who love writing and inspire message.