Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

bazel

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?

like image 922
ryancheung Avatar asked Oct 26 '16 10:10

ryancheung


People also ask

Which compiler does Bazel use?

On Ubuntu, the default compiler is the first gcc compiler in the PATH . On macOS, the default compiler is the Apple LLVM compiler. To use Clang on Ubuntu, add --config=clang after any bazel build, bazel test or any other bazel commands.

What C++ compiler does Bazel use?

Bazel still uses g++ as compiler. After an hour, Bazel uses clang++.

Where is Bazelrc?

bazelrc in your workspace directory (next to the main WORKSPACE file). It is not an error if this file does not exist.

What is Bazel toolchain?

A Bazel toolchain is a configuration provider that tells a build rule what build tools, such as compilers and linkers, to use and how to configure them using parameters defined by the rule's creator.


2 Answers

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.

like image 175
hlopko Avatar answered Sep 20 '22 14:09

hlopko


bazel build --cxxopt='-std=c++11' main:hello-world This would work, but I wonder if there's way to set this cxxopt globally, like CMAKE_CXX_FLAGS.

like image 38
ryancheung Avatar answered Sep 18 '22 14:09

ryancheung