Swig(Tcl):执行我的c ++程序时可以调用Swig函数吗?

时间:2016-04-08 09:43:21

标签: c++ tcl swig

我有一个c ++程序,它有一个Tcl解释器。 我包装我的函数并手动将它们添加到Tcl解释器中。 是否可以自动包装并添加它们?

以下是简化代码:

#include <stdio.h>
#include <tcl.h>

class SystemData {              // I have a class which link to all the data and function
public:
    void print(){
        printf("Hello!\n");
    };
};
                                // I wrap the functions manually. But I'm tired to maintain them.
int Hello( ClientData clientData, Tcl_Interp *interp, int argc, const char **argv ) {
   SystemData* system = (SystemData*) clientData;
   system->print();
}

int main (int argc, char *argv[]) {
    Tcl_Interp *interp = Tcl_CreateInterp();;
    SystemData* system = new SystemData; 
    Tcl_CreateCommand( interp, "hello", Hello, (ClientData)system, (Tcl_CmdDeleteProc *)NULL );

    Tcl_Eval(interp, "hello");  // I have a Tcl interpreter so that I can call any function in any time

    Tcl_DeleteInterp(interp);
}

我试图通过Swig将SystemData导出到Tcl:

// swig.cc
#include <stdio.h>
#include <tcl.h>

class SystemData {
public:
    void print(){
        printf("Hello!\n");
    };
};

SystemData* systemData;
int main (int argc, char *argv[]) {
    Tcl_Interp *interp = Tcl_CreateInterp();;
    systemData = new SystemData; 


    Tcl_Eval(interp, "load ./swig.so swig");
    Tcl_Eval(interp, "puts $systemData");

    Tcl_DeleteInterp(interp);
}

我的Swig界面:

/* swig.i */
 %module swig
 %{
 /* Put header files here or function declarations like below */
class SystemData;
extern SystemData* systemData;
 %}

extern SystemData* systemData;

编译命令:

swig -tcl swig.i
g++ -fpic -c swig.cc swig_wrap.c -I/usr/local/include
g++ -shared swig.o swig_wrap.o -o swig.so

但是,puts $systemData的结果是

NULL

我也尝试过不加载swig.so 但是,puts $systemData的结果是

can't read "systemData": no such variable

有人有想法吗?

0 个答案:

没有答案