1

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.

Mark
  • 5,286
  • 5
  • 42
  • 73

1 Answers1

2

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++
kursun
  • 214
  • 2
  • 23
  • 1
    This worked. But I like how on linux you only have to specify the flag once in the .bazelrc. Is there a similar feature for bazel on Windows? – Mark Oct 02 '19 at 03:07
  • 1
    Update the answer for using option with bazelrc – kursun Oct 02 '19 at 03:30
  • 1
    I have some trouble when set `copts = ["/std:c++17"]`, I change to `copts = ["-std=c++11"],` – Carl Lee Apr 14 '21 at 03:18