最終更新:2026-10-12
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 は依存関係の取得とビルド成果物のキャッシュを自動で行い、2 回目のビルドは基本的に数秒——これが伝統的な Make より速い核心です:コンテンツハッシュによるリモート/ローカルキャッシュ+厳密な依存クロージャ。
本サイトのプロジェクトをビルドする完全な実戦は Bilibili 動画をご覧ください:第 1 回 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++那些事