将Boost库添加为Bazel依赖性C
Add boost library as Bazel dependency c++
我有以下设置,可以将Boost库添加到我的C 项目。 /opt/boost/release
包含我的Boost include
&lib
Directories.my 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",
],
)
main.cpp
#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.so
添加到exclude
中。如果您实际上需要Python,那么快速修复将是将-lpython
添加到BOOST.build
中的linkopts
(这不是我建议生产的解决方案,因为您无法控制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",
],
)
相关文章:
- 没有 srcs 的 Bazel cc_library 不会自行编译
- 使用 Bazel 生成 QT 资源文件
- 可视化 Bazel C++预编译标头实现
- 使用 Bazel 编译 QT 应用程序时访问资源
- 为Bazel工具链指定sysroot
- 如何在 Bazel 项目中设置 Catch2
- 如何将 glog 与 bazel 一起使用?
- 使用Bazel构建具有不同编译器/链接器选项的C/C++依赖关系
- 如何在 Bazel 构建命令期间传递 FLAG 如 -DVERBOSE?
- 在 macOS 上的 CLion 中导入 Bazel 项目失败
- Bazel:http_archive()中url的相对本地路径
- 使用Bazel C++教程;build不创建/使用共享库
- 如何在bazel中同时处理外部依赖项和我的项目
- 在Bazel中显示编译器信息
- C++ / Bazel:如何包括角<>支架系统接头?
- 安装使用Bazel构建的库
- 使用Bazel构建Tensorflow
- Bazel 允许我包含来自全局安装库的标头
- 使用Bazel构建系统构建具有循环依赖性的C++代码
- 将Boost库添加为Bazel依赖性C