Modern C++ Course, Lecture 1: Build Systems (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome again to this new lecture about build system so without further ado let's get started with the presentation so today we're going to see build systems that is going to be the foundation that you will need is a basic technique that you will need in order to build your c plus plus program so this is one of the most important lectures if not the most important lectures we are going to give you during this course build systems are basically a tool that will allow you to build any cross-plus program and at the beginning it's a bit hard to get used to it but with some experience uh it just gets easier and easier over time so believe me stick with me it might be a little complicated but this is something you need to understand and it's a tool you need to use in order to build any crossbars program so build systems somehow rely on something i like to call the software development ecosystem that is basically all the tools related to software development so most of the times when we think about writing software what we like to think it's about the source code that is the program that you're you want to write that is basically the center of this picture but there's also a lot of other stuff related that is basically satellite tools to this software development or this source code so the one we're going to see today is basically build system that is a tool that will allow you to build your programs there's many other tools around software development like the text editor that is the one used to write your code some linters and can link through your code and tell you some errors you might have in the future the tool chain you're using depending on the target system for example which operating system you are using to develop your software packages for example the debugger that we might have a tutorial in the future about this the continuous iterations and continued development that is something that will allow you to test your software on a web service platform to to make sure that everything is working formatting tools and so on so basically these are some of the tools you might use when you're writing software it's not the most important foundation for writing software to know the tools but in order to write proper software you need to use the tools and one of those tools is speed systems and it's one of the most important because if you cannot build your c plus plus program then there is no chance you will get to run your c plus plus code so as we said on the last lecture c plus plus is a compile language and i will try to repeat this on every lecture but i might forget so apologize in advance and therefore we need to somehow build our program so let's jump directly into the compilation process so what is a compiler as we discussed in the last lecture it's basically another program so we say let's create this compiler this translator to translate our c plus plus code into something that the computer understands so basically the c plus the c plus plus compiler is in charge of transforming horrible c plus plus code into binary format so something that the computer can actually understand and execute binary code is basically this language so the computer and the sender is basically in forms of zeros and ones but it's basically in terms of high voltages and low voltages for those who knows what we're talking about this is basically a really simplified diagram of a compiler where basically we see here that we have our source code on the left and then we invoke the compiler or we call the compiler meaning that we will pass to the standard input of this other program that is a compiler the source code and this compiler will give us some binary code some binary format data that we can execute in our processor that this could be for example my laptop i'm using right now to record this lecture so so far on the hello world example we did this really simple compilation uh process that was basically invoking the c plus plus compiler and just specifying through the standard input which file you want to compile this will be the program and then it will give you an executable file that is called a dot out and it's ready to run so far no surprises but the answer sorry the question would be will be always that easy this somehow already sounds way to simplify and sadly the answer is no and then all all these top stuff you will need to do you are going to learn during this course and especially on the first homework assignment so this build system lecture is so important that there's one dedicated homework that goes within this lecture so after you finish this lecture you're ready to do this homework and this is actually the first homework and the first important lecture we will give you because if you don't know how to build a product and the rest is something you cannot even look into it so pay attention please and then make sure you learn and you understand how this works so every time you compile some program behind the scenes there's always four steps running the first one is something that we we got from t that is to pre-process uh the source file that is for example when we were talking about including some hero file then the pre-process will be in charge of doing this and then it will basically copy text copy paste text around files so this doesn't check anything in particular but it's basically pre-processing your programs then the second step is going to be to compile it this is somehow confusing or misleading because traditionally you say compiler program to everything but believe me that when i say compiler program i basically mean to build a program following these four steps and not just the compile but the compilation technically speaking the compilation process is going to take this c plus code that has been already pre-processed and then it's going to produce some assembly code that this depends strictly on the target processor you're using in my case it's going to be a 64 bits intel processor so this is going to be the assembly code that the compiler will generate after we have this assembly then we will also assembly or sometimes people also call this step compile this language into some object binary so now we're going to take all these assembly instructions and then we're going to turn this into a binary format data that the computer can understand and last but not least we're going to link this program with some for example with the standard libraries or with some third-party libraries in order to get the the binary the final executable so all these four steps we can depict a picture like the one in the here on the slide and then this is more or less all the four steps we're going to see so first we have the pre-process step so here and then the output of this pre-processed step is going to be the pre-process file and then if we pass this to the compiler then we get an assembly source file and then if we assembly this assembly code then we get the final binary file and then the fourth and last step that is going to be the linker that this could also invoke some other third part libraries will give me the executable so this sounds complicated but actually you don't need to do this manually or to understand this manually every time you build a program you only need to see it once and that's why we put the lecture on the first place and then once you get the intuition on how stuff is working then you can really move forward and start using for example better tools like c make make and so on so let's jump into this compilation process step by step but checking what's going on so for that let me bring my terminal and then let me clear the output and then i am here for example on this directory where i will build a simple example let's remove everything and now if i list the directory it's completely empty so let's start with a really simple hello world program so for that let's include the iostream header file and then let's create our main function and then let's call hello world and then no error so so far so good now let's build this example so this is how we did it on the first lecture so now if we leave the directory and then i execute this it's going to give me the same example as before but now let's really dive deep into all these four steps and then let's see what we can do so the first steps the first step sorry is going to be preprocess and this typically will output a file that is not strictly necessary but it's common to put a dot i extension so if i invoke the my siblings compiler like you see on the screen this is going to give you the pre-process file on the sender output so this is going to be on the output terminal but of course we want to redirect this to a file so we can inspect it a bit so let's do it and if you see we have a main.i file that we are going to open right away so if you see now this file is basically quite big and there's a lot of stuff i didn't write myself and this is basically everything that was copy pasted from the i stream her file so the preprocessor went for this file it copy all its content replace all the token strings and put it here on my file and if you pay even more attention if you go to the bottom of the file you will see my c plus pass program running here but also the include statement is not here anymore because it got replaced with all the the hero file that we just got from iostream so this is basically how it looks the main.i that is a pre-process file after the pre-processing step so this is so far you cannot use it you can not run it therefore we move to the next compilation step that is going to be the compilation so again technically speaking this is going to provide you with a simply code that you still cannot run but you could turn it into binary options so for this we need to pass the s flag here and then now we need to provide not the main.cpp file but the preprocess file because this is the one so this is the second step on the compilation so we need the output of the first step right so if we do this now we have a third file that's going to be the assembly so never do this but you can open it and then try to understand the assembly code so this is basically how it looks after the compilation process after so first sorry first after pre-presenting the file and then compiling it to assembly uh format so this is basically ready to be turned into binary for this particular processor and for this i will invoke the assembly so for that we need to hold this and then do assembly this sorry and if i check now the output i will have this main.o that is going to be a binary format object y if i try to open this file with my text editor of course i'm not going to see anything meaningful because it's all binary and makes sense of course then the last step but not least is basically linking and to invoke the linking which is called to the fourth and last step of the compilation process we say okay let's link this main.o object file and then by default we're going to link against the standard library of the of for example gcc that is a compiler we're using and for example here you will have the symbol to get through main and then to run your program successfully so if you check now so if you do this this will give you an output but let's put it into a file that we will call main and this is basically the last step and now we have our main executable that is basically the final problem so this was basically all the four steps done by hand do you want to do this every time you build a program not really so this is just an example to show you what's going on behind the scenes uh when you are using this and if you want to know how i got to know all these four steps you just do manual gcc and if you check here these are the three steps basically and then there is also a really nice description on how this works basically compilation cannonball up to four stages pre-processing compilation proper assembly and linking and here you have all the description how to use these four commands basically so if we recap basically if we want to compile our program even when we don't know it we're going to go through all these four steps first pre-processing then compiling then assembly to a binary and lastly linking of course when we call the c plus plus main.cpp all this is hidden from the user and then we get add the output here the final executable so now that you already know how to compile and you understand the four steps of the compilation process we can dive deep into building libraries again as a recommendation before you continue with the video i will recommend you to stop this video right now and try on your computer the four steps and play around with different type of uh file names change some stuff on the c plus plus co and to get some intuition on what's going on there so what is a library i like to think that the library is nothing but a bug or a collection of function implementations let's call it later in this lecture we're going to see more stuff like classes and so on but for now let's stick to functions or symbols so when i say a bug it's basically because inside this bag if you open it you will find different binary implementations so this means stuff that the your processor can execute and give you some output all connected together or all packed together into one but of course because we are working on linux and everything in linux is a file this library is going to be a file so the libraries is basically we can call this that is multiple object files that are logically connected but not necessarily so you could probably have a library uh with some unlockable connection with hindi's library and there's no problem with that and there's basically two type of libraries on the c plus wall or basically on any compiled language that one is going to be static library that they are typically faster to run but of course they would this takes a lot of space because they will become part of the finally the sorry the final binary and also we have dynamic libraries now we can say that they're somehow slower but this can be copied to the target file system and then reference later uh by the main program i will explain a bit more about this in a second so how to create a static clarity so this is one command we're going to see an example later and then remember the static libraries are just like archives so think this about like a bag of symbols and now we need to make a full stop here and then we are going to see this picture that it's very likely that you're going to see this again and again again on my lectures why because it's really important to this thing between the static world and the dynamic work on a ziplex plus program when i say static i'm basically thinking speaking and trying to reason about everything that is happening at compile time this is always like this so stick this to this concept and believe me you will get to understand a lot of stuff during the full course so static for me is basically have has the same meaning that compile time and the dynamic is basically whatever is happening at runtime so it's equivalent when i say dynamic to runtime so whatever is happening when i start so after i start running my program then i will call this something dynamic so if we go back to the previous slide and then now we jump back into the static and dynamic libraries so when we build a static library and then when we link our program with this static library this is going to happen at compile time so when i build my program when i hit make the static glory is going to be booked and it's going to be part of my final binary therefore we say that the it's not that we say it's something that is happening the binary file size is going to be bigger and this for you maybe not a problem but believe me if you are targeting mobile robots for example where you have infinite amounts of space on the file system this is not efficient especially if you have on your target system let's say 10 or 15 different applications that's using the same library if you statically link this library with your 15 applications all your 15 binaries is they they're going to hold a copy of the same library making each binary really big and maybe you cannot even put these files in your file system on a desktop computer on your laptop this might not be a big problem but believe me that for mobile robots this will turn into a problem on the other hand the dynamic library when we link a dynamic library with your program this basically means that it's something that is going to happen after we start executing the program so when we link we basically put a promise to the program we say okay there will be a reference on this file that must be located on the file system and when we start running the program when we start execution every time we need something from this larry we are going to go for this file open it and then take the implementation of whatever function you're using from there therefore you need to have the library on your file system when you're running the program not when you're compiling it right this is the main distinction of course if we go to the mobile robot example where we had 15 applications let's say now these 15 applications they're using the same library but they're using also the same file on your file system and this library it's not anymore part of the final binary and then we reference this library all these 15 applications every time each application needs something from the library so this is more or less the main distinction and again believe me i'm going to repeat this picture many many times because it's really important to reason about the static work and the dynamic world on c plus plus programs so how we can start building our own libraries so to do that we need to start talking about the declaration and definition of functions and then we're going to see functions on the next lecture but for now it's safe to assume that you all understand what is a function on a program and then also we are going to extend this declaration and definition when we start seeing classes on the future so a function declaration can be separated from its implementation detail so this is how it looks on c plus plus a function declaration so we say that it sets up an interface because it's basically going to show the input parameters of the function what is the return value the origin type of the function and whatsoever but so far we have no clue of what what this function is doing we know that is boy so it's basically not going to return anything the name of the function is called funct name in this example and also it's going to take one integer parameter but we have no clue so far what this is doing but this is basically the input output perspective the interface of this function this is the declaration and i also like to think this as a promise for the compiler so when you see a declaration of a function it's basically a promise for the compiler that someone will provide implementation for this function and the implementation or the definition basically holds all the source code the real meat of the of the function on on its body so basically here what we see is again the same prototype so basically the same interface of the function but here with the implementation details so we are basically printing some messages to the center output so nose not much fun so far but it's a simple example to to keep going so this is the main difference between declaration and definition so how we can start building letters with this so now we understand the distinction between what is the declaration of a function and its definition or its implementation so in order to libra libraries we will want to basically split the function declaration on header files and implementation is going to be on cdp files why is this because when we want to use something from our own libraries or from someone else we only need when we develop the code we only want to know the interface of the function so in this case if i want to let's say that we can safely ignore the implementation for some for a moment and then the only thing i want to know when i'm using this library is that prototype so which type you're returning which arguments do you take and that's why we want the hair file we only want the high level description of the function and this dot tools.http this is the hair file we're going to put the function declaration so there will be no source coding here and this is the only file we need when we pre-process and compile the program to say okay we include this tools hpb http sorry that is basically copy pasting the header file into this program.tpp and then we are going to call this function so basically this is the the function the call for the from the this library and then later we need to provide the implementation for this function and the link is going to be in charge of of getting this implementation when we build it so this is basically how we typically split the the definition or the implementation and the declaration so in this case i call these tools because let's pretend otherwise there was some tools in here and here we have the hair file and here the cpp file with the real source code and this is the main application of the target application that is going to invoke is going to use the functions from this library but then can we just build as before so we say c plus plus program cpp actually no and then we're going to get a link error but in order to show you this and then so you can believe me let's build an example on the fly so let me then open a terminal here and let's start baking this project from scratch so let's go to attempt directory and then let's create example project or let's let's call this my library and then let's cd into this library and then that's where we are right now and to start let's basically let me go back to the slide and let's um [Music] let's do these three functions sorry these three files let's create it and then let's put some implementation in there so sorry let's go back to the my terminal and then to create files let's just use the touch command hpp and then let's call it main for the program dot cpp so if i list here this is what i have so far and not too exciting so let's open an instance of this code here so we can put some code and then after we open this let's exit the terminal because i'm not going to use it anymore so okay so this is more or less my workspace let's put it simple let's open the header file here that is completely empty let's put the main application to the right and then let's put the sorry the cpp file over here so that's it so this is more how i like to have it right now so this is our workspace and let's build first the hero file then the cpp file and the main application so let's get started so let's say here that we want to have some function that is called grid and then takes notes are no arguments and then that's going to be basically the function declaration so this is a grid function that looks to basically greet someone or something and then takes no argument and basically it's a subroutine so it's not doing anything of course we can help the reader provide some comments and this is a function that will read you and then so far so good then on the implementation on the definition of the function let's include the tools dot app file and here we are going to provide the real implementation the real c-class plus code for this function so then let's repeat the prototype of the function but now we open the scope operator because we're going to put here implementation so in order to output something to the terminal we are going to use the iostream hero file and then here we just do see out and then i love build systems for example and this is just a function so let's put some comment here like say how much you love to build siblings programs right and then now there is a caveat so when we include this file we are going to copy paste whatever is on the hair file on this or file but if we jump to the main program we also need to include the tools.hpp file in order to use this function the grid function but the premise is when we compile a full program we're going to get twice this function declaration and this is basically a syntax error so in order to do so to fix this error that actually is a nice exercise you could try it and then you will see on which of the four steps that you should probably know in which one is going to fail uh this is going to give you an error in order to avoid this error we are going to specify the preprocessor to only include this file once so pragma once this basically tests a preprocessor the first time you see this file copy whatever it should should be and then stop there otherwise we will get twice it's like the same as having this two or three times here that this is going to give you a syntax error okay so far so good this is more or less and the basics you need in order to build this library now on the main program we are going to create a main function and then let's invoke this function and then this doesn't look to be the let's also return 0 here doesn't look to be the big thing but believe it's a lot because we have a library here on the left and basically we have a main program our main application calling a function from the library so maybe you want to already go ahead and try to build this example as before so let me list the work directory so we have our main application and the library files but if you do c plus plus vendor cpp you are going to have an error so that is the same or very similar to what i put on the slide what it's saying here is basically undefined reference to the grid function why is this because the linking process is the part that is failing so the fourth step of the full bill or the compilation process is failing and actually you can also so ld is basically the gcc linker this will also give you a hint or which step is failing so basically you cannot tell that this is not compiling so to say you could say that it's not building but actually the problem so it is compiling but it's not linking so if you go to the four steps the last step is the one that is missing so basically then the question is like what is linking so basically linking is when the library is a binary object that contains some compiled implementation of some method and linking will map function declaration to the compile implementation so basically when if we go to our source code so this function declaration needs to have a real reference to this implementation and if you actually pay attention we didn't compile this translation unit these two cpp so no one knows where is the implementation for this particular function so to use the library we need always a hero file but also we call it api that is basically where the function declaration is and also the compile library so now we need to compile the library so how to build a library so here is basically again i'm repeating the example i'm building on the bs code so basically we have our folder with the tools here file the cpp file and then in short we will separate the code into modules and then basically this is the full implementation so you can also stop the video and try it by yourself but now we are going to build this library by hand so this looks like a lot of commands and they are actually they are so there are four commands in order to build such a simple library that you you may think already this is an overkill and i agree with you but stick with me because the the more you understand these four steps the more you understand the linking process the compilation and assembly the more you you're going to be able to debug your build when it's not really working so the first thing we're going to do is to build a library so no one built the library and this is basically the first two steps over here so let me highlight this for you so these first two steps is basically going to give you the library of the this tools library so let's without further ado let's just do it so basically again empty sorry let me jump here empty directory and then let's basically do c plus plus and then the dash c flag is stands for compiling and then let's compile the tools.cpp and then basically this will give you and this binary object file is already pre-processed and compiled so pre-process assembly and compile version of the library tools that only inside here there is only one symbol there is the grid function so now we want to organize these models into a library and for that we are going to use the arc archive program that is i a r and then basically this is going to give you this lib tools library and then we're going to do this for this example as an static library so let's do it so basically if we do r and then we pass these arguments this is going to be the name of the library and it's really important you always start with the prefix slip otherwise this is not going to work and i'm going to show you why later and then here let's put all the object files that we need for this library so in this case we only have these tools.oh so it's only one model now let's pretend we have more and more so we can put it here all the objects files we want to sip we want to gather together in this bag that we call a library and if we list now now we see this leap tools that is basically the static larry we just filled with just one symbol that is the grid symbol now that we have the library we need to compile that is the time step for the we did for the library translation unit the tools.tpp we need to compile our main application this will give us another object file that is going to be related to the main application and lastly the fourth step of the compilation process we need to link everything together in order to do so we're going to call again our compiler and we're going to say please link this main.o object and then with the lib tools library and we're going to upload this to a main function so let's do this on the terminal right away so let me clear here and then let's call the compiler for the main cpp file and now we have this new object file here that is going to have the binary code for this and it's going to have the reference for the static library so we need to link it otherwise we're not going to be able to run it because that's what we had before an undefined reference to the grid function then if you go back to a slide you can stop the slider right down this command basically we're going to call the c plus plus compiler again i'm basically skipping the standard because we are not really using anything from the c class 17 but if you put this it's not going to make any difference and then this dot l with the capital letter is going to indicate the linker please look for libraries on this current directory and this dash l tools is basically telling the linker link against the leap to libraries and that's why we always prefix a layer with the leave and then lastly the o we already use it in the past is to get some output file that we're going to call it the main so basically let me check my cheat set again so main.o dash l basically please link with lyrics on this directory because the libraries it's on this directory but if you have it somewhere else on your system then this you will need to change and then let's link sorry link again the leap tools library and then let's output this into a main file that is called main and this is going to be the executable if you list now finally you have we have our main application and then let's clear the screen and then let's try to run this and i love build systems so as you recap look all the files that you have right now and look how simple this library looks even when it's simple it's actually really hard to build because there's a lot of steps involved in order to get there but at least we reach these headers and source separation we have on the left here our library code and on the right we have the the target application that is the main application that is a clean implementation of this library so that's what put us into the final stage to introduce you with build systems so building by hand it's actually really hard if you think about it so four commands to build such a simple hero war example with just two symbols or maybe one it's way too much of course if you think that you want to change for example this implementation then you need to build this translation unit then you need to recompile the library you need to archive this into a library then relink again and then you will get the new target application which is a lot of effort as you can already imagine so the question is like how does this scale to big projects so how we are going to deal with this when we have more than two files or a bigger library so this of course is impossible to maintain and therefore humans have created pill systems so business into rescue and then let's dive into this let's do an example and then you will have all the tools necessary to build your projects during this simple express course so you may be wondering what are these systems so they are basically tools and then you should not get lost with it so the concepts never change and that's why we provide you first with the intuition on what's going on behind the build process but the tools might change in the future so don't get lost with big build system because it's just a tool that might change the important thing is you need to understand what is the build system doing for you there's many of them nowadays of course because people have been doing this for years already and the main idea of a build system is to automate this build process we just did by hand of course somehow we really need to do this on an automated way so we can change something and really easy easily rebuild the project they began as shell script in the past of course you may wonder okay four commands maybe i can put some parameters write a bash script or a shell script and then i will have my build system of course this is doable but again as you might imagine it's really hard to maintain then they turn into something that is make files so make file is basically the first open source build system out there was created in the gnu project the same that created gcc but then they also turned into meta bit systems like cmake that is nowadays the most popular tool used out there so why we call it metabill system because uh some the tool like cmake is going to give you a build system that will allow you to build your project so accepted cmake is not a build system it's just a build system generator so it's not a real build system you need to use an actual build system like make or ninja in order to build your project so this is basically uh like a something i wish at this moment so let's pretend we never sausi make we don't know anything i will make this is what i would like to do basically so i'm dreaming at this point in time so basically i would love to replace this let's highlight it with yellow the first command that this will give me the tools.o file and the second command that's going to give me the final library archive with just something like this so i would love to say to have some build recipe where i say please add library and create it with this name it's called tools and then the translation units that will be part of this library are tools.cpp and could be much more but in this case we'll have one file so this is something again i'm wishing that someone gives me this tool because that will make me really happy then we have the third step that is going to give me some it's a github file and then actually it's basically these two steps are also combined somehow but let's say that we want to add some executable that is we want to call it main because that was the name of the example and then for this we need the main.cbb translation unit but again it could be more and then lastly but not least i also want to link these two modules together with this command that is the four commandment we just run and then for this i would love to have something like okay please link this main target with the tools library and then luckily so you're in luck this actually exists and this is real c make code so basically you can automate this uh four lines of shell script with just three with a really simple recipe uh for c make so c make will simplify your wheel so it's one of the most popular build tools out there does not build your code so keep in mind that you will always need to do two steps when you run cmake first you are going to generate the build system and then you're going to build it so it's always two steps and then keep this in mind uh it's cross platform so you can also use it on windows on mac os you can target different architectures and so ever sorry whatsoever and it's very powerful but still the build receipt is really readable so make files work great but when the project gets bigger and bigger and bigger the makefile is actually turning into something that is really hard to maintain and to read so how we can build cmake so in order to do so let's first turn our library project into a cmake project as an example and then we are going to go go back to the slides okay so we are back with our text editor first let's clean up a bit the the workspace let's remove everything this uh actually we can use the globe you should stream from the last tutorial and then this will give me main and then this will give me only the source files i need to create the project so in order to create a simic project we need to provide the cmakelist recipe and then for this we are going to create this file over here and now if you see that let me basically close everything and just stick with the c make list file that is of course empty with because we just created so the first thing we need to specify to cmake is the minimum version this is somehow mandatory so we need to provide it and then let's put version 31 just because and then we need to specify the project and then for example my library is going to be the the project name so far and then for example we can put the standard like same c plus plus 17 but let's keep it for now because we are not really going to use anything from c plus plus 17 so we can safely skip and then one thing we could do with cmag for example is to include directories and then this will specify where to look for header files in this case the her file is basically here on dot so on this particular directory so this will be enough for now and now let's try to remember what we did with the manual build and let's try to copy it with cmx so the first thing we did is to build the library then we say add library and then we call this lettering tools so here's the name of the library and for now we only use this 2 cpp file that is here on my workspace right the tools.cpp file then we need to add the executable and this we call it main just because we wanted to collect this and then we need this main.cpb translation unit and the last step was to link these two guys together and for this we use target link libraries and then we call let's link the main application with the lib tools library and then something i like to do with cmake is to set this variable that is cmake export compiles command that this will give you a json file we're just about to see to to inspect somehow what's going on okay so far so good how we build this project with cmake the first thing we need to do let me put this here it's create a build directory and build it's just a name you can put whatever name you like here most of the people will be using build so put the name you you prefer but it's actually the same now let's go into this bit directory and then now we need to inbox emac so cmake needs to know where do you have your cmake list file in this case it's going to be going to be here on this directory so we just do cma dot dot and if you see now cmx just run with the recipe we just provided and there's some information but for example for example let's check this one that says the sips plus compiler is gnu so it's gcc version 9 and whatsoever some more information but not too much we care so far but as you might er know because we just said it we didn't build the project so far so cmake after running gave us a build system so now by default if you don't specify anything it's going to be make but in this case uh so in this case it's make so if you check this build directory you will see this make file that here is basically the bill system so now we're ready to make our project so if you just hit make you will see all this output and then without doing anything we just did all the four steps we just did before manually but through the build system so how cool is that and actually now that you understand how everything works this is you can read this and understand it and it's not going to be black magic for you so building c plus plus object tools.tbp.oh so this was the first command we did when we did manual stuff linkin c plus plus static library lib tools so this is when we invoked the archive command and then basically we created the the archive the static library then we finish with the target tools this is just a debug message then building c plus plus object main blah blah blah this is basically the third command with it and last but not least linking the executable main with the libtop slavery and as you might expect here we have the libtops library and also i mean application that if we run it we just have the same output as before so how cool is that how simple it is to use these build systems and actually let's open the compiles command.json on my text editor and if you inspect somehow this you will see that you have two commands that is basically the compilation commands that they look really similar to which just did what we just did manually so we invoke c plus plus this is going to tell where to look for help fights so dash i and then it's going to output the the binary object module and it is going to be the cpp file so this is one way uh to know uh what is the build system doing other thing you could do let me remove everything so we clean um and then let's remove this sorry now it's clean now we rerun cmake and stuff do we make we are going to specify make to be super verbose so verbose equals one and then we run make and then this i recommend you to do it in your computer but if you inspect now all the intermediate steps so this is going to be really really similar what but with some other stuff to what we just did when we were building uh the project by hand so this is not super important you can just to make but in case you want to go more deep into what's the build system doing you can just do verbals and check what it's doing so actually in our case we don't do not need these two stuff so these two lines so our build recipe is basically the three lines we wished before to have and then i'm happy we have a tool like cmake to simplify the build process for us so now let's jump back into the slide so as you see the process from the user perspective is very simple first you create you go into your project folder in this case was the my library folder then you need to create a bit directory and then you need to go into this field directory run cmake and then make make is going to build the real project the step number four is going to give you a build system the bid process is completely defined in cmakelist.txt and children file so you can also put more list files inside each directory on your project to simplify somehow the build process sorry not the build process but the the the build system development and actually this is something that is useful because one way that i always do and i also recommend you to do to get to know a project is through its skill system so by understanding how people is building their projects you can also get to understand and you will get the chance to get the intuition of what's going on on that project by just knowing what the build system is doing which is rather nice so here we have something really similar to what we just did so again if you're watching this video please stop it go to your text editor and try to reproduce what i just did and also try to play around with the names to see how you can break it and whatsoever the only thing we didn't do is to say the cmx standard because we are not using anything from c plus plus 17 but for the sake of completeness i put it here for the slides so you should basically always have it there just in case because in the future we are going to see c 17 related stuff this is more or less how a typical project structure will look so you're not forced to follow this project structure but it's somehow how i like to organize my code most of the times when i'm writing ctrl projects so we have the build directory the result directory with some binary and leave library files they include where the directory where all the hair files will lie the resource directory the test directory and whatsoever so this is more or less if you want to start a project project from scratch this is probably a good starting point just as a structure so without thinking on it which what thing you will put in which file basically so the compilations options in cmx again are rather simple so instead of specifying the flux and for the for the compiler you can specify on the build script so this is not so relevant so far so i will just leave this slide for you to read in the future and then lastly but not least so there's some teammate stuff you should know so i'm not an expert in cmake and i guess you should also there's no need for you to become an expert but the more you know about cmake the easier it's going to be for you to develop your build system so cmake is just a script in language like python and other languages so of course it has features of a script in language like functions control structure variables and whatsoever and then something really important in cmake is that all variables are strings so keep this in mind if you want to set some variable you can use the set value and then you specify the the variable and then you specify the value for that variable if you want to get for example to print to the standard output which value is on this variable you need to use the syntax and then if you want to somehow provide some message to the user you call the message function with the message which is also a string and of course you can have warnings errors so if you don't have some library stop the scenic process there and whatsoever so this is more or less like a summary of three four stuff you should know for cmake but believe me stick to the lectures continue with your homeworks and then you will get used without much effort so this is basically how you build project with cmake such as cd cmake and make so you will always repeat this and then lastly for making these projects so keep in mind that if something goes wrong it's actually really easy to start over because before remember when we did the build process by hand we have all the files there on our working directory and was a bit messy to remove all the files we didn't er we wanted to regenerate to make sure we have a clean build from scratch if we want to do this with cmake it's very simple we just go to our terminal and then we just re completely wipe out completely remove the build directory and now we have our clean project again and then we can re uh rerun basically the whole process so we go to bill we just run cmake and then we make we just build our project again and then it's working so maybe sometimes when stuff is not working it's a good idea to just remove the build directory and start from scratch when it's something is really not working so point package is basically probably the command you will use if you if you need for example to to to use some third part library like opencv so in this case we are not going to see right away how to use third party libraries because for the first three or four lectures you are not going to to need it and also for the homework but this slide here is basically uh for you future reference for you when you need to go and find packages for opencv and or libraries so for the homework i highly recommend this video on youtube so it's 100 seconds introduction for for git so git again is part of the software development environment or ecosystem that you need to use in order to write source code and then these are basically the reference so all the information i took from the gcc manual it's big long but if you have some time it's also a good idea to to read to make sure you understand all the details of the compilation process so that's basically it thank you very much for your attention and again get used to build systems they are great tools that will allow you to build this your seedless class programs and then we allow you to move power into this learning journey i hope you enjoyed the lecture and then let's see you on the next one thank you very much for your attention
Info
Channel: Cyrill Stachniss
Views: 4,089
Rating: undefined out of 5
Keywords: robotics, photogrammetry
Id: zOmUHM0sFOc
Channel Id: undefined
Length: 56min 33sec (3393 seconds)
Published: Sun Oct 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.