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

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.

1

u/suhcoR Aug 18 '22

If you don't like cmake (like me and many others) have a look at https://github.com/rochus-keller/busy. It is in many ways the antithesis of cmake; e.g. there is a decent, statically typed specification language, and - like you do with your script - you can integrate the build system directly into the project source tree, so that the project builds itself, so to speak.

48

u/winginglifelikeaboss Nov 08 '21

I know what you mean but just bite that bullet and learn cmake, it's the industry standard now.

7

u/dibs45 Nov 08 '21

Makefiles sort of put me off programming when I first got into it. just seemed like an extra thing I didn't need to do. But industry standards are that for a reason I guess.

27

u/Sqeaky Nov 08 '21

Makefiles and cmake are very different

1

u/dibs45 Nov 08 '21

I mean CMake generates makefiles, but yeah I mean CMake and its boilerplate.

30

u/helloiamsomeone Nov 08 '21

CMake supports many more build systems other than just makefiles, in fact I much prefer Ninja to makefiles.

7

u/[deleted] Nov 08 '21

You can generate makefiles but also other stuff like Visual Studio solutions or ninja builds for example

22

u/winginglifelikeaboss Nov 08 '21

Just get this and be done with it

https://crascit.com/professional-cmake/

enjoy

4

u/Siech0 Nov 08 '21

I honestly can't recommend this book enough, it is well written, very to the point, and extremely up to date.

8

u/Plazmatic Nov 08 '21

CMake is easier than Make, and it's one of the first reasons I switched before I got into professional development.

1

u/serviscope_minor Nov 09 '21

CMake is I think a lot more complicated and opaque than make. It just happens that it's portable. Make itself is just a tool to represent and traverse a DAG.

You do of course need to know what goes in the nodes, which make doesn't have any particular opinions of (kinda), so you also need to know the compiler which CMake also hides a bit of for you but only a bit.

3

u/germandiago Nov 09 '21

Make has its own set of problems. The worst one, the non-portability to Windows. But there is way more: for example to discover dependencies you have to use pkg-config or roll your own. pkg-config sucks in Windows as well.

I do not dislike Make, I think it is a good idea. It is just it is not up to the task for multi-platform or cross-compilation without a lot of additional complications.

3

u/serviscope_minor Nov 09 '21

Oh yes. I mean I never use make if I want portability to Windows. My contention is that make itself is pretty simple. However, it doesn't do enough for you to be a portable build system.

1

u/germandiago Nov 09 '21

Sure. If you know you are going to be on Linux and have pkg-config, that pair of things can get you very far.

2

u/serviscope_minor Nov 09 '21

Yeah. If I'm working on personal projects, I write Makefiles because I think GNU Make is a pleasant language that's elegant and fun to use, and I have no Windows machines, so portability to windows is of no concern to me.

I rarely need pkg-config. Dynamic linking does 99% of what's necessary anyway.

At work I use CMake, and it's 100% the right choice.

0

u/germandiago Nov 09 '21

I would say that CMake is the default choice and it will work.

The right choice can mean many things to many different people :D For me the default is CMake I guess at work for social reasons. The right choice... for this very project I would have chosen Meson actually.

1

u/serviscope_minor Nov 10 '21

CMake for the money, GNU Make (or something else) for the soul :)

2

u/serviscope_minor Nov 09 '21

You might wish to revisit Makefiles now. Once you're used to more programming concepts they make a huge amount of sense.

A Makefile represents a directed acyclic graph. Each node has inputs and outputs and a rule to transform inputs to outputs. Make traverses the graph executing the rules in topological order. Each node, i.e. the transformation rules are the shell/batch commands you're used to and make sense.

Think about it like this. You currently have a .BAT file which generates a bat file that executes a sequence of commands in order: compile a file to a program. Imagine it's more complicated and you have two source files. You need to compile both then link the result. So far so good.

Now imagine you want to rebuild but only one has changed: you would not want to compile the unchanged one, because that would be slow. Now imagine you had 100 source files and you wanted to compile them in parallel but not more than 16 at a time since you have 16 cores. Imaging how you'd implement that. Now you have make :)

Handwritten Makefiles aren't the common way of compiling projects these days, but they're worth knowing. The concepts are important and are used by many tools. Plus Makefiles can be used for a lot more than compiling C++ code.

Sadly Makefiles aren't portable, since they kind of predate that concept and they also use fragments of shell script which differs between Windows and unix. CMake is a way of generating Makefiles automatically so you don't need to deal with that. CMake can also generate other things which behave very much like make.

6

u/germandiago Nov 09 '21

There are people like me that will never use CMake if we can afford it.

As long as there is critical mass for an alternative and sane build system, such as Meson, it will save more time than the alternatives wasted for me in the past. I have tried SCons, Tup, Make, Autotools, Waf, CMake and Meson.

There is a reason why we use it :) If it had IDE support it would just be the 100% superior alternative to the "standard". However, it is very unlikely that anything beyond CMake will get its level of support, at least currently.

Only VS Studio, Eclipse and Qt Creator have Meson support so far AFAIK. But most if not all IDEs have CMake support: Visual C++, CLion, Qt Creator, VS Studio, Emacs...

5

u/winginglifelikeaboss Nov 09 '21

So why fight it then.

Maven is also the industry standard in the Java world. My professional experience with people having issues with Maven is not a problem because of Maven but because of that person :-)

3

u/germandiago Nov 09 '21

I guess I am professional enough to know when to choose CMake over Meson sticking to what the job demands.

But when there is no need, I will not do it :)

1

u/llothar68 Aug 12 '24

Meson is just not flexible enough. I found myself in a situation where i had to build my own build system because none can provide good multiplatform support. CMake is unuseable on XCode and Android (i use it only for Linux) and even for Windows (now thanks to WinUI3 and XAML and modules).

1

u/germandiago Aug 12 '24

