0

I really don't understand what "Cmake" is, I know the building process which is consist of:

Preprocessor
Compiler
Assembler
Linker

but where is "Cmake" in this process? what is its role and why do we need it? I read that, it helps in compiler dependencies, but I don't get it. please help me to know more about it and thanks in advance.

Huster
  • 43
  • 2
  • Are there specific parts of the documentation you didn't understand? It might be useful to narrow down the scope of the question since CMake can end up being fairly complex. https://cmake.org/overview/ – Dave Newton Dec 09 '21 at 22:44
  • Actually I didn't understand the general idea, and that is all what I want to understand: the general idea. – Huster Dec 09 '21 at 22:46
  • Have you tried to read something on this topic? E.g. that question: https://stackoverflow.com/questions/40083642/why-do-we-need-cmake – Tsyvarev Dec 09 '21 at 22:46
  • Can I say that, it is a configuration tool helps us to choose on which compiler we want to build our source code file? – Huster Dec 09 '21 at 22:52
  • I recommend using Premake (https://premake.github.io/). It is MUCH better! – adam.hendry Dec 10 '21 at 19:26

3 Answers3

3

Cmake is a build system configuration generator. To understand what that is, you must first learn what a build system is (such as make, msbuild).

It's challenging to combine the tools that you've listed for a large project to produce multiple variations of multiple executables and or libraries. The complete build process may consist of a large number of differing commands that must be invoked in the correct order, and may each have a large number of options that must be passed to the tool. This is what a build system automates. It invokes those build tools with appropriate options as described in the configuration that you write for it. They also provide convenient features such as incremental re-compilation (checking whether a translation unit has already been compiled previously, avoiding re-compilation when the source hasn't been changed).

But there is a problem: There are many build systems each of which have their own configuration format, and those build systems are often specific to particular system. In order to compile a project on multiple differing systems (such as windows, linux, osx), you would have to write and maintain configuration for each. This is what a build system generator solves: You write a single configuration, and cmake configures your system specific build system, and you invoke that build system through cmake.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

CMake is a cross-platform build tool. The idea is to let you define how to build a C++ project at a higher level of abstraction than however you build on one particular platform.

The issue is that although C++, the language, is independent of platform, how you build on any particular platform is not. Say you are implementing an application and would like to support Windows, Linux, and MacOS. If you do not use CMake or something like it this means that you will have to separately maintain a Visual Studio solution file on Windows, a MAKE file on Linux (say), and an Xcode project file on the Mac -- and this assumes that your application has no third party dependencies. If it does have third party dependencies then handling those in a cross-platform manner is a whole other can of worms. You might use per-platform scripts to manage dependencies, for example.

The idea of CMake and similar tools is to solve this problem. You define how to build your project and find its dependencies once and use the CMake definition across platforms.

jwezorek
  • 8,592
  • 1
  • 29
  • 46
0

Like many build tools, it creates output objects based on input artifacts (e.g. by running a compiler). It only rebuilds an output object if...

  • if one of the input has been modified since last time the output object was built
  • if the build recipe for the output object has changed

This can be done with a classical Makefile also, but writing a Makefile that lists all dependencies for building an output object (including header files for instance) is not trivial.

scottbiker
  • 192
  • 1
  • 4