TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An Introduction to Modern CMake

267 pointsby uyoakaomaover 6 years ago

15 comments

ahartmetzover 6 years ago
Somehow I&#x27;m never happy with CMake guides. The official book is ridiculously outdated among other things, the official documentation is complete and current but provides no guidance, and none of the blog-documentation about it gives a complete picture. Some of it is bad advice. This one doesn&#x27;t talk about the dependency graph, something I consider very important in a build system. It is also missing a lot of other stuff. (Fair enough, it&#x27;s provided for free.)<p>I wrote the CMake training material for KDAB (www.kdab.com) while I worked there. In my biased opinion it&#x27;s the best CMake guide around, especially after it was extended and polished by coworkers :). I tried to mix explaining the &quot;inner logic&quot; of a build system, and CMake in particular, with examples, resulting in something I&#x27;m reasonably happy with. The main difficulty was ordering the topics to avoid too many forward-references while also not starting with a lot of not-obviously-interesting groundwork [1]. (I don&#x27;t work there anymore btw, so the interest I have is getting my work used)<p>This intro to modern CMake should be good because Steve is a major CMake contributor and good at explaining. The presentation even contains a dependency graph! <a href="https:&#x2F;&#x2F;steveire.wordpress.com&#x2F;2017&#x2F;11&#x2F;05&#x2F;embracing-modern-cmake&#x2F;" rel="nofollow">https:&#x2F;&#x2F;steveire.wordpress.com&#x2F;2017&#x2F;11&#x2F;05&#x2F;embracing-modern-c...</a><p>[1] Aside: I figured that &quot;There are two types of bad software documentation: math textbooks and cooking recipes. The former does not explain why you are doing things and other helpful context, the latter won&#x27;t help you if you need to do something differently, which is almost always.&quot;
评论 #17899591 未加载
评论 #17901621 未加载
评论 #17902920 未加载
tux1968over 6 years ago
The recommended compatibility boilerplate for new projects is tedious. There must be a way to include this knowledge in cmake itself rather than requiring every &quot;properly&quot; configured project to get this right:<p><pre><code> cmake_minimum_required(VERSION 3.1) if(${CMAKE_VERSION} VERSION_LESS 3.12) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) else() cmake_policy(VERSION 3.12) endif() </code></pre> This is all before you even start thinking about your own project.
评论 #17900633 未加载
评论 #17898136 未加载
评论 #17901676 未加载
评论 #17898839 未加载
评论 #17899734 未加载
overgardover 6 years ago
It’s better than every other build system, but god I wish they had just used a preexisting language for it. The syntax for CMake just feels so janky and weird, and it’s another set of things I have to remember and (eventually) forget. It’s not like it has these amazing language concepts nothing else can do; and if they’re worried about size or dependencies something like Lua would add like no overhead and be super simple to link.
评论 #17902678 未加载
评论 #17901809 未加载
superkuhover 6 years ago
The problem with modern cmake is that it <i>cannot</i> run on older systems. So if you want to use it you end up either static compiling an incredibly hard and tedious depedency tree or have to use some just released OS.<p>What I want out of a make system isn&#x27;t bleeding edge features. I want to be able to use it for more than 3 years.
评论 #17898381 未加载
评论 #17898035 未加载
评论 #17901642 未加载
评论 #17897972 未加载
评论 #17898284 未加载
评论 #17898564 未加载
mr337over 6 years ago
This is quite welcoming. I have tried to digest cmake docs and get so lost.<p>Am I the only one that can’t grok cmake docs?<p>Edit: spelling fixes
评论 #17898440 未加载
评论 #17898193 未加载
评论 #17898130 未加载
评论 #17898467 未加载
MrQuincleover 6 years ago
What I find a big shortcoming from CMake is that it does not have support for building for multiple architectures at once.<p><a href="https:&#x2F;&#x2F;cmake.org&#x2F;pipermail&#x2F;cmake-developers&#x2F;2014-September&#x2F;022929.html" rel="nofollow">https:&#x2F;&#x2F;cmake.org&#x2F;pipermail&#x2F;cmake-developers&#x2F;2014-September&#x2F;...</a><p>Quote: &quot;The fundamental problem with supporting multiple architectures is that pretty much all of CMake is designed to support one architecture at a time. Modules&#x2F;*, CMakeCache.txt, etc. are all built around only finding, using, and building one artifact per library (OS X universal binaries work with multiple architectures because they are still only one file). I think even your &quot;toolchain scope&quot; approach would end up being used in practice to wrap the entire CMakeLists.txt file.&quot;
评论 #17900936 未加载
nwmcsweenover 6 years ago
Please don&#x27;t use cmake, the dependency chain it pulls in on a bare machine is massive.
评论 #17898695 未加载
评论 #17898757 未加载
评论 #17898658 未加载
评论 #17898603 未加载
评论 #17901656 未加载
评论 #17900342 未加载
quietbritishjimover 6 years ago
This is really not an introduction to CMake. It is more like a collection of the author&#x27;s opinions about CMake, barely categorised into sections and some quite contentious.<p>An introduction would be example driven, starting with a very short but complete example:<p><pre><code> cmake_minimum_required(VERSION 3.1) project(FooAndBar) add_executable(Foo foo.cpp) add_executable(Bar bar.cpp) </code></pre> It would explain projects and targets (noting the different meaning of “project” to some IDEs including Visual Studio: project&lt;-&gt;solution and target&lt;-&gt;project). Then you would progressively build from there. Start by adding a dependency such as protobuf to show find_package() and target_link_libraries(), showing both the new protobuf::libprotobuf target dependency and the old-style ${Protobuf_INCLUDE_DIRS} and ${Protobuf_LIBRARIES} variables. Then make some shared code between your two executables into your own library, discussing shared vs static. I would not even mention variables until after this point (even though ${Protobuf_INCLUDE_DIRS} already is a variable).<p>In other words, an introduction should be top-down and pedagogical. This is the exact opposite of that: picking through a CMakeLists.txt from the bottom up, one line at a time, before you even know what the point of it is.<p>Perhaps I misread the title: I read it as “introduction to CMake [but using modern techniques]”, but maybe it’s “introduction to the modern bits of CMake [assuming you already know the old stuff of CMake]”. But even that could be example driven, admittedly with more effort: start with an old crusty CMakeLists.txt with plenty of bad habits and make it better one step at a time. Or have lots of little CMakeLists.txt with one bad habit at a time, and fix each of those.<p>I am not convinced by some of the recommendations it makes either, although I think there will always be some disagreement about some of these things. I have already given my view on the “cmake_policy” atrocity (which is the very first thing in “Introduction to the basics”!) in another comment here. And in the examples there are workarounds for old versions of CMake that don’t have targets for e.g. find_package(boost) by creating interface targets using the old _LIBRARIES and _INCLUDE_DIR variables. These are very neat but not very accessible to CMake beginners. It would be much simpler to put up with using the old variables, or commit to increasing your minimum supported CMake and forget them entirely.
gjasnyover 6 years ago
I could also recommend Craig Scott&#x27;s recent CMake book: &quot;Professional CMake&quot;: <a href="https:&#x2F;&#x2F;crascit.com&#x2F;professional-cmake&#x2F;" rel="nofollow">https:&#x2F;&#x2F;crascit.com&#x2F;professional-cmake&#x2F;</a><p>IMHO it&#x27;s the best available CMake book available right now. I especially liked the recommendations at the end of every chapter.
Sir_Cmpwnover 6 years ago
I think the best advice I can give to someone learning about cmake is to use meson instead. I once had dozens of codebases using cmake, and have since moved most of them to meson and start most new new projects with meson.
评论 #17900156 未加载
ur-whaleover 6 years ago
CMake is the perl of build systems: useful and feature-fat but one of the worse DSL syntax I&#x27;ve had to grapple with, barely better than that of a Makefile
评论 #17900821 未加载
codedokodeover 6 years ago
I used CMake and I liked that it can produce NMake files, which can be used for building with Windows SDK (not bloated Visual Studio, SDK contains just headers and compiler) on Windows XP.<p>And generally it is much easier to use than manually write Makefiles or use Autotools with weird unintuitive syntax. As I remember, they use `dnl` keyword (download?) for comments!
评论 #17900878 未加载
DoofusOfDeathover 6 years ago
One of my problems with &quot;modern&quot; CMake is the same as my problem with &quot;modern&quot; C++:<p>Their proponents argue for using certain new language features to get things done. But because of backwards-compatibility concerns, the system still supports the old, quasi-deprecated constructs for doing those same things.<p>And so you end up with an overly complicated language that seems to have multiple, reasonable ways to do the same thing. And codebases containing a mix of the two, even for newly written code.<p>IMO it would be better for CMake to make a clean break, and have CMake 4 <i>only</i> support &quot;modern&quot; CMake.<p>(edit: And, as I&#x27;ve posted elsewhere, switch to a more robust scripting language.)
kevinoidover 6 years ago
There are some good recommendations in here! Does anyone know of a way to check for, or enforce, these recommendations? The only CMake linters I could find seem to focus on whitespace and naming issues.
khazhouxover 6 years ago
Can anyone with experience with Bazel, Buck, or Pants chime in?
评论 #17903139 未加载
评论 #17902708 未加载