我有一个c++程序,它有一个Tcl解释器。我包装我的函数,并将它们手动添加到Tcl解释器中。是否可以通过Swig自动包装并添加它们?
以下是简化的代码:
#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有人有主意吗?
发布于 2016-04-11 09:43:33
问题在于编译命令。我的最后命令是:
swig -c++ -tcl swig.i
g++ -fpic -c swig.cc swig_wrap.cxx
g++ -shared swig.o swig_wrap.o -o swig.so
g++ swig.o swig_wrap.o -o swig.out -g -I/usr/local/include -L/usr/local/lib -ltcl8.5
setenv LD_LIBRARY_PATH /usr/local/lib:/usr/local/lib
./swig.out上述命令的输出如下:
swig.i:22: Warning(454): Setting a pointer/reference variable may leak memory.
_906e600000000000_p_SystemDatahttps://stackoverflow.com/questions/36496413
复制相似问题