How to set C++ standard version when build with Bazel?

ryancheung picture ryancheung · Oct 26, 2016 · Viewed 11.3k times · Source

I'm kinda new to C++. I know how to set C++ version with CMake, but don't know how to set C++ version in Bazel.

Maybe set with the copts parameter in cc_libary but I have to set this in every cc_libary?

Answer

hlopko picture hlopko · Apr 13, 2017

To set the standard using the default C++ toolchain in Bazel you can set environment variable BAZEL_CXXOPTS, e.g. BAZEL_CXXOPTS="-std=c++14". You can also set it from the command line or from .bazelrc using --repo_env=BAZEL_CXXOPTS. : is the flag separator.

Alternatively you can pass --cxxopt to Bazel, or put it into .bazelrc, e.g. --cxxopt='-std=c++11'.

The robust solution to specifying C++ toolchain in Bazel is to use the CcToolchainConfigInfo. See the documentation at https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html and https://docs.bazel.build/versions/master/cc-toolchain-config-reference.html.