Skip to content

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!

安装完默认放在 `~/bin`,可以先确认一下:
gpadmin@gpadmin:~$ ls ~/bin/ bazel
## 2.配置 PATH

安装器会提醒你把 `~/bin` 加进 PATH。编辑 `~/.bashrc`,在文件末尾加一行:
export PATH="\(PATH:\)HOME/bin"
然后重新加载使其生效:
source ~/.bashrc
## 3.验证与第一个构建
bazel --version
能打印版本号就说明装好了。接着到任意一个带 WORKSPACE/MODULE.bazel 的项目目录里跑:
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~

Comments