How to Integrate Abseil C++ with Existing Projects: CMake and Bazel Guide
Integrate Abseil C++ into existing projects by adding it as a submodule or external dependency, then linking the absl target via CMake's add_subdirectory() or Bazel's git_repository rule while exposing headers through the absl/ directory.
This guide demonstrates how to add the abseil/abseil-cpp repository to production C++ codebases using either CMake or Bazel build systems. Whether you need header-only utilities like absl::string_view or compiled components like absl::Time, these integration steps work with any C++17-compatible project.
Prerequisites and Repository Architecture
Abseil C++ maintains dual build system support with Bazel as the primary system and CMake as the secondary option. The repository structure separates header-only modules from compiled libraries that generate static object files.
Key architectural details to understand before integrating:
- Header-only modules: Most utilities (strings, containers, utility) require no linking
- Compiled components: Libraries like
absl/timeandabsl/logproduce objects defined inabsl/base/CMakeLists.txt - Namespace convention: All public symbols reside in the
abslnamespace under theabsl/<module>/include path - Target availability: CMake exposes targets like
absl_timeandabsl_logthroughCMake/abslConfig.cmake.in, while Bazel defines targets inBUILD.bazel
CMake Integration Method
The CMake workflow centers on adding the Abseil subdirectory and linking specific component targets rather than manually managing include paths.
Adding Abseil as a Dependency
Fetch the source code via submodule or tarball:
git submodule add https://github.com/abseil/abseil-cpp.git external/abseil
git submodule update --init --recursive
In your project's CMakeLists.txt, add the subdirectory to expose Abseil's build targets:
add_subdirectory(external/abseil)
This processes the top-level CMakeLists.txt which defines the absl static library target and component-specific interface libraries.
Linking Targets and Selecting Components
Link the full library or select only required components:
# Link everything (simplest approach)
target_link_libraries(my_exe PRIVATE absl)
# Or link specific compiled components only
target_link_libraries(my_exe PRIVATE absl_time)
target_link_libraries(my_exe PRIVATE absl_log)
For header-only usage without compiled code, skip target_link_libraries and add only the include directory:
target_include_directories(my_exe PRIVATE external/abseil)
The component targets (absl_time, absl_strings, etc.) automatically handle dependencies and include paths as defined in the generated configuration.
Bazel Integration Method
Bazel integration uses the WORKSPACE file to fetch external dependencies and reference them via repository-qualified labels.
Workspace Configuration
Add the following to your WORKSPACE file:
git_repository(
name = "abseil_cpp",
remote = "https://github.com/abseil/abseil-cpp.git",
tag = "20240702.0", # Use latest LTS tag or specific commit SHA
)
This fetches the repository including its BUILD.bazel file which exports the complete target graph.
Build Target References
Reference Abseil in your BUILD files:
cc_binary(
name = "my_app",
srcs = ["main.cc"],
deps = ["@abseil_cpp//absl:absl"], # Full static library
)
For granular dependencies, reference specific modules:
deps = [
"@abseil_cpp//absl:time",
"@abseil_cpp//absl:strings",
]
Build with standard Bazel commands:
bazel build //:my_app
Header-Only Usage vs. Compiled Libraries
Understanding when linking is required optimizes build times and binary sizes.
Header-only modules (no linking required):
absl::string_viewabsl::StrJoinabsl::optional/absl::variant
Compiled modules (require linking):
absl::Timeandabsl::Now()(fromabsl/time)absl::Logfunctionality (fromabsl/log)
When using only header-only components, CMake projects can omit target_link_libraries entirely, and Bazel projects can depend on @abseil_cpp//absl:headers to receive only include directory propagation.
Complete Working Examples
These minimal examples demonstrate end-to-end integration patterns found in CMake/install_test_project/simple.cc.
CMake Example
CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(DemoProject CXX)
add_subdirectory(external/abseil)
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE absl_time)
main.cpp:
#include <iostream>
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
int main() {
absl::string_view msg = "Hello, Abseil!";
std::cout << msg << "\n";
auto now = absl::Now();
std::cout << "Now: " << now << "\n";
return 0;
}
Build commands:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/demo
Bazel Example
WORKSPACE:
git_repository(
name = "abseil_cpp",
remote = "https://github.com/abseil/abseil-cpp.git",
tag = "20240702.0",
)
BUILD:
cc_binary(
name = "demo",
srcs = ["main.cc"],
deps = ["@abseil_cpp//absl:absl"],
)
main.cc:
#include <iostream>
#include <vector>
#include "absl/strings/str_join.h"
#include "absl/time/time.h"
int main() {
std::vector<std::string> parts = {"Bazel", "and", "Abseil"};
std::cout << absl::StrJoin(parts, " ") << "\n";
absl::Time now = absl::Now();
std::cout << "Now: " << now << "\n";
}
Build and run:
bazel build //:demo
./bazel-bin/demo
Key Source Files and Configuration
These files govern how Abseil integrates with downstream projects:
CMake/abslConfig.cmake.in: Generates the CMake configuration file that defines target exports likeabsl::timeandabsl::stringsforfind_package()consumersBUILD.bazel: Contains Bazel target definitions including@abseil_cpp//absl:absland component-specific librariesabsl/base/CMakeLists.txt: Compiles source files that require object code, such aslog_severity.ccCMake/install_test_project/simple.cc: Reference implementation demonstrating minimal header inclusion and library linking
Summary
- Add Abseil via Git submodule or Bazel's
git_repositoryrule to fetch theabseil/abseil-cppsource - CMake integration uses
add_subdirectory()followed bytarget_link_libraries(my_target PRIVATE absl)or specific component targets likeabsl_time - Bazel integration references
@abseil_cpp//absl:abslindepsattributes, or granular targets for specific modules - Header-only modules (most of the library) require no linking—just add the repository root to include paths
- Compiled modules like
absl::Timerequire linking the appropriate targets as defined in the build configuration files
Frequently Asked Questions
Do I need to compile all of Abseil C++ to use it in my project?
No. Most Abseil components are header-only and require no compilation. Only specific modules like absl::time, absl::log, and absl::status generate object files that must be linked. You can include headers from absl/strings/ or absl/container/ without linking any libraries.
Can I use Abseil C++ with C++14 or earlier standards?
No. According to the abseil/abseil-cpp source code, all public symbols require C++17 or later. Ensure your compiler flags include -std=c++17 (GCC/Clang) or /std:c++17 (MSVC) before including Abseil headers.
How do I link only specific Abseil modules instead of the entire library?
In CMake, use component-specific target names like absl_time, absl_strings, or absl_memory instead of the catch-all absl target. In Bazel, reference specific labels such as @abseil_cpp//absl:time or @abseil_cpp//absl:strings in your deps attribute. This reduces link-time overhead and binary size.
Where should I place Abseil headers in my project structure?
Place the Abseil repository root (containing the absl/ directory) anywhere accessible to your build system, commonly in third_party/abseil or external/abseil. Add this directory to your compiler's include path, then reference headers using #include "absl/strings/string_view.h" or similar paths. The absl/ prefix is required and enforced by the library's structure.
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 →