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(<ime); return ctime(<ime); }
接口文件
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();
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.
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.
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.To illustrate, suppose you wanted to wrap the following C++ data structure:
To wrap with SWIG, you might specify the following interface:// 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) { } }; }
Now, compiling (Python):// 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>;
$ 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