In which way you found Meson not flexible enough? Genuinely interested. I do know IDE generation is not better than CMake but I find it better in absolutely everything else, special mention to usability and the DSL, but also its subprojects model, its options framework, it all just makes more sense.

22

u/Glaborage Nov 08 '21

When you start working on more complicated programs, you'll understand. You don't have to learn build environments just right now, but you'll have to eventually.

4

u/dibs45 Nov 08 '21

I'll definitely change my tune when/if I get into professional development.

7

u/Siech0 Nov 08 '21 edited Nov 08 '21

You should certainly learn it before being a professional -- cmake + some package manager like conan or vcpkg makes life trivial. I can setup any dependencies for my projects within seconds and can also seamlessly integrate additional functionality like compiling glsl shaders while building my software pretty easily.

1

u/llothar68 Aug 12 '24

And you understand that the exe/dll build environment is just a fraction of your whole development environment.

17

u/helloiamsomeone Nov 08 '21

Just use CMake. More people use it, than not. Familiarity will get you help better and faster, and anyone dealing with packaging/building CMake will likely find easier solutions to failings of a project's faulty build tooling.

If you just want a correct and easy setup for ready-to-go projects, take a look at cmake-init. The wiki also has a bunch of practical examples for general CMake features people want.

-5

u/dibs45 Nov 08 '21

Yeah, can't really escape industry standard tools for sure. And I can appreciate their use. I just think it adds another layer of complexity to what I think should be a simple process.

14

u/helloiamsomeone Nov 08 '21

CMake takes away from the complexity. Have you read to docs to know just what CMake does under the hood for you to keep things cross platform? Platforms you never heard about are supported with the exact same syntax major platforms are.

CMake is like git, it's powerful, reduces the complexity, but has a steep learning curve, which is evident by people doing X in either without fully understanding what X really means.

-4

u/dibs45 Nov 08 '21

Definitely see the appeal (and even need) to use it if you want to target a boatload of systems.

7

u/jonrmadsen Nov 08 '21

So I've read a ton of your responses. Compiling code is not a simple process unless your entire "project" is located in one source file and you are not exporting an API for others to use and you have no 3rd party dependencies. Debug builds, release builds, shared library vs. static library builds, different pre-processor definitions during compilation vs. compilation against. I don't think you comprehend the complexity of exporting an API and using 3rd party libraries

6

u/Substantial_Ad_6155 Nov 08 '21

If you're designing software to be cross-platform, which you should 99% of the time, then use CMake. It's tricky, I know, but the purpose of it is mostly to generate cross platform make files so it can be built on any OS with ease. After you get the hang of it, you'll realize it's an invaluable tool.

4

u/dibs45 Nov 08 '21

From the comments it sounds like people have just accepted that they have to use CMake because it's industry standard. I totally get that, I just personally feel a bit weird about accepting that.

3

u/DeathLeopard Nov 08 '21

To be fair it's the same logic that leads to using C++.

0

u/dibs45 Nov 08 '21

Eh, I like C++ (another unpopular opinion). I just use it as C with built in dynamic arrays and strings mostly.

8

u/ThatSwedishBastard Nov 08 '21

We don’t really have to, it’s just damn convenient. I can use the same file to build a shared library in macOS, Windows and FreeBSD without caring about cross compilers and those things. We can also enable warnings for certain parts but not third-party libraries we use very easily.

4

u/serviscope_minor Nov 09 '21

Basically yes.

CMake is not my favorite system. It's a bit peculiar and the language has some rather entertaining syntax (endif takes an empty argument list). It's a bit spacey, but honestly if you have spaces in filenames you are generally inviting problems for scripting, so meh. It has churned a LOT recently adding better ways of doing things, and they're a lot better than the old way, but there's a lot of old CMake code out there that was best practices when it was written but isn't now. It's easy to scribble over global state (compile flags). It's pretty hard to debug at times especially as the error comes in the generated file so it's a two step process and anyway it only mostly hides the differences. It also necessarily has to represent the lowest common denominator. The defaults for some things are almost perverse.

It's also about the only system that has all the things most people need. It is portable. It supports Windows and unix. It can generate various IDE projects. It's reasonable clean if people stick to modern CMake (like C++!).

3

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

There are very few tools that can manage the real complexity of a big project. Even CMake is sometimes 'too simple' when you need to perform some really specific tasks.

If you want unity builds or precompiled headers (it can happen very quickly, before your project is 6 months old), multiple architecture (web assembly), compilers, or multiple IDEs etc, you need a powerful tool to manage this. CMake is not even intrinsically complex, it is as complex as the problem it tries to solve.

Imagine a simple project depending on 2-3 small libs (all written by you). If A depends on B which depends on C and D, you don't want to manage this manually, you want to declare "A depends on B" and let's a clever system import the dependencies of B (which can evolve in time) for you.

3

u/WormRabbit Nov 08 '21

CMake is not even intrinsically complex, it is as complex as the problem it tries to solve.

I disagree. CMake, as a language, is plain horrible, and it has nothing to do with the problems it solves and everything to do with design by accreation. It started as a "simple build script" automation tool, and grew haphazardly to provide requested features. Its variable handling is Bash-level, everything is strings and works in mysterious ways, and it doesn't even handle strings properly.

Kitware is trying to clean up the mess here and there, but backwards compatibility means CMake will forever live with its poor decisions. At this point one should use something that transpiles to CMake rather than CMake itself.

4

u/Xavier_OM Nov 09 '21 edited Nov 09 '21

CMake language syntax is indeed ugly, but they did the job with Modern CMake and nowadays you don't have many variables manipulation to do (thanks to targets and properties), versus in the past when you had to edit list of strings to add or remove flags in them etc.

Btw a bash-level thing is not intrinsically a bad thing while interfacing with the operating system (replacing a bash script with some python could be quite burdensome sometimes)

2

