Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with linking boost::python::numpy

I wrote a small example to show what's going on.

my_test.cpp

#include <iostream>
#include <boost/python/numpy.hpp>
namespace np = boost::python::numpy;
int my_Foo() 
{
    Py_Initialize();
    np::initialize();
    std::cout << "hello\n";
    return 0;
}
BOOST_PYTHON_MODULE(my_test)
{
    using namespace boost::python;
    def("my_Foo", my_Foo);
}

py_test.py

import my_test as t
t.my_Foo();

I compile all with command :

g++ -shared -fPIC -o my_test.so my_test.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

And I get this error :

ImportError: /home/my_test.so: undefined symbol: _ZN5boost6python5numpy10initializeEb

And it works when I comment this line

//np::initialize();

I have no idea how to fix it. I have read similar questions on the forum, but none of the solutions helped me. I tried update boost, update python, link libraries, put generated module before other module during compilation - nothing helps. I will be grateful for any help.

like image 204
Dominik Avatar asked Jun 26 '18 08:06

Dominik


Video Answer


1 Answers

On Bionic -lboost_python is not enough. You are missing -lboost_numpy.

On Xenial you won't find prebuilt libraries yet:

sudo apt -y install libpython2.7-dev libboost-python-dev
git clone https://github.com/ndarray/Boost.NumPy
cd Boost.Numpy
mkdir build
cd build
cmake ..
make 
sudo make install

replace in your code boost/python/numpy.hpp with boost/numpy.hpp also replace namespace np = boost::python::numpy with namespace np = boost::numpy; |

g++ -o test5.so -fPIC -shared test5.cpp -lboost_python -lboost_numpy -I/usr/local/include -I/usr/include/x86_64-linux-gnu/python2.7/ -I/usr/include/python2.7
enter code here

 ~> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64 ldd test5.so 
   linux-vdso.so.1 =>  (0x00007ffe9cd36000)
   libboost_python-py27.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0 (0x00007ffba47bd000)
   libboost_numpy.so => /usr/local/lib64/libboost_numpy.so (0x00007ffba45a2000)
   libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ffba4216000)
   libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ffba3ffe000)
   libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffba3c34000)
   libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ffba3a17000)
   libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007ffba3489000)
   libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ffba3180000)
   /lib64/ld-linux-x86-64.so.2 (0x00007ffba4c11000)
   libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ffba2f66000)
   libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ffba2d62000)
   libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007ffba2b5f000)
like image 194
Kaveh Vahedipour Avatar answered Sep 23 '22 12:09

Kaveh Vahedipour