Compiling large applications can take a lot of time, especially during development when small changes often trigger rebuilds. Sccache, a modern caching tool and an alternative to Ccache, helps mitigate this problem by storing and reusing previously compiled objects. This is particularly advantageous when switching between branches or rebuilding shared dependencies. This tutorial explains how to use Sccache with CMake.
CMake provides a property called CMAKE_<LANG>_COMPILER_LAUNCHER
that allows you to wrap compilers with a launcher tool. By configuring Sccache as the launcher, compilation requests first pass through the cache, which can significantly reduce build times.
Below is a simple CMake configuration that attempts to find the sccache
binary and, if available, enables it for both C and C++ compilation:
cmake_minimum_required(VERSION 3.27)
project(myapp)
set(CMAKE_CXX_STANDARD 17)
find_program(SCCACHE_PROGRAM sccache)
if (SCCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
message(STATUS "Sccache found: ${SCCACHE_PROGRAM}")
else ()
message(STATUS "Sccache not found")
endif ()
add_executable(${PROJECT_NAME} main.cpp)
This configuration works with both Unix Makefile and Ninja generators.
Leave a Comment
Cancel reply