使用boost :: numpy时发生LNK2001错误 [英] LNK2001 error when using boost::numpy

查看:129
本文介绍了使用boost :: numpy时发生LNK2001错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Python调用C ++ .dll并返回一个numpy数组.我正在使用

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更新5
  • 为lib64-msvc-12.0预编译了
  • boost 1.63.0
  • 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);
}

我不确定#define BOOST_PYTHON_STATIC_LIB,但是没有它,python无法打开pyd文件.我怀疑python无法在动态版本中解析对MSVCR120.dll的引用,但我只是在猜测.

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.

下一步是包括<boost/python/numpy.hpp>并遵循说明,然后从创建numpy::ndarray开始.是的,我知道虚无与获取价值的意图相矛盾,我只是想让事情尽可能简单.

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;
}

导入和命名空间声明编译无误. 在下一步中,我遇到了链接器错误.虽然Py_Initialize()工作正常,但np::initialize()导致链接器抛出

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

错误LNK2001:无法解析的外部符号"void __cdecl boost :: python :: numpy :: initialize(bool) (?initialize @ numpy @ python @ boost @@ YAX_N @ Z)

error LNK2001: unresolved external symbol "void __cdecl boost::python::numpy::initialize(bool)" (?initialize@numpy@python@boost@@YAX_N@Z)

然后np::ndarray const example_tuple = np::array(tu)导致

错误LNK2001:无法解析的外部符号类 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)

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.

任何有关如何使我的链接器开心的提示都将受到高度赞赏.

Any hints on how to make my linker happy are highly appreciated.

[更新-问题已解决] 原来,错误的根源仅仅是boost的probuild版本没有libboost_numpy-vc120-mt-1_63.lib,所以我按照

[update - issue solved] It turned out that the source of the errors is simply that the probuild version of boost comes without a libboost_numpy-vc120-mt-1_63.lib so i followed the instructions here to build it. Using this parameters:

b2 -j8 --toolset=msvc-12.0  release link=static runtime-link=static address-model=64 --build-type=complete stage --with-python 

现在对我有用的导入看起来像这样:

The import which is now working for me looks like this:

#define BOOST_PYTHON_STATIC_LIB  
#define BOOST_LIB_NAME "boost_numpy"
#include <boost/config/auto_link.hpp>
#include <boost/python/numpy.hpp>

推荐答案

与其他Boost组件一样,boost_numpy不会自动引入所需的库.要解决此问题,请将这些行放在您的一个源文件中:

The required library is not pulled in automatically by boost_numpy like it is for other Boost components. To fix this, place these lines in one of your source files:

#define BOOST_LIB_NAME "boost_numpy"
#include <boost/config/auto_link.hpp>

这将使用Boost的自动链接功能来确定适合您的构建配置的boost_numpy库(例如libboost_numpy-vc120-mt-1_63.lib),并生成一个#pragma,要求您的链接程序包括它.

This will use Boost's automatic linking feature to determine the appropriate boost_numpy library for your build configuration (such as libboost_numpy-vc120-mt-1_63.lib) and generate a #pragma that asks your linker to include it.

这两个行可能应该包含在boost/python/numpy.hpp中.遗漏可能是一个错误.

Those two lines probably should be included in boost/python/numpy.hpp. The omission may be a bug there.

顺便说一句,这些行可以根据需要重复多次,每次更改定义LIB_BOOST_NAME,以包括任何其他缺少的Boost库. (auto_link.hpp有意不使用包含保护,因此允许使用这种类型.)

Incidentally, those lines can be repeated as many times as necessary, changing the definition LIB_BOOST_NAME each time, to include any other missing Boost libraries. (auto_link.hpp intentionally does not use an include guard, thus permitting this type of usage.)

这篇关于使用boost :: numpy时发生LNK2001错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