What you’ll learn
In this tutorial you learn how to:- Set up the build environment
- Use
--toolchain_resolution_debugto debug toolchain resolution - Configure the C++ toolchain
- Create a Starlark rule that provides additional configuration for the
cc_toolchainso that Bazel can build the application withclang - Build the C++ binary by running
bazel build //main:hello-worldon a Linux machine - Cross-compile the binary for android by running
bazel build //main:hello-world --platforms=//:android_x86_64
Before you begin
This tutorial assumes you are on Linux and have successfully built C++ applications and installed the appropriate tooling and libraries. The tutorial usesclang version 19, which you can install on your system.
Set up the build environment
Set up your build environment as follows:- If you have not already done so, download and install Bazel 7.0.2 or later.
-
Add an empty
MODULE.bazelfile at the root folder. -
Add the following
cc_binarytarget to themain/BUILDfile:Because Bazel uses many internal tools written in C++ during the build, such asprocess-wrapper, the pre-existing default C++ toolchain is specified for the host platform. This enables these internal tools to build using that toolchain of the one created in this tutorial. Hence, thecc_binarytarget is also built with the default toolchain. -
Run the build with the following command:
The build succeeds without any toolchain registered in
MODULE.bazel. To further see what’s under the hood, run:Without specifying--platforms, Bazel builds the target for@platforms//hostusing@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8.
Configure the C++ toolchain
To configure the C++ toolchain, repeatedly build the application and eliminate each error one by one as described as following. Note: This tutorial assumes you’re using Bazel 7.0.2 or later. If you’re using an older release of Bazel, use--incompatible_enable_cc_toolchain_resolution
flag to enable C++ toolchain resolution.
It also assumes clang version 9.0.1, although the details should only change
slightly between different versions of clang.
-
Add
toolchain/BUILDwithThen add appropriate dependencies and register the toolchain withMODULE.bazelwithThis step defines acc_toolchainand binds it to atoolchaintarget for the host configuration. -
Run the build again. Because the
toolchainpackage doesn’t yet define thelinux_x86_64_toolchain_configtarget, Bazel throws the following error: -
In the
toolchain/BUILDfile, define an empty filegroup as follows: -
Run the build again. Bazel throws the following error:
CcToolchainConfigInfois a provider that you use to configure your C++ toolchains. To fix this error, create a Starlark rule that providesCcToolchainConfigInfoto Bazel by making atoolchain/cc_toolchain_config.bzlfile with the following content:cc_common.create_cc_toolchain_config_info()creates the needed providerCcToolchainConfigInfo. To use thecc_toolchain_configrule, add a load statement totoolchain/BUILDright below the package statement:And replace the “linux_x86_64_toolchain_config” filegroup with a declaration of acc_toolchain_configrule: -
Run the build again. Bazel throws the following error:
At this point, Bazel has enough information to attempt building the code but it still does not know what tools to use to complete the required build actions. You will modify the Starlark rule implementation to tell Bazel what tools to use. For that, you need the
tool_path()constructor from@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl:Make sure that/usr/bin/clangand/usr/bin/ldare the correct paths for your system. Note that the compiler is referenced by the name “gcc” for historic reasons. -
Run the build again. Bazel throws the following error:
Bazel needs to know where to search for included headers. There are multiple ways to solve this, such as using the
includesattribute ofcc_binary, but here this is solved at the toolchain level with thecxx_builtin_include_directoriesparameter ofcc_common.create_cc_toolchain_config_info. Beware that if you are using a different version ofclang, the include path will be different. These paths may also be different depending on the distribution. Modify the return value intoolchain/cc_toolchain_config.bzlto look like this: -
Run the build command again, you will see an error like:
The reason for this is because the linker is missing the C++ standard library and it can’t find its symbols. There are many ways to solve this, such as using the
linkoptsattribute ofcc_binary. Here it is solved by making sure that any target using the toolchain doesn’t have to specify this flag. Copy the following code totoolchain/cc_toolchain_config.bzl:Note that this code uses the GNU C++ library libstdc++. If you want to use the LLVM C++ library, use “-lc++” instead of “-lstdc++”. -
Running
bazel build //main:hello-world, it should finally build the binary successfully for host. -
In
toolchain/BUILD, copy thecc_toolchain_config,cc_toolchain, andtoolchaintargets and replacelinux_x86_64withandroid_x86_64in target names. InMODULE.bazel, register the toolchain for android -
Run
bazel build //main:hello-world --android_platforms=//toolchain:android_x86_64to build the binary for Android.
cc_toolchain_config for the differences or
create a separate rules (i.e. CcToolchainConfigInfo provider) for separate
platforms.
Review your work
In this tutorial you learned how to configure a basic C++ toolchain, but toolchains are more powerful than this example. The key takeaways are:- You need to specify a matching
platformsflag in the command line for Bazel to resolve to the toolchain for the same constraint values on the platform. The documentation holds more information about language specific configuration flags. - You have to let the toolchain know where the tools live. In this tutorial
there is a simplified version where you access the tools from the system. If
you are interested in a more self-contained approach, you can read about
external dependencies. Your tools could come from a
different module and you would have to make their files available to the
cc_toolchainwith target dependencies on attributes, such ascompiler_files. Thetool_pathswould need to be changed as well. - You can create features to customize which flags should be passed to different actions, be it linking or any other type of action.