11

I am getting what I think is a strange seg fault when I am trying to pass boost::numpy::ndarray as an argument:

#include <iostream>
#include <boost/python.hpp>
#include <boost/numpy.hpp>


void say_hello(boost::numpy::ndarray& my_array)
//void say_hello(int x) This works fine
{
  std::cout<<"Hello"<<std::endl;
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("say_hello", say_hello);

}

I know the example is silly, but I should not be getting a seg fault there, and this is the smallest example I was able to reduce the problem to. Maybe I need to specify the ndarray type or number of dimensions, but I could not find any documentation on it. I was looking at this, but it didn't seem very helpful. My gut feeling is I am missing something simple, but I just don't see it.

When I run this:

In [1]: from hello_ext import *

In [2]: import numpy as np

In [3]: say_hello(np.array([3,4,5]))
Segmentation fault (core dumped)

My Makefile:

PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)


BOOST_INC = /usr/include
BOOST_LIB = /usr/lib

TARGET = hello_ext

$(TARGET).so: $(TARGET).o
    g++ -std=c++11 -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python -lboost_numpy -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so

$(TARGET).o: $(TARGET).cpp
    g++ -std=c++11 -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cp

1 Answer 1

11

I knew it was something simple. I needed to add these two lines:

Py_Initialize();
boost::numpy::initialize();

as explained : here seg fault results after any attempt to use boost::numpy::ndarray if the above lines are not ran.

Therefore: my file becomes:

#include <iostream>
#include <boost/python.hpp>
#include <boost/numpy.hpp>


void say_hello(boost::numpy::ndarray& my_array)
//void say_hello(int x) This works fine
{
  std::cout<<"Hello"<<std::endl;
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    Py_Initialize();
    boost::numpy::initialize();
    def("say_hello", say_hello);
}
Sign up to request clarification or add additional context in comments.

2 Comments

But in the linked example, the initialisation is done in a C++ main(). Here, we are in a python module.
Thank you very much!! This is the kind of cryptic segfault not even GDB can help with

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.