Thank you for making a video that starts at the most basic and builds upward in complexity. Trying to understand CMake coming from the opposite direction (ie: from an output pane in VS Code that spits an error out at you why it can't build your binary file) is very difficult. THanks for including Ninja as well because I've been seeing mentions of Ninja and knew it was a "generator" but didn't know what that meant until now. 10 RUclips logos of out 10 for this video.
Used it and others w/ 42 yrs as programmer, analysry manager, director , and now, retired from code for a living to code for fun. It always was fun - they paid me to have fun!!!.
I know how to write C programs, but I've always had big training wheels. As I started to dig deeper, I've come across the term Cmake, but never had a clue what it was. Thank you for your clear introduction.
Very useful, thanks! I have been retired for years now, but made my living as a software developer for decades. I wrote almost entirely in C, and worked on projects that required a lot of effort to construct and maintain the makefile. In recent years I began returned to programming as a hobby, including tinkering with MCUs. When I discovered that CMake is now a thing, I was quite daunted. Oh no - another meta level of build system to learn. So I avoided it. But I guess it's time to bite the bullet and learn it, because I have pretty much hit the limit with arduino IDE. So once again , thanks for this intro to CMake.
I have been using C and C++ since the 90s and have never touched a make file. It's just such an archaic concept when we have automation tools. Can you imagine constructing a make file for a project with 10,000 files? Thing is, CMake is also pretty bad. I've literally spent days trying to get CMake working.
@@GaryExplains My last job as a software guy was for a company in Calgary, Alberta. This was an ill-fated (an ill-conceived) effort to produce a message-passing based operating system, and ALL of the application layers on top of it, to implement large scale SCADA systems. I came to the company late in the game, and became something of a Cassandra because it quickly became obvious to me that the basic structure of the software was completely unworkable. (Imagine a software team of 20 people trying to implement linux, X11, the Chrome browser, and all the glue as one monolithic system. Nope....) IIRC, the software - from the lowest hardware level up - was built using make, and a dauntingly complicated makefile. Coding and maintaining this alone was a major effort. At that time in my career I had never heard of CMake, and don't actually know whether it existed. This build strategy was in microcosm representative of the project's design flaws, but was not its biggest flaw.
All the great content are free and I have nothing to complain. But (sorry) this other than some AI stuff is what I have expected from Gary. Thank you for the effort. The explanation was crystal clear!
Thanks. Could you do a similar beginners guide to setting up your first github/gitlab project on a home network NAS or local hard drive? Or maybe you've already covered this?
👍 Fun fact: GNU make provides a (huge) set of default rules. If the executable is made of a single source file (not limited to C or C++) then you don't need a Makefile at all. Just run "make hellow". The executable base name must be that of the source file, it's the only requirement. More precisely, the executable name is derived from the source base name.
@@rajmajumdar5253 Yup. Just try it. Note that default flags will be used unless you pass them to make or via the environment. Example: `make CFLAGS=-Os -Wall hellow`.
Thank you Gary. Please continue explaining Cmake further. BTW, the random example gives the same result every time. Even on my computer I get the same sequence [ 0.767554, 1.104780, 1.069596, 1.084488, 0.245279 ,0.580639, 0.292428, 1.135251, 0.902894, 0.226394 ] . Not very random... 😁
As with all Pseudo-Random Number Generators (PRNGs), they just use a mathematical algorithms to generate numbers. Without providing a seed (which this program doesn't do) then they will always generate the same sequences.
Gary, that is strange, why is that implemented if resulting the same effect 🙉 .. when i started computing on ZX81 it was very good random numbers generator, we were doing tests with colours, or symbols, Later on, i was using Atari 520 ST, and it was lighting fast with numbers never repeated .. I'm missing these computers, so romantic times, exciting and i was soooo happy to exchange software listings with friends to see results, testing days, and days, sorting programs, etc 🌞 On ATARI 520-ST compilers GFA-Basic ultra fast, objective, Pascal, Mark Williams C compiler with a system like Linux .. Thanks 🎉
The program isn't the important thing here, but the build system. As I mentioned to the OP, a seed needs to be provided to every PRNG. On many systems (but not all) the seed is automatically set on the first call to get a random number, the seed is often based on the current time and therefore different every time the program runs.
@@zorabixun It can sometimes be helpful to generate a random but repeatable sequence of numbers to have the same conditions on every run. Therefore delivering the seed to the random number generator is an extra and depending on your application optional step.
Thanks, Gary. This is outstanding - this saves me days of explaining CMake for new hires. How about a similar look at the GNU autotools? There are still loads of projects using them.
(Useful tip in last paragraph) I've been using make since System V but have wondered about cmake. This was a great explanation, as the man pages are, well, long and don't encourage reading. I'm not convinced, as make is quick to set up for small/medium projects (default rules). What's always been tricky (for over 40 years) is knowing what the dependencies are in order to define the rules! Clang/LLVM (and I think gcc) has the -M option that can list the dependencies in Makefile format, but not cmake. And it's not in the standard man page! Just run cc -M test.c (or whatever) and it'll list what goes in to making it. -MM is the same, but without header files.
Great video Gary, it is appreciated! If you make a followup video, could you explain a little about what those other cmake files are in the build directory? And of course, a bit more about the options in the CMakeLists.txt file. 👍
In addition to not having to type and tell gcc to compile all the files by using make/cmake, you also avoid recompiling items that were not updated/touched since the last time they were compiled. This saves a lot of computer time and was probably more important in the good old days.
I wish I had watched this first! My first look at cmake was to build opencv on widows--not for the faint of heart. This video gave me the overview I needed. Thanks so much!
Despite having used cmake many times, I learnt a few things. For example I did not even know CMake is for Crossplatform Make. Also the examples for more complicated Makefile were top notch where the interested person could look up or just understand looking at it (e.g. %.o: %.c). Absolutely amazing video, always so pleasant to watch Gary's videos.
That's mostly how we use it... to create project files for MS Visual Studio when importing an open source library. But it sucks. It does an awful job of it, and that's only with a lot of luck. Mostly it just fails with errors. And as I commented somewhere else, it always fills the source tree with absolute paths. Terrible idea. The documentation is also shocking.
@@robby3467 Yeah. CMakeLists.txt name is also weird. I still don't know what PRIVATE does. Things like add dependencies are given after add_executable which is weird. I think it grew out of some person who just hacked something quickly to suit his needs. I don't think it's the best, but unfortunately it has become standard. Maybe the best way to dethrone it is to go one step up and now make a CMakeLists.txt generator with a saner syntax.
wonderful video! I really need an introduction to CMAKE… I do think you glossed over the biggest feature of make and make files… And that is it understands when a file has been touched or modified and only compiles the things it needs to in order to generate the output… It does this by comparing the timestamps on the file modification time… Great video I've been long time subscriber your explainers are the best keep doing what you're doing! And continue to success!
Thank you, this video is very much appreciated. I have some programming background - I learnt Borland Pascal in school and in first course at university... have done a tiny bit of PHP and JavaScript, some small projects on Arduino. But since I moved to work in UK... I have in a way wasted 10 years doing manual labour jobs. I want to move back to electronics and programming, I'm slowly working on starting my RUclips channel. I have wanted to learn C/C++ and need it for electronics projects, and especially now with Raspberry, I have to learn how to compile programs from source on Linux. So I have heard about GCC, have some rough idea about it all, but this video is a conspectus of how to say - missing peaces to link it all together.
A key difference between a build system like make and a shell script is what you illustrated at the end by deleting a few lines from "random.h": make compares the dates of any files it knows how to generate with the dates of the files that depends on and only runs the rules if the generated file is older. A shell script would run all the rules every time. Some viewers might have missed that when you edited "random.h" make ran the rules to generate "random.o" and "main" but not the rule to generate "main.o" since that was newer than "main.c"
The way i do it is to setup directories for everything that makes sense to go together, and then each directory gets its own CMakeLists.txt to build a library out of these files, and the main CMakeLists.txt includes all the directories ti get those files to be active and links the libraries I have created. (I do embedded work, we can't have dynamic/shared libraries, all needs to be statically linked)
That likely depends on the distribution you are using. But if it isn't installed yet and you use anything that traces back to Debian, a sudo apt install cmake will fix that for you. I would think that pretty much any modern distribution at least has it available in their repository.
Hey Gary, thanks for your great contribution ! One question: In the video you did mention several times Visual Studio but you did not explicitly mention "Visual Studio Code" or vscode. What about this environment ?
Thank you, a good starting point for a beginner. I learnt to use Borland Turbo Pascal, that was my language of choice. I mainly work with hardware, I need to get more used to software tools. Do you gave an example of using VS code to program a pi pico in C/c++?
Thank you, I was always confused about the difference between GCC & Make & CMake but this explains everything. I do hope one day C & C++ will get a default package manager like with Java you have maven and gradle so easy to use both.
I use MS Visual Studio on Windows 99.9999% of the time. It does all of that stuff for you and way more. It even integrates Clang / LLVM for those who don't like MSVC. If you rename a file or add a file, it'll automatically handle that, etc.
This is such an approachable tutorial. Thank you for Making this ;) Edit 01 - This is probably the best C-make for beginners tutorial out there. Full stop. End Edit 01 I had one question. Just like random.h which needs to be compiled , the Math.h is also a header file. So why did we need to link against that instead of also compiling that header ? Thanks,
The functions defined on math.h are not part of the standard C library, so you need to link again libm explicitly. So I am not linking against the header but to the library that has the functions defined in the header.
@@GaryExplains got it. Thanks for the clarification. Just FYI i'm here because I want to integrate a plugin for Unreal Engine . Specifically to integrate a recent version of OpenCV. All documentation & videos on the topic are years old so it's become difficult. So this kind of knowledge (building from source, DLL etc ) is very essential. Thanks and god speed.
Interesting but with my clumsy fingers and weird dyslectic issues I'm using an IDE with all the settings I need. I never understood why many prefer to write instead of clicking on a button. I'm also using pragma once.
Thanks Gary for great tutorial :) Small contribution from myself: if you want to use cmake for your Visual Studio compiler, then: 1. cmake in your build_vs subdirectory, with command: cmake -G "Visual Studio 17 2022" .. 2. build created solution with msbuild: MSBuild.exe hellow.sln -p:Configuration=Release
They are the same. Most random number generators are pseudo random which means they generate the exact same sequence until the "seed" is changed. A common seed is the current time as it is always changing.
@@GaryExplains Ah ha. I understand. Also wanted to say I found the video very helpful as I'm trying out Fortran openMP techniques (with and without debug) and the project is CMAKE based.
This video seems to be approaching optimum for explaining these tools! Good length, depth and clarity, like a glass dild… Seriously though, it’s great.
You see, this is what makes Linux interesting as opposed to the other OS's and their tools. At least, you can then go to another level and start looking at other types of source files that make up smaller linux programs and learn how the build process proceeds so that installing from source has a more meaningful impact in terms of comprehension.
I get your point and I agree, but technically it isn't Linux that makes this possible, but open source in general. For example, you can look at and compile and install htop on macOS or FreeBSD.
This morning, I bumped into CMake and started looking into learning enough about it to be able to use it, but then I was distracted by the build system for Zig.
Hey, no problem! It sounds like CMake, GCC, and all that are new to you - that's totally fine. Let me know if you have any questions about the video at all. 👍
@@GaryExplains the video itself was sufficient for me , i learned everything i needed to start a new project thanks to you man , appreciate your efforts
@@simonbooth4888That's a matter of how those third party libraries are installed as part of the project. If the complier doesn't need -I and -L to find the headers and library binaries, it's no different from linking to libm. If they provide pkg-config files, there's find_package(PkgConfig), which gives you the pkg_check_modules(...) command to discover the paths and compile flags. If cmake knows about the library in question, it has other ways (I'm typing on a phone, so can't recall the commands off the top of my head). If it's a vendored library, then your root CMakeLists.txt should pull in the vendored library as another target. Dealing with vendored libraries is really something that would be covered as part of a video on project organisation, though.
@@h_pranav_nayak its working when I trying flash the led, but I'm so confused how to setup cmakelist.txt for my environment, actually i want someone teaching how cmakelist.txt work on raspberry pi pico, how to setup the directory, i have many of problem how to setup the lib on cmakelist...I ready done to add lib on cmakelist code, but when try to running, the error showing "lib not found", and i try search use "locate lib" it shows the existence of the library
I do show it, but later in the video when I demonstrate what the build system does when you change a files it is just a declaration for the random function. 1 line of code.
@@GaryExplains Thanks. I saw when I finished the video. I just initially got the compile error for the missing file, but it was easy enough to dummy something in until you exposed it.
@@GaryExplains I had to see a dozen ads in a 20 min video, stopping every couple of minutes, ok that you deserve a revenue for your work, but over a certain limit I'm no longer motivated in following a youtuber.
I understand. But as I said it is Google that picks how many ads are shown and where. Not me. Remember that Google gets a much larger fraction of the advert revenue than the video creator. Your beef is with Google, not me.
Thanks for this series Gary. A request. Please do a video on how to control all the make flags using cmake. For instance, I hate that cmake always hides the real gcc compile commands. I absolutely loathe that. I want to see the 1000 character long actual gcc commands as they are executed. How do I force cmake NOT to hide that?. It builds this complex structure of hierarchical Makefiles, and I never know how any code is actually being built. The options to cmake are where all the utility lies. As someone who grew up on make, it is incredibly frustrating not to be able to see what is happening when I compile a project.
There is an option to control CMake's verbosity (and of course the verbosity of the output it produces. I don't remember what it is, as I only tried it once and didn't really see the benefit over a mostly silent build that only shows warnings and errors (or in my usual project setup only errors since I treat warnings as errors and set my compiler to nag a lot). I found it easily when I googled for it. So if you prefer to see tons of text scroll by, CMake has got you covered. 😉 I am not saying that either of the two is better, I think this is mostly a matter of personal preference.
fatal error: 'random.h' file not found I have main.c and random.c in the directory Ether I run this command - gcc -c main.c Or this command - gcc -o main main.c random.c -lm Both are giving same error.
@@robby3467Better than Autotools hell though, however bad the CMake language itself is. I'd use Meson for builds if I could, if only because it's less crufty.
CMake sucks, right? We found it always filled the source tree with absolute paths. There should be a way to create relative paths but could never find anything that worked.
Sorry to be so blunt, but CMake is an absolute piece of garbage (in my opinion). I've spent too many hours, if not days, tearing my hair out or turning it grey, trying to beat CMake into submission. Worst piece of build tooling I've had the misfortune of meeting in over 25 years of software development. It never works out of the box. It's a piece of crap. Even watching this video... I feel the stress. Sorry, but I just had to vent. Is this really the best the open source community can do? It's still an archaic process. This is 2024. That said, you provided a nice overview.
Don’t agree at all. CMake is an excellent build tool compared to alternatives like GNU autotools. The CMake team works with the major C++ compiler teams and Microsoft’s cross platform tools in Visual Studio rely on CMake. They also have a package manager vcpkg which integrates with CMake. That said, in my view the best IDE for working with CMake is CLion from JetBrains. If you wish to avoid hair loss it is the best option, the integrated CMake debugger is excellent.
What's the alternative, do you use Meson? Can you give a specific example where Meson, or another system, works better for you than Cmake? (I have no experience with Meson and not much with Cmake.)
Can you please provide reasons of why you think this way? For one i see people complain about it's scripting sytanx, and even while its true, it's not a huge deal for me; what else do you think is wrong with cmake?
Cmake is such a pathetic messy build tool. No wonder a lot of beginners get overwhelmed when trying to learn C++ and they just go for much more newer better fleshed out languages with far much better build tools. We have to be thankful though because the languages that came after learned from mistakes made by the earlier ones.
I will unsubscribe if you don't change the "demo"-intermission jingle! I would be deaf if wearing headphones. Super loud on my laptop speakers. Otherwise, good cover of basics! liked!
The most perfect tutorial on cmake and make. It rightly takes half of a video to build up to the requirement of the tool.
Thank you for making a video that starts at the most basic and builds upward in complexity. Trying to understand CMake coming from the opposite direction (ie: from an output pane in VS Code that spits an error out at you why it can't build your binary file) is very difficult. THanks for including Ninja as well because I've been seeing mentions of Ninja and knew it was a "generator" but didn't know what that meant until now. 10 RUclips logos of out 10 for this video.
Used it and others w/ 42 yrs as programmer, analysry manager, director , and now, retired from code for a living to code for fun. It always was fun - they paid me to have fun!!!.
I know how to write C programs, but I've always had big training wheels. As I started to dig deeper, I've come across the term Cmake, but never had a clue what it was. Thank you for your clear introduction.
Very useful, thanks! I have been retired for years now, but made my living as a software developer for decades. I wrote almost entirely in C, and worked on projects that required a lot of effort to construct and maintain the makefile. In recent years I began returned to programming as a hobby, including tinkering with MCUs. When I discovered that CMake is now a thing, I was quite daunted. Oh no - another meta level of build system to learn. So I avoided it. But I guess it's time to bite the bullet and learn it, because I have pretty much hit the limit with arduino IDE. So once again , thanks for this intro to CMake.
I have been using C and C++ since the 90s and have never touched a make file. It's just such an archaic concept when we have automation tools. Can you imagine constructing a make file for a project with 10,000 files? Thing is, CMake is also pretty bad. I've literally spent days trying to get CMake working.
@toby9999 Interesting comment. How did you build projects with 10,000 files without a build system?
@@GaryExplains My last job as a software guy was for a company in Calgary, Alberta. This was an ill-fated (an ill-conceived) effort to produce a message-passing based operating system, and ALL of the application layers on top of it, to implement large scale SCADA systems. I came to the company late in the game, and became something of a Cassandra because it quickly became obvious to me that the basic structure of the software was completely unworkable. (Imagine a software team of 20 people trying to implement linux, X11, the Chrome browser, and all the glue as one monolithic system. Nope....) IIRC, the software - from the lowest hardware level up - was built using make, and a dauntingly complicated makefile. Coding and maintaining this alone was a major effort. At that time in my career I had never heard of CMake, and don't actually know whether it existed. This build strategy was in microcosm representative of the project's design flaws, but was not its biggest flaw.
Long time C# developer but doing some c/c++ as a hobby. This is the best make/cmake introduction I have seen, thanks!
Thanks Gary, make was a bit of a mystery to me this really sorted it out.
Loved this thanks Gary for sharing ❤
Glad you enjoyed it
All the great content are free and I have nothing to complain. But (sorry) this other than some AI stuff is what I have expected from Gary. Thank you for the effort. The explanation was crystal clear!
Thanks. Could you do a similar beginners guide to setting up your first github/gitlab project on a home network NAS or local hard drive? Or maybe you've already covered this?
watched a few times. enjoyed the simpilicity and clarity in your tutorials.❤❤ It feels good learning with gary
Awesome! Thank you!
👍
Fun fact: GNU make provides a (huge) set of default rules. If the executable is made of a single source file (not limited to C or C++) then you don't need a Makefile at all. Just run "make hellow". The executable base name must be that of the source file, it's the only requirement. More precisely, the executable name is derived from the source base name.
Wait what really?
@@rajmajumdar5253 Yup. Just try it. Note that default flags will be used unless you pass them to make or via the environment. Example: `make CFLAGS=-Os -Wall hellow`.
Thank you Gary. Please continue explaining Cmake further.
BTW, the random example gives the same result every time. Even on my computer I get the same sequence [ 0.767554, 1.104780, 1.069596, 1.084488, 0.245279 ,0.580639, 0.292428, 1.135251, 0.902894, 0.226394 ] . Not very random... 😁
As with all Pseudo-Random Number Generators (PRNGs), they just use a mathematical algorithms to generate numbers. Without providing a seed (which this program doesn't do) then they will always generate the same sequences.
Gary, that is strange, why is that implemented if resulting the same effect 🙉 .. when i started computing on ZX81 it was very good random numbers generator, we were doing tests with colours, or symbols,
Later on, i was using Atari 520 ST, and it was lighting fast with numbers never repeated ..
I'm missing these computers, so romantic times, exciting and i was soooo happy to exchange software listings with friends to see results, testing days, and days, sorting programs, etc 🌞
On ATARI 520-ST compilers GFA-Basic ultra fast, objective, Pascal, Mark Williams C compiler with a system like Linux .. Thanks 🎉
The program isn't the important thing here, but the build system. As I mentioned to the OP, a seed needs to be provided to every PRNG. On many systems (but not all) the seed is automatically set on the first call to get a random number, the seed is often based on the current time and therefore different every time the program runs.
@@zorabixun It can sometimes be helpful to generate a random but repeatable sequence of numbers to have the same conditions on every run. Therefore delivering the seed to the random number generator is an extra and depending on your application optional step.
Thanks, Gary. This is outstanding - this saves me days of explaining CMake for new hires. How about a similar look at the GNU autotools? There are still loads of projects using them.
(Useful tip in last paragraph)
I've been using make since System V but have wondered about cmake. This was a great explanation, as the man pages are, well, long and don't encourage reading.
I'm not convinced, as make is quick to set up for small/medium projects (default rules).
What's always been tricky (for over 40 years) is knowing what the dependencies are in order to define the rules!
Clang/LLVM (and I think gcc) has the -M option that can list the dependencies in Makefile format, but not cmake. And it's not in the standard man page! Just run cc -M test.c (or whatever) and it'll list what goes in to making it. -MM is the same, but without header files.
cmake is best compared to GNU Auto tools, mind.
Great video Gary, it is appreciated! If you make a followup video, could you explain a little about what those other cmake files are in the build directory? And of course, a bit more about the options in the CMakeLists.txt file. 👍
Very nice - thanks!
Thank you!
You're welcome!
THIS IS THE VIDEDO !!!! THIS IS IT.
I love this back to basics series. Gary is great again !
Thank you. This is good info. I can compile basic projects, and used make 30 y ago and have forgotten the bit I knew then. This will certainly help.
In addition to not having to type and tell gcc to compile all the files by using make/cmake, you also avoid recompiling items that were not updated/touched since the last time they were compiled. This saves a lot of computer time and was probably more important in the good old days.
I wish I had watched this first! My first look at cmake was to build opencv on widows--not for the faint of heart. This video gave me the overview I needed. Thanks so much!
I'm glad you found it helpful!
Despite having used cmake many times, I learnt a few things. For example I did not even know CMake is for Crossplatform Make. Also the examples for more complicated Makefile were top notch where the interested person could look up or just understand looking at it (e.g. %.o: %.c).
Absolutely amazing video, always so pleasant to watch Gary's videos.
That's mostly how we use it... to create project files for MS Visual Studio when importing an open source library. But it sucks. It does an awful job of it, and that's only with a lot of luck. Mostly it just fails with errors. And as I commented somewhere else, it always fills the source tree with absolute paths. Terrible idea. The documentation is also shocking.
@@robby3467 Yeah. CMakeLists.txt name is also weird. I still don't know what PRIVATE does. Things like add dependencies are given after add_executable which is weird.
I think it grew out of some person who just hacked something quickly to suit his needs.
I don't think it's the best, but unfortunately it has become standard. Maybe the best way to dethrone it is to go one step up and now make a CMakeLists.txt generator with a saner syntax.
At least this is not one of those confusing tutorials in RUclips when you first time tried to search help of CMake :D
wonderful video! I really need an introduction to CMAKE… I do think you glossed over the biggest feature of make and make files… And that is it understands when a file has been touched or modified and only compiles the things it needs to in order to generate the output… It does this by comparing the timestamps on the file modification time… Great video I've been long time subscriber your explainers are the best keep doing what you're doing! And continue to success!
As an absolute beginner this video was very useful and informative! Thank you!
Glad it was helpful!
Thank you, this video is very much appreciated.
I have some programming background - I learnt Borland Pascal in school and in first course at university... have done a tiny bit of PHP and JavaScript, some small projects on Arduino. But since I moved to work in UK... I have in a way wasted 10 years doing manual labour jobs.
I want to move back to electronics and programming, I'm slowly working on starting my RUclips channel. I have wanted to learn C/C++ and need it for electronics projects, and especially now with Raspberry, I have to learn how to compile programs from source on Linux.
So I have heard about GCC, have some rough idea about it all, but this video is a conspectus of how to say - missing peaces to link it all together.
very useful refresher
Perfect explanation for beginners.
Thank you for your excellent explanation keep up the great work.
Glad it was helpful!
A key difference between a build system like make and a shell script is what you illustrated at the end by deleting a few lines from "random.h": make compares the dates of any files it knows how to generate with the dates of the files that depends on and only runs the rules if the generated file is older. A shell script would run all the rules every time. Some viewers might have missed that when you edited "random.h" make ran the rules to generate "random.o" and "main" but not the rule to generate "main.o" since that was newer than "main.c"
clean and clear explanation
Glad you found it helpful!
@4:25 thanks for the video ❤
why don't we have math.o same as other two. Why is it -lm? What makes math lib and random lib different?
Random lib is made by us, math lib is provided by the system.
Thanks. Very useful
Thank you well explained
💌
How do we organize larger projects? Most projects have trees of directories filled with files and a single config/make sequence to compile them all.
One of the projects I work on has 6000 folders and 90,000 files. There's no way I'd be using a toy-like utility like CMake for that, and we don't.
The way i do it is to setup directories for everything that makes sense to go together, and then each directory gets its own CMakeLists.txt to build a library out of these files, and the main CMakeLists.txt includes all the directories ti get those files to be active and links the libraries I have created. (I do embedded work, we can't have dynamic/shared libraries, all needs to be statically linked)
loves to see it
This is very useful. Is CMAKE installed on Linux by default or do we need to download it? 👍
That likely depends on the distribution you are using. But if it isn't installed yet and you use anything that traces back to Debian, a sudo apt install cmake will fix that for you. I would think that pretty much any modern distribution at least has it available in their repository.
Hey Gary, thanks for your great contribution ! One question: In the video you did mention several times Visual Studio but you did not explicitly mention "Visual Studio Code" or vscode. What about this environment ?
Thank you, a good starting point for a beginner. I learnt to use Borland Turbo Pascal, that was my language of choice. I mainly work with hardware, I need to get more used to software tools. Do you gave an example of using VS code to program a pi pico in C/c++?
Thank You for sharing.
Nice video! consider also go to more advance stuff about cmake! like variables or if statement and ...
Thank you, I was always confused about the difference between GCC & Make & CMake but this explains everything. I do hope one day C & C++ will get a default package manager like with Java you have maven and gradle so easy to use both.
Glad it was helpful!
I use MS Visual Studio on Windows 99.9999% of the time. It does all of that stuff for you and way more. It even integrates Clang / LLVM for those who don't like MSVC. If you rename a file or add a file, it'll automatically handle that, etc.
Excellent!
How do you get around the fact that we didn't create random.h in the 2nd demo?
I don't understand. What do you mean?
@@GaryExplains 2:44 2nd line is
#include "random.h"
so the compiler will try to open a file called random.h which we didn't create ?
I did create it, I just didn't show it. It is the same random.h file I show later in the video.
This was great! Thank you so much.
Glad you enjoyed it!
This is such an approachable tutorial. Thank you for Making this ;)
Edit 01 - This is probably the best C-make for beginners tutorial out there. Full stop. End Edit 01
I had one question. Just like random.h which needs to be compiled , the Math.h is also a header file. So why did we need to link against that instead of also compiling that header ?
Thanks,
The functions defined on math.h are not part of the standard C library, so you need to link again libm explicitly. So I am not linking against the header but to the library that has the functions defined in the header.
@@GaryExplains got it. Thanks for the clarification.
Just FYI i'm here because I want to integrate a plugin for Unreal Engine . Specifically to integrate a recent version of OpenCV.
All documentation & videos on the topic are years old so it's become difficult.
So this kind of knowledge (building from source, DLL etc ) is very essential.
Thanks and god speed.
Interesting but with my clumsy fingers and weird dyslectic issues I'm using an IDE with all the settings I need. I never understood why many prefer to write instead of clicking on a button.
I'm also using pragma once.
Thanks 👍
Ninja is the way to go with CMake. It works on Windows and MacOS as well as Linux.
Thanks Gary for great tutorial :)
Small contribution from myself:
if you want to use cmake for your Visual Studio compiler, then:
1. cmake in your build_vs subdirectory, with command:
cmake -G "Visual Studio 17 2022" ..
2. build created solution with msbuild:
MSBuild.exe hellow.sln -p:Configuration=Release
@5:32 those two lists look the same.
They are the same. Most random number generators are pseudo random which means they generate the exact same sequence until the "seed" is changed. A common seed is the current time as it is always changing.
@@GaryExplains Ah ha. I understand. Also wanted to say I found the video very helpful as I'm trying out Fortran openMP techniques (with and without debug) and the project is CMAKE based.
This video seems to be approaching optimum for explaining these tools! Good length, depth and clarity, like a glass dild…
Seriously though, it’s great.
what about zig and opinion on as a c compiler drop in replacement
I did a video on that.
You see, this is what makes Linux interesting as opposed to the other OS's and their tools. At least, you can then go to another level and start looking at other types of source files that make up smaller linux programs and learn how the build process proceeds so that installing from source has a more meaningful impact in terms of comprehension.
I get your point and I agree, but technically it isn't Linux that makes this possible, but open source in general. For example, you can look at and compile and install htop on macOS or FreeBSD.
Awesome intro! Next stop, the Linux kernel 😅
👍👍Thanks sir!
Does it also work with Fortran
Thank you
You're welcome
This morning, I bumped into CMake and started looking into learning enough about it to be able to use it, but then I was distracted by the build system for Zig.
are you using git bash or anything else like unix
Linux, as I say at 02:20
@GaryExplains oh sorry for not getting it before
Hey, no problem! It sounds like CMake, GCC, and all that are new to you - that's totally fine. Let me know if you have any questions about the video at all. 👍
@@GaryExplains the video itself was sufficient for me , i learned everything i needed to start a new project thanks to you man , appreciate your efforts
Thanks for tge video. I wanna use cmake for cmocka.
No external library linking explanation which is the most confusing bit when using third patry add-ons. i.e. need a follow up
External library like libm?
@@GaryExplains External libraries like display drivers on a pico
@@simonbooth4888That's a matter of how those third party libraries are installed as part of the project. If the complier doesn't need -I and -L to find the headers and library binaries, it's no different from linking to libm. If they provide pkg-config files, there's find_package(PkgConfig), which gives you the pkg_check_modules(...) command to discover the paths and compile flags. If cmake knows about the library in question, it has other ways (I'm typing on a phone, so can't recall the commands off the top of my head). If it's a vendored library, then your root CMakeLists.txt should pull in the vendored library as another target. Dealing with vendored libraries is really something that would be covered as part of a video on project organisation, though.
If you want a more in depth explanation, "Modern CMake with C++" is a good choice.
Why numbers not random?
It is a pseudo random number generator. All pseudo random number generators create the same sequence until the "seed" is changed.
@@GaryExplains Groovy, thanks.
I'm so confused how cmake work on raspberry pi pico
It doesn't "work" on the pico
You cross compile code on your PC and then flash the binaries on the pico
@@h_pranav_nayak its working when I trying flash the led, but I'm so confused how to setup cmakelist.txt for my environment, actually i want someone teaching how cmakelist.txt work on raspberry pi pico, how to setup the directory, i have many of problem how to setup the lib on cmakelist...I ready done to add lib on cmakelist code, but when try to running, the error showing "lib not found", and i try search use "locate lib" it shows the existence of the library
What was random.h? You included it, but never showed it.
I do show it, but later in the video when I demonstrate what the build system does when you change a files it is just a declaration for the random function. 1 line of code.
@@GaryExplains Thanks. I saw when I finished the video. I just initially got the compile error for the missing file, but it was easy enough to dummy something in until you exposed it.
Nice Rush video length
Can you possibly cram more ads in your videos?
Eh? I don't cram in the ads, Google does that and it is automatic. Yes, I enable ads, but after that it is all Google, not me.
@@GaryExplains I had to see a dozen ads in a 20 min video, stopping every couple of minutes, ok that you deserve a revenue for your work, but over a certain limit I'm no longer motivated in following a youtuber.
I understand. But as I said it is Google that picks how many ads are shown and where. Not me. Remember that Google gets a much larger fraction of the advert revenue than the video creator. Your beef is with Google, not me.
Thanks for this series Gary. A request. Please do a video on how to control all the make flags using cmake. For instance, I hate that cmake always hides the real gcc compile commands. I absolutely loathe that. I want to see the 1000 character long actual gcc commands as they are executed. How do I force cmake NOT to hide that?. It builds this complex structure of hierarchical Makefiles, and I never know how any code is actually being built. The options to cmake are where all the utility lies. As someone who grew up on make, it is incredibly frustrating not to be able to see what is happening when I compile a project.
There is an option to control CMake's verbosity (and of course the verbosity of the output it produces. I don't remember what it is, as I only tried it once and didn't really see the benefit over a mostly silent build that only shows warnings and errors (or in my usual project setup only errors since I treat warnings as errors and set my compiler to nag a lot). I found it easily when I googled for it. So if you prefer to see tons of text scroll by, CMake has got you covered. 😉 I am not saying that either of the two is better, I think this is mostly a matter of personal preference.
FOLKS THIS IS THE VIDEO, DONT GO SEARCHING ANYMORE.
fatal error: 'random.h' file not found
I have main.c and random.c in the directory
Ether I run this command - gcc -c main.c
Or this command - gcc -o main main.c random.c -lm
Both are giving same error.
You need random.h as well. I show it at one point in the video. It is just a header for that random function.
The best book to learn CMake is “Professional CMake by Craig Scott”.
"Professional" and "CMake" should never go together is one sentence :)
@@robby3467 apparently it is the best book with free new editions 😌
@@robby3467Better than Autotools hell though, however bad the CMake language itself is. I'd use Meson for builds if I could, if only because it's less crufty.
CMake is one of the reasons I now use Rust
CMake sucks, right? We found it always filled the source tree with absolute paths. There should be a way to create relative paths but could never find anything that worked.
I went Rust and never looked back
I tried Rust and I did look back.
Sorry to be so blunt, but CMake is an absolute piece of garbage (in my opinion). I've spent too many hours, if not days, tearing my hair out or turning it grey, trying to beat CMake into submission. Worst piece of build tooling I've had the misfortune of meeting in over 25 years of software development. It never works out of the box. It's a piece of crap. Even watching this video... I feel the stress. Sorry, but I just had to vent. Is this really the best the open source community can do? It's still an archaic process. This is 2024.
That said, you provided a nice overview.
Don’t agree at all. CMake is an excellent build tool compared to alternatives like GNU autotools. The CMake team works with the major C++ compiler teams and Microsoft’s cross platform tools in Visual Studio rely on CMake. They also have a package manager vcpkg which integrates with CMake. That said, in my view the best IDE for working with CMake is CLion from JetBrains. If you wish to avoid hair loss it is the best option, the integrated CMake debugger is excellent.
What's the alternative, do you use Meson? Can you give a specific example where Meson, or another system, works better for you than Cmake? (I have no experience with Meson and not much with Cmake.)
Can you please provide reasons of why you think this way?
For one i see people complain about it's scripting sytanx, and even while its true, it's not a huge deal for me; what else do you think is wrong with cmake?
You haven't been subjected to the hell that is GNU Autotools, then?
Cmake is such a pathetic messy build tool. No wonder a lot of beginners get overwhelmed when trying to learn C++ and they just go for much more newer better fleshed out languages with far much better build tools.
We have to be thankful though because the languages that came after learned from mistakes made by the earlier ones.
I will unsubscribe if you don't change the "demo"-intermission jingle! I would be deaf if wearing headphones. Super loud on my laptop speakers. Otherwise, good cover of basics! liked!
🙏👍
Great explanation. Thank you