2

From what I gather, a good way to do this is to use the Boot.Python library, like this simple example; please don't recommend alternatives like Cython as a solution. But when I try to use boost::python data types, my cpp file won't build.

example_boost.cpp:

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

#include <iostream>

namespace bpy = boost::python;
namespace bnp = boost::python::numpy;

void do_stuff(const bnp::ndarray& input_array) {
    ...
};

/*
 * This is a macro Boost.Python provides to signify a Python extension module. This enables me to import example_boost.cpp and call do_stuff() within a Python file. 
 */
BOOST_PYTHON_MODULE(crf) {
    // Expose the functions
    boost::python::def("compute_factor_out_msgs", compute_factor_out_msgs);
}

Running make...

Undefined symbols for architecture x86_64:
      "boost::python::converter::object_manager_traits<boost::python::numpy::ndarray>::get_pytype()", referenced from:
          boost::python::detail::caller_arity<1u>::impl<OutMessages (*)(boost::python::numpy::ndarray const&), boost::python::default_call_policies, boost::mpl::vector2<OutMessages, boost::python::numpy::ndarray const&> >::operator()(_object*, _object*) in example_boost.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [example_boost.so] Error 1

Notes:

  • make was successful if I don't use the bpy and bnp types, so my Makefile is correct.
  • I have Boost v 1.63.0 installed via homebrew, on Mac OSX El Capitan
  • Using C++ 11 and Python 2.7
1
  • make was successful if I don't use the bpy and bnp types, so my Makefile is correct. Nope, it misses the linker flags to the libraries providing the symbols in bpy and bnp. Commented May 19, 2017 at 16:17

1 Answer 1

1

Two problems with the posted example code:

  1. To resolve the make issue, the Makefile would have to also link -lboost_numpy.

  2. Even though it would compile, the result would be a seg fault (stack overflow) because we need to first initialize with

    Py_Initialize(); bnp::initialize();

as explained here.

Sign up to request clarification or add additional context in comments.

Comments

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.