Я изучаю SWIG для вызова библиотек C ++ в Python. Одна проблема заключается в том, что когда я использую одномерный массив в C ++ и хочу назвать его в Python как Numpy arrary, я получил ошибку.
Заголовочный файл: example.h
#include <iostream>
using namespace std;
class Example {
public:
void say_hello();
void add(int x, int y, int *result);
int sub(int *x, int *y);
void array_add(int *a, int *b, int *c);
};
Файл C ++: example.cpp
#include "example.h"
void Example::say_hello() {
cout<<"Hello Example."<<endl;
}
void Example::add(int x, int y, int *result) {
*result = x + y;
}
int Example::sub(int *x, int *y) {
return *x-*y;
}
void Example::array_add(int *a, int *b, int *c) {
c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
}
Файл интерфейса SWIG: example.i
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "typemaps.i"
%include "numpy.i"
%init %{
import_array();
%}
%apply int *OUTPUT { int *result };
%apply int *INPUT { int *x, int *y};
%apply int *INPLACE_ARRAY1 {int *a, int *b, int *c};
%include "example.h"
Файл установки: setup.py
#!/usr/bin/env python
from distutils.core import setup, Extension
import numpy
import os
example_module = Extension('_example',
sources=['example.cpp', 'example_wrap.cxx',],
)
setup (
name = 'example',
version = '0.1',
author = "Frank Tang",
description = """Simple swig C\+\+/Python example""",
ext_modules = [example_module],
py_modules = ["example"],
)
файл: test_example.py
test_example.py
После того, как я запустил «python test_example.py», я получил сообщение об ошибке следующим образом. Я использую macOS.
(virtualenv) bogon:source tangsg$ python test_example.py
Hello Example.
7
3
Traceback (most recent call last):
File "test_example.py", line 18, in <module>
example.Example().array_add(a, b, c)
File "/Users/tangsg/Projects/test/source/example.py", line 115, in
array_add
return _example.Example_array_add(self, a, b, c)
TypeError: in method 'Example_array_add', argument 2 of type 'int *'
(virtualenv) bogon:source tangsg$ ›
Сообщение об ошибке