Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add boost library as Bazel dependency c++

Tags:

c++

boost

bazel

I have the following setup to add boost library to my c++ project. /opt/boost/release contains my boost include & lib directories.My boost version is 1.65.1 and built with following command as per the documentation.

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

I tried both with and without --with-python=python3 flag.

WORKSPACE

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

BUILD

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

main.cpp

#include <boost/progress.hpp>

int main()
{
    return 0;
}

And when I build this using the bazel build command

bazel build //experiments:experiments

I get the following error.

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
like image 518
nipunasudha Avatar asked Nov 27 '17 17:11

nipunasudha


1 Answers

Since you're linking your executable to Boost Python/Numpy, you also need to provide Python symbols.

If you don't need Python, the easiest solution is add libboost_numpy.so to exclude in your glob. If you actually need Python, a quick fix would be to add -lpython to linkopts in BOOST.build (this is not a solution that I would recommend for production, since you have no control over Python versions, etc..)

like image 186
nicolov Avatar answered Nov 15 '22 00:11

nicolov