swig教程

So you want to get going in a hurry? To illustrate the use of SWIG, suppose you have some C functions you want added to Tcl, Perl, Python, Java and C#. Specifically, let's say you have them in a file 'example.c'

C程序,需要加入到Tcl, Perl, Python, Java and C#.中

 /* File : example.c */
 
 #include <time.h>
 double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
 	
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }
 
 

Interface file

接口文件

Now, in order to add these files to your favorite language, you need to write an "interface file" which is the input to SWIG. An interface file for these C functions might look like this :

接口文件形式如下:

 /* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 

WINDOWS下設置

With Microsoft Visual C++ 6.0:

Create a new Win32 DLL project:

File -> New -> Project tab: Select Win32 dynamic-link library and specify a name.

In Tool -> Options -> Directories it is handy to add include file directory for your script languages include files and to add library file directory for your script languages library files.

In project settings -> C/C++ -> Preprocessor definitions: add __WIN32__.

Then just add your source and header files to the project and build.

You may also want to insert your interface file and specify how Visual C++ shall compile it In Project settings -> Custom build.

 

  • Enter "SWIG" in the description field.
  • Enter "swig -<script language> <other options> -o $(ProjDir)/$(InputName)_wrap.c $(InputPath)" in the "Build command(s)" field
  • Enter "$(ProjDir)/$(InputName)_wrap.c" in the "Output files(s)" field.

 

With Microsoft Visual C++ 7.0 (Version 2002):

To create a new Win32 DLL project:

File -> New -> Project tab: Select Managed C++ Class Library and specify a name.

Otherwise the process is the same as that of Microsoft Visual C++ 6.0 above.

Running SWIG under Microsoft Windows

Windows 下運行SWIG

SWIG also works perfectly well under all known 32 bit versions of Windows including 95/98/NT/2000/XP. SWIG is typically invoked from the command prompt and can be used with NMAKE. Modules are typically compiled in the form of a DLL that can be dynamically loaded into Tcl, Python, or whatever language you are using. With a little work, SWIG can also be used as a custom build option within MS Developer Studio.

That's it (well, more or less)

That's about everything you need to know to get started. Here's the short checklist :
  • Make sure you specify a module name.
  • Use ANSI C/C++ syntax
  • Figure out how to compile a shared library module / dynamic link library (may require reading a few man pages for your compiler).
  • Relax.

Surely there's more to it...

The above example is intentionally simple, but the general idea extends to more complicated C/C++ programming tasks. In fact, it is important to know that SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language---exposing the underlying functionality in a very natural manner.

To illustrate, suppose you wanted to wrap the following C++ data structure:

 // pair.h.  A pair like the STL
 namespace std {
    template<class T1, class T2> struct pair {
        T1 first;
        T2 second;
        pair() : first(T1()), second(T2()) { };
        pair(const T1 &f, const T2 &s) : first(f), second(s) { }
    };
 }
 
To wrap with SWIG, you might specify the following interface:
 // pair.i - SWIG interface
 %module pair
 %{
 #include "pair.h"
 %}
 
 // Ignore the default constructor
 %ignore std::pair::pair();      
 
 // Parse the original header file
 %include "pair.h"
 
 // Instantiate some templates
 
 %template(pairii) std::pair<int,int>;
 %template(pairdi) std::pair<double,int>;
 
Now, compiling (Python):
 $ swig -python -c++ pair.i
 $ c++ -c pair_wrap.c -I/usr/local/include/python2.1
 $ c++ -shared pair_wrap.o -o _pair.so
 $ python
 Python 2.1 (#3, Aug 20 2001, 15:41:42) 
 [GCC 2.95.2 19991024 (release)] on sunos5
 Type "copyright", "credits" or "license" for more information.
 >>> import pair
 >>> a = pair.pairii(3,4)
 >>> a.first
 3
 >>> a.second
 4
 >>> a.second = 16
 >>> a.second
 16
 >>> b = pair.pairdi(3.5,8)
 >>> b.first
 3.5
 >>> b.second
 8
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章