How to use select to properly detect whether I am building C++ code in Windows or Linux?

后端 未结 2 1904
故里飘歌
故里飘歌 2021-01-01 20:05

I am writing a sample C++ project that uses Bazel to serve as an example idiom for other collaborators to follow.

Here is the repository: https://github.com/thinlizz

相关标签:
2条回答
  • 2021-01-01 20:21

    Add a .bazelrc to your project. Add the lines build:vs2019 --cxxopt=/std:c++14 and build:gcc --cxxopt=-std=c++14. Build your code bazel build --config=msvc //... or bazel build --config=gcc //....

    0 讨论(0)
  • 2021-01-01 20:23

    @bazel_tools contains predefined platform conditions:

    $ bazel query @bazel_tools//src/conditions:all
    @bazel_tools//src/conditions:windows_msys
    @bazel_tools//src/conditions:windows_msvc
    @bazel_tools//src/conditions:windows
    @bazel_tools//src/conditions:remote
    @bazel_tools//src/conditions:host_windows_msys
    @bazel_tools//src/conditions:host_windows_msvc
    @bazel_tools//src/conditions:host_windows
    @bazel_tools//src/conditions:freebsd
    @bazel_tools//src/conditions:darwin_x86_64
    @bazel_tools//src/conditions:darwin
    

    You can use them directly in the BUILD file:

    cc_library(
      name = "impl",
      srcs = ["Implementation.cpp"] + select({
        "@bazel_tools//src/conditions:windows": ["ImplementationWin.cpp"],
        "@bazel_tools//src/conditions:darwin": ["ImplementationMacOS.cpp"],
         "//conditions:default": ["ImplementationLinux.cpp"],
      }),
      # .. same for hdrs and data
    )
    
    cc_binary(
      name = "demo",
      deps = [":impl"],
    )
    

    See the documentation for select for details on the syntax.

    0 讨论(0)
提交回复
热议问题