





















































Hi ,
Welcome to this week’s edition of ProgrammingPro!
In today’sExpert Insight, we bring you an excerpt from the recently published book, CMake Best Practices -Second Edition, which discusses how to optimize and maintain CMake projects using a compiler cache, specifically ccache, to speed up C and C++ builds.
Related Titles from Packt
News Highlights: Android 15 launches with enhanced developer tools; PyPi faces security risks in package management; .NET 9 introduces crucial security updates, .NET Framework remains vulnerable; and Elasticsearch reverts to open source amid tech shifts.
My top 5 picks from today’s learning resources:
But there’s more, so dive right in.
Stay Awesome!
Divya Anne Selvaraj
Editor-in-Chief
PS: Thismonth’ssurvey is now live. Do take the opportunity to tell us what you think of ProgrammingPro, request learning resources, and earn your one Packt Credit for this month.
Developing for iOS? Setapp's 2024 report on the state of the iOS market in the EU is a must-see
How do users in the EU find apps? What's the main source of information about new apps? Would users install your app from a third-party app marketplace?
Set yourself up for success with these and more valuable marketing insights in Setapp Mobile's report, "iOS Market Insights for EU."
EventTarget
class, providing a simplified, event-driven approach to track changes to data.Send + Sync + 'static
bounds through Structured Concurrency and thread-per-core.Here’s an excerpt from “Chapter 14: Optimizing and Maintaining CMake Projects” in the book, CMake Best Practices -Second Edition, by Dominik Berner and Mustafa Kemal Gilor, published in August 2024.
Ccaches work by caching compilations and detecting when the same compilation is done again. At the time of writing this book, the most popular
program for caching compile results isccache
, which is open source and distributed under the Lesser General Public License 3(LGPL 3). The ccache
program not only affects incremental builds but also fresh builds, as long as the cache is not deleted between the two runs. The cache created is portable between systems running the same compilers and can be stored in remote databases so that multiple developers may access the same cache. Officially, ccache
supports GCC, Clang, andNvidia CUDA Compiler(NVCC), but people claim to have run it for MSVC and Intel compilers. When usingccache
with CMake, it works best with Makefile and Ninja generators. At the time of writing this book, Visual Studio wasnot supported.
To useccache
with CMake, the CMAKE_<LANG>_COMPILER_LAUNCHER
cache variable is used, where <LANG>
is replaced with the respective programming language. The recommended way is to pass this in using a preset, but to enableccache
for C and C++ inside aCMakeLists.txt
file, the following code canbe used:
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
endif()
Passing the variable from a preset or from the command line or a preset is also a good alternative, especially because the configuration ofccache
is done easiest by usingenvironment variables.
Usingccache
with the default configuration might already bring a considerable improvement regarding build times, but if the build is a bit more complex, further configuration might be necessary. To configureccache
, certain environment variables starting withCCACHE_
can be used; for full documentation of all configuration options, refer to theccache
documentation. Common scenarios that need special attention are combiningccache
with precompiled headers, managing dependencies that are included usingFetchContent
, and combiningccache
with other compiler wrappers, such asdistcc
or icecc
for distributed builds. For these scenarios, the following environment variables are used:
CCACHE_SLOPPINESS
to pch_defines,time_macros
. The reason for this is that ccache
cannot detect changes in#defines
in the precompiled header, and it cannot tell if__TIME__
,__DATE__
, or__TIMESTAMP__
are used when creating precompiled headers. Optionally, setting include_file_mtime
to CCACHE_SLOPPINESS
might further increase the cache hit performance, but it carries a very small risk of arace condition.FetchContent
), setting CCACHE_BASEDIR
to CMAKE_BINARY_DIR
might increase the cache hit rate; this might bring a performance boost especially if there are many (sub)projects fetching the same dependency. On the other hand, if the sources in the project itself are the ones that take more time to compile, setting this toCMAKE_SOURCE_DIR
might bring better results. It needs to be tried out to learn which one brings thebetter result.CCACHE_PREFIX
environment variable is used to add commands for these wrappers. It is recommended to useccache
first when chaining multiple wrappers so that the results of other wrappers may alsobe cached.Passing environment variables to CMake using a configure preset, as described in "Chapter 9,Creating Reproducible Build Environments", is the recommended way; this can either be combined with detectingccache
inside the CMakeLists.txt
file or the ccache
command may also be passed using the following preset:
{
"name" : "ccache-env",
...
"environment": {
"CCACHE_BASEDIR" : "${sourceDir}",
"CCACHE_SLOPPINESS" : "pch_defines,time_macros"
}
},
With these configurations, usingccache
can yield very large benefits to the compile time, but caching compiler results is a complicated matter, so to get the full benefit, theccache
documentation should be consulted. In most cases, usingccache
will probably bring the most performance benefit with a relatively trivial setup. Other tools, such asdistcc
for distributed builds, work very similarly from the CMake perspective, but require a bit moresetup work.
CMake Best Practices -Second Edition was published in August 2024. Packt library subscribers can continue readingthe entire book for free or you can buy the bookhere!
That’s all for today.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. Complete ProgrammingPro archives can be foundhere. Complete PythonPro archives arehere.
If your company is interested in reaching an audience of developers, software engineers, and tech decision makers, you may want toadvertise with us.
If you have any suggestions or feedback, or would like us to find you a Programming learning resource on a particular subject, take the surveyor just respond to this email!