Version 37 of C++/Tcl

Updated 2004-11-28 08:42:12

Richard Suchenwirth 2004-11-23 - The very generic name stands for "A C++ library for interoperability between C++ and Tcl" by Maciej Sobczak, based on the Boost project. On SourceForge it goes under the name cpptcl at .

LV So is this cpptcl different from what is documented on this wiki page - cpptcl? RS: Yes - that's the risk with too-generic names... The software I discuss here was only announced on 2004-11-22, but looks pretty powerful.

It "was inspired by the Boost.Python library and was designed to provide a similar interface"... "The C++/Tcl library depends on the Tcl core library and on the Boost library (see ). Any recent version of these two should work fine."

The documentation (one could also call it a tutorial) makes a good impression - it looks as if combining C++ and Tcl, both for making shared libraries and embedded interpreters, go very easy, for instance compared to SWIG.

Here's (untested, but drawing from the documentation) how to make a shared library with two commands (that both are served by the same C++ function):

 #include "cpptcl.h"

 int sum(int a, int b) {return a + b;}

 CPPTCL_MODULE(Example, i) {

 $ tclsh
 % load ./
 % + 3 4

Has anybody made practical experiences with this? Especially on Windows? According to the Boost site, it should be possible.

MAK (11 Oct 2004) - Looks very interesting. The code (both for cpptcl its self and how you use it) seem very nicely compact. cpptk also looks very interesting. I'm curious how much overhead the Boost library would add, though; I'll have to look into that. Two things I don't see from the documentation, though:

1. What's the license? 2. How would you create a function that can take a variable number of arguments (a la widget configuration parameters)?

snichols The documentation looks very strait forward. So, I thought I would try compiling the hello example Tcl extension for Windows. However, there are some header files and library dependencies, and I do not know where to get these. Its looking for something called boost/shared_ptr.hpp and a couple other dependencies. - RS: Scott, that dependency on Boost from is documented even on this page :^)

snichols Thanks for the info on Boost. I'll try including the headers and give the hello example another try. I think I would have use for this. It appears to simplify some of the Tcl to C++/C library programming and would be handy for small C++ Tcl extensions, like ones I have written in the past.


Ok, this is what I have found out from compiling the sample hello world C++/Tcl extension under Windows. The code will not compile with Microsoft Visual Studio 6 because of this error, "template class has already been defined as a non-template class." Microsoft has confirmed that their older VC++ 6.0 compiler does not support this (the template thing), but luckily their newer 2003 C++ compiler does. So, I downloaded and installed the free Microsoft C++ 2003 Toolkit to compile this from Microsoft's web site. It does compile ok, but I get the following link error when compiling and linking with cl (MS 2003 C++ Compiler):

 cl main.cpp cpptcl.cpp /I C:/Tcl/Include /I C:/Programming/boost_1_32_0 /I C:/Downloads/TCL/cpptcl-1.0.0 /link /dll c:/Tcl/lib/Tcl84.lib

 Generates Code OK
 Following Link Error Returned:

 "main.obj : error LNK2019: unresolved external symbol "void __cdecl boost::throw_exception(class exception const &)" ([email protected]@@[email protected]@@Z) referenced in function "public: __this
        call boost::detail::shared_count::shared_count<class Tcl::details::callback0<void> *,struct boost::checked_deleter<class Tcl::details::callback0<void> > >(class Tcl::details::callback0<void> *,struct
        boost::checked_deleter<class Tcl::details::callback0<void> >)" ([email protected]@[email protected]@@[email protected][email protected]@[email protected]@@@[email protected]@@[email protected]@[email protected]@[email protected]?$callback0
        @[email protected]@[email protected]@[email protected][email protected]@[email protected]@@@[email protected]@Z)"

Thanks for the help with my link error (above) in advance. Once I get through the link error, I feel good that I can port some of my previous Tcl C++ extension to use this, and write some new ones. Is my cl command line missing a lib file or something? I have only included the one Tcl84.lib.


Well, I was able to compile and link the hello world example. I added /EHs to the compiler options. Which basically tells the compiler to ignore unhandled exceptions. The winning MS C++ 2003 command line syntax was:

 cl main.cpp cpptcl.cpp /EHs /I C:/Tcl/Include /I C:/Programming/boost_1_32_0 /I C:/Downloads/TCL/cpptcl-1.0.0 /LD c:/Tcl/lib/Tcl84.lib

NOTE: The include and lib paths above are unique to my computer I would assume. You will need to change these to match where you downloaded and placed these files. Now, I get the following load error when loading in the library in Tcl:

load C:/Programming/C++/CplusplusTcl/main.dll couldn't find procedure Main_Init

Do I need to include the Tcl Init function even though its not used under Windows?

snichols Well after a little expermintation I finally got the Hello World! example working in Windows. The only thing I had to add was the extern "C" command below. My DLL was called Main.dll (I wouldn't recommend this name. I used it for testing only). Tcl, uses the file name to look for the Initialize function in Tcl. The other thing I noticed too is that the console out didn't appear in the Wish interp, but did appear in a Tclsh interp. Also, remember that this will not compile with Visual Studio 6, you need MS 2003 C++ to compile this.

 /* Microsoft Windows Hello World Example */
 /* Compiled using MS 2003 C++ Tool Kit */
 /* Console out only appeared in Tclsh. */
 /* Does not appear in Wish for some reason */
 /* Compiled File Name: Main.dll */
 /* Scott Nichols */

 #include "cpptcl.h"
 #include <iostream>

 using namespace std;

 extern "C"
   __declspec(dllexport) int Main_Init(Tcl_Interp* interp);


 void hello()
     cout << "Hello C++/Tcl!" << endl;

     i.def("hello", hello);

snichols I have been trying the C++/Tcl library and it looks very promising, and I am planning on converting one of my Tcl extension. I have a feeling the new sources will be much shorter then going through the standard Tcl interface to C. A couple things I would like to see added include:

1. Tcl Package Provide. This will allow extension programmers to generate pkindex.tcl scripts and use, "package require <insert name>" instead of the load command. This is more generic then having to put the file path to where the actual library file is located.

2. Support for Tcl stub libraries. This will make any custom extensions written with C++/Tcl backwards compatible with future versions of Tcl. An advantage to stub libraries is that the extension author does not have to recompile their sources when a newer version of Tcl is used.

3. Support for overloaded functions. This will allow for C++ functions with the same name but with different signatures to be called from Tcl. I am especially interested in overloaded constructors. Overloaded constructors would allow for the Tcl programmer to create the object different ways. Which is a common implentation of overloading.

4. How to return Tcl Error.

Maciej Sobczak - The C++/Tcl library depends on some of header-only Boost libraries. This means that it is enough to download Boost, unpack it somewhere and add that path to the compiler's include search path. No compilation of Boost is required. The license is stated in each source file, but will be soon stated more explicitly. It is extremely permissive, anyway.

 // Permission to copy, use, modify, sell and distribute this software
 // is granted provided this copyright notice appears in all copies.
 // This software is provided "as is" without express or implied
 // warranty, and with no claim as to its suitability for any purpose.

Robert J. Peters' GroovX [L1 ] package makes for an interesting comparison [L2 ].

Category Language