例えば、速度が要求されるコードはC/C++で、それ以外はTclで記述するとか、
GUI部だけTcl/Tkで、エンジン部はC/C++で記述するという役割分担が可能です。
また、tcltestと組み合わせて、C/C++のライブラリやプログラムのテストを行えます。
Tcl拡張を作成する第3の手段とも言えます。
/* 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); } |
/* example.h */ extern double My_variable; extern int fact(int n); extern int my_mod(int x, int y); extern char *get_time(); |
次に、SWIGに入力するインタフェースファイルを作成します。
/* example.i */ %module example %{ #include "example.h" %} |
次に、Tclモジュールの構築をします。
コマンドで以下のようにタイプして下さい。
SWIGは、example_wrap.cというラッパを生成します。
UNIX(gcc)の場合:
% swig -tcl8 example.i % gcc -fpic -c example.c example_wrap.c -I/usr/local/include % gcc -shared example.o example_wrap.o -o example.so % tclsh % load ./example.so example % puts $My_variable 3.0 % fact 5 120 % my_mod 7 3 1 % get_time Sun Feb 11 23:01:07 1996 |
Windows(Visual C++ 6.0)+ActiveTclの場合:
% nmake -f example.mak % tclsh % load ./example.dll example % puts $My_variable 3.0 % fact 5 120 % my_mod 7 3 1 % get_time Sun Feb 11 23:01:07 1996 |
その他の詳細なサンプルはSWIG and Tclにあります。