簡體   English   中英

添加 boost 庫作為 Bazel 依賴 C++

[英]Add boost library as Bazel dependency c++

我有以下設置將 boost 庫添加到我的 C++ 項目中。 /opt/boost/release包含我的 boost includelib目錄。我的 boost 版本是 1.65.1 並按照文檔使用以下命令構建。

$ sudo ./bootstrap.sh --prefix=release --with-python=python3 --with-icu=
$ ./b2 install -j 4

我試過有和沒有--with-python=python3標志。

工作空間

new_local_repository(
    name = "boost",
    build_file = "boost.BUILD",
    path = "/opt/boost/release",
)

boost.BUILD

cc_library(
    name = "boost",
    srcs = glob(["lib/*.so*"]),
    hdrs = glob(["include/**/*.hpp", "include/**/*.h"]),
    includes = ["include"],
    visibility = ["//visibility:public"],
    linkstatic = 1,
)

建立

cc_binary(
    name = "experiments",
    srcs = ["main.cpp"],
    deps = [
        "@boost//:boost",
    ],
)

主程序

#include <boost/progress.hpp>

int main()
{
    return 0;
}

當我使用 bazel build 命令構建它時

bazel build //experiments:experiments

我收到以下錯誤。

ERROR: /home/nipuna/vinci/engine/experiments/BUILD:1:1: Linking of rule '//experiments:experiments' failed (Exit 1): gcc failed: error executing command 
  (cd /home/nipuna/.cache/bazel/_bazel_nipuna/d2b463266f25f5449a4796ce18e325db/execroot/__main__ && \
  exec env - \
    LD_LIBRARY_PATH=/home/nipuna/software/clion-2017.2.3/bin: \
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -o bazel-out/local-fastbuild/bin/experiments/experiments '-Wl,-rpath,$ORIGIN/../_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib' -Lbazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib '-fuse-ld=gold' -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/bin -B/usr/bin -pass-exit-codes -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/experiments/experiments-2.params)

Use --sandbox_debug to see verbose messages from the sandbox.
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyCObject_Type'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyObject_CallFunction'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyErr_SetString'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyImport_ImportModule'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyEval_CallFunction'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyExc_AttributeError'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyErr_Print'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to '_Py_NoneStruct'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyObject_GetAttrString'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyExc_RuntimeError'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyCObject_AsVoidPtr'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyInt_FromLong'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyObject_Size'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyErr_Occurred'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyErr_Format'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyObject_IsTrue'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyBool_FromLong'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyLong_FromUnsignedLong'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyExc_ValueError'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyObject_CallMethod'
bazel-out/local-fastbuild/bin/_solib_k8/_U@boost_S_S_Cboost___Uexternal_Sboost_Slib/libboost_numpy.so: error: undefined reference to 'PyExc_ImportError'
collect2: error: ld returned 1 exit status
Target //experiments:experiments failed to build
INFO: Elapsed time: 0.422s, Critical Path: 0.10s

由於您將可執行文件鏈接到 Boost Python/Numpy,因此您還需要提供 Python 符號。

如果您不需要 Python,最簡單的解決方案是添加libboost_numpy.soexclude在您的 glob 中。 如果你確實需要的Python,速戰速決是添加-lpythonlinkoptsBOOST.build (這不是一個解決方案,我會建議進行生產,因為你有過Python版本沒有控制,等等。)

您可以使用由 Bazel 組織維護的rules_foreign_cc

工作空間

workspace(name = "example")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""

http_archive(
        name = "rules_foreign_cc",
        strip_prefix = "rules_foreign_cc-master",
        url = "https://github.com/bazelbuild/rules_foreign_cc/archive/master.zip",
)

load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()

http_archive(
        name = "boost",
        build_file_content = all_content,
        strip_prefix = "boost_1_68_0",
        sha256 = "da3411ea45622579d419bfda66f45cd0f8c32a181d84adfa936f5688388995cf",
        urls = ["https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.tar.gz"],
)

建立

load("@rules_foreign_cc//tools/build_defs:boost_build.bzl", "boost_build")

boost_build(
    name = "boost_fiber",
    lib_source = "@boost//:all",
    static_libraries = ["libboost_fiber.a"],
    user_options = ["--with-fiber"],
    visibility = ["//visibility:public"],
    deps = [":boost_context"],
)

boost_build(
    name = "boost_context",
    lib_source = "@boost//:all",
    static_libraries = ["libboost_context.a"],
    user_options = ["--with-context"],
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "helloworld",
    srcs = [
        "main.cc",
    ],
    deps = [
        ":boost_fiber",                      
    ],
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM