I'm trying to compile a target with c++17 with bazel and visual studio 2019 on Windows 10.
I tried having --cxxopt='-std=c++17'
inside .bazelrc
in the same directory of my workspace, but this didn't work.
For instance, I got the error
C:\Users\marki\plasty>bazel build --verbose_failures labeling:semantic_seg
INFO: Analyzed target //labeling:semantic_seg (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: C:/users/marki/plasty/labeling/BUILD:1:1: C++ compilation of rule '//labeling:semantic_seg' failed (Exit 2)
class template optional is only available with C++17 or later.
...
labeling/semantic_seg.cpp(183): error C2429: language feature 'structured bindings' requires compiler flag '/std:c++17'
Target //labeling:semantic_seg failed to build
INFO: Elapsed time: 1.184s, Critical Path: 0.84s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
I also tried --cxxopt='/std:c++17'
with similar results.
You need to add the option in cc_binary instead. For example:
cc_binary(
name = "hello-main",
srcs = ["hello-main.cpp"],
deps = [":hellolib"],
copts = ["/std:c++17"],
)
This will pass the option to the compiler cl.exe.
UPDATE
If you want to build with option in bazelrc, there is already an answer for that in: How to set C++ standard version when build with Bazel? except that it is using c++11, so for c++17, add these in .bazelrc
build:c++17 --cxxopt=-std=c++1z
build:c++17 --cxxopt=-stdlib=libc++
build:c++1z --cxxopt=-std=c++1z
build:c++1z --cxxopt=-stdlib=libc++