HaiXin Tie
HaiXin Tie

Reputation: 2079

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

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:

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",
    ],
)

Link: Minimal code example on Github

Approach 2. Building libzmq using rules_foreign_cc:

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

cmake(
    name = "zmq",
    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",
    ],
)

Link: Minimal code example on Github

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.

Upvotes: 1

Views: 40

Answers (0)

Related Questions