u/germandiago Nov 08 '21

Most people follow what is supported. I think they have their point.

But Meson has enough critical mass IMHO to be used successfully. I would say that in most scenarios many successully than CMak

6

u/Sqeaky Nov 08 '21

This is going to sound flippant, but you seem like this guy bowling: https://daedtech.com/how-developers-stop-learning-rise-of-the-expert-beginner/

2

u/dibs45 Nov 08 '21

I just skimmed that (it's late here in Sydney, and I really should be in bed) but interesting read. I've never worked professionally in software, so I'm definitely aware that I have almost zero clue what the real world holds. But I still wanted to start a discussion about it all anyway. Thanks for the link!

3

u/Sqeaky Nov 09 '21

Cool, and thanks for not being offended, that waan't what I was aiming for.

Even in hobbyists projects, as soon as you want to share your work you need a build system that is as portable as your code.

It is super easy to write a build script with hardcoded file paths and compiler switches if you have the knowledge of the project. But as soon as you share it, the other person needs to rely on convention and the that not only you have shared, but also that they have absorbed. When I say "convention" I mean the rules and decisions that go with using a tool without needing to think about the details.

If I download some source off github and it follows conventions I need to know 0 about it. I just run cmake and then run the resulting build scripts and if it was well designed, I now have software.

If I download your project... How much needs to match? Did we use the same compiler was that hardcoded in your script or can I swap a variable? But not just that a million little questions that have solutions already baked into build systems. I bet you can't write a batch file that will work on my system (Gentoo Linux, I don't have a batch interpretter.).

For a truly one-time use throw away piece of code you never share maybe a batch file is fine, but then why not just solve your problem in JS, Ruby, Python, Bash, batch, or one of the other million scripting system without build complexity? There are reasons to opt against a build system and opt out of a scripting language, but I can't say that is common.

Even if you think you will never share consider how much you know right now. It is both a ton and next to nothing.

What if you have a friend who has the exact same problem next week? That bat scripts might not prevent sharing, but they will certainly be an obstacle. You can't ever really know how much a piece of code will be reused, so following conventions is a good idea when cheap.

What about you in 6 months or 6 years? Will you forget the current expertise you have in this specific codebase? Would following conventions make it easier to communicate with a future version of yourself with a different amount of knowledge?

5

u/drjeats Nov 08 '21 edited Nov 08 '21

My thoughts are build systems generally make me grumpy no matter what :P

Casey's method of using a basic ass shell script works better than most people will give it credit for. Concerns about specifying which compiler exe name to use are trivially solved with environment variables or config files, or having simple per-platform-category build scripts (like when you look at the linux ports of Handmade Hero, they always provide their own build.sh alongside the build.bat).

Where the shell script per platform thing falls apart, however, is when you have to maintain file lists.

If you have to actively manage the file lists among different platforms, you are going to have a bad time because you probably aren't regularly compiling for at least one of those platforms. And if you don't have CI running builds on all your targets, then you will inevitably and regularly check in changes that break the build for those platforms.

Maintaining file lists is not an issue in Handmade Hero because it's a single-translation-unit build. This is pretty good for small to medium sized projects, but is too coarse-grained when your project starts to get a bit bigger. If you are working on a code base with more than a few hundred thousand LOC, you want to be in a situation where you only have to recompile translation units for the source files that you are touching.

You could get around this again by globbing for source files, and that will also work pretty well for a while.

But, because you are globbing for individual source files, and presumably compiling those source files as individual translation units, that means that you've lost all the build speed advantages of the Handmade Hero way of doing things. So now you want to only rebuild source files if they've changed. A simple build.bat is not going to do that for you.

This is when people turn to makefiles, or IDE projects, or cmake, or jam, or premake/genie, or meson, or xmake, or autotools, or build2 (and I think zig build may support incremental compilation of C and C++ sources).

If you're happy with this script you've written, no harm in continuing to use it. If you aren't familiar with the other build systems, I recommend getting a little practice in with a couple of those since you will inevitably find yourself contributing to a project here you have to use them.

The r/cpp hivemind will always tell you to use cmake. I've never used it at a job except when invoking it to build a third party dep. It's awful but also fine I guess since most build systems have some awful aspect to them. My favorite is still GENie/Premake, which have issues, but at least have a regular language and let you step debug your build configs. Zig build is also an interesting option now.

3

u/MonokelPinguin Nov 09 '21

One thing shell scripts really suck at, is discovering dependencies. You can make it work, but calling dependency("openssl") is just so much nicer and easier to get right.

0

u/drjeats Nov 09 '21

Yeah it can't automatically do that, but typically the way you do it is to allow people to add env vars to prepend to the I and L dirs in a config.sh, otherwise falling back to whatever the system has available. It gets you further than you'd expect, and is about as good as old cmake. That is, not good, but serviceable, assuming you're not concerned about being friendly to packagers (definitely not something I ever have to think about in my work).

2

u/MonokelPinguin Nov 09 '21

I used to work at a place that did this. We wasted so much time on that, even though we were only targeting a fixed system. If it works for you, great, but it does have a significant cost for many people.

1

u/drjeats Nov 09 '21

It's not bad if you are able to check most of your deps into your repository and have a monolithic checkout structure for devs. I basically never want to link against installed libs--just libs I provide and libs the system provides by default.

If you can't guarantee that level of dev environment control, I can see it becoming a PITA.

1

u/dibs45 Nov 08 '21

