我在 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/