C++ Headers: hdrs, srcs & Layering
In Make or CMake, the compiler can see every header on the include path, declared or not. That's how C++ codebases end up with hidden dependencies: app.cc includes a header from a library it never declared, and everything works until someone removes that library from a dependency of a dependency. Bazel closes most of these holes. This lesson shows which ones, and the one it can't close with the toolchain in this course.
Where a header goes
| Attribute | Who may #include the header |
|---|---|
hdrs |
the library's own sources and every target that depends on it (the public interface) |
srcs (a .h listed there) |
only the library's own sources (a private header) |
textual_hdrs |
files meant to be included textually, not compiled on their own (.inc tables, X-macros) |
headers of deps |
your sources, and transitively your dependents' sources |
headers of implementation_deps |
your sources only: they are not passed on to your dependents |
Check 1: undeclared headers don't exist
A compile action's sandbox contains only declared inputs. A header that no rule lists is not in the sandbox, so the compiler can't find it:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir lib
cat > lib/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "codes", srcs = ["codes.cc"]) # forgot codes_table.h
EOF
echo '#define MAX_CODE 99' > lib/codes_table.h
printf '#include "lib/codes_table.h"\nint max_code() { return MAX_CODE; }\n' > lib/codes.cc
bazel build //lib:codes 2>&1 | grep -E 'fatal error|^ERROR: Build'
echo "== without the sandbox"
bazel build --spawn_strategy=standalone //lib:codes 2>&1 | grep -A2 'undeclared' | sed 's#/home/learner/ws/##'lib/codes.cc:1:10: fatal error: lib/codes_table.h: No such file or directory
ERROR: Build did NOT complete successfully
== without the sandbox
ERROR: lib/BUILD.bazel:2:11: Compiling lib/codes.cc failed: undeclared inclusion(s) in rule '//lib:codes':
this rule is missing dependency declarations for the following files included by 'lib/codes.cc':
'lib/codes_table.h'
Even when you turn the sandbox off, Bazel still catches it: GCC writes a .d file listing every header it read, and Bazel compares that list with the declared inputs ("undeclared inclusion"). The fix is to list the header, in srcs if it's private to the library.
Check 2: implementation_deps hide headers from dependents
Here //lib:math uses //lib:base internally. Because base is an implementation_deps of math, the binary that depends on math can't reach into base:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir lib app
cat > lib/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "base", srcs = ["base.cc"], hdrs = ["base.h"])
cc_library(
name = "math",
srcs = ["math.cc"],
hdrs = ["math.h"],
implementation_deps = [":base"],
visibility = ["//visibility:public"],
)
EOF
printf '#pragma once\nint twice(int x);\n' > lib/base.h
printf '#include "lib/base.h"\nint twice(int x) { return 2 * x; }\n' > lib/base.cc
printf '#pragma once\nint quad(int x);\n' > lib/math.h
printf '#include "lib/math.h"\n#include "lib/base.h"\nint quad(int x) { return twice(twice(x)); }\n' > lib/math.cc
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(name = "good", srcs = ["good.cc"], deps = ["//lib:math"])
cc_binary(name = "sneaky", srcs = ["sneaky.cc"], deps = ["//lib:math"])
EOF
printf '#include <cstdio>\n#include "lib/math.h"\nint main() { std::printf("quad(3) = %%d\\n", quad(3)); }\n' > app/good.cc
printf '#include <cstdio>\n#include "lib/base.h"\nint main() { std::printf("%%d\\n", twice(3)); }\n' > app/sneaky.cc
bazel run //app:good 2>/dev/null
bazel build //app:sneaky 2>&1 | grep -E 'fatal error'quad(3) = 12
app/sneaky.cc:2:10: fatal error: lib/base.h: No such file or directory
good links fine (the object code of base is still linked in), but sneaky can't include lib/base.h. Use implementation_deps for every dependency that doesn't appear in your public headers: it enforces the layering and means that a change to base.h no longer recompiles everything that depends on math.
Check 3: layering_check and transitive headers
With plain deps, headers flow transitively. A binary that depends on math (with base in its deps) can include base.h directly without declaring base, which is exactly the hidden dependency we wanted to avoid. Bazel's fix is the layering_check feature: the toolchain generates a Clang module map per target and compiles with -fmodules-strict-decluse, so including a header from a target you don't directly depend on is a compile error.
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir lib app
cat > lib/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(name = "base", srcs = ["base.cc"], hdrs = ["base.h"])
cc_library(name = "math", srcs = ["math.cc"], hdrs = ["math.h"], deps = [":base"], visibility = ["//visibility:public"])
EOF
printf '#pragma once\nint twice(int x);\n' > lib/base.h
printf '#include "lib/base.h"\nint twice(int x) { return 2 * x; }\n' > lib/base.cc
printf '#pragma once\nint quad(int x);\n' > lib/math.h
printf '#include "lib/math.h"\n#include "lib/base.h"\nint quad(int x) { return twice(twice(x)); }\n' > lib/math.cc
printf 'load("@rules_cc//cc:defs.bzl", "cc_binary")\ncc_binary(name = "sneaky", srcs = ["sneaky.cc"], deps = ["//lib:math"])\n' > app/BUILD.bazel
printf '#include <cstdio>\n#include "lib/base.h"\nint main() { std::printf("%%d\\n", twice(3)); }\n' > app/sneaky.cc
bazel run //app:sneaky 2>/dev/null
bazel run --features=layering_check //app:sneaky 2>/dev/null
gcc --version | head -16
6
gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Both runs succeed: the auto-configured toolchain here is GCC, which has no module maps, so the toolchain doesn't support the feature and --features=layering_check is silently ignored. With a Clang toolchain (the auto-configured one when CC=clang, or toolchains_llvm), the second build fails with an error like:
app/sneaky.cc:2:10: error: module //app:sneaky does not depend on a module exporting 'lib/base.h'Teams on Clang typically enable it for everyone with common --features=layering_check in .bazelrc; teams on GCC rely on implementation_deps, code review and tools such as include-what-you-use.
Include paths: strip_include_prefix and includes
Third-party code often expects #include "fmt/core.h" while its files live at third_party/fmt/include/fmt/core.h. Two attributes adapt the paths:
cc_library(
name = "fmt",
hdrs = glob(["include/fmt/*.h"]),
srcs = glob(["src/*.cc"]),
strip_include_prefix = "include", # headers become includable as "fmt/core.h"
visibility = ["//visibility:public"],
)strip_include_prefix (and its partner include_prefix) creates a virtual include directory of symlinks, so only the declared headers are reachable under the new name. The older includes = ["include"] attribute adds a raw -isystem path instead; it works, but it exposes every file in that directory and is best kept for imported prebuilt code.
Test yourself
A .cc file includes a header that no rule declares. What happens in a sandboxed Bazel build?
- It compiles, because the file is on disk
- The compiler can't find it: the sandbox only contains declared inputs
- Bazel adds it automatically
- Only a warning
What does implementation_deps change compared with deps?
- The dependency isn't linked
- The dependency's headers aren't visible to the library's dependents
- The dependency becomes a shared library
- Nothing
Why did --features=layering_check not reject the transitive include in the course image?
- Layering check only works on Windows
- The auto-configured GCC toolchain doesn't support it; it needs Clang module maps
- It only checks Python
- It needs a remote cache