6

Using Boost 1.63.0, I've coded up the following:
vectors.cpp

/* Boost/Python headers */ 
#include<boost/python/module.hpp>
#include<boost/python/def.hpp>
#include<boost/python/extract.hpp>
#include<boost/python/numpy.hpp>
#include<cmath>

using namespace boost::python;
namespace np = boost::python::numpy;

double eucnorm(np::ndarray axis){

  const int n = axis.shape(0);
  double norm = 0.0;
  for(int i = 0; i < n; i++){
    double A = boost::python::extract<double>(axis[i]);
    norm += A*A;
  }
  return sqrt(norm);
}

BOOST_PYTHON_MODULE(vectors){
  def("eucnorm", eucnorm);
}

I've compiled this using:
g++ -shared -fpic -I /usr/include/python2.7 -I /foo/bar/boost_1_63_0 -lboost_python -o vectors.so

And I get the following error upon import:

from vectors import *
ImportError: ./vectors.so: undefined symbol: _ZN5boost6python9converter21object_manager_traitsINS0_5numpy7ndarrayEE10get_pytypeEv

What does this mean, and how do I fix this?

thestatnoob
  • 193
  • 2
  • 8

1 Answers1

10

Add:

-lboost_numpy -lboost_python 

When you build the .so.

By the way, if you want to find out about such problems at build time (rather than having to try import in Python): Force GCC to notify about undefined references in shared libraries

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • My machine doesn't have sudo rights, so if I try with `-lboost_numpy` (my machine has boost 1.60 under /usr/include...) then I get a `/bin/ld: cannot find -lboost_numpy collect2: error: ld returned 1 exit status` error. Instead I've tried `-L /foo/bar/boost_1_63_0/lib` but that returns the same `ImportError` from Python. – thestatnoob Feb 16 '17 at 14:51
  • @thestatnoob: You need both `-L/your/path/to/boost` and ALSO `-lboost_numpy`. Did you try with both? The -L gives the directory, `-l` gives the filename. – John Zwinck Feb 16 '17 at 15:03
  • It works, for the most part. If I compile as: `g++ vectors.cpp -shared -fpic -Wno-undef -I /usr/include/python2.7 -I /path/to/boost/include/ -L /path/to/boost/lib/ -lboost_numpy -lboost_python -o vectors.so` it works OK. One downside: I need the `libboost_numpy` files in my working directory for it to work :( – thestatnoob Feb 16 '17 at 15:38
  • 1
    Thanks for pointing me to the `-Wl,--no-undefined` option. It is very helpful. – sfinkens Apr 10 '18 at 13:45