Discussion:
[Swig-devel] Swig interface file for a C++ class
Harika Parakala
2017-05-12 22:28:04 UTC
Permalink
Hello,
I have a C++ code which integrates Python with C++ or which parses the
python commands. I want to create a .dll for the C++ code through SWIG. I
wrote a .i class for my C++ code and created a .dll.
I have loaded the .dll in Tcl. But unable to understand how to call it from
Tcl interpreter.
Could you please let me know if .i class I wrote is valid or not. And also
please let me know how to call the function from Tcl by passing the
arguments.

Attaching C++ class and .i class .



Thanks,
Harika.
m***@comcast.net
2017-05-13 10:31:57 UTC
Permalink
Hello, I have a C++ code which integrates Python with C++
Your code seems to be an example program (including the main()
function) which starts a python interpreter and then loads and
executes code from the command line arguments (argv). I'm sure this C
code works fine if compiled and run in the normal way.
which parses the python commands. I want to create a .dll for
the C++ code through SWIG. I wrote a .i class for my C++ code
and created a .dll. I have loaded the .dll in Tcl. But unable
to understand how to call it from Tcl interpreter.
You might be able to get this example to work. But at most you will
probably only want to run it ONCE as Py_Initialize and Py_Finalize
are documented as not really being reliable if called more than once:

https://docs.python.org/2/c-api/init.html

Calling it once can probably work (I really would not do it a second
time). You will probably need to write some typemaps to translate a
Tcl list of strings into the char **argv paramater of your main
function. Something like what is documented here:

http://www.swig.org/Doc1.1/HTML/Tcl.html#n11

Even getting this much to work seems like a lot of work and the
whole thing will be a house of cards. I'm sure you could implement
calling python from Tcl in a much more clean and reliable way if your
implemented it as an embedded python interpreter.

You should read/study the python docs on embedding:

https://docs.python.org/2/extending/index.html#extending-index

https://docs.python.org/2/c-api/index.html

Once you have a handle on how this works you can create a handful of
C functions with arguments that are more friendly to Tcl and easily
wrapped by SWIG.

Mike

Loading...