gpt4 book ai didi

bazel rules_go : linking go binary against a static c++ library (. a file) 由工作区中的另一个目标生成

转载 作者:行者123 更新时间:2023-12-03 14:48:58 25 4
gpt4 key购买 nike

我在我的 go 二进制文件中使用 confluent-kafka-go 库,这个库需要与 librdkafka 链接。我项目中的其他目标使用 librdkakfa,所以我使用 rules_foreign_cc 的 cmake_external 规则生成了静态 librdkafka.a 和 librdkafka++.a:

   //this is my "third_party/kafka/BUILD" file:

load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")

cmake_external(
name = "librdkafka",
cache_entries = {
"RDKAFKA_BUILD_STATIC": "ON",
"WITH_ZSTD": "OFF",
"WITH_SSL": "OFF",
"WITH_SASL": "OFF",
"ENABLE_LZ4_EXT": "OFF",
"WITH_LIBDL": "OFF",
},
lib_source = "@kafka//:all",
static_libraries = [
"librdkafka++.a",
"librdkafka.a",
],
visibility = ["//visibility:public"],
)

生成 librdkafka 库和头文件就好了:
 $ bazel build //third_party/kafka:librdkafka 
INFO: Analysed target //third_party/kafka:librdkafka (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //third_party/kafka:librdkafka up-to-date:
bazel-genfiles/third_party/kafka/librdkafka/include
bazel-genfiles/third_party/kafka/librdkafka/lib/librdkafka++.a
bazel-genfiles/third_party/kafka/librdkafka/lib/librdkafka.a
bazel-genfiles/third_party/kafka/copy_librdkafka/librdkafka
bazel-genfiles/third_party/kafka/librdkafka/logs/CMake_script.sh
bazel-genfiles/third_party/kafka/librdkafka/logs/CMake.log
bazel-genfiles/third_party/kafka/librdkafka/logs/wrapper_script.sh
INFO: Elapsed time: 0.187s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

现在我试图在我的 go_binary 规则中链接 librdkafka.a(我不需要 librdkafka++.a 文件,只需要 c 版本):
    go_binary(
name = "foo",
srcs = ["foo.go"],
cdeps = [
"//third_party/kafka:librdkafka",
],
cgo = "True",
static = "on",
visibility = ["//visibility:public"],
deps = [
":ox_go_proto",
"@com_github_confluentinc_confluent_kafka_go//kafka:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
],
)

但是当我运行时,我得到了数百个未定义的 rdkafka 引用 bazel build //foo:foo (我的二进制文件)
unction _cgo_52d112b951a8_Cfunc_rd_kafka_AdminOptions_destroy: error: undefined reference to 'rd_kafka_AdminOptions_destroy'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_AlterConfigs: error: undefined reference to 'rd_kafka_AlterConfigs'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_AlterConfigs_result_resources: error: undefined reference to 'rd_kafka_AlterConfigs_result_resources'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_read_only: error: undefined reference to 'rd_kafka_ConfigEntry_is_read_only'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_sensitive: error: undefined reference to 'rd_kafka_ConfigEntry_is_sensitive'
bazel-out/k8-fastbuild/bin/external/com_github_confluentinc_confluent_kafka_go/kafka/_objs/go_default_library%linux_amd64%cgo_c_lib/adminapi.cgo2.pic.o:adminapi.cgo2.c:function _cgo_52d112b951a8_Cfunc_rd_kafka_ConfigEntry_is_synonym: error: undefined reference to 'rd_kafka_ConfigEntry_is_synonym'
bazel-out/k8-

最佳答案

您已经接近了,但是您需要将 cdep 添加到融合的 Kafka go 库中,而不是添加到您的 go_binary 目标中。我假设您正在使用 Gazelle 和 rules_go 来拉入外部 deps。您需要使用 go_repository “patch” 属性来传递补丁文件以在生成的 BUILD 文件中对其进行排序。
我在最近的一篇博客文章中记录了这一点:
https://rotemtam.com/2020/10/30/bazel-building-cgo-bindings/

关于bazel rules_go : linking go binary against a static c++ library (. a file) 由工作区中的另一个目标生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55698979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com