Right way to add .h dependency to go_library using cgo?

34 views
Skip to first unread message

HaiXin Tie

unread,
Sep 30, 2024, 1:51:31 AM9/30/24
to bazel-discuss
Hi Bazel Cgo experts,

I am trying to build a go_binary that depends on com_github_pebbe_zmq4, a go wrapper of the libzmq in C++. I've been struggling to make pebbe/zmq4 "see" zmq.h in libzmq, and can't make it work after trying multiple approaches:

Approach 1. Building using libzmq from Bazel Central Registry: bazel-cgo/BUILD.bazel at main · thx123/bazel-cgo (github.com)

load("@rules_go//go:def.bzl", "go_binary")

go_binary(
    name = "zmq_client",
    srcs = ["zmq_client.go"],
    deps = [
        "@com_github_pebbe_zmq4//:go_default_library",
    ],
    cgo = True,
    cdeps = [
        # Including the full library doesn't make it work, either.
        # "@libzmq//:libzmq",
        "@libzmq//:libzmq_headers_only",
    ],
)
# Minimal code example on Github: thx123/bazel-cgo (github.com)


Approach 2. Building libzmq using rules_foreign_cc: bazel-cgo/BUILD.bazel at fcc · thx123/bazel-cgo (github.com):

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
load("@rules_go//go:def.bzl", "go_binary")

package(default_visibility = ["//visibility:public"])

# Dependency of cppzmq
cmake(
    # Note: This exact name is required to make "cppzmq cmake target work.
    name = "zmq",
    # This uses Ninja to build under the hood, which is faster than make.
    #generate_args = ["-GNinja"],
    lib_source = "@zeromq//:all_srcs",
    out_static_libs = ["libzmq.a"],
)

go_binary(
    name = "zmq_client",
    srcs = ["zmq_client.go"],
    deps = [
        "@com_github_pebbe_zmq4//:go_default_library",
    ],
    cgo = True,
    cdeps = [
        ":zmq",
    ],
)
# Minimal code example on Github: thx123/bazel-cgo at fcc (github.com)

What am I missing? I've read through go_binary and rules_foreign_cc docs multiple times but can't find the right way, also tried the Bazel Slack #Go channel here, but haven't found out the solution yet, so would appreciate some guidance from Cgo experts. Thanks!


Regards,
HaiXin
Reply all
Reply to author
Forward
0 new messages