Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Purpose Behind CMake

Tags:

build

cmake

I am trying to understand the purpose behind CMake. Why it is designed like it is right now. Here are some questions I would like to have answered.

  • Why does cmake generate makefiles instead of just building the project?
  • Why are cmake files a series of commands and not just configuration files eg: ini/xml/yaml
  • What are the commands that I write into the CMakeLists.txt supposed to do? Just calling the compiler would be too easy I guess
  • In which order am I supposed to do the commands?
  • Is everything case insensitive? Can I write everything lower case?
  • Why do tutorials advise me to list every source file explicitly?
  • How do I structure my CMakeLists.txt to keep it short and simple to maintain. Every file I looked up in real projects looked very cluttered.
like image 549
Arne Avatar asked Sep 09 '14 18:09

Arne


People also ask

Is CMake widely used?

Our brief research showed that CMake and 'make' were the most popular cross-platform tools, having ~30% of users each, while both Autotools and qmake had less than 7% of users.

How do CMakeLists TXT work?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

How make and CMake works?

The build process with CMake is straightforward: configuration files, called CMakeLists. txt files, are placed in the source directories and are then used to create standard build files. It can generate makefiles for many platforms and IDEs including Unix, Windows, Mac OS X, MSVC, Cygwin, MinGW and Xcode.


1 Answers

It can be difficult to understand how CMake works. I'll do my best to briefly answer some of the questions you posted.

Why does cmake generate makefiles instead of just building the project?

CMake is a cross-platform make system and is compiler independent. It doesn't generate just make build systems, but also generates Visual Studio solution files. With the same CMakeList.txt file someone can easily create a windows build (Visual Studio) or a Linux build (g++).

Why are cmake files [a series of] commands and not just configuration files eg: ini/xml/yaml

Compilers don't use a universal configuration, so CMake must adapt for the target compiler.

CMake files are a series of commands to generate the appropriate configuration for the target compiler.

What are the commands that I write into the CMakeLists.txt supposed to do? Just calling the compiler would be too easy I guess

They are suppose to generate the appropriate configuration for the target compiler, and they will if written correctly.

In which order am I supposed to do the commands?

In the order that matters, if the order matters at all. Yes it's a vague answer, but it depends on the project. For example the include() command will be used to add additional cmake files, and if you include them in the wrong order, it can break generation of the build system.

The first command in your cmake file must be the minimum required version with the latest version of CMake (3.0.1). From there it depends. :)

For example this command will not work with versions of CMake less than 2.6. cmake_minimum_required (VERSION 2.6)

See some of the tutorial links at the end of this answer.

Is everything case insensitive? Can I write everything lower case?

As stated on the CMake wiki's language syntax page, all of the commands are case insensitive, but you must consider case for contents passed to a command.

Why do tutorials advise me to list every source file explicitly?

Because you have to list every source file explicitly. This can be done inside a cmake file or a separate file list that is referenced in the cmake file. Please see Antonio's answer for a caveat of using a separate file list.

CMake does provide a command aux_source_directory which collects the names of all the source files in a specified directory and stores the list to a variable, but it should only be used for "generated" source files for the same reason mentioned in Antonio's answer.

How do I structure my CMakeLists.txt to keep it short and simple to maintain. Every file I looked up in real projects looked very cluttered.

It's very easy to clutter a CMake file, and one way to help with this is to use .cmake files that you reference using the include command.

A good resource (although still under development) can be found on the CMake about page.

It's broken up into several sections:

  1. Overview
  2. Statistics (under development)
  3. Participants
  4. Documentation
  5. License
  6. Success Stories
  7. Publications
  8. News

Tutorials

CMake Tutorial - From the official CMake documentation page

Getting Started With CMake - A tutorial that uses CMake with gcc.

Using CMake To Build QT Projects - A tutorial that uses CMake with QT.

like image 148
jmstoker Avatar answered Sep 27 '22 18:09

jmstoker