2012-01-31 81 views
1

我收到了一組第三方.lib文件和一個.h文件,我想用SWIG打包,這樣我就可以在另一種語言中使用。所有SWIG的例子都與C \ C++源代碼有關,但在我的情況下,我沒有源代碼。
我應該怎樣做不同來創建包裝?SWIG預編譯庫

回答

2

雖然SWIG示例可能包含允許讀者編譯和嘗試它們的定義(源代碼),但您會注意到接口文件(.i)的所有示例僅包含聲明(通常在頭文件中找到的內容),這是SWIG需要創建一個包裝的全部內容。

寫的接口文件的方式,一般是這樣的:

/* File : example.i */ 
%module example 
%{ 
/* This block will end up verbatim in the generated wrapper - include your header 
* so that the wrapper will have access to its definitions 
*/ 
#include "your_header.h" 
%} 

/* The definitions in this part are used to generate the wrapper. 
* Copy the definitions you want to export to the other language from the header 
* and put them here 
*/ 

extern double variable_from_header; 
extern int function_from_header(int); 

如果你的頭文件很簡單,你要導出定義它,你可能有一個接口文件脫身那看起來是這樣的:

/* File : example.i */ 
%module example 
%{ 
#include "your_header.h" 
%} 

%include "your_header.h" 

通知的%include指令,它指示SWIG解析包含的文件,好像它是接口定義文件的一部分。另請參閱討論此方法的手冊的Section 5.7

一旦你有了你的包裝,你可以鏈接它與lib,因爲你將鏈接從示例中源代碼創建的對象。

+0

即使你沒有頭文件,你仍然可以使用第一種方法,如果你知道涉及函數的聲明 – Flexo 2012-02-01 22:51:38