2024 C++ and CMake Setup in Visual Studio Code: A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome to this video in this one we are going to see a better way to set up your C++ projects and solve many of the problems we saw in the previous videos where we used Visual Studio code configuration files like tasks. Json file and launch. Json file we saw that those files were specific to visual studio code and you also had a chance to see that the compiler paths were hardwired in those files these are things you don't really want to do in a professional C++ project there are many ways to improve on that there are many tools on the market to do things better but in this video we are going to be focusing on cic because it is a very popular tool within the C++ Community chances are if you become a professional C++ developer you will use cake at some some point and I believe that many of the popular C++ projects at least 80% are using cake so it is a thing that is going to help you on your way as a C++ developer so we will use that CAC is a build system generator and if that doesn't make any sense to you don't worry about that I am going to show you how to get started with that it is a tool that is going to take over the job of interfacing to your compilers and all you have to do is write a script that c understands and C is going to use different compilers if you wanted to use msvc it is going to use that if you wanted to use GCC it is going to use that it can even use the clang compiler it is very versatile another good thing about CAC is that we can use it from within Visual Studio code so we don't have to leave our tool of choice there is an extension that we can install to have it integrated and work really well that's what I am going to show you in this video here to make sure you do what we do in this video please make sure to install the cmake extension in your Visual Studio code installation so open Vis Studio code go to the extensions Tab and make sure that you have cake cake if you search for this it is going to give you cake language support for Visual Studio code this is the extension I want you to install but also install C tools to have a good experience with us these are the two extensions that I recommend you install after you have these extensions installed make sure you have a c installed on your system I am using let's go inside of Visual Studio code to see the CAC version I am using I am probably using CAC 28 you can check the version you have by running c-- version and it is going to tell me that I have 328 you need a recent version I would recommend at least 325 but why not get the latest version to install cmake all you have to do is search for it so cake install and you will go to the CAC website they will tell you ways you can download and install it so if you go to download uh if you go down here they have binary distributions you can download for whatever operating system are on we have an option for Linux we have Windows and Mac so you can download it and install it if you don't know how to install it you can search and say your Linux distribution for example so for Linux so for BTU you can say install T 28 328 on UB and you will have instructions this is really easy to install for Windows you just have to download these binaries I think what I downloaded is 328 and the MSI installer and you double click on it it is going to install CAC on your system if I go on my system here it should be in the C program files and we have CAC somewhere we have cake and it is in the bin directory here you can see that we have a few binaries for it after it is installed I think it is going to automatically add itself to the environment variables so if I do environment variable abl and go on environment variables here and go to my path variable let's make sure you can see that I should have cake in here let's hunt for it where is Cake yes it is right here so this is why I can call it on the terminal make sure this is included in your environment variables now you have a cic installed and in previous videos I showed you how to install the compilers so if you don't have any any of these compilers installed please watch the first video on Windows or the second video on Linux I show you how to install these compilers and if you have cake and these compilers installed you have already requirements and we have also installed the C++ extension here these are the requirements once we have this requirements we will have to write a CX script for our project and I tried to make this script file as minimal as I could but these are the things you need to set up a basic C++ project using cmake the first thing you do cmake minimum required this is a command that is going to set up a minimum version for your cake project there is a project command that is going to specify the name of your project the version of your project and the languages that your project is using C supports multiple languages but in this case we are using it for C++ and we say C++ by cxx here the other thing we do we set the C++ standard that we want our project to use we want to use it 23 and the standard is set in this C variable this is a CCH variable here again don't worry about these things just do everything I do here and try to build the project like I build it and you will be picking up things as you go cake is not as hard as many people make it out to be it is complex but it is possible to just use a section of it to do what we want to do with C++ and that's the point of this video here if you don't know what these set commands do don't worry I will show you how we use them when we build this project here so stay with me and you will thank yourself for that in your career as a C++ developer do another set command which is going to make sure that this standard is required for our project to build and what this does is if you try to build this project with a compiler that that doesn't support this standard The Bu is going to fail and it is probably going to give you a message saying that your compiler is old it doesn't support the features that my application needs down here I want you to experience one of the benefits of using cic which is giving you the ability to bring in thirdparty dependences to Bringing third party dependencies you have to include the fetch content module just like we do here and in this case we want to bring in the fmt library that many C++ projects use to print things nicely before we even talk more about this Library I think we can go on our browser and search for it let's say C++ fmt and it is a library let's see the description so they say it they will probably say it better than I can it is an open source formatting Library profile in a fast and safe alternative to C++ streams and C stdio so it is a good Library I think modern C++ is going to build in features that this Library provides but at this time not all compilers support this so we have to use an external library for this and to use fetch content you say fetch content declare I am probably losing you here but don't worry these are things you have to use in your career as a C++ developer so we have two commands fet content declare and fet content make available and we say the library that we want to bring in where the library is going to be coming from you specify that through the G repository option here and you say the git tag that you want to bring in this case I am going to assume that you will be getting your dependencies from GitHub but it is possible to get them from other git repositories as well so if we go back to the G repository for fmt that we just looked at you can see it right here to get to the link you use you come to code and copy this link here this is what I did I copied that link and pasted that inside of my G repository option here and after that you specify a tag that you want to pull for your library let's come back to the git repository to get the tag you come to your git Repository and click on this button that says master or main click on this button and you see that we have an option to get the tags and your tags are going to be listed here let's go back and see the tag that we are using we are using 1011 it is right here so this is the tag I am using if you want you can use a new tag but you can also use what I am using here it's not very old so this is how you do things and now after you do do the fetch content declare thing you can do fetch content make available and pass the name that you specified in here the name here doesn't have to match what you specify in the git repository here it can be any name but it has at least to be relevant to your library that you are trying to pull with cake if you haven't used simic before you probably think this is difficult but please do what I am doing here you will appre appreciate the benefits of bringing in this Library once you know how to do this you will be using the same technique to pull other libraries for example if you want to pull in s FML to do some guies if you want to pull in libraries to do encryption and whatever this is really going to make your life easy so make sure you spend some time to use this and see it running after you see it running you can even read the documentation to kind of reverse engineer your way into C because it is a great tool so after we set up this third party library that we want to bring in we will set up an executable so you say add executable this is going to set up an executable Target so this is going to be the name of the executable it's going to be named rooster in this case you can name it whatever you want and this executable is going to be made up of our main.cpp file here how do we make our executable use the third party dependencies that we have broken you have to use the target link libraries command you specify the Target that you want to link to other libraries and you put in this private thing at this point I would like you to trust me so I'm not going to try and explain this because it may confuse you if you are a beginner but you specify the thing you want to link to in the library that you pulled in this case it is fmt colon colon fmt in SX speak this is called the target but don't worry about this once you see this working I will show you a way to know how to get this name here so this is our simx script and it will be using a standard main. C++ file which is going to be specified in here so I think I have talked a lot let's go to visual studio code and start using this thing but before we do that make sure you have the C++ extension installed make sure you have the CM extensions installed also make sure you have simic installed and one of the compilers that you will be using to build your project once you have these requirements in we are going to open Visual Studio code okay and open a folder it just needs to be an empty folder we will be putting in our main. CPP file and as usual let's go to our documentation let's see do we have the thing from CPP reference yes we have it here so we're going to grab the code that we would like to comp mile using C++ 20 or 23 let's put in the code and now we need to set up our CX script which is going to be taking over to build our project let's create it we're going to add a new file you need to name it C list.txt so let's do that you you need to add an S after name it exactly like I am doing it here and we need to pass in the cake minimum required command it needs to be the first thing and the version is going to be let's uh require 325 this is a good version we can use and we need to specify the project so let's name our project rooster and uh the languages that we need support and we want C++ and after that we want to set the cmake standard so let's say cmake cxx standard make sure you type everything I type here typing these things is going to help you understand them so we want 23 as the standard so let's say 23 and we want to make it a requirement so cake a cxx standard required and we need to make this on another thing we can do is is to add the executable so add executable okay it's going to be named rooster let's name it rooster with lowercase and it's going to be made up of the main CPP file now we need to bring in the fmt library the first thing you need to do is to include fetch content so let's include fetch content and after that we need to say f content decare we will specify the name of the library that we want to pull in this case it's going to be fmt and after that we will specify the git repository this is going to be coming from so repository and we will go back to the GitHub link and grab the code so we can copy this link here and bring it back into our project and we need to specify a tag so let's say get tag and I think we did 10110 let's go back and make sure so let's go back to the G repository and look at the tags that we have available and we have 10.1.1 I think this is what we used let's grab that and put that into our project so it's 10.1.1 and now we can go down and say fetch content make available I have a suggestions but I am not taking them here because I want to type this with you now once we type these lines fetch content declare and fetch content make available this is going to tell cake to download the fmt library from this link here and make it available for consumption in our project and we can tell our main CPP file to pull stuff from this Library by running the target link libraries command so we will say Target link libraries and specify the Target that we want to pull stuff from our third party dependency it's going to be rooster and we will put in this private keyword here and we specify fmt colon colon fmt and again I want you to trust me on this thing we do here by the way if you want to learn CAC from start to a good level I have a video series that I am going to link in the description below so you can watch to really learn more about all these things I am doing here but I just want you to start using it for C++ after you finish the video you can even copy this zic list. txt file and paste it in new projects that you create and do the things that we do here in this case you are just learning trust the process it is really going to be easy for you to use C++ if you invest in learning a bit about cmake you don't have to learn it all you can just learn the 20% that will allow you to do 80% of the things that you want to do you can think about it that way okay now that we have this ZX script in place we need to tell Visual Studio code to use it to build our project and there are many ways to do that inside of Visual Studio code but I am going to show you the easiest one in this video here and that is using kits the first thing you need to do you come to view and say command pallette and then choose cake select a kit or you can even scan for kits to see the kits that you have on your computer a kit is really a compiler and tools associated with that compiler to build your project if we scan for kits they are going to be scanned and then we can do view command pallet and then select kit if we do that you see that they detect a lot of compilers on my system here but what I am going to do is to choose for example Visual Studio and amd64 here let's try to select this and now you see that something went wrong with my project we couldn't find Fetch content because I have a typo here there so I should say fetch content and I said something else now let's see if this goes through and now I have another typle here fetch content make available we should say fetch cont content make available fetch content make available let's try to configure again view command pallette and select a kit and select the same kit I did earlier and now everything is going to work you see that they say that the build files have been generated to this build folder and now your project has being configured if you have typos you can check the gate repository for this video and make sure that you don't have any typos you can even copy this section of code and paste it in your C list.txt file once we have this we need to build our project but before we build it let's take a look in the build folder and see the project files that have been generated you see that we have generated a visual studio project but we don't have to open Visual Studio to build this project we can build it right from Visual Studio code now that we have configured tried our project we need to build it let's go back to command pallet and say cake build you see the option here you can even hit F7 but I don't like this shortcut because it's not easy to see on video so I am going to select cic bu and notice what is happening here this is going to build the project okay and it is going to say that we have built the rooster binary roster. exe this is our binary and this this project has linked against the fmt library you can see it here let's try to run it it has been generated in World debug as you can see here so we can hop over there on our terminal so let's do that we can CD build CD into the build folder and CD into debug and if we do LS you see we have a rooster binary that we can run if we run it it is going to say whatever is output by our application here which is using this structure but we can even try to use fmt if you go to the documentation of fmt you see that you to use it you may include fmt format. H and for example you can say fmt print the sky is blue I'm not going to try to do anything fancy with it but you can do a lot with fmt in this case I want want to use it as a proof that you have pulled a third party dependency and that you are using it in your CCH project something that is not trivial in C++ but CAC is really making this easy for us to do let's try to build the project again we can hit F7 if we want because you already know that so the build is good you see that we have can use fmt print if we run the application you can even run the application through this run button here if you do that it is going to run the application you see that it says the sky is blue on the same line if we want we can put a new line of character here so let's say back sln and put the needed semicolon and try to run again okay it's going to run and it is going to say the sky is blue let's see that we can debug and to debug you just put a break point somewhere let's put that on line 20 for example and hit this debug button in the status bar if we hit it you see that our binary is built is plugged into the debugger and we can step through things for example but before we step through stuff you can hover over things to see what they are made of this is really cool another thing you can do is to hit the button here to step through stuff and you can even look on the left here to see the local variables if you go here let's step over this and look at the local variables you see we have our points here and we can really do things with this and now you can build run and debug an application using C using cake another thing I want you to see is how you can add new files to this so if we want to add a new file you can add a person file a header file and you add a person CPP file and I am going going to grab the code from my repository because I don't want to uh I don't want to type these things I can steal these files from my previous projects that I did so I am going to copy the header file here so let's copy the code for our person class and put that inside of Visual Studio code here we are going to steal the code for the CPP file and put that in place and let's go to CPP i s swap to these things so let's grab the code for the CPP file and put that in the CPP file I can select this take this out and put that here let's go back to the header sorry for this but this happens when you are working on a projects so copy and go to the header file and put that in place now if we want this to be picked up by our project we need to go back in the cake list. txd file and add this to our executable command we can say person. CPP we don't need to put in person. h because person. H is included by person. CPP but if you want you can put it there I see some people do that it's not going to hurt so for example you can do person. H and this is going to also work I think this is going to work you see that the build files have been generated every time you modify the C list.txt file Visual Studio code is going to pick that up and try to reconfigure your project and now you can run the application uh I think we can stop debugging before we attempt to run it so let's try to run it it should run and give us output here but now we can create a person object so let's go to the main CPP file and show you that you can do that so we can uh say person or we need to include person first so include person. H and then we can create a person object p and p e for example and this is going to do what we want to do again we need to build the project and run it but you can just hit the Run button here which is going to build the project and run it in one go you see it is saying I am eating here you can really do all kinds of crazy things the good thing about cake is that you can easily also switch compilers so for example if we want to build this project using the GCC compiler that I have installed on my Windows machine all I have to do is to find the location of the build directory here reveal in explore I am going to close Visual Studio code because it is using it and I'm going to Nuke this build folder and open Visual Studio code again so the build folder is gone but what I can do now is to view command pette and select the kit again now I am going to make sure to choose the GCC compiler that is installed in my C minw directory so it is GCC 13 here I am going to select it and CAC is going to configure my project to use that compiler to build my C++ code you can see that it is using G 1320 it is in this location on my system it is going to download the dependencies so you can see that my bu files have been generated in this folder if I go in the build folder you see I have a deps folder an underscore deps folder if I go in you see that I have fmt downloaded and built and my project is going to link against that but now I am using the GCC compiler hopefully you can see how powerful cmake is we can still do the things we did earlier for example we can hit the Run button to run the application but before we run it we need to build it so let's hit the build button here this is going to try and build the project using the GCC compiler on Windows and the build is finished with the exit Code Zero which means that the build was successful now we can try to run the project I don't know why it's not running so let's go to terminal and go into the build directory and uh we have rooster so it is in here it has been built successfully let's see if we can debug it we can hit the debug button here and it is not working for whatever reason let's try to close Visual Studio code and open it again and if we try to run now it's going to run the thing okay it's going to build the project and run it now it is running if we try to debug we will debug the project and hit the breakpoint in a minute you see we have hit the breakpoint and we can do all these things that we do with our debuggers but now notice that we are using the g++ compiler you can even use the clang compiler but it gave me problems when I tried debugging so let's try to do that because we can do that with seic again I am going to close the project and delete this build folder and open Visual Studio again Visual Studio code I mean and we will start doing our thing again for example view command pallet and select a kit we want to select a kit to build with now we are going to be peing the clang compiler that is installed in our MW directory and we are going to hunt for that GCC clang 16 we have a clang 16 installed in our C minw folder I am going to pick this and again then it is going to attempt to configure my project using that compiler you see that we are using Clank 16 here the build files have been generated now we can build by clicking on this build button here and it is going to build the project the build is finished with exit Code Zero which is code for Success now we can run the application so it is going to run without a problem but if we try to debug it let's hit the debug button here it is not going to be working and I didn't have enough steam to research into why this is not working but if you want you can just use msvc and GCC that's going to be enough if I have more time I will try to research on why the clang compiler is not working here but now you get the gist of it it is really good to use cmech to set up your things another thing that is good about CCH is that this produ is going to be more or less understandable by other idees if I take this project for example and open it in Visual Studio it is going to understand it and be able to build it because most IDE understand the cake language and they can process this cic list. txd file I think I have this project on Linux so I am going to open it there let's open a remote connection to my Linux box and I am going to open a project that looks more or less like this and show you that we can really do these things here so let's open vs code CPP here and say cic with Kats this is the folder we are going to open you see we have a C list.txt file that looks more or less like what we did here it is going to be pulling fmt and using it but uh the main CPP file is not going to be looking like what we had before but this is not a problem we can even uh copy the code and bring that in here so if we have the code here copy put that in front of everything oh no we don't do that what do we do we can uh grab things in sections for example we can grab stuff from the main CPP file we can do all these things so let's do that we put the code here in front to be us used and we grab the Declaration of the structure so we put that in front of the main CPP file this is something we can do okay and we include the files that are needed we have a compare IO stream and set let's put those in place in front of fmt now we can do the things we did on Windows for example let's make sure we are using C++ 23 we are now we can view command pallet and select a kit and it is going to show me compilers installed on this system I will be picking GCC again so let's choose our GCC version which is GCC 11 you see it is in user Bean g++ it is showing the compilers on my Windows machine because this is using Windows subsystem for for Linux but don't use these use the Linux compilers user b& g++ this is what we are going to choose and it is going to configure the project again it is going to pull the dependency which is fmt and Builder we will wait for the dependency to be built you see build files have been generated to this folder if you go in uh the build folder you see that we have the files in here and we can build the project by hitting build so what we need to do is to hit the build button here and build finished and build finished with exit Code Zero this is good we can hit the Run button here and it is going to run and everything is really working fine if you want to get to the stus bar that I have here you have just to put a settings file in your dot do vs code folder and put in this setting that you want the status bar to be visible this is what you need to to do again you can get this code from my GitHub repository this is really all I had to share in this video showing you how you can use cic to build your C++ projects from within Visual Studio code I showed you that this works both on Windows and Linux and we don't have to mess with Json configuration files like we did in a few previous videos but again this is not the best way there are other ways and I will show you how to use preset files in a next video and in another video I will show you how to go crazy and use C++ modules which is really the future with C++ but for now I hope you enjoyed the video if you did please make sure to like share and subscribe leave a comment to let me know what you think if you want to learn about cic I have an extensive cic series make sure to look in the description below and you will find a link to watch the entire series and let me know what you think guys I am going to stop here and I will see you next time
Info
Channel: LearnQtGuide
Views: 5,620
Rating: undefined out of 5
Keywords: c++, cplusplus, cmake, visual studio code, vs code, kits
Id: 4U-lnfxY2U0
Channel Id: undefined
Length: 35min 0sec (2100 seconds)
Published: Sat Jan 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.