This page contains resources that help you use Bazel with C++ projects. It links to a tutorial, build rules, and other information specific to building C++ projects with Bazel.
The following resources will help you work with Bazel on C++ projects:
In addition to general Bazel best practices, below are best practices specific to C++ projects.
Follow the guidelines below when creating your BUILD files:
Each BUILD file should contain one
rule target per compilation unit in the directory.
We recommend that you granularize your C++ libraries as much as possible to maximize incrementality and parallelize the build.
If there is a single source file in
srcs, name the library the same as
that C++ file’s name. This library should contain C++ file(s), any matching
header file(s), and the library’s direct dependencies. For example:
cc_library( name = "mylib", srcs = ["mylib.cc"], hdrs = ["mylib.h"], deps = [":lower-level-lib"] )
cc_test rule target per
cc_library target in the file. Name the
[library-name]_test and the source file
For example, a test target for the
mylib library target shown above would
look like this:
cc_test( name = "mylib_test", srcs = ["mylib_test.cc"], deps = [":mylib"] )
Follow these guidelines for include paths:
Make all include paths relative to the workspace directory.
Use quoted includes (
#include "foo/bar/baz.h") for non-system headers, not
Avoid using UNIX directory shortcuts, such as
. (current directory) or
For legacy or
third_party code that requires includes pointing outside the
project repository, such as external repository includes requiring a prefix,
arguments on the
cc_library rule target.