Learn C++ With Me #1 - How to Compile and Run C++ Code

In Software Development

Learn C++ With Me #1 - How to Compile and Run C++ Code - read the full article about C++ 2021, Software Development and from Tech With Tim on Qualified.One
Tech With Tim
Youtube Blogger

hello everybody and welcome to a brand new tutorial series on this channel where im going to be covering the c plus language for beginners so this tutorial series is fully suited for anyone who has never used c plus plus before never written any code in their life before that said it obviously will be easier to follow along if you programmed before but i will make sure i explain everything and if youve never programmed before this is the place to be dont worry i will teach you everything you need to know theres going to be no prerequisite information or any tech literacy you really need to know before going through a series like this with that said though this series is titled learn c plus with me or you probably saw that in the thumbnail at least now the idea behind this series which is a little bit different than what i usually do is i am not extremely familiar with c plus plus in fact i just started learning c plus plus the other day and i thought it would be an interesting idea to kind of have a tutorial series where i am learning alongside you so right before i film the video im going to mess around with whatever it is that im going to be teaching and then immediately hop into the video and start teaching it personally i find the best way to learn is to teach and i just thought this would be kind of a cool series so again just want to be super clear im non-expert at c plus im gonna do all of the necessary research beforehand to make sure im not giving you guys incorrect information but i love explaining stuff to people i love learning a 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 whos 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 said in this first video here im going to discuss how you can compile and run a c plus plus program im going to show you how to set up your coding environment whether youre on mac linux or windows and then in future videos well actually get into the code and i will go all the way up until object oriented programming in c plus so with that said lets dive in [Music] so before i actually get into the install and setup steps i figured it wouldnt 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 its not that hard to learn especially if you havent learned another programming language before and its really not that difficult its just different than some other languages like python or javascript so dont be intimidated by that if youve 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 youre not going to regret learning c plus plus as your first or maybe even second programming language now with that said c plus plus is a very popular programming language its used by tens of millions of developers its probably in the top five maybe in maybe even in the top three of the most popular programming languages out there its been around for a really long time its used for a ton of different applications its 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 its very efficient and performance optimized if youre 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 its a great language youre definitely going to enjoy it learning it and its super versatile you can do a ton of stuff with it so with that said lets now dive in to the install setup steps so what im 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 i dont know what its called on windows like or not windows or mac whatever their default little text editor is you can write it in that you dont need like a dedicated code editor to write c 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 youre writing code youre going to want to install a text editor or a ide so like something like visual studio code if you dont know what that is dont worry about it in this tutorial series im 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 youre someone whos just starting out and you dont kind of know about any other text editors or ide just go with the sublime text editor its 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 now if youre on windows your install steps are going to be a little bit different than if youre on mac so if youre 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 youre on mac your setup is done you actually already have a c plus compiler installed on your computer ill show you how to access that in a minute if youre on linux same thing you already have a compiler in your system however if youre on windows you do not so you need to install one so theres 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 installer 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 installed 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 on our disk or i guess on our drive so go ahead run through this install again link in the description once its installed you need to make sure you remember where you installed okay so now that you have this installed assuming youre on windows again if youre on mac you can kind of fast forward a bit until this is done what you want to do is you want to find this program from your file explorer so open up your file explorer go to your drive im going to go to this pc im 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 gws ming gw64 now go inside of all these folders that im showing you so go inside of this one go inside of the ming gw folder then go inside of bin so again this is specific if youre on windows now what you want to do is you want to get the path to this bin notice here theres all kind of executable files here these are actually a bunch of different interpreters that you can use for other programming languages or sorry not interpreters compilers so you can see g plus plus this is one that were actually going to use for this video right here again you dont 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 compiler from our command line youll see how this works in a second but what youre going to type here in 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 tab that looks like this again you could type in edit the system environment variables open this up what youre going to see is you have all of these different tabs go to the advanced tab go to environment variables and then you should see something that says path now when youre on path what youre going to do is youre going to press edit youre probably going to 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 thats the path we just copied so were going to press new and then were just going to paste in that path that we have you can see i already have mine here so im not actually going to do this but just go ahead and paste it in so let me just remove that because i dont need it twice so once that is in there youre going to press ok and then youre going to press ok again and then if apply is able to be pressed press apply it probably wont be then you just press ok now what youve done is youve added the path to that install location so 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 get some type of output even if it says fatal error thats totally fine that is good if you see something that says g plus plus is not a recognized command 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 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 were 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 now again that just should work if youre on macro linux i dont know why that wouldnt work that should work for you and then were kind of ready to go so now this is a good point for me to discuss what i mean by compiler because i keep saying this word compiler youve probably heard me say interpreter 2 a compiler kind of takes the source code that we write and it transcribes it or translates it into a binary executable file so by default whenever you write c plus code you cant just run that code that is just you know plain text on your computer the computer doesnt understand that so what the compiler does is it takes this code it translates it into machine language or into a binary language that a uh that your computer can actually understand and that it can run so whats going to happen is every time we write our code whenever we want to run it were going to compile the code translate it into this new file and then this new file will have our program in a binary format we will then run that program and there we go well run that file that will 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 wouldnt have to do something like this okay so hopefully that makes sense uh that is what i mean by compiler so now what were going to do is were going to write our very first c plus program so youre going to open up subline text on whatever editor or whatever operating system youre using or if youre not using subline text open up your editor and create a new file now this new file you create must have a dot cpp file extension cpp stands for c plus plus so whenever you write your c plus code youre going to put it in a dot cpp file now if youre working in a text editor like sublime text as soon as you start typing youll notice your text is going to be highlighted in different colors that kind of tells you that youre getting syntax highlighting in that this editor recognizes that the code that youre writing is c plus plus code so we will continue the tutorial in one second but i need to quickly thank the sponsor of this video and this series which is algo expert algo expert is the best platform to use for preparing for your software engineering coding interviews they have over 140 coding interview questions on the platform which are just the highest quality resources you can use to prepare for your technical coding interviews so with that said get started using algo expert today by clicking the link in the description and using the code tech with tim for a discount on the platform so what were going to do is write a really standard hello world program im not really going to explain what im writing just write it out here this is just the test that our environment is set up so what were going to do is the following were going to say include and then inside of angle brackets were going to say io stream like that then underneath this line what were going to type is using namespace std you might get this autocomplete if youre working in in sublime text and then were going to put a semicolon at the end of this line a semicolon 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 theyre saying were 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 uh c plus plus library so just like pieces of syntax again you dont have to know what that means just trying to give you a bit of information now were going to create a function were going to say int main were going to put two parentheses like this and then were gonna put two curly braces and inside of here were gonna write the following line were gonna write c out were gonna write these two things right here i think these are less than signs although i always mix up if theyre less than or greater than youre gonna write two of these here and then youre gonna put inside of quotation marks make sure theyre double quotation marks hello world exclamation point semicolon and then on the next line youre going to say return 0. 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 c plus plus program all this is going to do is print this hello world so when we run this code all were going to see is that a little bit of textures of the console that says hello so go ahead and save that control s and then what you want to do is you want to compile this file so to compile this file you need to open up your terminal or your command prompt depending on the operating system youre on and you need to navigate to the directory youre storing this file in so ive created a folder on my desktop its called c plus plus tutorial inside i have my file tutorial1.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 youre on windows here is if you just go to the directory from your file explorer like you normally would so lets 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 cmd this will open up the command prompt in this location so notice the location is where we just where and then youre good to go so if youre on mac youre probably going to be in some directory that is not your desktop youre probably going to be in your users home directory which means you should have something like users and then whatever your name is or something like that so what youre going to do is youre going to navigate to wherever youre storing this file so if its on your desktop for example youre going to type cd desktop and then thats going to bring you to your desktop so lets actually just go ahead and do this ignore the two lines i just typed lets say im on mac and im in this this area i type cd desktop i go to my desktop notice now the path changes the desktop then im going to type cd and im going to go to whatever directory this file is contained in so maybe you have a folder called c plus tutorial 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 it auto fills c 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 dot this will bring you back to the parent directory so well move you back one notice here this moves me from c swiss tutorial back to desktop anyways i have this uh command prompt open in this directory now what im going to do is compile my c plus file to do this im going to type g plus this will work on both mac and windows im going to type hyphen o which i believe stands for out then im 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 im going to call this run or ill call this like run program or something call it whatever you want doesnt matter and then after this youre going to put the name of the file you want to compile in this case the name of my file is tutorial1 so whatever you named your file you put here and then you press enter so g plus plus hyphen o run program name of the file that contains the code you wrote dot cpp press enter youre gonna see that youre gonna get no output if you get no output that is good now what you can do is you can type this name right here that contains the kind of output file so im 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 youre on mac or linux type dot slash run program and that should work so either of them should probably work but on mac and linux im not sure because i havent done this 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 now just notice something here if i change this what im kind of printing out to the screen to just say hello and i save this file and then i go ahead and i try to run this code again so i do run program notice its still printing hello world the reason for this is i have not recompiled my code 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 youve already typed so since im 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 ill 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 well 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 arent getting frustrated and stuck at a ton of things youll probably notice with my tutorials im very verbose i go through everything and i just want to make sure im giving you as much information as i can so that this is the only resource you need to go to you dont need to go and look up a million things after the video because i didnt cover them so with that said i hope you guys enjoyed if you did make sure you leave a like subscribe to the channel i will see you in another youtube video

Tech With Tim: Learn C++ With Me #1 - How to Compile and Run C++ Code - Software Development