Hello World with CMake and Ninja | Build Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is nahyan and in this video i'm going to talk about cmake so before i introduce you to cmake let me let me just write a basic c program and let me show you how we're going to be compiling it so the main goal is to build a c program so i'm gonna write a c program here so i'm gonna call it main dot c and it's going to be very basic so it's going to be just a hello world program and i'm just gonna print hello world with a new line and uh i have to compile it so the thing is that the file is in this folder called ninja uh and what i can do is excuse me i have to have to go into this directory and what i can do is i can call the compiler and ask it to compile this uh c program for us and make an executable so the way i'm gonna do this is i'm going to be using clang by the way in mac os if i use gcc uh i can i can ask for the version and it's gonna show that it's apple clan apple actually uh masked gcc with clang so if you call gcc clang is gonna call eventually so that's what they did so we are going to be using clang but we're but the command is gcc it's misleading i know anyway so what i can do is i can call gcc and i can pass the main file main.c file and i can ask it to give me an output called i don't know main and if i run this we get an executable this executable is created after compiling this main.c file so i can just run the main.c file excuse me the main executable and it's gonna do as we expected you know that is printing hello world to the screen so let's say you have 10 or 20 c or c plus plus files and this comprises your these files comprise your program and this can get very tedious i mean you have to compile all the files and form an executable and maybe you can you can have dependencies like maybe libraries and um you can imagine that that this command which is very short right now can get very long and you don't want to worry about running this command in you know various environments because of issues with other operating systems so the thing is that you you have to automate it in some way so this is where a build system comes into action comes into the scene so one of the you know most amazing build systems is unix make and we have a newer one in them in the scene called ninja so what ninja is that it is it's a build system and it's very oh man i have to try build system for this uh and in is focused on speed and it's very small so let me let me show you how to compile this uh file using ninja so i'm gonna i actually went to the manual and i want to write our own ninja files so what this ninja file is is essentially going to hold a recipe for building our program so i'm going to just copy the base again on the syntax example and i'm going to edit it to our needs so as you can see they actually named it build.ninja and we're going to name it the same you know for our project so i'm just going to call it build.ninja and i'm going to paste it in and what and what this um whole recipe is going to do is it's going to take a fu.c uh file and it's gonna build 4.0 file so that's not exactly what we want we want a main executable we want an executable name main and we want to compile it from main.c and the command is actually gcc some flags well we don't need flags and and it's gonna uh create a create an object file so we don't actually want that we want gcc to create an executable so removing that flag essentially creates an executable so it's going to take an input the input is whatever we placed in the rule so just like a function this this cc takes main.c and this is exactly what the input is and the output is essentially what we want to output it as so we want to up output it as uh main just plain main and if i save this and and run ninja here by the way by the way you need to have ninja installed in your system to be able to run this is saying that there is a syntax error it's not expecting endo file here so let me create a new line and if i run this again you can see that it ran successfully and if i if i delete this um oh man if i delete this delete this executable and i run this again you can see that this executable is created by ninja and is working properly so in summary this build automation system is very useful when you have a complex project and even for simple projects you don't have to worry about all these commands because they are filled in for you you don't have to remember what you ran previously or what you run usually because this whole thing is going to be filled out for you ninja is gonna do it for you so that's the point we have a rule and it's called cc you can name your rule anything you want i'm gonna i'm just gonna keep it as cc here and the command is this gcc thing so the command that we ran manual is now gonna be handled by ninja and all we are asking ninja to do is build a main an executable name main and using the command cc and we're gonna pass main.c as an input and main is the output so this is exactly what it's gonna do and you know that teaching your ninja is beyond the scope of this video all i'm trying to do is show you how we're going we're we can be using various tools for doing the same thing that is compiling or building our project so and our project comprises a single executable so the problem with ninja and other build tools is that they're pretty specific so you have to write the specific commands and this can break down in other environments or other systems so as you can see we are using gcc here and um and this can this can be a problem because you may be using microsoft c compiler uh on windows or maybe clang on mac or maybe any other compiler that you want so if you want your program to be compiled by everyone on most environments um you want a build automation tool such as cmake and this is where cement comes into the scene cmake is actually at a pretty higher level than ninja or unix make it's because all you have to tell c mac to do is what you want and cmake is gonna generate the commands for you and with ninja you have to tell the commands so i'm gonna i'm gonna uh look into cmake so let's say i want to um use cmake to create an executable because cma can be used for doing a lot of things so i'm gonna go to examples i'm going to pretend that i don't know anything so i'm going to copy as you can see this uh program excuse me this example comprises a cmake file so i'm going to create this file so i'm assuming that cmake is working will work just like ninja in the sense that it holds all the instructions instructions in a file so i'm going to be copying this very basic example cmg file and paste it in and as you can see it is the minimum version required it's just like english and i'm going to call this project this is the name of the project i'm going to remove the command comments uh just to make it less cluttered so i'm going to call the project i don't know main because this program's executables name is main why not so there's no subdirectory here so what i want to do is i want to include i want to add an executable to this project so i'm gonna you know just by googling i went to the manuals page for adding an executable so what this executable add executable command does is that is gonna take the name of the executable that we want to create and these things are you know optional and the main thing that we need are the sources the sources which are going to be used for creating and for creating this executable so i'm going to take the ad executable by the way i'm using via an extension for c mac in vs code you can use a similar extension for your text editor so i'm gonna write you know call this command i'm gonna say that i want an executable called i don't know let's make make it different this time let's call it full and the source files by the way as you can guess these are comma zip excuse me space separated white space separated so there's no example here but um i think this is good enough i can google further for examples but i don't think we need it this time so i'm going to call this main.c because our file is main.c so all this is going to do is and this this is going to add an executable recipe to our project and this executable is going to be called full and it's going to be compiled from this main.c file and by the way you can be having multiple uh files multiple source files uh for creating a single executable but we have one in this case so what i'm going to do is i'm going to be doing this i'm going to be building our project using cmake so what cmake is going to do is cmake is going to be using cmake is going to be generating a build system file and the generator can be picked by us so we're going to be using ninja for this so let me show you so i'm going to be calling cmake and saying that all right so the current directory which is indicated by the dot holds the cmak file so what cmake is going to be doing is that it's going to go checking the current directory the directory can be anything you can be using relative or absolute uh paths and uh right now i'm using dock which indicates the current directory and all i'm saying is that the current directory holds the cmg file so it's going to be finding the cmg file and the build folder by the way it can be using cmake help and you're going to be getting all the information so so we're gonna be specifying a build directory excuse me so i'm gonna be specifying a build directory let's call it build and let me give you guys give it a space and uh in the help you can see that they actually provided a list of generators that are available in our platform so the default one is unique make files uh but i'm going to be using ninja so i'm going to be specifying the generator using the g flag as you can see from excuse me see from it's taking a while all right so here is the g flag and is for specifying a build system generator so what this is going to do is cmake is going to take the recipe which is in the cmg file and it's going to be building or generating build system files so in this case we're going to be specifying ninja and let's see what happens so as you can see there's a build file and it's saying that the build files have been written to the build uh directory because we specified the directory as build and what this is gonna do is that there are plenty of files but the main file uh is the build.ninja and is just like the build.ninja file that we created obviously is much longer and we shouldn't be editing editing this because it's explicitly written uh that we should not be editing this and all this is doing is that it's going to be taking the main.c file um if i can find it here it's going to be taking the main.c file and it's going to be compiling this as an executable and this is all generated all by cmake we don't have to worry about this at all so the gener build files are created but not our executables so to build our project using the generated build files we have to ask cmake to build this and we're going to be specifying the directory where the build files are so this the build files are in build directory this is getting confusing i guess but you can see what this will do in a while and it all is saying that we created it created an executable called full and in the build directory there there is an executable called full uh that that has been created right now so we can excuse me we can drag it in we can we can drag the executable in our terminal and we can hit enter to run this so as you can see this prints exactly what we wanted hello world and let's say we change our mind we want to create a library we want to compile this whole thing as a library not an executable so what we can do is you can guess at a library and and i can check the manual for this and library and let's see what kind of arguments this is gonna take so it's gonna take a name it's gonna take what it's gonna take the type of library that we want to create so it's going to be either static or shared or a module so i'm just gonna say that it's going to be a shared library so i'm gonna paste it in and then we can exclude from we don't have to worry about this and then we have to paste the sources and you can see the triple dot indicates that we can have many source files uh for a single library and this makes sense but we have only a single source file so let's do the same but this time i'm gonna run the commands together i mean in one in one line so um all that by the way ignore this this is just a warning we have to provide the ipad but right now it doesn't really matter uh so you can see that we have a leapful dot daily dynamically linked library which is exactly what we wanted we wanted a shared library which is also called a dll in windows and this is this this is exactly what we asked cmac to do so you can see that we don't have to worry about the commands for generating a shared or a static library or an executable or anything what we just did is what we wanted to create we just shared that we just told explicitly what we wanted and cmake actually managed everything so right as you can see now we have a live food on a a is actually uh an extension for static static libraries so we just create a static library this time and we could be we could be creating a module so let's see what this does so this is a shared object also called a module so as you can see we can do whatever we like by the way we can be having excuse me not and project we can be adding an executable called full from main.c and let's see uh the thing is that uh there is a problem because because uh there's another target with the same name and we can say go to just to prevent a conflict and you can see that a project can have an executable or many executables many libraries you name it you want it thank you for watching and i'll see you guys in the next video
Info
Channel: Use Your Illusion
Views: 4,062
Rating: undefined out of 5
Keywords:
Id: 4TwW0CgBh9s
Channel Id: undefined
Length: 16min 29sec (989 seconds)
Published: Fri Mar 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.