c++ - 在使用 Bazel Build 构建项目期间处理/选择接口(interface)的不同实现

标签 c++ build bazel

我在 cpp 中有两个接口(interface)实现,我想每次在构建 (Bazel Build) 项目时选择其中一个实现,并且每次生成的可执行应用程序都应具有相同的名称。 我的 BUILD 文件如下所示:

cc_library(
name = "printme1",
srcs = ["Firstimplementation.cpp"],
hdrs = ["source.h"],
)

cc_library(
name = "printme2",
srcs = ["Secondimplementation.cpp"],
hdrs = ["source.h"],
)

cc_binary(

name = "call",
srcs = ["main.cpp"],
deps = [
        ":printme1",
    ],

)

cc_binary(

name = "call2",
srcs = ["main.cpp"],
deps = [
        ":printme2",
    ],

)

使用此 BUILD 文件,我可以获得两个不同的结果,但生成的可执行应用程序具有不同的名称。我正在构建它如下:

$ bazel build call ---> 在 Gen-bin 文件夹中使用名称 call 给我可执行文件

$ bazel build call2 ---> 在 Gen-bin 文件夹中使用名称 call2 给我可执行文件

最后我应该怎么做才能有相同的名字,我可以在每次我自己的选择实现时调用 include。

提前致谢。

最佳答案

您可以使用 select连同 config_setting . 应该是这样的:

config_setting(
    name = "printme1",
    values = {
        "define": "p1",
    },
)

config_setting(
    name = "printme2",
    values = {
        "define": "p2",
    },
)

cc_library(
    name = "printme",
    srcs = select({":printme1" : ["Firstimplementation.cpp"],
                   ":printme2" : ["Secondimplementation.cpp"]}),
    hdrs = ["source.h"],
)

cc_binary(
    name = "call",
    srcs = ["main.cpp"],
    deps = [
        ":printme",
    ])

在这种情况下,您需要在构建应用时定义:

bazel build --define p1 :call

关于c++ - 在使用 Bazel Build 构建项目期间处理/选择接口(interface)的不同实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47744150/

相关文章:

c++ - 如何在 C++ 中的大端和小端值之间进行转换?

c++ - 如何让我的代码从矩阵输出非零元素?

c++ - 为什么我的编译器不喜欢这个语句?

java - Ant Input Task中如何判断输入是否为空?

node.js - npm 脚本的时间/性能指标

c++ - 在清除之前检查一个 vector ?

python - 使用 SCons 进行真正的分层构建?

bazel - 如何让 Bazel、ccache 和沙箱协同工作(ccache 只读文件系统)

opencv - 预计在平面命名空间中出现 OSX Tensorflow + Opencv : Symbol Not Found,

c++ - 使用 Bazel 更改编译器命令行