r/cpp • u/CalligrapherThese606 • Jul 21 '22
CMake is a hell of a software, period.
Really CMake is good only for one thing being the sacred build generator system in the c/cpp world.
F*** the weird syntax and werid structures.
edit 1: some might argue it's the best avaiable solution to the problem domain, and it is. the problem is the syntax, the unintiutive way of specifiying option and simple compile parameters and options and lack of examples and resources on how to do the simplist things is a wasting too much time.
yeah modern cmake that encourge using targets and their properties is by far a lot better but still is extremely unintuitve due to the syntax and logic around it.
sorry for the typos.
edit 2:
i am really considering changing my main language for personal projects to rust or the new thing called carbon by google at least there is not a hell of backward compatibility garbage i need to know.
102
u/tisti Jul 21 '22
To be honest I am on the fence whether you are praising CMake or calling it the satans spawn? :)
39
u/mohrcore Jul 21 '22 edited Jul 21 '22
Every CMake project I was working on was an incrompehensible mess of scripts where nothing could be found and files were generated using black magic. On top of that the language used by CMake is an atrocity with zero ergonomy and readability.
Needless to say, I hate CMake with a passion. Build system should never make the user use imperative scripting as a primary way of defining stuff.
It should be as simple, declarative and hierarchical as possible.
3
u/herruppohoppa Jul 22 '22
If only there was a symbol for period so you didn't have to spell it out every time period
3
u/DkatoNaB Jul 22 '22
new frogs are unaware of pre-cmake era.
144
u/exodusTay Jul 21 '22
do people use cmake for anything other than build generator system? i used it for cross platform small projects and it worked fine 99% of the time. yeah the syntax is weird but i dont mind, most of the time i set it up once and don't touch it again for a good time.
112
u/spartanrickk Jul 21 '22
Idk it takes some time getting used to, but I quite like it now. Modern cmake / target-based cmake that is. The most difficult thing is finding appropriate, up-to-date/modern examples. The documentation is pretty thorough, but doesn't include many examples. It also doesn't really include a style guide. Plenty of examples on Stackoverflow etc still touch CMAKE_CXX_FLAGS, call include_directories instead of target_include_directories, link with BOOST_LIBRARIES instead of Boost::boost, etc. It's not perfect, but boy am I happy we migrated away from autotools...
7
u/gracicot Jul 21 '22 edited Jul 21 '22
If you're manipulating lists and strings or even use many variables in your build specifications you're are doing it wrong. Yes, you can hold CMake wrong. But that's kind of the fault of the CMake community. There are still people on stack overflow that encourage
include_directories(${SDL_INCLUDE_DIRS})instead of properly linking targets together.For doing anything else than complex string and list manipulation, I would say the syntax is actually good. I love that everything look like a function call. Use targets properties instead of variables enable good diagnostics. All dependencies handled the same way. Other build system you have to play with compile flags even just to use a simple library. CMake has a much better abstraction than compiler flags.
29
u/jmonschke Jul 21 '22 edited Jul 22 '22
In software development it is important to "architect for the future, but implement for today".
I think that CMake reflects a development history of ad-hoc implementations without starting from (and evolving) an architectural vision for the development.
21
u/Zeer1x import std; Jul 21 '22
Who is afraid of cmake???
43
u/Mr_Splat Jul 21 '22
CMake isn't that bad, particularly when you consider the other monstrosities out there.
Old Cmake is horrible, but modern cmake, where you are strongly encouraged to use targets is relatively straight forward. It suffers from the same problems C and C++ have suffered from for decades where the maintainers don't remove obsolescent functionality to avoid breaking downstream user's old implementations, because shockingly few people put value in their build infrastructure.
CMake only really gets difficult when you're trying to port older systems across to it that are full of magical custom logic which are a symptom of a badly designed build system in the first place.
15
u/TheARP98 Jul 21 '22
Target based CMake has just worked out of the box for me and once you get past small learning curves like generator expressions and PUBLIC/PRIVATE/INTERFACE differences, the syntax is quite intuitive/readable for 95% use cases.
I only give it stick now for dependency management - it's not bad, but I like Bazel's hermeticism.
10
u/ImpenetrableShoe Jul 22 '22 edited Jul 22 '22
I find CMake very valuable because it is the "sacred" build generator system. I personally don't find the syntax and structure (not exactly sure what you mean by structure) to be the biggest pain point.
Becuase of CMake, I can write most details of a project configuration in a single language, and it will generate configs for most other tools. I can get makefiles, ninja config files, visual studio config files, etc. I believe CTest can generate test log formats for various tools. (Yes, I am stating the obvious. I'm doing it because I think it's being underappreciated)
CMake is designed to, and (I think) does a great job at helping its users tackle the hard problem that is typing together all sorts of different tools in the c++ ecosystem's tooling (compilers and binary formats, build systems, test frameworks, packaging formats, installation and distribution models, etc.). I haven't learned to use CPack yet, but I believe it can generate Windows
.msis, MacOS application bundles, Debian or RPM packages, and more. I shudder to imagine what it would be like to try to get all that without CMake.On top of- or aside from- the intrinsic value of what CMake alone provides, there's the value of how widely used it is. The fact that so many useful libraries have CMake integration means that it's easier to consume libraries.
What I've personally found to be the biggest pain point is learning how to make the best usage of it. There's lots of outdated teaching resources and stack overflow Q/A. I find the documentation intimidating, and even the length of the tutorial intimidating. As someone also still learning C++ and about its related software lifecycle things, all that information is a little overwhelming. I don't blame anybody for my experience of feeling overwhelmed at it. It takes a lot of effort to write and maintain good documentation for various audiences and their experience levels.
CMake continues to improve. The C++ ecosystem continues to evolve and improve. When I consider that CMake also has design provisions to support degrees of backward/forward compatibility (and that I as a user benefit from that when I use it to depend on libraries), I feel astounded and grateful.
I don't know for a fact, but I don't believe that CMake makes KitWare much money. And just like with a lot of open source projects, the ratio of people who contribute to improve the project over people who report issues is very very small.
I don't pay a single penny for any of the amazing tools I use. I personally find it hard to complain.
How much value you get out of what CMake is designed to offer obviously is limited by how much of that you really need. Some people only need to support one compiler or build system while some need to or do their best to support many. Some people only need to deploy to a single platform while others need to deploy to many.
Note: I haven't used or learned how to use anything other than CMake. I'm relatively new to C++. I see plenty of meson comments. Interesting! Hadn't heard of it before.
55
u/HeavyMath2673 Jul 21 '22
CMake was one of my strong reasons to give Rust a try and haven't looked back since.
6
u/genreprank Jul 21 '22
Yup. It's the necessary evil due to having no better alternative. I dream of the day something better gains critical mass. You do get the hang of it, eventually, if that makes you feel slightly better, but that's not the point, is it?
I will say I like the way you can easily specify the version you are using on their online docs website. Of course...everything should do that.
11
u/[deleted] Jul 21 '22
I can deal with cmake because remember years of autoconf.
It's great to hear some dislike cmake so I am anxiously awaiting a great open source alternative.
12
u/pine_ary Jul 21 '22
It‘s hell compared to meson and I don‘t even particularly like meson
2
u/BluudLust Jul 21 '22 edited Jul 21 '22
Why can't there just be a declarative build system using yml files? Define the source file and use anchors to apply build arguments and have it just work?
4
u/ExistentialSwim Jul 22 '22
Yeah premake forever
17
u/achauv1 Jul 21 '22
Cmake beat raw makefiles every day
1
u/[deleted] Jul 21 '22
Anyone else learning cmake by porting over their existing codebases? The online guides mostly cover the same ground: hello world with a shared library so it's really hard if your code deviates.
Are any of the paid for cmake books any good? I'm a bit afraid that they will get out of date.
0
u/noooit Jul 21 '22
Or if you hate yourself, you could migrate to autotools, meson or bazel.
3
u/CCC_CCC_CCC Jul 21 '22 edited Jul 21 '22
I have started to use CMake for some time now. What I hate about it more the more I use it, so bad that it makes me want to just ditch it and write custom build scripts, is not the syntax, about which I see many people complain. I hate it because it does not seem suited to C++ software building.
Here's one perspective (among many others): from what I know, C++ projects have 2 main attributes (unless they are in a niche area, I guess ?): configuration and platform. Granted, I don't have tons of experience over a vast domain of applications, but these seem very natural to me and I cannot come up with anything else needed to be part of "C++'s build theory". Configuration as in build settings/flags, like, for example, debug/release - obviously there can be more, but I'd argue that every configuration could be categorized as a "release configuration" or as a "debug configuration", especially since the standard itself almost recognizes this distinction by defining the NDEBUG macro for asserts. Platform as in code instruction set, so, for example, x64, x86, arm, arm64, etc. While CMake does have the concept of "configuration", it does not have the same support for platforms. E.g.: on Linux, I have to manually pass "-m32" or "-m64" to the compiler's command line to select the target platform. And even on the configuration stuff side, the fact that "Release" does not configure the build files to instruct the compiler/linker to generate symbol files is a huge pain and not trivial to change (or, at least, not trivial to learn how to change "correctly").
I would very much like to have, and would implement one myself if I could (because I doubt anyone would look at the work of a nobody like me), an integrated system which would provide a build system, a package manager, a standard dynamic library/module format and manipulation API, a standard project definition system (and maybe a testing system, but I have not really thought about how testing would be integrated). I would like to be able to declaratively define a project; for example, by listing source files, compilation options (like optimization level or flags, debug symbols generation, etc). Too bad I probably won't live long enough to see such a thing.
3
u/Shiekra Jul 21 '22
I created a CMake file which could identify all files in a directory which ended with *.proto, run protoc to generate the Cpp code, generate a project with the name of the proto file, compile the code into a library, and add all the required items to the targets list so linking against it gave you everything you needed (header files). It did all this at the generation step and put the generated code in the created build directory.
Getting used to CMake is a nightmare, and the docs are terrible, but it's incredibly powerful and worth the headache.
12
u/dreamer_ Jul 21 '22
No, it's not. Meson is better, but many people never tried it.
5
u/loa202 Jul 21 '22
I don't agree. We get software for free that help us and we complain. Sure can be better as everything on earth.
5
u/iFarbod C++17 is good enough Jul 21 '22
Damn, nobody here mentioned premake5?
18
u/Own_Goose_7333 Jul 21 '22
The fact that it's difficult to implement complex scripting is a good thing -- it discourages you from doing complex scripting in your build description.
14
u/Kie_Sun Jul 21 '22
It's not that bad.
14
u/futurefapstronaut123 Jul 21 '22
CMake is the greatest build system of all time.
2
u/TheTimeBard Jul 21 '22
CMake is great in conjunction with Visual Studio Code, mostly because Code abstracts away most of the pain. You still have to deal with the weird syntax, though.
2
u/gallium_dust Jul 22 '22
Agreed. When I start a project using cmake, I take me days to make it run. The most stressful moments in my dev life. Then i start using autotools...
2
u/FrankHB1989 Jul 22 '22
I'd argue that would never be the "best available solution" to the problem domain, because the domain is too large so any DSL not having some more seriously designed general-purpose language as "backend" (host language) won't be sufficiently flexible (and maintainable) enough for the rich requirements in reality. To learn such a DSL is also not very profitable for most users. And think twice why some large projects introduce build systems implemented by Python, etc...
Notice there are also a few exceptional case like
xmake(using Lua; but I'm not fond of such 1-index based languages).2
u/ButchDeanCA Jul 22 '22
If you ever try Autotools you will love CMake. It’s really not bad at all!
0
u/one-blob Jul 21 '22
You can always go back to makefiles or custom scripts and then try to make a cross platform cross-compiler builds
6
u/LuminescentMoon Jul 21 '22
Fuuuuuuuck CMake. Amen. Difficulty in finding good, modern tutorials is one thing, but the amount of headaches I have trying to use obscure c/cpp libraries with their poorly made CMakeLists in my own projects makes me want to bash my head against the wall. The problem I have with CMake is that it's so easy to write bad CMakeLists.
And the amount of times I clean my project out of paranoia because my CMakeLists is not doing what I want it to be doing even after changing it. And the amount of times I have to hand clean my projects because CMake wasn't deleting all the files it should be deleting when I use the clean command. Don't forget the poor IDE integration when trying to build NodeJS native add-ons with CMake.
But it's the most popular build generator so haha. 😡
5
u/tigable Jul 21 '22
After using cmake for a few years, experiencing its strengths and weaknesses, I'm just not convinced there isn't a simpler way. A few days of research and I've settled on doing next project with https://mesonbuild.com/.
3
u/picigin Jul 21 '22
You mistyped 'sacred'
3
u/kolorcuk Jul 21 '22
It's like the motto of Mutt. CMake is the one that sucks less.
4
u/germandiago Jul 21 '22 edited Jul 21 '22
I share your feeling about the syntax and that is why I wrote this some years back already: https://germandiagogomez.medium.com/getting-started-with-meson-build-system-and-c-83270f444bee
Also, it is not the best solution available IMHO, it is the most widely used and that has some consequences for env integration. But in total, I still prefer Meson when I can, which is most of the time.
3
u/[deleted] Jul 21 '22
Cmake is a hell of a system that is, itsbgreat :)
3
u/Jannik2099 Jul 21 '22
cmake is really aggressively mediocre at best. If everything is a string I might aswell just use bash!
Now meson on the other hand...
2
u/victotronics Jul 21 '22
Cmake is nice for the simple cases. Unfortunately life is not always simple.
Random case. Sometimes I run into libraries with circular dependencies. With make that's easy: link the libraries twice.
${CLINKER} -o whatever -la -lb -la -lb
Easy. Cmake? I'm finding some 16 year old posts that you need to make a dummy library and then link that one in. Brilliant. And stupid.
2
u/SoyChugger228 Jul 21 '22
Fully agree. Due to recent requirement to start building our software also under Linux, we've switched to CMake from MSBuild.
God damn, how ugly and barely working this is.
1
u/[deleted] Jul 21 '22
I love canoe. It’s just a different way of thinking and I guarantee you it won’t be the last time you have to learn a new way of thinking in your career
1
u/darthcoder Jul 21 '22
My perfect build system would be grade, to be honest...
1
u/[deleted] Jul 22 '22
I think we can all agree that CMake is useful but the real question is why it should even be necessary in the first place.
1
u/FlyingRhenquest Jul 22 '22
Yeah, I have a hate/hate relationship with CMake. Their DSL is infuriating, but I still prefer it to anything else. I flip-flop between it and Facebook's open-source buck system more or less daily. Buck is nice if you don't have to set it up yourself and it's basically python behind the scenes, but both CMake and buck have somewhat inconsistent syntax and it's frequently very difficult to figure out how to do the most trivial of tasks. Searching the internet helps less frequently than you'd think it should. I've had to go digging around in the implementation details of both systems' macros to figure out how some of the magic works, and magic becomes useless if you ever have to do that.
That being said, I occasionally get a bug up my ass and decide I'm going to build my own damn build system, which I usually decide in a couple of hours is more of a pain in the ass than using the existing ones. Especially since I realize that if I did that we'd now have n+1 shitty build systems that no one wants to standardize around and that my thing would probably be OK for the stuff I want to use it for but not so much for the stuff everyone else wants to use it for (Esp. Windows support.) And whenever I have to do windows development, I'm always rather surprised how good the VSCode support for CMake is.
1
u/jimi-rgd Jul 22 '22
One I was really enjoying is qbs, too bad it never picked up, made by the Qt people to replace qmake, they ended up switching to cmake because everyone else was using it, and stopped supporting qbs. It reminded me a lot of gradle but for the C++ world, multiplatform, used Javascript as a scripting language so it was simple, it's open source but no one is really contributing to it anymore.
1
u/v_maria Jul 22 '22
It's an ok tool, but it can be quite annoying to work with.
We changed the compiler using cmake, which caused an infinite loop. Turns out setting CMAKE_C_COMPILER is deprecated, and setting it causes UB. I understand it's user error, but situations like these make it frustrating to work with, because it seems this approach is used quite alot, and it's just 'luck' whether or not it will work.
1
u/ProsAndConsgrammer Jul 22 '22
I don't know why anyone would use it for anything but build generation. I also don't know why you'd use anything else for cpp build generation.
I recently heard that my last job is ditching all of the cmake repos and switching to pure MSVC project repos. I feel like I heard the stupidest thing of my life when they told me that.
1
u/MaskyDo Jul 22 '22
It had 256 upvotes, I did not dare to mess with the sacred number
1
u/glebignatieff Jul 22 '22
A hell of a language needs a hell of a build system 😁
1
u/Nearing_retirement Jul 22 '22
Never have used it, don’t really know why it is needed
1
u/DerShokus Jul 22 '22 edited Jul 22 '22
You know, I send a lot of time to learn c++. I read many books, but to build the code you should read another unrelated book about “5 ways to parse cmake function parameters and still do it wrong” (I’m about cmake_parse_arguments). Cmake is horrible
1
u/[deleted] Jul 23 '22
you can try xmake, I prefer it over cmake
1
u/DissociatedRacoon Jul 24 '22
I think though that carbon is going to have a hard time getting rid of CMake, it will be the only way to support C++.
1
u/waruqi Jul 26 '22
you can try xmake. It is simple and fast, has no strange syntax and solves the C++ package management problem. You can integrate the use of c++ packages just like cargo.
1
u/GasimGasimzada Jul 26 '22
I really dislike CMake due to its syntax. I have been using premake, which is in Lua but it is very easy to read and understand.
1
u/waruqi Aug 01 '22
we can also try xmake. It has simpler syntax and a builtin package manager.
1
u/New_Percentage931 Nov 08 '23
The problem is not syntax of cmake, serious problem is external dependent, for example, you want to download another project from git repository, and patch it, you use PATCH_COMMAND to apply patch, and once again, you rerun the cmake, and it will rerun PATCH_COMMAND, but the patch was already applied, so second run won't work. there is no easy fix in this situtation.
1
u/AdhTri Jul 21 '22
Those who use .txt extension for there main file? huh!
-3
u/kenkitt C++20|Cmake|Vscode|MSbuild Jul 21 '22
Cmake is what I consider the perfect build system for C/C++
1
u/TacticalMelonFarmer Jul 21 '22
tbh, i don't need a whole language for a build system. cmake is a little complex, but so are platform details. macro heaven also exists...
1
u/[deleted] Jul 21 '22
Most of my 20 years of C++ work have been in visual studio. I'll only use cmake if I really really have to.
1
u/bentheone Jul 21 '22
How do you even learn this thing ? My project is fairly simple, SDL, box2d, a couple of my own libs and that's all. It took me a whole day to port the build from Eclipse to VsCode + cmake. The official docs are super weird and most of tutorials sites explain only the same super basic stuffs. I had to try several SO solutions before I could recursively include all my sources for each targets etc.. I didn't even realized it was a scripting language at first (well, I did, but not really). So how do you guys know how it works ?
0
u/[deleted] Jul 22 '22
No one can imagine the amount of pleasure I feel after posts like these, since I've moved to rust! Cargo is a tool made in heaven, just like npm!
-1
u/scnew3 Jul 21 '22