简体   繁体   English

未定义符号:使用 numpy 和 swig 时的 PyArray_API

[英]undefined symbol: PyArray_API when using numpy and swig

I am trying to wrap in Python some code written in C with Swig.我试图用 Swig 在 Python 中包含一些用 C 编写的代码。 The purpose is to create numpy arrays in Python and use them in C as a pointer to doubles.目的是在 Python 中创建 numpy arrays 并在 Z0D61F8370 中将它们用作指向 CAD1D412F5Z22 的指针。

Here is the code:这是代码:

In Swig:在 Swig 中:

%apply (int DIM1, double* INPLACE_ARRAY1) {(int n, double* x)} extern void f(int n, double* x);

In C:在 C 中:

void f(int n, double* x)

Unfortunately when I try to load the module, I obtain the following error message:不幸的是,当我尝试加载模块时,我收到以下错误消息:

ImportError: _test.so: undefined symbol: PyArray_API

Thanks!谢谢!

We'll need some more details as to how you build/link this.我们需要更多关于如何构建/链接它的详细信息。 Based on the available information, my best guess is that since SWIG does not support C (I know there's a fork), but your other code is, you linked the final module library with the C compiler instead of the C++ one.根据可用信息,我最好的猜测是,由于 SWIG 不支持 C(我知道有一个 fork),但您的其他代码是,您将最终模块库与 C 编译器链接,而不是 ZF6F897C9FDCF8EEB3C2F

Here's a basic working example to play with (assuming you copied numpy.i to your local directory).这是一个基本的工作示例(假设您将 numpy.i 复制到本地目录)。 First, fun.i :首先, fun.i

%module fun

%{
#include "f.h"
%}

%include "numpy.i"

%init %{
import_array();
%}

%apply (int DIM1, double* INPLACE_ARRAY1) {(int n, double* x)}
extern void f(int n, double* x);

then fh :然后fh

#include "numpy/arrayobject.h"

extern "C" void f(int n, double* x);

run swig:大口喝水:

$ swig -python -c++ fun.i

compile your f.c implementation (which you didn't provide; C compiler is fine), then link:编译你的 f.c 实现(你没有提供;C 编译器很好),然后链接:

$ g++ -O2 -o _fun.so -shared fun_wrap.cxx f.o `python3-config --includes` -fPIC -I`python -c 'import numpy; print(numpy.get_include())'` -Wno-cpp

and use:并使用:

>>> import fun
>>> import numpy as np
>>> x = np.array(range(10), dtype=np.double)
>>> fun.f(x)
>>>

So now, if I check nm _fun.so | grep PyArray_API所以现在,如果我检查nm _fun.so | grep PyArray_API nm _fun.so | grep PyArray_API , I get: nm _fun.so | grep PyArray_API ,我得到:

0000000000009740 b _ZL11PyArray_API

so it's in the data section and not a symbol to be resolved, but note that the name is mangled and so only resolvable by C++.所以它在数据部分中,而不是要解析的符号,但请注意,名称已损坏,因此只能由 C++ 解析。

EDIT: (As discussed in the comment below.)编辑:(如以下评论中所述。)

There is a strong PyArray_API data symbol in numpy's _multiarray_umath submodule (on my system anyway). numpy 的 _multiarray_umath 子模块中有一个强大的 PyArray_API 数据符号(无论如何在我的系统上)。 I don't think the following is a solution, but it may help figuring out where the problem might be as in principle, the "import_array" part should take care of the same.我不认为以下是解决方案,但它可能有助于找出问题所在,原则上,“import_array”部分应该处理相同的问题。

Compare the result of this:比较这个结果:

>>> import numpy
>>> import fun # the SWIG module, test in your case I think

versus this:与此相比:

>>> import numpy.core._multiarray_umath as um
>>> import ctypes
>>> h = ctypes.CDLL(um.__file__,  ctypes.RTLD_GLOBAL)
>>> import fun # or import test if that's the name

This may show whether the reason it works for me and not for you is due to differences in our respective numpy installations.这可能表明它对我有用而不对你有用的原因是由于我们各自的 numpy 安装的差异。 (Both work for me.) (两者都为我工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM