Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Call Custom C ++ library in TCL scripts

214 views
Skip to first unread message

Lakshmi Ganesh

unread,
Apr 26, 2023, 12:22:50 PM4/26/23
to
Hello All,

I am relatively new to TCL, the topic I am exploring is that I would like to see whether we can call the methods from my custom c ++ library in TCL scripts. Is there any command available in TCL to call the method from external C ++ library. I am assuming this library has to be put along with the other TCL libraries.

Any réponse is much appreciated.

Thanks,
Ganesh.

Rich

unread,
Apr 26, 2023, 1:31:29 PM4/26/23
to
Lakshmi Ganesh <lakshmiganes...@gmail.com> wrote:
> Hello All,
>
> I am relatively new to TCL, the topic I am exploring is that I would
> like to see whether we can call the methods from my custom c ++
> library in TCL scripts. Is there any command available in TCL to
> call the method from external C ++ library. I am assuming this
> library has to be put along with the other TCL libraries.

Tcl does not have a native foreign function interface.

The traditional way to do this is to write a small amount of C (or in
your case C++) glue to provide that interface between scripts and C (or
C++) functions.

However, there is a FFI extension:

https://wiki.tcl-lang.org/page/Ffidl

The wiki page does say the last update was 2018, so your mileage may
vary with it. The page also links to a couple other extensions.

Lakshmi Ganesh

unread,
Apr 26, 2023, 2:18:02 PM4/26/23
to
Thank you very much for the pointer RIch !!!

clt.to...@dfgh.net

unread,
Apr 26, 2023, 2:57:42 PM4/26/23
to
You might also take a look at some wiki pages:

https://wiki.tcl-lang.org/page/Hello+World+as+a+C+extension

https://wiki.tcl-lang.org/page/C%2B%2B+object%2Dstyle+Tcl+example+extension

I learned the Tcl C API from Brent Welch's Practical Programming in TCL and TK
The 4th edition is about 20 years old now, but it has a good write up on the C API.
It is much better than trying to figure it out from the man pages.

Others can probably recommend more recent books too.

Dave B

Christian Gollwitzer

unread,
Apr 26, 2023, 4:43:03 PM4/26/23
to
Am 26.04.23 um 18:22 schrieb Lakshmi Ganesh:
> I am relatively new to TCL, the topic I am exploring is that I would like to see whether we can call the methods from my custom c ++ library in TCL scripts. Is there any command available in TCL to call the method from external C ++ library. I am assuming this library has to be put along with the other TCL libraries.
>

You can use SWIG to write the glue code for: https://www.swig.org/ SWIG
can interface functions, classes, and (to some extent) templates.

Christian


Robert Heller

unread,
Apr 26, 2023, 5:10:59 PM4/26/23
to
At Wed, 26 Apr 2023 11:17:59 -0700 (PDT) Lakshmi Ganesh <lakshmiganes...@gmail.com> wrote:
There is a program called "SWIG" (Simplified Wrapper and Interface Generator),
that can be "fed" a C or C++ header file and will generate a C or C++ source
file that will provide the "glue" code.

>
>

--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Jacob

unread,
Apr 26, 2023, 6:21:16 PM4/26/23
to
I found Ashok's CFFI package to work well.

https://cffi.magicsplat.com/

Ashok

unread,
Apr 27, 2023, 11:21:34 AM4/27/23
to
Does your library export a C++ API or a C API? Second, is the library a
shared library or static?

The ffidl/cffi solutions will only work well with shared libraries that
export a C API.

There is no standard C++ ABI (name mangling, parameter passing etc. all
can differ between compilers and even versions of the same compiler). So
if you are exporting a C++ API, then you will need to either write your
own wrapper or use SWIG (compiling with the same compiler as your library.)

/Ashok
0 new messages