Better CMake Part 1 -- Basic Project Setup and Usage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone my name is jeff and welcome back to my channel you just gotta create a branch and get on with it today we're gonna start a series on everyone's favorite c plus plus topic cmake the goal with this series is to provide tips and tricks so you and i can be more effective with cmake in our projects now is cmake perfect absolutely not however a solid foundation in cmake fundamentals can easily turn cmake into a tool that works for you and not against you i think most people get frustrated at tools when they sink unforeseen amounts of time into getting something done that should be easy or in debugging someone else's poor code my hope is that watching this series will help you navigate your own usage of cmake and when you come across cmake in the wild make good decisions about how to improve it now in this video we're going to take a look at very minimal examples so we can talk through how cmake likes to structure things when building cnc plus plus code my goal is that the next few videos will focus on cmake fundamentals then we'll build on in future videos when we do more sophisticated things like super builds and custom commands so enough with the intro already let's go look at some code so what i've got set up here as an example is a little hello world program uh we can actually just have a look at it it's all it is it's a good old printf hello world so it's just placeholder c plus plus because we're going to talk about the build system not talk about c plus plus so you have a little program um it's pretty simple the next question is how do i how do i build it there's the command line there's like like manually invoking gcc or your compiler if you're on windows create a visual studio project um you could use something like make or ninja and there's just so many options but cmake gives us the ability to do pretty much all of the above without having to only program for one because cmake if you don't know generates build system files so you write your cmake once then it'll create build system scripts to be invoked on your native platform depending on how you configured cma so the first thing we got to do is create a cmakelist.txt but to streamline things i already have this open vs code so i'm going to go ahead and undo that and move over here we're going to create ourselves a cmake list dot text and there's basically three ingredients we need we need a c make required to say what version of cmake that we require for this cmake list to effectively work this is not necessarily saying you can't be on your local machine using a newer version of cmake and accidentally make an incompatibility but if a older version of cmake is run on the cmake list this line will say hey that's illegal i'm choosing 3.1 here at the time of recording as 3.17 i think is the latest release i highly recommend you go with as new as you can um but i'm just going to say 3.1 just for the sake of today because why not uh the next thing we're going to establish a project and i'm going to call this hello world that's the name of a project and i'm going to say we're going to use languages cxx now what this section here is technically optional but i think it's always good to say what languages you're going to use because that'll invalidate which compiler cmakes can go look look for um but hey we got a project and then our last thing is we're going to actually create our executable which is add executable we could just say hello world which that's fine we can add our main.cpp because we are this cmakelist is in the same directory with main.cpp so we can just say that right there this is it we now have ourselves a minimum c make list to use uh we'll talk through some of this in a bit but if i move over here um let's create a build directory and already some of you might be like a build directory what is that cmake um it it if if we go ahead and run cmake on the directory with the cmake list which is one up from from here then we take a look at what it generated it generated lots of things and so one great thing about cmake is you can completely separate your build from your source so you won't do things like accidentally commit build scripts that are non-portable especially like binary files when you go and actually compile this like i can just do that right here and all the stuff uh like like there that's the executable hello world just run it hey runs hello world the thing here is all of this stuff if i wanted to start with a fresh build i can just remove it and i don't have to worry about knocking out my source files that are up here now interestingly this is a common pattern with cmake is people create a build directory in their source tree i am a really big fan of having a completely separate build tree um that so if you see i have i have home home source and then home build that means this is really just a bunch of build directories i have so if i make one i usually like to make one for each project and go in there i can now do cmake on that source tree and build compile and they are if i could blow away all of my build directories and not have harmed a single source directory i think that's just a good habit to get into but um of course that's up to you it's your machine it's your setup let's make this let you do this so we've already blown through a couple of things there are build directories we have source directories at the end of the day this is what this is all you need so what are some cool things that i can show you with this one is uh different uis that cmake works with now if you already have a cma cache which means you've run cmake once you have a live build directory that's pointing to us a source directory you can now run cmake and cmake cc makes cmake uh not cake cma gui you can all you can run all of these tools um on the build directory itself there's no need to to go and provide the sorcerer like you can like there's nothing wrong with that but um cmake once you create a live build directory prefers that you stay with one source directory so you can't just swap that out you have to start from scratch which is fine that's expected because um swapping that out and keeping that state in between builds live but you're pointing to different source trees would be crazy um it's already hard enough to do that within one c plus project so anyway all this to say that there's two uis that c make um ships with that are really useful one cc make uh which it's entirely curses based so it's you can use it entirely from your terminal and you know you can configure and generate which is interesting because when you run to cmake manually the configure and generation phase are done both automatically so what what this is is if i were to go in and there are all these cache variables that we could you know turn on and off configure says we're going to run cmake to make sure everything is prepped and then generates can actually generate the build scripts that we're going to use to invoke um you know that that just know that that phase is different so then if we if we run cmake gui which is a acute version of the ui it's just all the same stuff but in a clickable mouse mouse clickable interface that has some nice things um you should check out if you use cmake gui um the grouped and advanced checks grouped will say if you have a bunch of cmake cache variables from your project that share a prefix grouped will will put them together so if i were to like if i were to head over to a more non-trivial project cma gui if i ungroup there's just stuff everywhere and if i hit group it's nice that i can see all the osprey options all the tvb options all the g test options et cetera and then advanced that'll be a topic that we talked about later you can uh you can put things advanced or non-advanced so i think it's really good to first see what is not advanced options and then advanced of course like your your developer of your project you or someone else that their code that they're making that you're building snow advanced probably means you really know what you're doing but again it's your cmake to decide what to do we can do more let's change generators so let's let's blow away this build uh and let's say um we're gonna run cmake but we're gonna use ninja what's really cool is now this one c make list now lets me run ninja to build and ninja is really fast by default on on linux and mac you get unix make files so just type make that's why probably in most readmes of projects it's just run cmake and mate because by default you get unix make files then on windows there's different visual studio generators i'm not a windows developer much so i set it up in my continuous integration for projects that i work on and then kind of forget about it because i tend to work on linux but at the end of the day it's the same thing you run cmake on a directory get all this stuff and then you build and there you go last tip um we're gonna do this again now let's say you're in a build script or you never want to think about which generator you chose when you first ran cmake i could say run ninja here but i want to be able to just have because right now cmake knows what build system you chose what build it it knows that ninja is the target so what scene it comes with is cmake dash dash build if you run that on your build directory it will invoke the underlying build system that you generated for in a generic way so you could say c make dash if we remove all this and run cmake on this now this is unix make files if i do cmake build now i get the equivalent of of make it's probably enough for today's video getting familiar with source directories and build directories a minimal cmake list as well as familiarity with some ui tools like cc make and cma gui would be a good start coming up next we're going to look at some basic cmake language topics such as syntax and strings and lists and variables and control flow let me know in the comments down below if these are things you're interested in to make sure that these videos are helping you with what you face in your cmake if you enjoyed this video please like and subscribe and until next time happy coding everyone
Info
Channel: Jefferson Amstutz
Views: 40,955
Rating: undefined out of 5
Keywords: CMake, C++, programming
Id: ffwB60oKr-w
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.