Last updated:2026-10-12
Building C++ Projects with Bazel¶
This site's projects (including CPlusPlusThings) have switched to Bazel builds. Here I record the complete process, from a fresh install to a successful build — every step actually run through.
1. Installation¶
Taking Linux as an example, the official recommendation is to install a user-level version with the installer script (no root required): gpadmin@gpadmin:~$ ./bazel-6.1.1-installer-linux-x86_64.sh --user Bazel installer
Bazel is bundled with software licensed under the GPLv2 with Classpath exception. You can find the sources next to the installer on our release page: https://github.com/bazelbuild/bazel/releases
¶
Build information¶
- Commit Uncompressing......WARNING: Invoking Bazel in batch mode since it is not invoked from within a workspace (below a directory having a WORKSPACE file). Extracting Bazel installation... .
Bazel is now installed!
Make sure you have "/home/gpadmin/bin" in your path.
For bash completion, add the following line to your : source /home/gpadmin/.bazel/bin/bazel-complete.bash
For fish shell completion, link this file into your /home/gpadmin/.config/fish/completions/ directory: ln -s /home/gpadmin/.bazel/bin/bazel.fish /home/gpadmin/.config/fish/completions/bazel.fish
See http://bazel.build/docs/getting-started.html to start a new project!
gpadmin@gpadmin:~$ ls ~/bin/ bazel export PATH="\(PATH:\)HOME/bin" source ~/.bashrc bazel --version bazel build //... ```Bazel automatically fetches dependencies and caches build artifacts, so the second build is basically instant — this is the core of why it beats traditional Make: content-hash-based remote/local caching + a strict dependency closure.
For a complete hands-on walkthrough of building this site's project, watch the Bilibili video: Episode 1: Step By Step — Building This Project; it pairs best with this post. Questions are welcome in the comments~
Related Topics¶
- Debugging Bazel Builds with LLDB | C++那些事
- CMake Essentials in Practice | C++那些事
- Getting Started with the Conan C++ Package Manager | C++那些事
- C++ Insights Compile-Time Observation | C++那些事
- Interactive C++ in Jupyter Notebook | C++那些事
- C++ string to int | C++那些事
- The C++ dbg-macro Debugging Macro | C++那些事
- GCC Compilation Flags Explained | C++那些事
- Debugging C++ Programs with GDB | C++那些事
- GDB Python Extension Debugging | C++那些事