The enums does not get passed to TCL when we have

2019-09-16 04:51发布

In continuation to question

how to pass enum values from TCL script to C++ class using Swig

I have following code

1) File : example.i

%module example
%{
      /* Put header files here or function declarations like below */
      #include "example.h"
%}

%include "example.h"

2 File example.h

class myClass {
    public:
        enum Type {one,two};
         myClass() {}
        static bool printVal(int val);
        static bool printEnum(Type val);
};

3) File example.cpp

 #include  "example.h"
 #include <iostream>
 using namespace std;

 bool myClass::printVal(int val) {
    cout << " Int Val = " << val << endl;
    return 0;
 }

 bool myClass::printEnum(type val) {
    cout << " Enum Val = " << val << endl;
    return 0;
 }  

If I link the swig files in the form of shared library it is working fine

swig -c++ -tcl example.i
g++ -c -fpic example_wrap.cxx example.cpp -I/usr/local/include
g++ -shared example.o example_wrap.o -o example.so
setenv LD_LIBRARY_PATH /pathtoexample.so:$LD_LIBRARY_PATH
tclsh
% load example.so
% myClass_printVal $myClass_one

But if the swig code and example.* files are linked statically I am getting following error

 % myClass_printVal $myClass_one
 can't read "myClass_one": no such variable

Looking forward for guidance/help

1条回答
孤傲高冷的网名
2楼-- · 2019-09-16 05:40

Firstly, if you use more of the path to the shared library, you don't need to alter the LD_LIBRARY_PATH variable. In the case that it's in the current directory (convenient for testing) you can just do this:

load ./example.so

When it's in the same directory as the current script, you instead do this rather longer version:

load [file join [file dirname [info script]] example.so]
# This also probably works:
#load [file dirname [info script]]/example.so

Secondly, you should check what the example has actually created; it might simply be using a different name to what you expect. You can use info commands, info vars and namespace children to find this out; they list the commands, variables and namespaces currently visible.

[EDIT from comments for visibility]: The variables are in the ::swig namespace.

查看更多
登录 后发表回答