2017-09-06 5 views
1

をにuint8:SWIG Cライブラリとctypesのは、私は例えば、私のCライブラリを生成するために、SWIGを使用している無効な型

mylib.pyをしてpythonで

int func(uint8_t* a) 
{ 
    return *a; 
} 

を_mylib.pyd:

import mylib 
import ctypes 
a = (ctypes.c_uint8 * 8)() 
mylib.func(a) 

しかし、Pythonの実行は私にエラーを与える:TypeError: in method 'func', argument 1 of type 'uint8 *'

私はタイプマップを検索し、私は私を追加します私の.iモジュールファイルには、次のようになります。

%module mylib 
%include "typemaps.i" 
extern int func(uint8_t* INPUT); 

pythonエラーです。

私はctypes.c_uint8を印刷してmylib uint8を印刷しました。私はmylib uint8がswigオブジェクトであることを発見しました。この仕事をするには?

+1

を使用してpython setup.py build_ext --inplace、テストを使用してビルドはSWIG''と互換性がありません。彼らは異なるタイプです。あなたの 'SWIG'インターフェースファイルに'%include "carrays.i"と%array_class(unsigner char、uint8ArrayClass) 'を組み込み、生成された' uint8ArrayClass(4) 'を使って、長さ4の配列。 –

+0

こんにちは、ありがとう、私にシンプルなテキストを表示できますか? –

+0

私が提供したキーワード –

答えて

1

私は、これは完全にマニュアルに記載されたと思いますが、ここに行く

TEST.H

#pragma once 
#include <cstdlib> 
#include <cstdint> 
int func(uint8_t* a, const size_t len); 

TEST.CPP

#include "test.h" 

#include <iostream> 

int func(uint8_t* a, const size_t len) { 
    int result = 0; 
    for (size_t i = 0 ; i < len ; i++) { 
    result += int(a[i]); 
    } 
    return result; 
} 

test.i

%module example 
%{ 
    #include "test.h" 
%} 

%include "carrays.i" 
%include "stdint.i" 
%array_functions(uint8_t, uint8Array); 

%include "test.h" 

setup.py

from distutils.core import setup, Extension 

setup(name="example", 
     py_modules=['example'], 
     ext_modules=[Extension("_example", 
        ["test.i","test.cpp"], 
        swig_opts=['-c++'], 
    extra_compile_args=['--std=c++11'] 
       )] 

) 

あなたがそれらを使用しようとするように `ctypes`から種類

import example 
g = example.new_uint8Array(3) 
example.func(g,3) 
関連する問題