| name | bazel |
| description | Bazel build system skill for C/C++ projects. Use when writing BUILD files with cc_library and cc_binary rules, registering toolchains, configuring remote execution, debugging sandbox issues, using query and cquery for dependency graphs, or migrating from CMake to Bazel. Activates on queries about Bazel, BUILD files, cc_library, cc_binary, Bzlmod, bazel query, remote execution, or Bazel toolchain registration. |
Bazel
Purpose
Guide agents through Bazel for C/C++ projects: writing BUILD files, cc_library/cc_binary rules, toolchain registration, remote execution, dependency graph queries, Bzlmod dependency management, and sandbox debugging.
Triggers
- "How do I write a Bazel BUILD file for a C++ library?"
- "How do I add external dependencies with Bzlmod?"
- "How do I debug Bazel sandbox errors?"
- "How do I query the Bazel dependency graph?"
- "How do I set up remote execution with Bazel?"
- "How do I register a custom C++ toolchain?"
Workflow
1. Workspace structure
my-project/
├── MODULE.bazel # Bzlmod dependency file (Bazel ≥6)
├── WORKSPACE # legacy (still needed for some features)
├── BUILD # root build file
├── src/
│ ├── BUILD
│ └── main.cc
└── lib/
├── BUILD
├── mylib.cc
└── mylib.h
2. Basic BUILD file — cc_library / cc_binary
cc_library(
name = "mylib",
srcs = ["mylib.cc"],
hdrs = ["mylib.h"],
copts = ["-Wall", "-Wextra", "-std=c++17"],
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"//util:helpers",
],
)
cc_test(
name = "mylib_test",
srcs = ["mylib_test.cc"],
deps = [
":mylib",
"@com_google_googletest//:gtest_main",
],
)
cc_binary(
name = "main",
srcs = ["main.cc"],
deps = ["//lib:mylib"],
linkopts = ["-lpthread"],
)
bazel build //src:main
bazel build //...
bazel test //lib:mylib_test
bazel test //...
bazel run //src:main -- arg1 arg2
bazel-bin/src/main
3. Bzlmod — modern dependency management
module(
name = "my_project",
version = "1.0",
)
bazel_dep(name = "abseil-cpp", version = "20240116.2")
bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "platforms", version = "0.0.8")
bazel mod graph | head -30
bazel mod deps --depth=2
4. Dependency graph queries
bazel query "deps(//src:main)"
bazel query "rdeps(//..., //lib:mylib)"
bazel query "filter('//lib', deps(//src:main))"
bazel cquery "deps(//src:main)" --output=files
bazel cquery "//lib:mylib" --output=build
bazel query --nohost_deps --noimplicit_deps \
"somepath(//src:main, //lib:mylib)"
bazel aquery "//src:main"
bazel aquery "//src:main" --output=jsonproto | jq '.actions[0].arguments'
5. Toolchain registration
platform(
name = "linux_x86_64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)
cc_toolchain_suite(
name = "my_toolchain",
toolchains = {
"k8": ":my_k8_toolchain",
},
)
cc_toolchain(
name = "my_k8_toolchain",
all_files = ":empty",
compiler_files = ":compiler_wrapper",
)
toolchain(
name = "my_cc_toolchain",
toolchain = ":my_k8_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
target_compatible_with = ["@platforms//os:linux"],
)
register_toolchains("//toolchains:my_cc_toolchain")
6. Remote execution
bazel build //... \
--remote_executor=grpc://buildgrid.example.com:50051 \
--remote_instance_name=main
bazel build //... \
--remote_executor=remotebuildexecution.googleapis.com \
--remote_instance_name=projects/my-project/instances/default \
--google_default_credentials
bazel build //... \
--remote_cache=grpc://cache.example.com:9092
bazel build //... --remote_upload_local_results=true
7. Sandbox debugging
bazel build //src:main --sandbox_debug
bazel build //src:main --verbose_failures --sandbox_debug
bazel build //src:main --spawn_strategy=local
bazel build //src:main --subcommands
For Bazel C++ toolchain configuration, see references/bazel-cpp-toolchain.md.
Related skills
- Use
skills/build-systems/cmake for CMake as an alternative build system
- Use
skills/build-systems/build-acceleration for sccache integration with Bazel remote cache
- Use
skills/compilers/gcc or skills/compilers/clang for compiler flags used in Bazel copts