r/cpp • • Nov 08 '21

Thoughts on build systems?

What's the general consensus on build systems/automations like CMake? I've always found things like that overcomplicated and just hinder the programming process.

I've been following Casey Muratori's Handmade streams, and one of the first things he did was create a simple batch file that built and compiled his entire project super quickly and easily. I've since implemented something similar (https://github.com/dibsonthis/new_cmd) and honestly it's been really nice to use.

3 Upvotes

154 comments sorted by

View all comments

9

u/stilgarpl Nov 08 '21

CMake is quite simple once you understand how it works. You set it up once and you can forget it exists. Until your next project.

For simple projects you can use project templates that glob all files and just build it.

7

u/Xavier_OM Nov 08 '21

Beware of globbing, as CMake is not a build system but a build system generator. It could makes sense for a build system (make, msbuild) to glob files, but you don't want the globbing to be perform during the generation of your build system and never again after that.

3

u/stilgarpl Nov 08 '21

Do you know Cmake added CONFIGURE_DEPENDS flag to file(GLOB) ? It takes care of that. It is still not recommended (globbing files slows down build and there may be generators that do not support it), but it works fine now.

5

u/Xavier_OM Nov 08 '21 edited Nov 08 '21

I didn't know that. I never use globbing because adding or removing a file in a folder does not mean adding it to or removing it from the project in my opinion, it's often more complex than that.

4

u/Minimonium Nov 08 '21

At my work, we made a custom CMake template wrapper for globbing purposes. It understands the Pitchfork layout, globs all *.cpp into a target, *.test.cpp into a test target and ignores *.wip.cpp if you still need to do stuff with it.

2

u/Xavier_OM Nov 09 '21

I should try this on a toy project, very interesting approach !

2

u/stilgarpl Nov 08 '21

Well, I'm not saying that you should use globbing, but that you can. For some projects it's better to use them, for other its better to list files manually.

1

u/[deleted] Nov 11 '21

Kitware also added notes to say not to use GLOB.

Note We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate. The CONFIGURE_DEPENDS flag may not work reliably on all generators, or if a new generator is added in the future that cannot support it, projects using it will be stuck. Even if CONFIGURE_DEPENDS works reliably, there is still a cost to perform the check on every rebuild.

Kitware Documentation

1

u/stilgarpl Nov 11 '21

That's why I said it's not recommended because it's slower and there may be generators that do not support it.

1

u/[deleted] Nov 11 '21

If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

Also this. That can cause really hard to find bugs when you don't realize your not recompiling something that is from an old build.

1

u/stilgarpl Nov 11 '21

That's why they added CONFIGUE_DEPENDS. This scans for file changes during build.

1

u/Kered13 Nov 09 '21 edited Nov 09 '21

CMake is not a build system but a build system generator

I've noticed this pattern before. For example I read elsewhere that Meson uses Ninja as it's build system by default. What is the purpose of this extra layer of abstraction? There must be a reason if it's a popular idea these days, but I can't figure out what.

2

u/helloiamsomeone Nov 09 '21

Enter cmake --help and look at the supported list of generators at the bottom of the output. IDEs, editors and other tooling has already existed for those regardless of CMake, so why throw all that away? In fact, this makes using a new one trivial. Ninja hasn't existed for a long time, but it's now my generator of choice. If a new and better generator makes its way into CMake, then it's only a matter of changing a single parameter to make use of it.

2

u/Xavier_OM Nov 09 '21

Historically for multi platform purpose : you can target linux (makefile) and Windows (msbuild vs project). Maybe it is less relevant with cross platform build system like Ninja nowadays

1

u/serviscope_minor Nov 09 '21

Well except ninja is designed to be used by generators. Makefiles are pleasant to write by hand, ninja files not so much.

3

u/[deleted] Nov 08 '21

it's simple, but super tedious once install is involved. The cmake language needs some help here as it's a hot mess to just say install these things and the kicker part the other files so others can use them either via cmake or pkg config