How to Build Abseil C++ from Source: A Complete CMake and Bazel Guide
To build Abseil C++ from source, clone the repository and use CMake with cmake -S . -B build && cmake --build build, or use Bazel with bazel build //... from the repository root.
Abseil is an open-source collection of C++17/20 library components that extend the standard library. The repository organizes independent modules under the absl/ directory, including absl/base, absl/strings, and absl/synchronization. The project officially supports CMake and Bazel build systems, with the top-level CMakeLists.txt serving as the primary configuration entry point.
Prerequisites and Repository Structure
Abseil requires a compiler supporting C++17 or later. The source tree organizes components as modular directories under absl/, allowing selective compilation of only the libraries you need.
Key directories include:
absl/base– Core utilities and configuration macrosabsl/strings– String handling and manipulation librariesabsl/synchronization– Threading primitives and synchronization toolsabsl/time– Time handling utilities
Build System Options
Abseil officially supports two build systems. Choose based on your project’s existing toolchain and integration requirements.
CMake (Recommended)
CMake is the preferred build system for most users, especially when integrating Abseil into existing projects or installing system-wide. The configuration in CMakeLists.txt (lines 28-68) automatically handles ABI compatibility by generating a pinned options.h header that reflects your selected C++ standard (cxx_std_17 or cxx_std_20).
Bazel
Use Bazel for hermetic builds or when working within the Google ecosystem. Invoke builds from the repository root with:
bazel build //...
CMake Configuration Options
When configuring the build, specify options using -D<NAME>=<VALUE> syntax. Key flags defined in CMakeLists.txt include:
ABSL_ENABLE_INSTALL– Enables installation rules (default ON for top-level builds)ABSL_BUILD_TESTING– Builds the Abseil test suite (default OFF)ABSL_USE_GOOGLETEST_HEAD– Uses the latest GoogleTest from HEAD for testsABSL_USE_EXTERNAL_GOOGLETEST/ABSL_FIND_GOOGLETEST– Controls how GoogleTest is located when testing is enabledABSL_BUILD_MONOLITHIC_SHARED_LIBS– Builds a single shared library (particularly useful on Windows)
The CMake scripts automatically create public targets prefixed with absl::, such as absl::base, absl::strings, and absl::synchronization. These targets propagate required compile options, include directories, and link dependencies via ABSL_PROPAGATE_CXX_STD, ensuring ABI compatibility across your entire dependency graph.
Step-by-Step CMake Build Instructions
Follow this workflow to compile Abseil from source:
- Create a build directory (out-of-source builds are recommended):
mkdir build && cd build
- Configure the project with your desired options:
cmake .. \
-DABSL_BUILD_TESTING=ON \
-DABSL_USE_GOOGLETEST_HEAD=ON \
-DCMAKE_BUILD_TYPE=Release
- Compile using the native build tool:
cmake --build . -j$(nproc)
- Run tests (optional):
ctest --test-dir .
- Install to a local prefix (optional):
cmake --install . --prefix $HOME/.local
Integrating Abseil into Your CMake Project
Treat Abseil as a sub-project using add_subdirectory() or as an installed dependency via find_package(). The absl:: prefixed targets automatically inherit the appropriate CXX_STANDARD settings.
Example: Linking Abseil components in your CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(sample_app)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(abseil-cpp) # Path to Abseil source checkout
add_executable(sample_app main.cpp)
target_link_libraries(sample_app
absl::base
absl::strings
absl::time
absl::synchronization)
Example: Building a shared library that depends on Abseil
cmake_minimum_required(VERSION 3.16)
project(my_lib)
add_subdirectory(abseil-cpp)
add_library(my_lib SHARED lib.cpp)
target_link_libraries(my_lib PUBLIC absl::base absl::synchronization)
target_compile_features(my_lib PUBLIC cxx_std_17) # Ensure callers use C++17+
Building and Running Tests
To build and execute Abseil’s comprehensive test suite locally:
git clone https://github.com/abseil/abseil-cpp.git
cd abseil-cpp
mkdir build && cd build
cmake -DABSL_BUILD_TESTING=ON -DABSL_USE_GOOGLETEST_HEAD=ON ..
make -j$(nproc)
ctest
The CMake configuration uses CMake/Googletest/DownloadGTest.cmake to fetch GoogleTest automatically when ABSL_USE_GOOGLETEST_HEAD is enabled. For system-installed GoogleTest, use ABSL_USE_EXTERNAL_GOOGLETEST instead.
Summary
- Abseil supports CMake and Bazel, with CMake recommended for most integration scenarios
- Configure builds using
-Dflags likeABSL_BUILD_TESTINGandABSL_ENABLE_INSTALL - Use
absl::prefixed targets (e.g.,absl::strings) to automatically propagate C++ standards and dependencies - Build out-of-source with
cmake -S . -B buildfollowed bycmake --build build - Link Abseil via
add_subdirectory()or as an installed package using the exportedabslConfig.cmakeconfiguration
Frequently Asked Questions
What C++ standards does Abseil support?
Abseil requires C++17 or C++20. The CMakeLists.txt automatically detects your compiler capabilities and generates an options.h header (around lines 28-55) that pins ABI-sensitive configuration choices to ensure compatibility across the built artifacts and consuming code.
How do I link specific Abseil components in CMake?
Link against namespaced targets like absl::base, absl::time, or absl::synchronization using target_link_libraries(). These targets are defined in the root CMakeLists.txt and automatically handle transitive dependencies, include directories, and compiler flags through the ABSL_PROPAGATE_CXX_STD mechanism.
Can I build Abseil as a shared library?
Yes. Set ABSL_BUILD_MONOLITHIC_SHARED_LIBS=ON when configuring with CMake. This builds a single shared library containing all Abseil components, which is particularly useful on Windows where DLL export/import semantics apply. Note that this produces a monolithic shared library rather than individual shared objects per component.
How do I handle GoogleTest dependencies when building tests?
When ABSL_BUILD_TESTING=ON, Abseil requires GoogleTest. You have three options controlled by CMake variables: use ABSL_USE_GOOGLETEST_HEAD=ON to download the latest version automatically via CMake/Googletest/DownloadGTest.cmake, set ABSL_USE_EXTERNAL_GOOGLETEST=ON to link against a pre-installed version, or use ABSL_FIND_GOOGLETEST to control discovery behavior.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →