CMake Tutorial EP 1 | Understanding The Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to a video on code tekken tutorials today we're going to talk about our lord and savior see make what you think i was going to say if you're a fan of this channel you might know that i talk about c plus and libraries and kind of project setups and been trying to find the best practice for that for years well ever since i started programming and i recently discovered that they're just flat out it isn't any there's nothing there's no standard there's no best practice there's a best practice for a certain person or institution it's a thing that they usually do but it doesn't really correlate with necessarily the best way for you to do it so you can't go online and just read about oh yeah here's what everybody does it's uh here's what some people do here's what other people do here's what other people do and now here i am saying hey look i've been doing this for a long time and uh i think this is my favorite way to do it it's a little confusing at first but once you get used to it it's pretty good so i learned back in the day with make files so if you don't know what a make file is it is just a file called a make file and it runs commands so at the top it'll have like a default and it'll say something that runs in bash like uh usually it'll be like g plus plus um your main.cpp and the out is whatever you want it to be make files are what cmake turns into basically cmake makes your make file which sounds confusing and weird to say but that's basically how it goes down so i'm saving this file i guess i'm gonna real quick i'm just gonna go into my projects and make a new folder i'm just gonna make a new folder for this there's a new folder button that's right here sure i could go into command line and do make dir and all that stuff but it's right here so we're just gonna call this uh our lord and savior now anyone who searches my computer sees my folders is gonna think i'm some kind of fundamental christian but you know whatever so we're going to name this file oops let's do a search now make file and you can do it with a capital m too that's fine so we save it all right so now that we have the make file the makefile runs which is the make command so i open my terminal here and we're gonna go cd into my projects and then our lords and savior that's gonna be funny to say maybe i'll actually put this repo up just to troll github a little bit that'll be that'd be fun all right so let's see what we got in here with a little bit of look and we just got to make files so if we type make well it's uh it's a little confused it says missing separator that's because make fire make files do not accept spaces and as you can see i got spaces here so what i actually need is this to be a tab so i just highlighted them all and press tab and there it tells me there's a tab that's just a feature of this editor and a new line there all right so now that we we save that we go back into our command line and we type make and it tries to do the thing of course it doesn't see the make doesn't or doesn't see the main so it's just like i don't i don't have that but if for example i just made one no hey i got it i got it i got what we're gonna do hey zeus yeah get it get it all right and of course we'll name this main.cpp we should obviously return a zero or something yeah i don't i don't care about syntax it's just an example of how to basically use a make file and why do you need to know this why am i showing this in a tutorial about cmake because this is like the entire thing that cmake does so if you don't know this it's just like it's like missing your roots when you're a tree so you have to understand the very basics of a make file is all it does is is just run whatever command you tell it to and it's really really a linux thing so windows has their own way of handling cmake for their builds for visual studio or whatever compiler you're using and you can you can get linux support on windows too so this can be exactly relevant to windows just depends on how you're building i can do a video on the windows version of this if you want i just started up this one because it was easy all right so we basically get our out and we can run her out of course dot slash and says hey zeus all right so that's make files got a list of things over here right here yeah if you think of anything else that should go in the next video let me know so i'm going to basically tell you how to get your project started i'm going to talk about levels and projects mainly and we'll see if i have time for variables and lives but that might have to be next video so i'm going to continue this and try to cover everything in cmake with this one so cmake relies on a top level file called see make lists dot txt it has to be exactly like that has to be capital c capital m capital l and list plural and dot txt has to be exactly that so i'm just going to use the linux command touch to create this file right now and now if i look at my directory i'll see that it exists right here along with my other files that i made earlier i'm just going to remove the other files because i don't need them make file and out goodbye all right so now that we have our cmake file we can run cmake and basically to run cmake you need to tell it where your source is where your build folder is and you can make your build folder where sources but please don't but just just know some projects do that and i think it annoys some people but once again like i said at the beginning of this video there is no standard for this either and there's some standard for the install we'll get to that way later about how to install with cmake because cmake can do everything everything you can think of that you want to do for your project it can do you want a nice little executable for windows that has the install that you used to see in for every other program cmake can do that that's kind of the end product so we'll get to that probably at the end but it's cool and it can do a lot of other things along the way that your project wants like run all the tests for your project and things like that so we'll step by into that way later for now let's stick with the very roots i just i just want to really sell this cmake thing because i think it is like the only way to go and if you're doing it some other way please just just do let's all do this okay this is this seems better and also don't take my word for do your own research yada yada don't think that i'm like 100 sold on anything because you know i like to consider everything so basically you type c make to call it command and of course it tells us the usage because we didn't give any commands you definitely need to at least do this bottom one you need to path to where you want to build it and a path to your source and it considers its current path from where you run it so what most people do is they make a directory called build and that's where they build everything i'm going to remove that real quick uh okay i'm going to do a recursive force build okay so i'm going to move that real quick because i do it slightly different i call the folder out and then build oh yeah i got to put a dash p in front of that for it to build all of those since there are multiple directories and then what you do is you go into your out folder and then build folders so you basically just go to the folder that you want to build in well you don't have to because as you saw up here you could specify the path to it so it needs to know where your source is so you go cmake dash s backer directory back another directory and that that's where the root level cmakelist.txt is actually that's the important thing it needs to know where that root one is where to start and we need to know our build path our build path is where we currently already are so we just put a dot for that so there we go that'll say the root cmakelist.txt is two directories back and we want to build here and this this is really the main thing this is the one thing you need to know if you know anything at all is this right here the source and where to build all the special options come later if you need them and there it goes and it's checking that file and it generates and spits it out so what we'll see in our build we'll see our cmic files our cma cache cmake install and a make file you don't touch any of these ever because they're generated with cmake instead you just edit your cmakelist.txt so let's just look at that make file so i'm just going to do a cat make file and here it is start from the top see it has all sorts of stuff default target all phony default target and as you can see it's got this command like this like i was doing with that makefile because this is just a make file it just builds it all out best based on your cmakelist.txt the only thing it knows about really is where our source store is because we told it and where the binary dir is dirt directory i'm just getting used to saying it that way and there's all our stuff but as you can see it doesn't actually do any building of any program there's no gcc there's no call to anything it's basically just got some root set up with with that actually doesn't do much but even so i guess the point of that is even a blank cmakelist.txt will function but let's go actually make rcmake list.text do its main thing so i'm going to open up visual studio code here on this directory and let's take a quick look we got the cmakelist.txt right here so at the top you make cmake minimum required this is the minimum version of oh version here of cmake that you require and we'll just put like 3.10.2 and you can always check by going in here and doing cmake i think it's just dash dash version and yeah i want 3.13.4 so a lot of people say you should use whatever you're currently using as latest version but if you know you're doing stuff that's backwards compatible you can make this older this doesn't matter a whole lot you just have to have something here basically make it some version that you know will work so that's one thing that is required here we're going to do a little different this time this time when we run our cmake we're going to run it with the source right where we are and the build down a couple directories like so so you can do it either way it doesn't matter where you run cmake from as long as you tell where the root cmake file is and you tell where the build is you're good and there it goes it generates it there's nothing really new here although then it's going to now probably in there do some kind of check for that in the cache or something so still doesn't do much so generally what you do on the top level one is you do add project call this uh our lord and savior oh lass it'll be olas and it crashes what is this oh it's not i'm sorry it's i don't think this is ad project i think it's just projects yeah there we go it's just project okay and the next thing we need to do if we wanted to actually build something is we say i think this is add executable and we'll give the executable name um hope not no less i think this is just project name we'll see i'll get an error if not but it should translate into this basically and i could even expand it out and say something else but it needs the source next here and the source is main.cpp so we want to build it into an object called whatever this project name is let's see if that's correct yep looks good so now when we run make which we can't because we're not in that directory so we got to go to the out and then into the build and then once we're in here we'll notice that's where the make file is we could look at the make file again just to see what it has now and we'll now see that it has some stuff in here about that cpp file that we put as the executable okay well let's let's just try running this makefile and see what happens built target okay so now let's look again see what we got yeah the project name was correct so yeah that is a correct thing to get your project name anytime you want to reference it so you'll see this little operator all over the place and it basically just references a variable cmake saves a lot of variables automatically like your project name basically any of these things you set it's probably saved somewhere and you can look up the documentation for for more about those and i'll mention them as i go you know the ones i use but basically you can do this for a lot of different things to reference names that way you don't have to keep typing the same note name over and over and if i change this name to something else then the project name throughout the rest automatically updates you know standard with variable type stuff for programming so cmake allows some of that special stuff another cool thing about it as a build system okay well we've got a basic project set up it does run let me get this back up here let's go ahead and run it with the dot slash and of course says the same thing that we already programmed earlier that's the very basics of getting cmake started with just you know something that executes and that's going to do for this video i will be covering more topics as we go i didn't even get the levels really all i got to got to was projects in the top level so really all i did was like top level so i also want to talk about levels and lives to talk a little bit about variables but i'll talk more about how to set your own because you can set your own variables and do do things with them i'll show you some examples that get into these things hopefully next video i'll try my best to make it concise this is a lot to explain and there's so many different directions to go that's actually quite hard to make it concise that's one of the things i struggle with on this channel and coding is the many many many different directions i could go and trying to decide which is the best one to actually like show because don't forget to hit like to help support me and anything else you want to do is much appreciated i'll see you in the next video peace out goodbye see you next time on oh god code wrecked and tutorials uh see make you know what you came here for you want you want to learn what's the best how do i stomp the competition with my build system
Info
Channel: Code, Tech, and Tutorials
Views: 76,182
Rating: 4.8227444 out of 5
Keywords: coding, programming, c++, learning, howto, tutorial, language, english, what is coding, how to learn coding, learn coding, cmake how to, cmake tutorial c++ linux, cmake tutorial linux
Id: nlKcXPUJGwA
Channel Id: undefined
Length: 15min 36sec (936 seconds)
Published: Sun Jul 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.