Compiling Multi-file C++ Source Code with CMake

  Рет қаралды 10,381

Kea Sigma Delta

Kea Sigma Delta

Жыл бұрын

There are plenty of C++ tutorials around, but most don't cover the essential step of how to compile multiple source files into one program. This video show how, using CMake...
Click here for a summary (and to download the example/template):
keasigmadelta.com/blog/compil...
Want to learn more CMake? Click the following link:
keasigmadelta.com/store/landi...
#programming #tech #softwaredevelopment #cmake #cpp #cpptutorial #cmaketutorial

Пікірлер: 30
@freznelite
@freznelite 9 ай бұрын
FINALLY! Somebody who described how to include multiple source files!
@KeaSigmaDelta
@KeaSigmaDelta 9 ай бұрын
You're welcome. Glad it's helpful.
@Sokenro
@Sokenro 3 ай бұрын
You have single handedly saved me hours of troubleshooting. Thank you! No Instructor in any of my CS classes or any resources online have talked about how to compile multi-file code and my project kept having so many errors upon building.
@KeaSigmaDelta
@KeaSigmaDelta 2 ай бұрын
Glad the video helped.
@alessandro_yt
@alessandro_yt 29 күн бұрын
Your videos covering cmake are awesome. Any plans to explain autotools with this kinda detail@ explanations?
@KeaSigmaDelta
@KeaSigmaDelta 29 күн бұрын
No plans for autotools, because I find it to be an absolute nightmare to use. Some people love to hate CMake, but it's a huge step up from autotools.
@theintjengineer
@theintjengineer Жыл бұрын
Yeah, after I understood how to work with CMake, all my C++ Projects are CMake Projects. PS: 3:18 actually you didn't really generate a Makefile. You're on Windows and used MSVC in its default form[it used -G "Visual Studio 16 2019"] -> you actually created a VS Solution. But I got you haha I sometimes use Linux, and before moving to using Ninja as my generator, I also had CMake generate Makefiles for me. Good video. Keep it up.
@KeaSigmaDelta
@KeaSigmaDelta Жыл бұрын
> But I got you haha Nah, you're just nitpicking. :-P Yes, it created a VS solution. I'm using "make file" as a generic catch-all for the file(s) that are then used for the actual build. The video isn't intended only for Windows devs.
@punchline1729
@punchline1729 11 ай бұрын
Hi, thank you for this tutorial. I have a question regarding the first part of the video; I've already created the build folder using the " command palette ", and also I use " visual studio " as my compiler, do I need to delete the build folder and do as you did or is there a way to make my compiler compile my files without having to recreate the build folder? perhaps via the terminal
@KeaSigmaDelta
@KeaSigmaDelta 11 ай бұрын
Only if you want to follow the tutorial 100%, and do a complete build from scratch using the commands I give. Otherwise, if the build scripts have already been created in the build folder (by "cmake .."), then you only need to run "cmake --build ." from within the build folder to run the actual build.
@punchline1729
@punchline1729 11 ай бұрын
@@KeaSigmaDelta thank you so much, I subed please don't mind my poor English, since It's not my native tongue
@KeaSigmaDelta
@KeaSigmaDelta 11 ай бұрын
@@punchline1729 You're welcome.
@sergeyshestakov4936
@sergeyshestakov4936 7 ай бұрын
Thanks!
@KeaSigmaDelta
@KeaSigmaDelta 6 ай бұрын
You're welcome.
@divyahegde5256
@divyahegde5256 4 ай бұрын
Hii, thank you so much for the video! I followed your video, and had one doubt... 'add_executable() ' should be kept as it is in the video or should it include the project and source file name that i kept?
@KeaSigmaDelta
@KeaSigmaDelta 4 ай бұрын
I recommend keeping it the way it is in the video. The ${PROJECT_NAME} variable contains the project's name, so the executable will be given the same name as the project. If you ever want to change the project name, then you only have to change it once (in the project() call) instead of twice. In programming you generally want to avoid repeating yourself... Using a ${SOURCE_FILES} variable is far more flexible than adding files directly to add_executable(). For example, you may have source files that are specific for Windows, MacOS, etc. It's easy to conditionally add them to the ${SOURCE_FILES} variable, whereas you'd have to have multiple add_executable() calls otherwise, with the common files repeated.
@divyahegde5256
@divyahegde5256 4 ай бұрын
@@KeaSigmaDelta Thank you for the clarification👍🤝
@KeaSigmaDelta
@KeaSigmaDelta 4 ай бұрын
You're welcome.
@sergeyshestakov4936
@sergeyshestakov4936 7 ай бұрын
The best explanation!
@KeaSigmaDelta
@KeaSigmaDelta 6 ай бұрын
Glad it helped.
@oracleoftroy
@oracleoftroy Ай бұрын
I personally don't get why people like to set a variable for source files. I'd just list them in the add_executable or add_library call directly, or better, use target_sources. That way you can enable modern C++ features like modules fairly easily and use generator expressions for any platform specific files. If the build becomes complicated enough to justify the variable and it cant be simplified, ok, introduce some variables, but I don't think they add any value as a default.
@KeaSigmaDelta
@KeaSigmaDelta Ай бұрын
Part of it's probably habit, which started with older build systems. I do think the script looks cleaner when setting a variable for source-files, though. Plus it's easier to switch when you do get to the point that it becomes necessary.
@vinniciusrosa8284
@vinniciusrosa8284 5 ай бұрын
How can I select the compiler? 6:21 It does not appers to me.
@vinniciusrosa8284
@vinniciusrosa8284 5 ай бұрын
I have gcc compiler installed
@KeaSigmaDelta
@KeaSigmaDelta 5 ай бұрын
You can skip that step if you only have one compiler installed. CMake will automatically use the compiler you installed. I have multiple compilers installed, and therefore need to tell it which one to use. To change the chosen compiler, you need to click the tool icon on the CMake status bar (the blue bar at the bottom of the video). In the video you can see [Visual Studio Community 2019 Release -amd64] to the right of the tool icon. Unfortunately, they changed the CMake Tools plugin since I created the video, and it now hides the status bar by default (and it's no longer blue). You can enable the status bar in the plugin's settings. However, there's no need to do this if you have only one compiler installed.
@bittondb
@bittondb 4 ай бұрын
When the filecount gets high, is GLOB the right way to go?
@KeaSigmaDelta
@KeaSigmaDelta 4 ай бұрын
My personal opinion: no. It's best to be 100% precise as to which files are included in the build. That means you add them all into the build script. If the list of files is starting to get too large, then it's probably time to organize your files into sub-directories for different parts of the code (e.g., graphics code and audio code in separate sub-directories). Then you add the files in separate CMakeLists.txt files which you include into the main one. This is one of the things I plan to cover in The CMake Tutorial: cmaketutorial.com/
@DrFooMod2
@DrFooMod2 4 ай бұрын
@@KeaSigmaDelta thanks. makes sense.
@plattcriceta1719
@plattcriceta1719 Жыл бұрын
isn't it sad that you actually have to write just another source code just to tell the compiler how it should work?
@KeaSigmaDelta
@KeaSigmaDelta Жыл бұрын
Not really. The compiler must do exactly what it's told, which means giving it precise instructions. I suppose you could write a compiler that tries to guess (e.g., just compile all the *.c/*.cpp files in a directory), but that's guaranteed to get things wrong. So, you'd be back to giving it more precise instructions again. A few quick examples where compiling everything would be a problem: - Some source files are for specific platforms (e.g., different files for Windows, Linux, MacOS X, etc.) - Some source files need to be compiled with special flags (e.g., some files should be compiled with SIMD enabled, and others without) On top of that, you may be using external libraries, and you may need specific versions of those.
Using Third Party Libraries Like RayLib in Your Project with CMake
6:47
CMake - the essential package
27:54
Code for yourself
Рет қаралды 8 М.
The Noodle Picture Secret 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 29 МЛН
IS THIS REAL FOOD OR NOT?🤔 PIKACHU AND SONIC CONFUSE THE CAT! 😺🍫
00:41
Countries Treat the Heart of Palestine #countryballs
00:13
CountryZ
Рет қаралды 29 МЛН
C++ in VS Code: Building your Code with CMake
9:37
Visual Studio Code
Рет қаралды 13 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 281 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 174 М.
Why I Like Programming in C.
3:16
Francisco Jinto Fox
Рет қаралды 24 М.
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 255 М.
How all C/C++ build Systems Work (Except for CMake and Premake)
4:53
Kea Sigma Delta
Рет қаралды 9 М.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Рет қаралды 883 М.
CMake vs Make - A developer's perspective
8:16
Kea Sigma Delta
Рет қаралды 3,7 М.
DC Fast 🏃‍♂️ Mobile 📱 Charger
0:42
Tech Official
Рет қаралды 485 М.
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 3 МЛН
Ждёшь обновление IOS 18? #ios #ios18 #айоэс #apple #iphone #айфон
0:57
Как работает автопилот на Lixiang L9 Max
0:34
Семен Ефимов
Рет қаралды 16 М.
wireless switch without wires part 6
0:49
DailyTech
Рет қаралды 4,8 МЛН