C++ in VS Code: Building your Code with CMake

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to our series on C++ development with Visual Studio code I'm Alexandra keer and today we're diving into the world of build systems in our last video we explored the C++ extension's new walkthrough for running and debugging your code in a single file if you're looking to get set up in vs code for C++ check out that video first what is a build system imagine you're working on a new C++ project it's just a single file you can still manually compile it's not a problem and you can just do this in the command line but as your project grows and you start having more and more files more and more dependencies you need to manage and targeting different platforms your project starts getting super convoluted and harder to manage this is where a build system comes in a build system is a set of tools and processes that can track how you want to compile and link your code you just need to describe your projects structure and dependencies and it will generate any files you need to build for you this saves you a lot of time and reduces the chance of making errors so examples of build systems include make ninja Ms build C which is a meta build system that actually lets you create any of these other build systems from the same file and C is a really popular choice in the C++ community and has first class support in vs code it's managed by kitware and it offers a lot of powerful features for managing large projects handling dependencies and supporting crossplatform development let's take a closer look at cake you can start off by installing C using the link kitb documentation in my case I'm on a Mac so I'm going to choose the most recent stable release for Mac if you're on a different operating system you just choose the right release candidate for you then you just click on the download now I have a little popup that I need to add C to my applications and I can now close this and start coding for the best experience using C mvs code you need to install the cic tools extension this extension makes it easier to manage cic projects the cic tools and C++ extension actually work hand inand CAC tools handles interfacing with the build process while the C++ extension provides intellisense and debuging nvs code just click on the extension icon in the activity bar from there just search for semac tools in the search bar select the extension and then click on install there we go now the machine has everything you need you have cic the C tools extension and from our previous video you have a basic C++ project the C++ extension a C++ compiler and a debugger now let's go back to the project that we originally created in our last video as a starting point we want to open our code which just printed out a basic string and in this case we're going to update the string so it's more relevant in our case since we're building C++ code using cic and we're going to save that so now it's going to print out congrats on building your C++ code using using cake now we need to actually configure cake and we need to tell cake about our project and how it's set up so that it knows how to build this process is only going to take Three Steps step one is that we need to create a cak list.txt file this is required in every cake project it is the central configuration file that informs seic on how to build such as the project configuration build targets and compiler options all you need to do is just create a new file in your project and then name it to list txt it's very important that you name this file exactly cak Lista txt so that cake is able to identify this file in your project I've just pasted what my cake project needs however each line depends on what you want the cmake build system to do for you so let's go through it line by line and some of these will probably also apply to your project first you need to specify the minimum cake version required by your project which in this case is 3.0.0 then you need to assign a project name which in my case is my cmake project this can be any name you want it to be I also specify the languages used in this project which is C and C++ the cxx just stands for Plus+ you can also optimally include C test which is a testing tool included in cmake enabling it which is what I do on line six here make sure that you can actually call the commands make test or C test to run tests for your project in the future but now comes the most important line which is to add an executable this is where where I just decide the type of executable which I want to generate through this project so in my case I want this to be an executable Target called my cmake project which matches my project name um and it will be based on the source file my CPP code. CPP finally I also added an extra line to set the property of my target to be the C++ language standard so that this information is passed over to the C++ extension and can be used for intellisense in my code there are a lot more customizations you can do to this file as your project grows so for example you can add more advanced testing scenarios or packaging dependencies using adore package and if you're looking to add SE to a more complex repo and need more information on how to edit your SE list. txt check out kitware documentation they have a lot of tutorials on how this process works now that we have a cake list txt step two is telling the build system what settings to use when building our project there are two different ways of doing this in vs code either using cake presets or kit some variants we recommend using cake presets to manage your cmake configurations so today we'll be using presets a presets file is just a Json file that lets your group settings together this is necessary because you often need different settings depending on the platform you're building for for example presets can include your generator if you want an executable for a Mac machine you will likely need a different generator than on windows with presets you can have a preset for Windows and one for Mac this makes it really easy to share configurations with other developers and to add to a continuous integration system now as you add more and more and more code to this project the process to build and run won't change you don't need to compile using longer and longer commands you can just build and run with two clicks if you have any questions on how to get set up with cmake or other C++ topics you want to learn more about feel free to leave a comment below or to contact us on GitHub to add the preset you just need to create a new Json file called C presets do Json I've already written my C presets Json so I can just paste this in and make sure that it saves but let's walk through this line by line so you know what a normal preset file looks like first you need to define a default preset which is what we're doing here then you will see a list of settings for that specific preset which includes a generator which since I'm on a Mac I'm using make files if you're on like a Windows machine this could be Ninja or another generator you also need to specify a binary directory which is the place where the cake will place all the executables into so in this case this is the build folder I have two different cache variables which set the build type to debug make sure you definitely have this line or have some sort of build type either debug or release or something else and I also set the export for compile commands to true now you don't need to do any extra work to make sure that the cake tools extension and C itself will be able to find the presets file in your project when you build so you do not need to reference C presets in cake lists or cake list and Cake presets specifically cake will actually search through your folder and identify these files based on their name because they're standard names now for the third step to set up cic on your machine you need to select a build Target this is the final product you want to receive after building in this case I want an executable file I can run so I choose my project executable first I need to reload my window since I just installed some new extensions so I'm going to use the command prompt to do that and just use the reload window command now you should be able to see the cake sidebar pop up on the left for you and the cake icon be available in the activity bar next I'm going to select my build Target so I should have now have the C command available to set my build Target and I want this build Target to be the same name as my project name so in my case it's my cmake project which is the name I chose in my cake list.txt now when I navigate over to my project status in the cmake side panel you can see that I am in the correct folder and I have my build set to my cake project and so now that the CAC tools extension is aware of how I want to build my project you can use CAC to generate all the necessary build files and this is where you can sit back and then CAC do all the heavy lifting for you you just need to select the configure button in the side panel and now you can find any of the generated build fils in the build folder of your project now you can use those fils to actually build using the build all projects button in the side panel this will create the final executable you need to run your code you can also use the project outline to select a specific Target to build for but this is optional once everything has been built to your specifications you can just run your code using the play button in the status bar there we see the output that we expected our program to produce so congratulations you just successfully use cic with the cic tools extension don't forget to like And subscribe until next time happy building
Info
Channel: Visual Studio Code
Views: 17,958
Rating: undefined out of 5
Keywords: vscode, visual studio code, vs code, CMake, cplusplus, demo
Id: _BWU5mWqVA4
Channel Id: undefined
Length: 9min 37sec (577 seconds)
Published: Fri Apr 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.