最后更新:2026-10-13
Bazel 构建 C++ 项目¶
本站的项目(含 CPlusPlusThings)已经切换到 Bazel 构建,这里把从零安装到能编译的完整过程记录一下,都是实际跑通的步骤。
1.安装¶
以 Linux 为例,官方推荐用 installer 脚本装用户版(不需要 root): 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 会自动拉依赖、缓存构建产物,第二次构建基本是秒级——这也是它比传统 Make 快的核心:按内容哈希的远程/本地缓存 + 严格的依赖闭包。
编译本站项目的完整实战可以看 B 站视频:第一期 Step By Step 上手编译本项目,配合本篇食用更佳。有问题欢迎留言区交流~
相关主题¶
- Bazel 与 LLDB 联合调试 | C++那些事
- CMake 入门与实战 | C++那些事
- Conan C++ 包管理入门 | C++那些事
- C++ Insights 编译期观察 | C++那些事
- C++ Jupyter Notebook 交互 | C++那些事
- C++ string 转 int | C++那些事
- C++ dbg-macro 调试宏 | C++那些事
- GCC 编译选项详解 | C++那些事
- GDB 调试 C++ 程序 | C++那些事
- GDB Python 扩展调试 | C++那些事