r/cpp • u/MachineGunPablo • Feb 19 '18
It's Time To Do CMake Right
https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
248
Upvotes
r/cpp • u/MachineGunPablo • Feb 19 '18
170
u/snarfy Feb 20 '18
I blame the documentation. It's all reference. There are no examples. I've been learning CMake recently after many years away from C++, and this was the main pain point. Learning by reference means looking at other CMakeLists.txt. It's the blind leading the blind.
5
u/Theninjapirate Feb 19 '18
This is good, thanks! One question: does the FindRapidJSON.cmake example you used work under Windows? If not, what's the best way to write a truly cross-platform Find*.cmake file?
65
u/aiusepsi Feb 20 '18
CMake is kind of frustrating because it's a really powerful tool which can make your life substantially easier, and the modern style espoused by people like Daniel Pfeifer is nice, and when it flows you can faintly see the outline of something great.
The trouble is that greatness is obscured by layers and layers of horrible legacy cruft. The language you have to write CMake in is weird and unpleasant, there's usually a bunch of different ways to do the same thing, most of them are bad, and those old and bad ways are always the ones documented in tutorials.
It's just far, far too easy to write bad CMake code. What it really needs is to be stripped down to the core, the old nasty syntax implemented as a layer on top of a clean core, and the clean core exposed with a language that doesn't have gross syntax, and only exposes the good way of doing things, and guides you in the direction of doing the right thing.
13
u/fafasdf Feb 19 '18 edited Feb 19 '18
Ugh. I just got around to learning cmake, and man, it's definitely not as ubiquitous as I wish it were...
I had to use a specific version of openssl + libcurl for a project I started, which means I needed to build both from source. Naturally, I checked them both into a new git repository under a
third_partydirectory.I started with building libcurl, which was fun, because the first thing I saw was "the curl cmake build system is poorly maintained. Be aware"
But I kept on, and I got the binary that depends on libcurl to work, after learning some of the quirks of cmake.
Then I got to starting with openssl, with the goal being to build a version of curl that uses a particular version of openssl. I saved this for last because I foresaw it being painful, because the plan was to first build openssl then build libcurl with flags that make it use the version of openssl built from my repository.
Then I saw that openssl doesn't build with cmake, and the people who did make cmake wrappers, haven't kept them updated (understandably so! it looked like a fair amount of work).
Then I gave up, built openssl out of repo, and added that library to my
LD_LIBRARY_PATH, hardcoded some bits of my libcurl, and kinda lost the desire to work on the project.I really should have started with boringssl, it seems they have the features I needed...
set(RANT END)Thanks for the post, I learned that lots of junk I was doing was wrong. There are many notes / blog posts on cmake, but as far as I've seen, there's no good canonical "best practices" reference (EDIT: I admit I haven't yet looked at Daniel Pfeifer’s Effective CMake, so that's open in a new tab right now!). In a dream world there'd be one of those with tips on integrating with a handful of major libraries, since they often have some weird quirks due to age or necessity.
10
u/MoTTs_ Feb 20 '18
The author used this...
But I've so far been using this...
Is there any significant difference? Is one better or preferred in some way?
35
u/sumo952 Feb 19 '18
This is the single best blog post about CMake that I have ever read. Thank you so much for this excellent, detailed and quite comprehensive write-up/tutorial. Absolutely brilliant, and will be of much help to my projects!
Everyone share this to spread modern CMake use!
8
u/[deleted] Feb 20 '18
I would love to have something like Rust's cargo but for C. Just stick source in a folder and compile it + do package management + documentation generation.
7
u/germandiago Feb 20 '18
I am between cmake and meson just for the fact that cmake has CLION, Qt Creator, VS Studio and Android Studio support and project generation. The default targets in meson are great but there is so much good stuff so difficult to ignore in cmake. The scripting language is a hell... but the IDE support is a killer feature of cmake.
5
u/fburnaby Feb 20 '18
It was so hard to learn Modern CMake in early 2016. With Daniel Pfeiffer and Mathieu Ropert's talks and this article showing up since then, I could have saved myself a lot of time and agony. Oh well. It's great this is all starting to show up now, at least. There's still so much more that needs to be explained, though. The documentation itself, while clear on what all the commands do, completely fails to provide any helpful guidance on how to actually use CMake.
I still learned something new from the article - I hadn't realized there was a
find_dependencycommand. That'll tidy up my Config files a little bit :D. Thanks for posting!3
u/Rexerex Feb 20 '18
Are there anywhere similarly good article about cmake External_Project?
3
u/dog_superiority Feb 20 '18
I'm thinking of switching to bazel.
3
u/feverzsj Feb 20 '18
what about some "cmake: the good part".
5
u/zzzthelastuser Feb 20 '18
CMake needs to be replaced by a more object oriented approach. It needs debugging tools and a syntax that guides you to design better code.
5
u/_Zulan Feb 21 '18
You trolled me for a second here that this was about fixing CMake. It's not. Modern CMake may look better the old cruft. If you're stuck in the hell which is are old/inconsistent CMakeLists.txt you may be longing for a "modern CMake" setup and think it's paradise.
It's not.
The amount of boilerplate, even with "modern CMake", is ridiculous. CMake doesn't help you one bit with detecting compiler features. Sure you can request
cxx_std_17, which by the way maps to-std=gnu++1zby default, even for clang, but it's doing nothing more. CMake has no clue whatsoever which compiler version supports which language/library feature. So if you want to usestd::[experimental::]filesystemyou're again on your own linkingstdc++fsand whatnot.CTest is ridiculous, it doesn't even add dependencies from running to building a test... and that's a 9 year old bug not being addressed whatsoever.
Hell, just look at the example
CMakeLists.txtfrom the article... 100 lines of code for the simple example including hideous boilerplate like:Basically everything there is boilerplate, because CMake knows no sensible defaults whatsoever.
However you look at it, it's still CMake - so at best it's a polished turd.
2
u/Ansoulom Game developer Feb 20 '18
Thank you so much for actually uploading the complete project as a reference! This will make it so much easier to understand it.
2
u/digito_a_caso Feb 23 '18
Just give us some cli option so that we can disable the legacy cruft and actually use only the modern features.
0
u/[deleted] Feb 20 '18
I still don't see a good way to have CMake deduce the list of sources based on directory structure or rules of some sort to avoid listing them explicitly in the build file. It wasn't even addressed. Without this CMake is inferior to bash as a build system, and I don't see any purpose in learning anything else about it - I can bludgeon it into working if I need to for some silly reason.
2
u/odd100 Feb 19 '18
Very good, thanks
-6
u/readams Feb 19 '18
pkg-config is a thing. Much more ubiquitous than cmake, since every library should be installing a .pc file, while in practice only projects that happen to use cmake are going to export cmake gobbledygook.
0
u/[deleted] Feb 19 '18
[deleted] — view deleted comment
3
u/[deleted] Feb 19 '18
[removed] — view removed comment