Version 10 of Invoking Tcl commands from C++

Updated 2008-11-14 08:18:43 by SL

Purpose: document what one needs to do in a C++ application to invoke Tcl script commands (i.e. ones without a directly invokable API).

[Can someone provide a pointer to some sample c++ code that interacts with Tcl?]

[It's really, really easy, easier than this page is yet making apparent. It'll take work, though, to show how easy it is.]


Example: TclODBC is an extension written in C++.

You can evaluate Tcl scripts, call Tcl functions and write new Tcl commands in C++ just like in C. The Tcl headers are safe for direct use in C++. The only gotcha for extension writers is that the init functions must be declared 'extern "C"' so that the compiler leaves their names alone for the load command.

Also with command-line compilers (like GCC) be sure to use the C++ compiler frontend (like g++) for linking so that you pick up the C++ runtime support automatically.

Obviously everybody that interfaces with Tcl from C++ encapsulates some of the Tcl objects in corresponding C++ classes. BR doesn't know of any effort yet to provide standard all-purpose C++ wrapper classes for this.


willdye (2004-12-27) In practice, I usually wind up just using some variant of Tcl_Eval(), but I'm constrained by coding standards already in place at my job. If I were starting from scratch, I'd do well to start by searching this wiki for references including, but not limited to:

cpptcl, Invoking Tcl commands from Cplusplus, Critcl does C++, Critcl, Cplusplus streams and Tcl channels, Swig, C++, and Why adding Tcl calls to a C/C++ application is a bad idea.

Regarding cpptcl, these links outside the wiki may also be of interest:

http://cpptk.sourceforge.net and http://groups-beta.google.com/group/comp.lang.tcl/msg/d2969ea3654be5f8