Я новичок в привязке Python C и уже некоторое время пытаюсь решить эту проблему. У меня есть внешняя библиотека C (Example.c), которую я хотел бы вызвать из Python. Я прочитал учебник по Swig и смог быстро создать оболочку. Проблема в том, что когда я вызываю API и получаю это:
>>> import Example
>>> dir(Example)
['Example_CreateConnection', 'trimmed to fit the screen']
>>> Example.Example_CreateConnection("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'Example_CreateConnection', argument 1 of type 'ExampleChar const *'
Казалось, что он не может найти тип ExampleChar
. Вот мой файл swig:
%module Example
%{
#include "ExampleSDK.h"
%}
%include "ExampleTypes.h"
%include "ExampleSDK.h"
ExampleTypes.h выглядит так:
#ifndef ExampleTypes_H
#define ExampleTypes_H
typedef wchar_t ExampleChar;
#endif /* ExampleTypes_H */
ExampleSDK.h выглядит так:
#ifndef ExampleSDK_H
#define ExampleSDK_H
#include "ExampleTypes.h"
void Example_CreateConnection(const ExampleChar *temp);
#endif /* ExampleSDK_H */
Ниже приведены командные строки, вызываемые для создания оболочки:
swig -python -I. Example.i
gcc -c Example.c -I/Developer/SDKs/MacOSX10.6.sdk/usr/include/
gcc -c Example_wrap.c -I/usr/include/python2.6 -I.
gcc -bundle -flat_namespace -undefined suppress -o _Example.so Example_wrap.o Example.o -L/usr/lib/python2.6/config/ -lpython2.6
Вот как выглядит Example.c:
#include "runetype.h" // for Mac wchar_t definition
#include "ExampleSDK.h"
void Example_CreateConnection(const ExampleChar *temp)
{
//do nothing
}
Я не уверен, что с ним не так. Я надеюсь, что кто-то сможет указать на ошибку, которую я сделал здесь. Спасибо.
С уважением,
Чуан Лим