简体   繁体   English

使用 bazel 如何构建和安装依赖于 protobuf 的 C++ 库?

[英]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我正在构建一个具有以下依赖项的 32 位 C++ 库

#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将以下内容作为 BUILD.bazel 文件

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 文件:

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 c++filt_ZTIN6google8protobuf8compiler23MultiFileErrorCollectorE

typeinfo for google::protobuf::compiler::MultiFileErrorCollector 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?我已经尝试通过在链接 opts 中取消注释 -lprotobuf 来链接 protobuf?:导致/usr/bin/ld.gold: error: cannot find -lprotobuf在构建过程中/usr/bin/ld.gold: error: cannot find -lprotobuf

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..这里的问题是 protobuf 在 64 位系统上构建为 32 个库,并且正在使用系统 protobuf 安装,即apt-get install protobuf etc..

This is the first time I am using bazel.这是我第一次使用 bazel。 And thus as opposed to cmake where one could use DCMAKE_PREFIX_PATH to specify the install dir of protobuf.因此,与可以使用 DCMAKE_PREFIX_PATH 指定 protobuf 的安装目录的 cmake 不同。 How should one link a 32bit c++ library with protobuf such that it can be used without throwing symbol errors?应该如何将 32 位 C++ 库与 protobuf 链接起来,以便可以在不抛出符号错误的情况下使用它?

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 .使用--copt=-m32 --linkopt=-m32 (在命令行上)而不是coptslinkopts

Having protobuf in deps is what tells bazel to link against it.deps有 protobuf 是告诉 bazel 链接它的原因。 If you want to do a 32-bit build, -m32 needs to be used when compiling everything.如果要进行 32 位构建,则在编译所有内容时都需要使用-m32 Currently you only have it while compiling and linking your target, so it's trying to link against a 64-bit protobuf.目前您只有在编译和链接目标时才拥有它,因此它试图链接到 64 位 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.如果您不想每次构建时都键入这些命令行标志,请将build --copt=-m32 --linkopt=-m32放在 WORKSPACE 旁边的.bazelrc 文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ 中使用 bazel 构建 Google tink 库? - How to build Google tink library using bazel in C++? 如何使用 Bazel 在 Windows 上使用 C++ API 库进行编程? - How to program with C++ API library on Windows using Bazel? 如何使用 Eigen 和 Bazel 构建一个简单的 C++ 演示? - How to build a simple C++ demo using Eigen with Bazel? protobuf C ++编译器的bazel规则 - bazel rules for the protobuf C++ compiler 如何使用Bazel构建包含openssl的第三方c ++库? - How to use Bazel to build third party c++ library that includes openssl? Bazel C++ 如何包含一个需要从 github 克隆的库,然后在它可以链接之前构建? - Bazel C++ how to include a library that needs to be cloned from github then build before it can be linked? 使用Bazel构建系统构建具有循环依赖关系的C ++代码 - Build C++ code with Circular dependency using Bazel Build System 为什么 Bazel 找不到 Visual C++ 构建工具? - Why Bazel does not find Visual C++ Build Tools? 如何在Windows 10 x64上使用Bazel构建和调试C ++可执行文件 - How to build and debug a c++ executable using Bazel on windows 10 x64 如何使用安装在Python站点包中的TensorFlow配置Bazel以构建C ++应用 - How to configure bazel to build C++ app using tensorflow installed in python site-packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM