我有一個 SWIG C-python 界面。我想讓 test.c 中的各個函式在我的 python 界面中可用。我覺得我在檔案上浪費了幾個小時,卻沒有找到簡單的方法。我知道的唯一選擇是:
%module test
%{
#include "test.h"
//#include "test.c"
%}
%include "stdint.i" # Add support for uint8_t, int32_t, etc...
/** Keep test immutable for now because we have const struct members in the
* header file which can not be reassigned.
*/
%immutable;
%include test.h // Test header for interface debugging
//%include test.c
//%import test.h
%mutable;
我在界面的 %{} 部分嘗試了 #include 'test.c'(此部分只是將檔案內容復制到目標界面)、%include 'test.c' 或 %import 'test.c'
但是我在'test.c'中有一大堆其他函式依賴于其他檔案,我無法在python中匯入生成的介面(ImportError:未定義符號:)
#include "test.h"
#include "someotherfunctions.h"
#include "specialdefines.h"
int testFunc(int n, int m) // The function i want to use
{
int res = n m;
return res;
}
int otherFunc(void)
{
otherDifferentFunc(); // Stuff from other header/c files which my interface doesnt know
}
SPECIALMACRO(X) // More stuff from other headers that i dont want in my interface
int foo = someOtherOperation(500);
頭檔案test.h:
/* Functions ----------------------------------------------------------------*/
int testFunc(int n, int m);
我覺得我沒有正確的方法在我的 Python 界面中提供單個函式,而不包括源檔案中所有頭檔案中的所有不需要的東西。實際上,我的“test.c”非常大,包含一大堆用于各種符號和使用的外部函式的標題。如果我沒有在我的 %{} 界面部分中 #include 所有這些,我會收到匯入錯誤。但是,如果我包含它們,它們又需要其他標題(它是一個大專案),并且我正在為一個很小的功能開始一個凌亂的包含鏈,不必要地膨脹我的界面。這不可能是 SWIG 的方式嗎?
如果有人知道解決此問題的好方法,請告訴我。
uj5u.com熱心網友回復:
%module test
%{
// Whatever goes here is directly copied to the generated wrapper code.
// Put what you need here for the wrapper to build, typically just
// your public headers.
#include "test.h"
%}
// Anything you declare or %import is processed to make the SWIG interface.
// You can %import "test.h" and all the functions *directly* in
// the header (not other #include in the header), are exposed to Python.
//
// If you only want one function, just put its prototype, like:
int testFunc(int n, int m);
當您構建包裝器時,痛飲 .i 檔案以生成 test_wrap.c,然后編譯 test.c 和 test_wrap.c 并在鏈接步驟將它們鏈接在一起。
一個例子:
測驗.i
%module test
%{
#include "test.h"
%}
// uncomment to bring in func1 and func2 (not func3, but it's still used by func2)
//%include "test.h"
// only exposes func1
int func1();
測驗.h
int func1();
int func2();
測驗.c
#include "test.h"
int func3() { return 3; }
int func2() { return func3() - 1; }
int func1() { return func2() - 1; }
makefile(Windows,微軟編譯器)
all: _test.pyd
test_wrap.c test.py: test.i test.h
echo swigging...
swig -python test.i
_test.pyd: test_wrap.c test.c test.h
cl /nologo /LD /MD /W3 /Fe_test.pyd /Id:\dev\python310\include test_wrap.c test.c -link /libpath:d:\dev\python310\libs
輸出(如上構建,僅 func1() 暴露):
>>> import test
>>> test.func1
<function func1 at 0x000001CF6DEF7F40>
>>> test.func1()
1
>>> test.func2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'test' has no attribute 'func2'. Did you mean: 'func1'?
輸出(構建取消注釋 %include 并注釋原型):
>>> import test
>>> test.func1()
1
>>> test.func2()
2
>>> test.func3()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'test' has no attribute 'func3'. Did you mean: 'func1'?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524601.html
