Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbol using Boost/Python

Tags:

c++

python

boost

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?

like image 289
thestatnoob Avatar asked Feb 16 '17 14:02

thestatnoob


1 Answers

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

like image 68
John Zwinck Avatar answered Nov 03 '22 11:11

John Zwinck