Bazel 中基于编译器(gcc vs clang)的配置 "copts"

标签 c gcc clang bazel

我有一个基于 Bazel 的 C 项目,我想用 gcc 编译它和 clang在 Linux 上。要在编译器之间切换,我在调用前加上 CC=<compiler 前缀,像这样:

CC=clang bazel build //my-package:my-target

这按预期工作,除了我还使用 copts 调用 Bazel 规则参数:

cc_binary(
    name = "my-target",
    srcs = [
        "my-src.c",
    ],
    copts = [
        "-std=c89",
        "-Wno-unused-label",
        "-Wno-unused-variable",
        "-Wno-unused-but-set-variable",
        "-Wno-misleading-indentation",
    ],
)

这有效,但我从 clang 收到“未知警告选项”消息:

warning: unknown warning option '-Wno-unused-but-set-variable'; did you mean '-Wno-unused-const-variable'? [-Wunknown-warning-option]

warning: unknown warning option '-Wno-misleading-indentation'; did you mean '-Wno-binding-in-condition'? [-Wunknown-warning-option]

是否可以指定 copts用于特定 Bazel 调用的 C 编译器的参数条件

最佳答案

创建一个 .bazelrc 文件并使用 --config 选项。

我使用 Visual Studio 2019 和 GCC 9 来构建我的代码。

我的.bazelrc:

build:vs2019 --cxxopt=/std:c++17

build:gcc9 --cxxopt=-std=c++17

Visual Studio 2019

构建

bazel build --config=vs2019 //...

测试

bazel test --config=vs2019 //...

海湾合作委员会

构建

bazel build --config=gcc9 //...

测试

bazel test --config=gcc9 //...

例如 FlatlandRT 使用这种方法来支持不同的编译器 -> 请参阅 .bazelrc

关于Bazel 中基于编译器(gcc vs clang)的配置 "copts",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58683098/

相关文章:

C 代码在 Windows 上运行(使用 Visual Studio 编译)但不能在 Linux 上编译(使用 gcc)

c++ - 为什么 std::swap 不能在 Clang/Win 下的 vector<bool> 元素上工作?

c++ - 使用 Clang 工具解析 C++ 标准头文件时出错

c++ - 他们实现的开源 C 编译器(gcc、clang)的实际标准在哪里?

c - 在尝试理解指针时,我有以下问题

android - __attribute__ ((__constructor__)) 在安卓中

c - 为什么我无法在C语言中使用for循环比较两个字符串[如果字符串彼此相反]

c++ - 是否允许编译器优化堆内存分配?

c++ - 错误 : wx/setup. h:CodeBlocks 上没有这样的文件或目录 wxWidgets

c++ - 使用 C++ 回调围绕 C++ 类编写 C 包装器