6

我在我的 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(我的 go 二进制文件)

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-
4

1 回答 1

1

您已经接近了,但是您需要将 cdeps 添加到融合的 Kafka go 库中,而不是添加到您的 go_binary 目标中。我假设您正在使用 Gazelle 和 rules_go 来引入外部部门。您需要使用 go_repository “patch” 属性来传递补丁文件,以便在生成的 BUILD 文件中对其进行排序。

我在最近的一篇博文中记录了这一点:

https://rotemtam.com/2020/10/30/bazel-building-cgo-bindings/

于 2020-10-31T17:59:06.443 回答