Advertisement

What is CMake and when should I consider using it?

Started by October 28, 2024 09:03 PM
3 comments, last by Alberth 2 weeks, 4 days ago

All I know about CMake is that it is a build tool with a scripting language that allows you to generate project files, but what does that mean exactly, what does it do, and how do I know if it's something I should use? I've been learning C++ for about a year making small programs in Visual Studio and I don't necessarily have plans to make a program that is cross platform which seems to be why you'd want to use CMake and also so that others can collaborate, but it still seems like something I'd want to be familiar with.

None

Cmake is for when you’ve got really complicated project-solution architecture. If all you’ve got are some CPP files, which is a straightforward compile, then Cmake might be overkill.

Advertisement

I've learned CMake and that's really handy to have strong knowledge of it. The benefits that I've got:

  1. I can extremely easily import ANY library I want, and if the library is uses CMake, I'm happy to see it;
  2. I can configure and build my project for any platform, so I can continue to work whenever I want. My laptop is with Linux and PC is with Windows;
  3. I'm able to export the release of my project just using a single command cmake --install;

But, I took some time to really get how to write an efficient and pretty CMake build script. I've spent around 2–3 months deeply investigating what's going on. And so on, it is not ideal. I've been fighting with the bug on version 3.20.0, and they've fixed it in 3.21.

I don't agree with @taby that it uses for complicated project-solution architecture. Furthermore, I used to use it and for me, it's harder to set the Visual Studio project up with all the libraries I need. And other developers like me will feel pain by working with your Visual Studio project. I don't use IDEs and happy to build, run and debug whenever I want.

I want to notice again. CMake is not ideal anyway. 99% project are easy to set up and maintain, but that fucking 1% took a year to set up. I've been rewriting the whole build system of the 10 yo published mobile game. There were just 6 separate project like XCode, Visual Studio and Android Studio.

None

steamdog said:
but what does that mean exactly, what does it do, and how do I know if it's something I should use?

You wrote a nice program and want to allow others to use it too.

In some way, they must arrive with a built executable from your sources. If it's all plain C++ from the ground up using only functions defined in the standard C++ libraries, that is not too difficult.

However, you may want to use a widget library to have a GUI front-end, or display graphics in a game-like way with eg SDL2, or something else outside standard C++. They must then also grab those libraries and install them.

They may however pick a different spot for putting them on their system, or eg get them pre-compiled from another source that also has different ideas about where to put the result.

For more fun, if you not only want to support Windows VS, but also different C++ compilers, or different OSes like Mac or Linux or … , then your configuration of how to compile is not going to work anymore.

CMake addresses these kinds of changes in a project. It knows about many OSes, it knows how to find installed libraries, it knows what scripts to generate for a different compiler, it knows where to install the result, etc etc.

In other words, CMake hides all those differences. Your users can run CMake on your code (including some CMake information what to combine and compile, etc), and it will handle all the install details for the above-like cases. After running CMake, it produces a script that will coimpile & install the built code. Your users can run that script, and it will make your program available at their system.

Advertisement