I would like my Bazel project to use c 17. There is a similar question (How to set C standard version when build with Bazel?) but the accepted answer does not port to MSCV.
MSCV needs --cxxopt='/std:c 17'
while gcc needs --cxxopt='--std=c 17'
.
Does anyone have a minimal example configuration that builds on the most popular Windows and Linux compilers (gcc, MSCV, clang) that sets the version to c 17? I am having a real hard time understanding the toolchain documentation.
The following did not work.
# .bazelrc
build --cxxopt=select({
"@bazel_tools//src/conditions:windows": "/std:c 17",
"//conditions:default": "--std=c 17",
})
CodePudding user response:
You can do something like this in your .bazelrc
:
# GCC 9.3
build:gcc9 --cxxopt=-std=c 2a
build:gcc9 --cxxopt=-Wall
build:gcc9 --cxxopt=-Werror
##build:gcc9 --cxxopt=-Wextra
build:gcc9 --define compiler=gcc9_3_0
build:macos --cxxopt=-std=c 2a
build:macos --cxxopt=-Wall
#build:macos --cxxopt=-Werror
##build:macos --cxxopt=-Wextra
build:macos --define compiler=clang12
# Clang 13.0.0
build:clang13 --cxxopt=-std=c 17
#build:clang13 --cxxopt=-mavx
#build:clang13 --cxxopt=-mavx2
#build:clang13 --cxxopt=-msse4.2
#build:clang13 --cxxopt=-Werror
build:clang13 --define compiler=clang13
build:clang13 --incompatible_enable_cc_toolchain_resolution
build:clang13 --cxxopt=-mwaitpkg
# Mingw
build:mingw --cxxopt=-std=c 2a
#build:mingw --cxxopt=-Wall
#build:mingw --cxxopt=-Werror
#build:mingw --cxxopt=-Wextra
build:mingw --compiler=mingw-gcc
# Visual Studio 2019
build:vs2019 --cxxopt=/std:c 17
build:vs2019 --enable_runfiles # https://github.com/bazelbuild/bazel/issues/8843
build:vs2019 --define compiler=vs2019
build:vs2019 --copt=-DWIN32_LEAN_AND_MEAN
build:vs2019 --copt=-DNOGDI
build:vs2019 --host_copt=-DWIN32_LEAN_AND_MEAN
build:vs2019 --host_copt=-DNOGDI
# Visual Studio 2022
build:vs2022 --config=vs2019
And then select a config when you do a build e.g. bazel --config=vs2022 //...
When you are too lazy to type every time the config you can create in your home directory (e.g. ~/.bazelrc
a file that contains build --config=config_for_the_machine
.
Config switches are costly. Bazel needs to rerun a lot of stuff if you switch the config. If you switch on one machine often between different configs (e.g. GCC and Clang) then use a Bazel Remote Cache server. I use one on my local machine - this makes config switches cheap since the Bazel Remote Cache server is able to store artifacts for different configs whereas Bazel seems only to store artifacts for the latest config.
CodePudding user response:
In each and every cc_binary target of your project, include the following.
# BUILD
COPTS = select({
"@bazel_tools//src/conditions:windows": ["/std:c 17"],
"//conditions:default": ["--std=c 17"],
})
...
cc_binary(
name = "example",
srcs = ["example.cc"],
...
copts=COPTS
)
Of course this is vey tedious and will break if someone tries to, say, use gcc on Windows. I'd be very happy to accept a better answer.