我正在开发一个基于
swig
的Python模块,用于使用c inline
。为此,我想使C
中的numpy
数组可访问。到目前为止,我使用了C类型,如unsigned short
,但我想使用stdint.h
中的uint16_t
等类型,以便无论我的模块遇到什么编译器时都能保持一致。
不幸的是,在使用stdint.h
类型时,c++
函数没有正确包装。给出的错误是:_setc() takes exactly 2 arguments (1 given)
。这意味着,这个函数没有被包装成能够接受numpy
数组的形式。当我使用例如unsigned short
时,就不会出现上述错误。
你有任何想法,如何让swig将numpy
数组映射到stdint-types
中吗?
interface.i
不起作用:
/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"
%init %{
import_array();
%}
%apply (uint16_t* INPLACE_ARRAY3, int DIM1) {(uint16_t* seq, int n1)};
extern int __g();
C++
函数无法正常工作:
#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern uint16_t* c;
extern int Dc;
extern int Nc[4];
void _setc(uint16_t *seq, int n1, int n2, int n3)
{
c = seq;
Nc[0] = n1;
Nc[1] = n2;
Nc[2] = n3;
}
interface.i
的工作原理:
/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"
%init %{
import_array();
%}
%apply (unsigned short* INPLACE_ARRAY3, int DIM1) {(unsigned short* seq, int n1)};
extern int __g();
C++
函数工作原理:
#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern unsigned short* c;
extern int Dc;
extern int Nc[4];
void _setc(unsigned short *seq, int n1, int n2, int n3)
{
c = seq;
Nc[0] = n1;
Nc[1] = n2;
Nc[2] = n3;
}
%apply (unsigned char *IN_ARRAY1, int DIM1) {(uint8_t * buff_ptr, int buff_size)}
- hmaarrfk