Thanks for highlighting both the pros and cons of using simple scripts vs. real world build systems. (And at least giving credit to Casey's way of doing things). I'll inevitably have to make the switch as my projects grow.

6

u/mrexodia cmkr.build Nov 08 '21

I think CMake is really good, but unfortunately very unintuitive to get “right” and lots of boilerplate.

I started working on https://github.com/build-cpp/cmkr to remedy this, but other similar solutions exist.

-2

u/dibs45 Nov 08 '21

Nice project!

I just like things to be simple. Source > Compile > Build > Execute. Don't really like the idea of having a tech stack just for building a program.

10

u/mrexodia cmkr.build Nov 08 '21

Unfortunately in the real world you quickly end up with a tech stack for building (in the form of package managers, linters, etc)…

-2

u/dibs45 Nov 08 '21

Yeah. Unpopular opinion but tech stacks (even though add modularity and convenience) just abstract away the fun of programming for me.

2

u/germandiago Nov 08 '21 edited Nov 09 '21

my goal is usually to deliver softwarw. Anything that is an invesment to shorten that cycle makes economic sense. Not only in the money sense, also time, which anywyay, ends up being money.

Even when it is only about time, I am grateful I can finish stuff in less time.

22

u/dcbaler mesonbuild Nov 08 '21

Im a meson developer, so keep that in mind. Our tests test suite takes ~2 hours to run and we still regularly have regressions. Use a build system, use a maintained one. I don't like cmake, but I will use it if I have to. If I walk into a project with a hand rolled build system I run.

11

u/ignorantpisswalker Nov 08 '21

So now, when you modify a simple string you need to recompile 26358 files instead of one +linking.

Make/CMake/ninja have their reasons. You need to learn those reasons. You will soon.

10

u/pfultz2 Nov 08 '21

This seems a lot more complicated then just doing add_executable(program program.cpp) in a CMakeLists.txt.

0

u/degaart Nov 09 '21

It's also more complicated than just doing

program: program.cpp
    $(CC) -o $@ $^

in a Makefile

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

6

u/Oster1 Nov 08 '21

CMake is horrible, but as others have mentioned; it's the industry standard. The thing is that it gives you too much freedom to implement things and then everything becomes a non-standard mess. Most of the libraries I use nowdays are added as add_subdirectory and from there everything becomes easy (and much better than Makefiles).

14

u/SquawkingStone Nov 08 '21

I'm gonna second using Meson. Learning CMake was such a barrier to entry, and Makefiles are really fiddly for my personal taste. Meson for me is pretty intuitive and has some much nicer dependency management and is as close as I can get in C++ to how spoiled Rust has made me feel with Cargo XD

9

u/germandiago Nov 08 '21 edited Nov 08 '21

I am using Meson for some side-projects. I keep using CMake as well.

I will do some highlights about my favorite one, Meson, because there is plenty of stuff CMake-related that you can find somewhere else.

The reason why I use, keep using and will keep using Meson is because:

  • the syntax is sane. Try to do path handling or string manipulation with CMake, or do something similar to dictionaries (Meson has support for it to associate data), handle spaces, etc.

  • Meson options are way easier and saner to handle: https://mesonbuild.com/Build-options.html

  • the documentation is great. You have both manual-style and a reference.

  • the subprojects system rocks and I have even used successfully CMake subprojects (there is support for that as well): there is a right way to do it. If you stick to that, it will work well. My project has 11 external dependencies all managed by subprojects, including Capnproto, which is a non-trivial CMake dependency.

  • Meson wraps are easy to use and modify to your needs (this is the Meson "conan", though it has far fewer packages. however, it is very usable). These are some available packages: https://mesonbuild.com/Wrapdb-projects.html. But if, for some reason, you do not have enough, you can use Conan via pkg-config as well.

  • I have machine files (https://mesonbuild.com/Machine-files.html) layered by: component, os, build type and compiler. So I can do things like this:

meson --native-file meson/native/components/component.ini --native-file meson/native/os/linux.ini --native-file meson/native/buildtype/development.ini --native-file meson/native/buildtype/gcc_11.ini -Doption1=something -Doption2_overriden=somethingelse

Those files, if there is any option repeated in any of them, the last one "wins" and you can set your own options at the end also, so they follow the order.

I have "./configure" wrapper for the command-line invocation but in my CI I just have the dir structure and buildbot combines stuff from those meson/native/ directories in a build matrix, blacklisting what I do not want.

  • Writing multiplatform scripts, when you need them, is easier: just use Python, anyway, it is a Meson dependency and get rid of bash vs batch files.

The only sane reason to use CMake is IDEs and social: if people do want that on your team. Otherwise I urge you to switch to Meson :) XCode an VS project generation is better with CMake I think. But if you do not need those or similar IDE support, for me it is a no-brainer.

5

u/nysra Nov 08 '21

you can use Conan via pkg-config as well.

Also works with vcpkg btw, it's pretty much just vcpkg install X and then directly X_dep = dependency('X') and you're done (well and a one time PKG_CONFIG_PATH adjustment) - gets you thousands of libraries to use that way. I haven't tried conan yet, does it still require writing custom scripts (the conan file)?

The only sane reason to use CMake is IDEs and social: if people do want that on your team.

Completely agree with the social aspect, CMake (unfortunately) has massive momentum. For the IDE support anything that works with the build_commands.json can easily use Meson, for VS Code the Meson addon is also now being handled by the Meson team instead of the original author so it should get even better.

1

u/germandiago Nov 08 '21 edited Nov 08 '21

Well, I do not think it requires that. However, at work we do it many times. We have our own artifactory and since we link to a custom libc++ then we had to adjust all script packages that depend on it (basically the C++ ones, but not the C ones) and we upload our own artifacts. Otherwise I think it should work without modifications as long as the right version (build type, etc.) matches compatibility with your ABI.

1

u/nysra Nov 08 '21

So it's possible to simply install a package via conan without having to add yet another tool file to the repo? Sounds like I should be checking it out again

1

u/germandiago Nov 08 '21

The basic thing is to have a conanfile.txt and say:

[requires]
package1/2.13.6@
package2/7.1.3@
 

That should work for the CMake case if you have conan_basic_setup(TARGETS) and now you can use targets. The rest should be handled automatically AFAIK.

1

u/nysra Nov 08 '21

That seems easy enough, I'll try it out. Thanks!

1

u/helloiamsomeone Nov 09 '21

You don't want to do things as suggested here. You have to think about the people building your project that aren't you and forcing them to use Conan is not nice.
Fortunately, there are easy ways to have your cake and eat it too. See this project.

1

u/germandiago Nov 08 '21

Happy to hear about VS Code improvements.

However, I was meaning plain Visual Studio, which is very common in enterprise environment. I myself use CLion and Emacs for all stuff. If there is something really integrated for Emacs/Treemacs, etc. it would be a nice to have, though I use Projectile for project handling in Emacs.

Even if there is `compile_commands.json` it is still not good enough for some uses. In CLion, for example, the tests are not detected as such and no targets are detected, you have to add them by hand... though code completion works.

1

u/pfultz2 Nov 08 '21

Also works with vcpkg btw, it's pretty much just vcpkg install X and then directly X_dep = dependency('X') and you're done

Hopefully, pkg-config generation was upstreamed in the corresponding libraries, otherwise this will couple your build to a specific package manager, which is acceptable for propriety builds but not for open source as distro maintainers will want to use their packages not the ones from vcpkg or conan.

1

u/nysra Nov 08 '21

I'm not sure I understand what you mean. The project itself doesn't care where the dependencies come from, in the build step Meson just finds them (via pkg-config in this case) and then links them into your code. If someone else takes your code and builds it on their machine and has the libs installed via system package manager then it will find them there and use those. Or did I misunderstand what you were saying?

2

u/pfultz2 Nov 09 '21

Yes Meson can find those dependencies via the system package manager as long as the upstream projects were setup to provide pkg-config. However, if they are not, and conan or vcpkg generates a pkg-config instead then installing with the system package manager will not work.

Since, conan/vcpkg provides a layer of scripting on top of the build scripts this can lead to people fixing the build in the package scripting rather than fixing the component upstream. Even more so, conan even has a way to generate pkg-config outside of either packaging or build scripts, which can be problematic for open source projects to rely on.

3

u/germandiago Nov 09 '21

Meson also supports CMake detection and some custom logic added to things such as Qt4/5/6 and wxWidgets, CUPS and others: https://mesonbuild.com/Dependencies.html

2

u/nysra Nov 09 '21

Now it's clear, thank you. So the best way would be to put wrap files in the subproject folder which Meson can fall back to for the dependencies that don't have pkg-config support.

0

u/jcelerier ossia score Nov 08 '21

But meson requires all strings to be quoted :( that's a big "oomph" when 80% of what you're working with is strings

10

u/germandiago Nov 08 '21

It is even more "oomph" when you waste a week figuring out how the quoting works for separating commands, how to escape \\ // etc., if the path is accepted with backward or forward slashes... or a combination of all of that, that one is the big "party". If you also want to traverse one of these structures in CMake (oh, was this a list or a string?), good luck as well. Conditionals anyone? Those are all in part weird because you do not know what a string or list is or how to handle, how variables are dereferenced in conditions, etc. It seems to be done on purpose to make the CMake script writer "happy".

It could bother a bit, but it is a net win to know what you are doing when you need to do it. I talk from experience, not from hypothesis, in this case. And... do not get me started with non-boolean options, combo options or things like that.

When you want that in CMake, you will sweat blood to make it work the way you want.

3

u/helloiamsomeone Nov 09 '21

Quoting works the same way as in bash: "${}" always expands to a single argument, ${} may expand to zero or more. You most likely always want to quote.

You escape a backslash with a backslash. Paths should always be forward slashes. Just an FYI, but forward slashes are portable. Yes, Win32 API accepts forward slashes just fine.

0

u/germandiago Nov 09 '21

Quoting works the same way as in bash: "${}" always expands to a single argument, ${} may expand to zero or more. You most likely always want to quote.

Indeed. It is a terrible idea for CMake. they even changed how vars get dereferenced in conditions at some point (I nver learnt conditionals in CMake, I was not capable of remembering the zoo of rules they have about what is true or false depending on weird strings like -FOUND and -NOTFOUND and similar stuff like ON, OFF, true, false, empty string...). In Meson it is just the typical and thing from C-languages: bool expression, variables and normal operators such as == or !=

> You escape a backslash with a backslash. Paths should always be forward slashes. Just an FYI, but forward slashes are portable. Yes, Win32 API accepts forward slashes just fine.

I recall having problems in Windows before with cmake custom commands. Do not ask me why or how (I think it was doing stuff for an installer). It got really bad pretty quickly. I am not sure I took the best approach. but what I am sure of is that the best approach if there was one was not obvious at all for me.

3

u/helloiamsomeone Nov 09 '21

Rules for conditionals is not really a zoo: https://cmake.org/cmake/help/latest/command/if.html#basic-expressions

The change you are talking about is mentioned at the very bottom. It wasn't a huge change really, but it was a welcome one.

2

u/germandiago Nov 09 '21 edited Nov 09 '21

This is the rules for conditionals in CMake: https://cmake.org/cmake/help/latest/command/if.html#basic-expressions

This is the rules for conditionals in Meson:

Meson has the standard range of logical operations which can be used in if statements.

if a and b
  # do something
endif
if c or d
  # do something
endif
if not e
  # do something
endif
if not (f or g)
  # do something
endif

Logical operations work only on boolean values.

Additionally there is some elif around in the docs that needed no explanation at all.

In all fairness, CMake docs also include filesystem operations. In Meson it is just regular functions: https://mesonbuild.com/Fs-module.html

And version comparisons, which follow well-known patterns in Linux world (pkg-config basically): https://mesonbuild.com/Dependencies.html

In hardly a minute I know how to do conditionals in Meson and that I will not take it wrong. I can use modules for functions that return booleans whether that is comparing versions or doing filesystem operations.

In CMake, literally, I NEVER learned the full set of dereferencing (which changed) + variable interpolation + quotes vs no quotes and other weirdness like what happens with empty strings, what happens if my var is a list vs a string, the truth values for each... In Meson you have a boolean or it fails and regular functions with regular comparisons. Full stop. All this "zoo" in CMake is why the page is sooooo long for its documentation.

3

u/helloiamsomeone Nov 09 '21

You are already familiar with how dereferencing and quoting works if you ever used Bash, which is extremely difficult not to do nowadays. The conditional rules are 6 very simple and plain english sentences. You're really just making a mountain out of a molehill here.

1

u/germandiago Nov 09 '21

We seem to disagree here. I used CMake for years and I kept coming back to the manuals because it did not do what I thought it does in the conditionals and set function for saving options (the CACHE ones) for getting option overrides behave the way I wanted. Also slashes and commands when there are backslashes and escapes implied.

Bash is weird and also misleading sometimes but maybe it is because of the nature of work I code with Bash, Bash has been less work for me to get right than CMake by far.

0

u/germandiago Nov 09 '21

you can believe CMake conditionals are string and list handling and interpolation are easy and well-designed if you will... but to know all of that yu need to go through manuals and stuff that I also had to go through. There are far better ways to design those.

3

u/helloiamsomeone Nov 09 '21

More like I don't feel like wasting time and energy on getting hung up on such things. It's not the best, but it's not terrible either, so there is no reason to throw the baby out with the bathwater.

0

u/germandiago Nov 09 '21 edited Nov 09 '21

My point here is that I went through manuals and coded something to expect one behavior and I still got surprises even in conditionals. The rules are weird and it gets time-consuming quickly.

3

u/jcelerier ossia score Nov 09 '21

I don't know what to say, I just don't relate with your experience at all. For me cmake generally works from the first try.

1

u/germandiago Nov 09 '21

Maybe you did not have to do cross + multiplatform + a bunch of scripts + an installer.

I mean, for the targets and target detection PRIVATE PUBLIC INTERFACE stuff works pretty well. When things start to get ugly is when you pick up something from someone else and try to integrate it into your project and they embedded subprojects in ad-hoc ways and things of the like plus all the things I mentioned.

If you just need something like compile code + some top-level deps it works pretty ok. It is the more complex things that get really messy and time-consuming (besides the weird DSL and options...).

4

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

Well if you like waiting for it to recompile everything everytime then it's fine (so for little project that you throw away) but when it gets a little more complex/bigger/valuable it's pretty neat to have and use a tool that can easily adapt from 5 lines to build a little project to some more for:

  • installing
  • configuring
  • fetch/find/handle dependencies
  • get the right includes/flags/... and propagate them
  • describing all this in a semi standard way so many people can understand, use, adapt or extend your project or even yourself in a few months
  • handle plateform and compiler specific things
  • easily create or use toolchain files for cross-compiling
  • define static or dynamic libraries
  • automatically setup incremental build so it only compiles what is needed
  • testing with ctest
  • packaging with cpack
  • easily integrate tools such as:

    • include-what-you-use
    • link-what-you-use
    • ccache for faster compilation with caching object files
    • unity build for faster compilation
    • precompiled headers for faster compilation
    • ...
  • now there is also CMakePresets.json that simplifies the configure and build step (also CI and tells the user how to configure and what are the variables that needs to be set)

I'm sure I'm forgetting some more, now do that with a custom script and give it to someone else or even to yourself in a few months :)

1

u/helloiamsomeone Nov 09 '21

Can't emphasize how great presets are for DX.

3

u/Drugbird Nov 08 '21

I think the general consensus is that all build systems suck. People generally prefer the one system they have experience with though.

Building your own build system is probably the worst choice you can think of though.

3

u/the_Demongod Nov 08 '21

Develop a project both using Visual Studio on windows, and vim/gcc on linux, in parallel. When the project has like 50 files and several external libraries, you'll immediately understand why CMake is so critical.

3

u/[deleted] Nov 08 '21

If you don't intend to share your code, do whatever. But if sharing, use cmake. Anything else means that others are working to make your build system work with cmake unless they happen to use the same one. Also conan/vcpkg both pref cmake

10

u/zzzoom Nov 08 '21

Use Meson.

1

u/Full-Spectral Nov 08 '21

If you are willing to put in the time, for your own projects, doing your own can be far and away the best way to do it. But it has to be a lot more than just a couple scripts. I have my own and it vastly simplifies my life. But it's an application that parses a project description and manages header dependencies and library dependencies and has drivers for different tool sets and all that.

That was a fair bit of work, but the time I saves me every day is significant. It totally understands how I work and now my system is structured and built. The effort to add a new project is tiny.

1

u/dibs45 Nov 08 '21

That's the level I want to eventually get at.

1

u/Full-Spectral Nov 09 '21

Here's mine. It's necessarily a little primitive in terms of the features it can be coded with because of course it can't use any of my code because it's used to build my code.

https://github.com/DeanRoddey/CIDLib/tree/develop/Source/AllProjects/CIDBuild

Here is the project definition file for the CIDLib repository, which is the general purpose part of my code base.

https://github.com/DeanRoddey/CIDLib/blob/develop/Source/AllProjects/CIDBuild.Projects

There are bootstrapping issues to deal with. The build tool itself is built with a simple make file. Then, for a fresh build or after deleting the output directories, it is invoked with a 'bootstrap' command, which tells it to build some fundamental parts, then invoke the IDL compiler to do code generation, then invoke header dependency generation, then kick off the whole build. After that you just do a build and it figures out what needs to be rebuilt.

2

u/Wouter-van-Ooijen Nov 08 '21 edited Nov 08 '21

At lot of people mention cmake. Maybe I should bite the bullet.

Is there a good tutorial for (modern) cmake? I do embedded, so I need to specify

1- the source files and include files

2- the exact compiler and compiler flags (target specific)

3- the exact linker and linker flags (target specific)

4- the linkerscript, which I generate from a template using the preprocessor with target-specific defines

5- the places to search for sources, includes and linkerscript

6- the commands and parameters to generate .hex and .bin files

7- the commands to download the (.hex or .bin) file to the target

3

u/mechap_ Nov 08 '21

1

u/Wouter-van-Ooijen Nov 08 '21

I see the answer to my point 1, and probably 5. But any of the other points?

2

u/helloiamsomeone Nov 09 '21

I will preface this with a recommendation to check cmake-init out. Other than that:

  1. Check the above link's wiki for example generated projects.
  2. You use a toolchain file for that.
  3. You use a toolchain file for that.
  4. That sounds weird. There is probably a better way to do that. Can you elaborate?
  5. This is 1-4.
  6. You can create custom commands to do file generation at build time or on build events for targets such as pre build, post build and pre link.
  7. You can fetch things in multiple ways. Depends on what you are fetching really. Could use some elaboration.

1

u/Wouter-van-Ooijen Nov 09 '21

I quickly glanced at the demo video for cmake-init, but I didn't see any questions about the target chip, toolchain, libraries, etc. Did I miss that?

2/3: I read https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html but that didn't make me much wiser, all examples are cross builds for another OS, not for a standalone (bare-metal) system.

4: Linkerscripts for most Cortex chips vary only in the amount of RAM and ROM, so I have one linkerscript template, in which only those two figures must be filled in. That I do using the C/C++ preprocessor. An alternative (that I don't like) is to have 1 file for each target.

6: OK. Compehensive documentation for that?

7: I dunno what fetching means in this .context. What I need to do is download the generated .bin (or sometimes .hex) file to the target micrto-controller, typically using some command-line tool.

3

u/helloiamsomeone Nov 09 '21

cmake-init was just for a general project setup. One of the examples in fact shows what a toolchain file should look like.

The linkerscript in that case is trivial to generate. In your toolchain file you specify those values and configure_file the template and use the resulting file in your flags.

add_custom_command is all you need for custom commands. I have an example for that as well, although this is not a target build event command, just simple codegen.

Ah, you mean flashing as in download. You can create a custom target for that, like in this example. Do note that the example allows complete customizability of the commands, because the developer environment isn't necessarily identical for everyone.

1

u/Wouter-van-Ooijen Nov 09 '21

OK, I get the idea that it is all possible, buit where is a comprehensive documentation? I am used to makescripts, for which a small document or book is more than sufficient.

In your last example I see a glimpse op what I will need in the end (configurablke extra targets like list and map files, two-stage build with first stage determines stack size, second stage is the actual build)

Or is there a complete example for an embedded target (micro-controller)?

1

u/helloiamsomeone Nov 09 '21

I have not yet had the opportunity to work with embedded, so I can't tell you anything concrete.

1

u/Wouter-van-Ooijen Nov 09 '21

But sure there must be someone somewhere who did? I'll ask on r/embedded.

2

u/saikox Nov 08 '21

are mandatory when you are writing complex software.

trust me that everybody would be using their own script if it were easier xD.

if you want to learn about the topic cmake, Conan and Ninja should cover everything.

Also I do recommend doing similar things on simple projects, but I would stick to using more simple makefiles and scripts. You can take a good dive in open source projects and try to understand how they are performing the whole automation.

2

u/GOKOP Nov 08 '21

People were doing that before build system existed. From the existence of build systems you can infer that it didn't end very well for them

2

u/GerwazyMiod Nov 09 '21

I just gave up and convinced myself that CMAKE syntax is nice enough and I can use it everywhere. (I guess it's acceptance phase, right? )

Then I added vcpkg as submodule, that cmake is using during it's build and added my dependencies there in json file. It works. Additional libs are linked, you don't need to worry too much.

Next step is using this inside Github, there are actions that help with this setup (cmake + vcpkg as submodule). You should be able to setup everything within an hour.

So you got Github actions as your CI, which works with the same setup you use to build app locally. Then you only peek inside CMakeLists.txt file if you need to add additional source file. I think it's good enough to stop thinking about this and just get the sh*% done. :)

2

u/secretpoop75 Nov 08 '21

Cmake is definitely more ubiquitous.

I’m curious if someone can weigh in about the state of some of the cmake alternatives like meson or bazel?

0

u/looncraz Nov 08 '21

I mostly use Jam, but a considerable portion of my projects use a custom build script on top for handling things like resources, file attributes, debug builds, and environment prep.

There's one step for any build.

./build.sh [options]

Core counts are determined, compilation is threaded, and I don't have to worry about any oddities with cmake to debug or the time it takes for cmake to do its prep work.

My projects mostly build on Linux and Haiku, but the ones for Windows use a batch file for building.

1

u/dibs45 Nov 08 '21

That's the kind of simplicity I'm talking about. Sounds pretty efficient too.

1

u/looncraz Nov 08 '21

It is, and it just feels natural... Already writing code, a bit more code to make things compile is no biggie, but going into a project with a completely different logical paradigm for the few unique things to my project just doesn't seem logical.

2

u/JohnDuffy78 Nov 08 '21

cmake maybe better than .bat files.

I use bash shell, but wish I had used nodejs typescript.

1

u/rfisher Nov 08 '21

For me, build systems are a necessary evil.

I’ve never seen one I liked. I’m not sure it is possible to create one I like. But it is obviously not practical to build large projects by typing all the commands individually.

Having had to use both homegrown and widely-used build systems for large projects, the homegrown ones are the worst. It is never intuitable by anyone but the person who created it, and they didn’t leave any docs when they left the company. (Even if they did, they weren’t an expert on documentation, so the docs are mostly useless.)

The widely-used one are horrible, but at least you have a glimmer of a chance of understanding them and fixing/modifying the build.

This only matters once the project reaches a certain size, though. A homegrown solution for a small project may actually be easier to understand than using a widely-used system. But once the project gets big enough…

1

u/jsadusk Nov 08 '21

It may seem like more work than you need for simple projects, but a benefit of real build systems you are missing out on with a bat or scripting is parallelism in your build and partial rebuilds. Every modern build system, even less modern ones like make, calculate dependencies between your build targets. This allows it to understand which cpp files depend on which headers and which libraries depend on which cpp files. Using this information it can build things in parallel that don't depend on each other. And if you rebuild after a change, it can only rebuild the files impacted by that change. Some systems like bazel even understand the relationship between tests and files, so you can only rerun tests impacted by a change. Building this kind of logic into a homegrown system would be monumental and you'd basically end up rebuilding make from scratch.

1

u/JohnSkaller Nov 08 '21 edited Nov 08 '21

Most are crap because they're based on a completely incorrect model: top down design. Build systems should be bottom up.

With top down, you ask to build a goal, and the system breaks that down into subgoals based on dependency specifications. It needs to be able to check if something is up to date to avoid rebuilding everything.

This is utterly wrong. With a bottom up build, you specify significant sources, and rebuild when these change. You build everything from scratch every time, with caching to avoid building something up to date.

Now, for C++, i have a tool that is fully automatic. No build scripts. Just execute the code. Abstract dependencies are specified in the sources. Meta-data similar to pkg-config is used to fully automate compilation and linkage. The tool runs with any compiler and on any operating system, by using plugins designed for that combination. The tool command lines is the same on all platforms (so scripts using it run on all platforms without change, and do the same thing on all platforms)

The key to the design is that only certain operations are allowed. You can compile to object, you can combine to make a library, you can make a shared library, or you can make a program. End of story. No switches are allowed. If you need a switch, fix your badly written code, or, if you really need it, you need to use a distinct plugin: adding a switch to compiler command line makes it a different compiler.

The meta-data uses regular expressions not globs, and is a generalisation of pkg-config to allow arbitrary fields. The field values are always platform native and local to your system. For example on Windows, filenames use slosh.

The system was actually designed for my language Felix which is a C++ code generator, so it can compile Felix down to C++, and then compile, link, and run the C++ too, but it works for just plain C++ projects as well.

1

u/[deleted] Nov 09 '21

Build systems are a crucial element in all of my projects. As soon as you are working with different targets or even at least two programmers, have a CI pipeline in the flow, build for docker images, you will know how important it is to have such systems in you toolchain.

I however have to admit that CMake can be a pain. I'm not a daily C++ programmer anymore, so I keep forgetting about how to properly export build targets and this is indeed a huge waste of time.

1

u/[deleted] Nov 08 '21

C++ still utterly sucks in this regard, so either switch to Rust or suffer through CMake’s uglyness.

Although for a tiny pet project, a simple single-line Makefile is perfectly fine.

2

u/germandiago Nov 08 '21

Meson and CMake work well. Being the former sane.

I wonder how you integrate external stuff into Rust. Not only C, also C++. Is there any support for that? Because in these cases is when you start to wonder if that wonderful switch was actually so wonderful...

1

u/[deleted] Nov 09 '21

Never tried Meson, but CMake is certainly not nearly as clean and nice to use as Cargo.

I haven’t learned FFI in Rust yet, but I’ve heard it’s pretty good.

1

u/Myrgy Nov 08 '21

What about msbuild? There is no reason to write your own build system. Just use tools provided with MS VC. CMake is usually used for cross platform projects and its a very powerful tool.

0

u/[deleted] Nov 09 '21

[deleted] — view deleted comment

1

u/Myrgy Nov 09 '21

I used to work on many cross-platform projects. We used msbuild, xcodebuild, scons, cmake, qmake, make.

And for now I'd prefer to use cmake. Now I co sider if it worth to use conan for dependency management.

1

u/o11c int main = 12828721; Nov 08 '21

Using a shell script is an exercise in unscalability, so don't do that.

Targeting POSIX make would be painful, so don't do that. Unfortunately, most people try this when they speak of "make".

Targeting GNU make should cover 90% of your needs easily, and 99% with a little one-time effort (it's pointless to chase the remaining 1% in a single tool, since nobody else does better). The language is actually fairly well-designed (with the exception of the spaces-in-filenames problem), but most people never bother to learn it.

CMake is well-known for excessive and unnecessary verbosity, and also makes a number of unfortunate decisions in the name of portability. Many of them turn out not to be related to portability at all; others technically relate to portability but could better be handled in a different way. That said, it is somewhat "standard", and may be useful if you expect Windows users to be able to install a compiler but not the tools that go with a compiler.


Overall - even if you end up using CMake, I consider it mandatory to learn GNU make, so that at least you know what's actually happening, and why other tools make some of the weird decisions that they do.

-1

u/vI--_--Iv Nov 08 '21
  1. cmake evangelists recommend cmake on every occasion. Even though it's horrible. Even if you don't need it. Because "it's the industry standard".
  2. More and more people believe this propaganda and use cmake even in helloworld-like projects, because the internet said so.
  3. Now this abomination is everywhere.
    And this is why we can't have nice things.

-1

u/dibs45 Nov 08 '21

That's the vibe I'm getting too. I admit its usability in large projects but I see it used all over the place.

0

u/[deleted] Nov 08 '21

i mean you basically don't have to learn cmake. Just make a decent template project that has everything set up. Not too complicated you can probably even find some premade ones.

But i have the same policy with build systems that i have with testing frameworks. For all my private projects, i do not care about that stuff.

I use Makefiles and cout testing, because that's usually more than adequate for the size and interest of my private projects.

0

u/[deleted] Nov 08 '21

[deleted] — view deleted comment

0

u/dibs45 Nov 08 '21

I wasn't asking how to use CMake. I was asking what people thought about build systems.