27

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?

RAM
  • 2,257
  • 2
  • 19
  • 41
ryancheung
  • 2,999
  • 3
  • 24
  • 25

3 Answers3

36

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.

hlopko
  • 3,100
  • 22
  • 27
  • 48
    Kind of outrageous that a tool that was created to "easily" build C++ projects doesn't even support the most obvious C++ compiler option. We can't be incorporating a whole new framework into our system for every tiny thing we want to do. Is there really no way to specify in the BUILD file? – Apollys supports Monica Jun 01 '19 at 01:29
  • 1
    Your CROSSTOOL link is now broken, and if I'm reading [this](https://docs.bazel.build/versions/0.27.0/skylark/backward-compatibility.html#disallow-using-crosstool-to-select-the-cc_toolchain-label) correctly the CROSSTOOL file is now discouraged. I can't make heads or tails of the [toolchain](https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html) page, but at least `--cxxopt=...` seems to work. – dimo414 Feb 22 '20 at 08:56
  • 6
    it is brittle to use --cxxopt to select a C++ standard if you are building in different platforms. for example --cxxopt=''-std=c++11" will work in linux gcc, but fails when using bazel for windows (which uses MSVC and requires --cxxopt="-std:c++11" – thinlizzy May 09 '20 at 20:12
  • 6
    I found that adding a .bazelrc file with the contents `build --cxxopt='-std=c++17'` in the same directory as the WORKSPACE file did the trick. I don't think it is reasonable to put a config like this in the BUILD file because it could lead to mixing C++ standards across BUILD files. Tying it to the workspace makes more sense, as there can be only one workspace (and hence one C++ standard) in which you are building at a time. – Randall Cook Feb 07 '21 at 18:03
9

Add this to your .bazelrc next to your WORKSPACE:

build --action_env=BAZEL_CXXOPTS="-std=c++20"

If you want to set multiple options, separate them with a colon:

build --action_env=BAZEL_CXXOPTS="-std=c++20:-Werror"

It is kind of a workaround as bazel sets an environment variable which bazel then uses. But it works.


BTW: build --cxxopt=-std=c++20 in the .bazelrc did not work form me.

Thomas Deniffel
  • 153
  • 1
  • 7
  • Separating multiple options with colons did not work for me; I had to put multiple `build --cxxopt='…'` lines in `.bazelrc` (which should be next to `WORKSPACE`). – Julia Nov 14 '22 at 11:45
  • @Thomas I have the same problem. ubuntu 18.04 and bazel version is 6.0.0. But when I try it on mac (bazel version : 6.0.0) it's OK. What's yours? – Daniel Lee Jan 06 '23 at 03:20
  • @DanielLee I am not quite sure. I guess it was either Ubuntu 20.04 back then - I upgraded since then. – Thomas Deniffel Jan 27 '23 at 09:01
8

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.

ryancheung
  • 2,999
  • 3
  • 24
  • 25
  • 1
    There is also BAZEL_CXXOPTS (and BAZEL_LINKOPTS) environment variable that you can set (https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_configure.bzl#L364). Flag separator is colon. – hlopko Oct 12 '19 at 07:56
  • 5
    Yes, this can be set globally by adding a line like `build --cxxopt='-std=c++17'` to a .bazelrc file in the same directory as your WORKSPACE file. – Randall Cook Feb 07 '21 at 18:05
  • 1
    `build --cxxopt='-std=c++17'` did not work for me, but `build --action_env=BAZEL_CXXOPTS="-std=c++17"` did. – Thomas Deniffel Jan 26 '22 at 16:59