简体   繁体   中英

Using bazel how does one build and install a c++ library that depends on protobuf?

I am building a 32bit c++ library that has the following dependencies

#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/dynamic_message.h>

with the following as a BUILD.bazel file

COPTS = select({
    "//conditions:default": [ 
        "-Wall",
        "-m32",
        "-fPIC"
    ],
})

LINK_OPTS = select({
    "//conditions:default": [
        "-m32",
        "-fPIC",
        # "-lpthread",
        # "-lprotobuf",
        # "-pthread"
    ],
})

.. proto libraries ...

cc_library(
    name = "proto_example", 
    srcs = [
        ....
    ],
    hdrs = glob([
        ...
    ]),
    copts = COPTS,
    includes = ["src/", "./"],
    linkopts = LINK_OPTS,
    visibility = ["//visibility:public"],
    deps = [
      "@com_google_protobuf//:protobuf",
      ":cpp_examples_proto"
    ]
)

And the following as the WORKSPACE file:

workspace(name = "protobufkdb")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_proto_grpc",
    sha256 = "7954abbb6898830cd10ac9714fbcacf092299fda00ed2baf781172f545120419",
    strip_prefix = "rules_proto_grpc-3.1.1",
    urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/3.1.1.tar.gz"],
)

load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_toolchains", "rules_proto_grpc_repos")
rules_proto_grpc_toolchains()
rules_proto_grpc_repos()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

load("@rules_proto_grpc//cpp:repositories.bzl", rules_proto_grpc_cpp_repos = "cpp_repos")
rules_proto_grpc_cpp_repos()

# C++
load("@rules_proto_grpc//cpp:repositories.bzl", "cpp_repos")
cpp_repos()

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()

http_archive(
    name = "com_google_protobuf",
    sha256 = "c6003e1d2e7fefa78a3039f19f383b4f3a61e81be8c19356f85b6461998ad3db",
    strip_prefix = "protobuf-3.17.3",
    urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.17.3.tar.gz"],
)

However when I include/embed the library in a given client I am presented with the error:

undefined symbol: _ZTIN6google8protobuf8compiler23MultiFileErrorCollectorE

The following is evident thereof:

c++filt _ZTIN6google8protobuf8compiler23MultiFileErrorCollectorE

typeinfo for google::protobuf::compiler::MultiFileErrorCollector

I have tried linking protobuf by uncommenting -lprotobuf in the link opts?: results in /usr/bin/ld.gold: error: cannot find -lprotobuf during the build?

The problem here is that protobuf is being build as a 32 library on a 64 bit system and the system protobuf installation is being used ie apt-get install protobuf etc..

This is the first time I am using bazel. And thus as opposed to cmake where one could use DCMAKE_PREFIX_PATH to specify the install dir of protobuf. How should one link a 32bit c++ library with protobuf such that it can be used without throwing symbol errors?

Could anyone provide me with some insight on how to get this working? Thanks

Use --copt=-m32 --linkopt=-m32 (on the command line) instead of copts and linkopts .

Having protobuf in deps is what tells bazel to link against it. If you want to do a 32-bit build, -m32 needs to be used when compiling everything. Currently you only have it while compiling and linking your target, so it's trying to link against a 64-bit protobuf.

If you don't want to type those command line flags every time you build, put build --copt=-m32 --linkopt=-m32 in a .bazelrc file next to your WORKSPACE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM