Compiling Multi-file C++ Source Code with CMake

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I've noticed some C plus plus beginners asking for help and how to even build their code they say their complaint is there are plenty of tutorials on basics of programs if else loops and all that sort of thing but there seems to be very little on how you take a bunch of source files and compile them into a program uh now I agree that seems to be skipped over and the C plus plus world can be complicated and messy so I've created a simple minimal example to show you how to do it with cmake as your build system let's have a look so here is my very simple two source file examples you've got main.cpp which is where the start of the program is and it calls a function from the hello.cpp file and here is hello.cpp's header file or dot h file and the question was how do you compile a multiple file program into one program and here is the C make list file for cmake as you can see it's nice and short which should mean it's very easy for you to follow first line as it suggests is telling a cmake which is the minimum required version of C makers now cmake has changed a lot over the years and obviously older versions don't support the latest stuff so we don't strictly need 3.24 but I suggest unless you have a reason to just leave it at a fairly modern version the next line says what the name is of our project so I've called it hello world because it's a the usual hello world introductory program next we set a variable called source files and we set this to the names of all the star.cpp files all the source files that we want to compile and the final line says add an executable so we are creating an executable called project name and the project name is created by this project line and this executable is built from the source files so it's built from main.cpp and hello.cpp and that's it so if we now I could build it in in vs code but let's do this from the command line the way that they usually tell you to so the first step because building creates all these files and we don't want to create a mess in our in our sauce tree we create a build directory so mkd build now we change to that directory with City build and we need to tell it's cmake we want to build build the files from The Parent Directory not not so that cmake list is not in the build directory it's in the Parent Directory and we give that with a double dot which is just it doesn't seem logical it's just the way it's done and double dot means The Parent Directory and now it's found my compiler cmake is working to generate a make file this so there it goes um generating the make file is done now we can compile it with cmake Double Dash build and this time we tell it use the current directory and that's given by a single dot again not obvious not intuitive it's just the way it's done and now the the generated build file is run and it's done so now because I'm running this on Windows and I'm using uh the visual studio compiler it's created a debug version in a subdirectory I'll show you if you look at the directory you can see there's a a debug directory and that's where the executable is so if I now go to City debug you will see helloworld.exe is there and if I run hello world you can see it prints hello world all done right that covers the basics however you're not going to get far unless you have a compiler and effectively a development environment set up so let me show you how to do that now with vs code and cmake there are multiple things you need to download and install in order to get up and running so first I suggest you download cmake from cmake.org download you can download the version for your system if you're on Linux you can get that by your favorite package manager once you've installed that I recommend downloading Visual Studio code and installing that and once you've done that you're still not done because you need to in within cmake you need to install the C plus and cmake tools packages I've already got them installed along with a few others what you'd normally do is you type into you click on the extensions button over here then and the extensions you type c plus slash C plus plus to get the C plus plus compiler it's important that you get the just get the Microsoft One which is everything that you need and then once you've installed that one for some reason it says reload required for me let's just let it reload and then the other one that you want is cmake tools so once again you would search in the Box up here and type cm8 tools there are other cmake tools or cmake plugins for visual studio this is the one that I recommend you get that's the one that I'm using after you've installed that one you will end up with this blue bar down here and at that point this one will probably say that the there is no active kit it's a bit of a bit weird terminology the kit is basically saying what your target is in my case I want to use the visual studio Community 2019 compiler it's basically choosing the compiler so in my case I chose the amd64 there's your studio compiler you can let it automatically choose what it thinks is the best one when I tried to do that it just said that GCC or Ming w32 was the the best option since I'm working on Windows I'd rather use visual Studio's compiler and from there you click on the build button here you'll see the little Cog and the build button down the bottom of the screen you click on that and you'll see the output here that it executes all the commands to build your project and you could also debug it from down here so you click on the debugger down here and it'll run well build and run and you'll see in the debug console it prints out hello world now there is a slight problem with visual vs code at the moment when you debug using a uh well where is it gone let me enlarge this to the entire screen so when you use this this button to debug the print statements won't appear until after the program exits for some reason if you want to be able to step through a program like this I'll I'll put two break points in just before and after the print hello then you need to go and add under vs code you add a launch.json I I suggest you just grab the one from my example which you'll be able to download from a link below this video uh just copy this this has launch configurations which use an external terminal when debugging and what that does is let me go to is it run start debugging what that will do is that will open an external console window which is that one there and of course I have everything covered up so let me bring that in front and now when you use this external terminal it's a little bit clunky but now if I let it run so let it execute this print hello you will see the hello world which you won't see if you use the the button down the bottom here which is now obscured let me just do that again if you run it from here first it won't show the debug console immediately and when you click on the debug console when you if you let it run the print hello you won't see the print hello down the bottom and the debug console down below until after the program exits so as I say to get around that right now you would add a launch.json file just as I say just copy my one and then you can use the Run start debugging here and it'll it'll open up an external console now obviously if you're writing a program that opens up its own window and displays things and doesn't use printf or print anything to the console then you don't need to do this so that's it all done this is the example as I say the most important thing is this is the cmake file for you to compile multiple source files into one program if you as you add more files you just add them in here so if you had a goodbye.cpp you would just add that in here and that should get you started that's it for now I will see you next time
Info
Channel: Kea Sigma Delta
Views: 7,310
Rating: undefined out of 5
Keywords: CMake, C++, software development, softwaredev, gamedev
Id: FFXCqKMUZ7Y
Channel Id: undefined
Length: 9min 47sec (587 seconds)
Published: Tue Apr 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.