Opened 6 years ago

Last modified 6 years ago

#12921 new Bugs

Linker Error with numpy in boost/python

Reported by: niko.koester@… Owned by: Ralf W. Grosse-Kunstleve
Milestone: To Be Determined Component: python USE GITHUB
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

I am the author of the attached (and solved) stackoverflow question: http://stackoverflow.com/questions/42899376/lnk2001-error-when-using-boostnumpy/42936160#42936160

My question is, is the described behavior a bug?

Let me start with the answer:

Import the Boost\python numpy package like:

#define BOOST_LIB_NAME "boost_numpy"

#include <boost/python.hpp>

The Problem description (copy from Stackoverflow):

I am trying to call a C++ .dll from Python and return a numpy array. I am using

Anaconda 2.7 x64 Visual Studio 2013 update 5 boost 1.63.0 prebuileded for lib64-msvc-12.0 I managed to compile this simple example frome here and run it in Python:

#include "stdafx.h" #define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp>

char const* greet() {

return "hello, world";

}

BOOST_PYTHON_MODULE(test) {

using namespace boost::python; def("greet", greet);

} I am unsure about the #define BOOST_PYTHON_STATIC_LIB but without it python could not open the pyd file. I suspected that python could not resolve the references to MSVCR120.dll in the dynamic build, but I am just guessing.

Next step was to include <boost/python/numpy.hpp> and follow this instuctions and start with just creating a numpy::ndarray. Yes, I am aware that void is in contradiction to the intention of getting values, I just wanted to keep things as simple as possible.

#include <boost/python/numpy.hpp> namespace p = boost::python; namespace np = boost::python::numpy;

void getNPArray() {

Py_Initialize(); np::initialize(); p::object tu = p::make_tuple('a', 'b', 'c'); np::ndarray const example_tuple = np::array(tu); return;

} The import and namespace declarations compile without error. At next step I encountered the linker error. While Py_Initialize() worked fine, the np::initialize() causes the linker to throw

error LNK2001: unresolved external symbol "void cdecl boost::python::numpy::initialize(bool)" (?initialize@numpy@python@boost@@YAX_N@Z) And np::ndarray const example_tuple = np::array(tu) causes a

error LNK2001: unresolved external symbol "class boost::python::numpy::ndarray cdecl boost::python::numpy::array(class boost::python::api::object const &)" (?array@numpy@python@boost@@YA?AVndarray@123@AEBVobject@api@23@@Z) As the linker is perfectly happy with the first example I am toally confused about what is going on here. I also tried to comment out all parts from the first example and just compile the second part witout any change in behaviour.

Change History (2)

comment:1 by Niko Koester <niko.koester@…>, 6 years ago

[update] I am sorry but I have to revise my statement that the definition #define BOOST_LIB_NAME "boost_numpy" solved the issue. The np::initialize() line was still commented out when I tested it. Actually I only gives an etxtra warning "C4005: 'BOOST_LIB_NAME' : macro redefinition"

So the issue is not solved for me.

comment:2 by John Maddock, 6 years ago

Component: Nonepython USE GITHUB
Owner: set to Ralf W. Grosse-Kunstleve

Please use Github to file Boost.Python issues.

Note: See TracTickets for help on using tickets.