Тёмный

Compiling Multi-file C++ Source Code with CMake 

Kea Sigma Delta
Подписаться 1,6 тыс.
Просмотров 9 тыс.
50% 1

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

Наука

Опубликовано:

 

3 апр 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 30   
@freznelite
@freznelite 8 месяцев назад
FINALLY! Somebody who described how to include multiple source files!
@KeaSigmaDelta
@KeaSigmaDelta 8 месяцев назад
You're welcome. Glad it's helpful.
@Sokenro
@Sokenro 2 месяца назад
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 3 дня назад
Your videos covering cmake are awesome. Any plans to explain autotools with this kinda detail@ explanations?
@KeaSigmaDelta
@KeaSigmaDelta 3 дня назад
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.
@divyahegde5256
@divyahegde5256 3 месяца назад
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 3 месяца назад
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 3 месяца назад
@@KeaSigmaDelta Thank you for the clarification👍🤝
@KeaSigmaDelta
@KeaSigmaDelta 3 месяца назад
You're welcome.
@sergeyshestakov4936
@sergeyshestakov4936 6 месяцев назад
Thanks!
@KeaSigmaDelta
@KeaSigmaDelta 6 месяцев назад
You're welcome.
@sergeyshestakov4936
@sergeyshestakov4936 6 месяцев назад
The best explanation!
@KeaSigmaDelta
@KeaSigmaDelta 6 месяцев назад
Glad it helped.
@oracleoftroy
@oracleoftroy 11 дней назад
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 11 дней назад
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 4 месяца назад
How can I select the compiler? 6:21 It does not appers to me.
@vinniciusrosa8284
@vinniciusrosa8284 4 месяца назад
I have gcc compiler installed
@KeaSigmaDelta
@KeaSigmaDelta 4 месяца назад
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 3 месяца назад
When the filecount gets high, is GLOB the right way to go?
@KeaSigmaDelta
@KeaSigmaDelta 3 месяца назад
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 3 месяца назад
@@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.
Далее
Why CMake?
13:03
Просмотров 23 тыс.
Be kind🤝
00:22
Просмотров 4 млн
Master Pointers in C:  10X Your C Coding!
14:12
Просмотров 266 тыс.
Compiling Multiple Files in VS Code
9:20
Просмотров 47 тыс.
CMake - the essential package
27:54
Просмотров 7 тыс.
31 nooby C++ habits you need to ditch
16:18
Просмотров 712 тыс.
Do you even test? (your code with CMake)
12:38
Просмотров 16 тыс.
Makefiles: 95% of what you need to know
1:01:53
Просмотров 108 тыс.
Modern CMake for C++
11:38
Просмотров 36 тыс.
Kyocera
0:49
Просмотров 301 тыс.