What is CMake used for?

What is CMake used for?

CMake is an open-source, cross-platform tool. One that uses compiler and platform independent configuration files to generate native build tool files. Ones that are specific to your compiler and platform.

A G++ GitHub repo question in the context of `cmake`.

I have an open source project, Current ([1], [2]). Historically, built as a header-only library, via the good old `make` with a handwritten `Makefile`.

In the past two years `cmake`-based use cases of Current, as well of my other C++ projects, are growing. Holy wars aside, while I do not consider CMake the best tool, it has three valuable features that come “out of the box”: native Visual Studio support for Windows builds, ease of using CMake-defined targets in other CMake-built projects, and a quick way to run my code under a GUI debugger in Linux.

So, it seems like `cmake` is in my life for good.

Now, I also got accustomed with `FetchContent`.

So that my CMake-built projects can make use of other projects natively, with a single-command build. Again, to prevent holy wars:

I am fully aware `FetchContent` is by far not the only way to fetch and include other git repositories in one’s project. However, it has proven to be good enough for my needs so far.

Since Current is a header-only library, the easiest I’ve found is to just add its sources directory into the `-I` path. See [3] for an example.

The actual top-level directory into which `current` will become cloned by `FetchContent` is `${CMAKE_OUTPUT_DIR}/_deps/current-src`.

Now, I am wondering how to best handle the `#include` directories. Had `FetchContent` allowed me to configure the name of the cloned directory. I could set it to `current` (or `c5t`, its short name). Add the directory into which this `current` or `c5t` is cloned into `-I` via CMake’s `include_directories`. And be happy with `#include “current/port.h”` or `#include “c5t/port.h”`.

But with `current-src` the above solution does not look feasible.

And, since `current` is large enough, there is a realistic possibility that `port.h`, for example, will be part of another C++ dependency of a CMake-build project. So, this dir name collision is really an issue.

At this point I have three basic solutions in mind:

⒈ Find a way to configure / patch / fork the very `FetchContent` CMake module. In addition, make it possible to configure the name of the top-level directory into which the code becomes cloned.

⒉ Move all the code of Current into some `c5t/` top-level directory, right in the GitHub repo, and do the same for all my other projects. Strictly speaking, with CMake-built projects, generally, there’s an `src/` subdirectory anyway, so I might as well rename that `src` into the very project name,

⒊ Look for alternatives to `FetchContent`, or, quite frankly, write its trivial version myself. Cloning a repo at a specified tag/branch into a specified directory is not that hard, after all, and I’ve made myself relatively familiar with CMake lately.

A friend tells me moving all the code of Current under `c5t` just because `FetchContent` decides to clone the repo under `current-src` instead of `current` is a suboptimal idea. And I agree just don’t want to invest much time into this, and a simple `git mv`, however ugly, does solve the problem in a few minutes. An even cheaper alternative, of course, is to create a `c5t` repo, that will just include the `current` one as a git submodule in the `c5t` dir.

Back To News

(1) Nobel Prize Winning Economist & Stanford Professor Paul Romer on Hyperinflation & Protecting Science

What is CMake used for?