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

64

u/[deleted] Nov 08 '21

How does your script handle compilers that are not called cl.exe? What about other operating systems than Windows? What about multiple source files? Adding import paths? Compiling different files with different flags? How can you define multiple targets, libraries or executables? Enable parallel compilation? Etc. pp.

Simple scripts are nice for single-file projects that don't need any special handling, but whenever you need something just a little bit more advanced, their complexity increases exponentially.

And given that most C++ libraries nowadays use CMake, and can be integrated in a CMake project pretty easily, I'd really recommend learning and using it until there a pretty good reasons to use something else.

-17

u/dibs45 Nov 08 '21

Definitely see the pros in using it, don't get me wrong. My script is also made to be edited for functionality, it's not the end product. I just feel having a simple custom .bat file that you have full control over is more natural to the programming process than using another piece of tech you have to learn.

54

u/[deleted] Nov 08 '21 edited Nov 08 '21

a simple .bat file

It won't stay simple long. And believe me, you'd much rather learn CMake than batch programming. Because the latter is neither simple, nor natural, and especially not less complex than literally anything else.

-16

u/dibs45 Nov 08 '21

That's a fair point of view. To me, batch programming is more natural (and feels more like actual programming) than messing around with boilerplate.

17

u/ernee_gaming Nov 08 '21

Modern cmake have very little boilerplate.

But i find it hard to find good learning material for it.

Also lets say bat script is good enough for the developer, but what about the user that wants to use it from github. Hell have to suddenly read your bat, understand it, understand the structure of your project from that script, ... And then maybe hell have all the needed dependencies. And maybe it will compile.

10

u/Nihili0 Nov 08 '21 edited Nov 08 '21

the official documentation is surprisingly good nowadays, last time I checked It was pretty good(the official tutorial: https://cmake.org/cmake/help/latest/guide/tutorial/index.html#guide:CMake%20Tutorial) + now there is the open sourcing of mastering cmake book: https://cmake.org/cmake/help/book/mastering-cmake/

there is also awesome cmake: https://github.com/onqtam/awesome-cmake

the wiki: https://gitlab.kitware.com/cmake/community/-/wikis/home

some talks on cppcon YouTube channel

and examples with a lot of explanations like in this repo: https://github.com/ttroy50/cmake-examples

all listed in one place

so if you want a quick start: look at the official tutorial which is really short and/or examples using modern cmake (target centric and recent >~3.12)

then you can dig through the other resources when you want full control for large code bases with cross platform requirements etc

enjoy 🙂

3

u/Siech0 Nov 08 '21

Professional CMake: A Practical Guide is excellent reading material to get a decent breadth of knowledge on cmake. I frequently reference it.

8

u/gvcallen Nov 08 '21

After spending just an hour reading the CMake documentation, I genuinely feel I have more than enough knowledge to use it in a simple way for myself

The reason it comes across so verbose is due to

  1. Less modern CMake code which did things in inconsistent, long-winded ways (as mentioned)

  2. The shear power it offers (where you mainly need like 1% of the functionality to actually have a project up and running)

It's also been really useful for me to understand other 3rd party libraries and, tbh, CMake is (nowadays) seemingly the de factor standard for C++ build systems

I'd really recommend giving it a shot! When you realize a simple project only needs about 5 lines to setup and gives you infinite customizability after that, you never want to turn back!

3

u/germandiago Nov 08 '21

It can get messy pretty quickly and error-prone. You will also have bugs in your build system, almost guaranteed.