/tags/ttn-post-libtool-1-4-3-upgrade/SWIG/Doc/Manual/Tcl.html
HTML | 2502 lines | 2005 code | 495 blank | 2 comment | 0 complexity | e642de8b1f6f7a237cb9c86049dbd277 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>SWIG and Tcl</title>
- </head>
- <body bgcolor="#ffffff">
- <a name="n1"></a><H1>21 SWIG and Tcl</H1>
- <!-- INDEX -->
- <ul>
- <li><a href="#n2">Preliminaries</a>
- <ul>
- <li><a href="#n3">Getting the right header files</a>
- <li><a href="#n4">Compiling a dynamic module</a>
- <li><a href="#n5">Static linking</a>
- <li><a href="#n6">Using your module</a>
- <li><a href="#n7">Compilation of C++ extensions</a>
- <li><a href="#n8">Compiling for 64-bit platforms</a>
- <li><a href="#n9">Setting a package prefix</a>
- <li><a href="#n10">Using namespaces</a>
- </ul>
- <li><a href="#n11">Building Tcl/Tk Extensions under Windows 95/NT</a>
- <ul>
- <li><a href="#n12">Running SWIG from Developer Studio</a>
- <li><a href="#n13">Using NMAKE</a>
- </ul>
- <li><a href="#n14">A tour of basic C/C++ wrapping</a>
- <ul>
- <li><a href="#n15">Modules</a>
- <li><a href="#n16">Functions</a>
- <li><a href="#n17">Global variables</a>
- <li><a href="#n18">Constants and enums</a>
- <li><a href="#n19">Pointers</a>
- <li><a href="#n20">Structures</a>
- <li><a href="#n21">C++ classes</a>
- <li><a href="#n22">C++ inheritance</a>
- <li><a href="#n23">Pointers, references, values, and arrays</a>
- <li><a href="#n24">C++ overloaded functions</a>
- <li><a href="#n25">C++ operators</a>
- <li><a href="#n26">C++ namespaces</a>
- <li><a href="#n27">C++ templates</a>
- <li><a href="#n28">C++ Smart Pointers</a>
- </ul>
- <li><a href="#n29">Further details on the Tcl class interface</a>
- <ul>
- <li><a href="#n30">Proxy classes</a>
- <li><a href="#n31">Memory management</a>
- </ul>
- <li><a href="#n32">Input and output parameters</a>
- <li><a href="#n33">Exception handling </a>
- <li><a href="#n34">Typemaps</a>
- <ul>
- <li><a href="#n35">What is a typemap?</a>
- <li><a href="#n36">Tcl typemaps</a>
- <li><a href="#n37">Typemap variables</a>
- <li><a href="#n38">Converting a Tcl list to a char ** </a>
- <li><a href="#n39">Returning values in arguments</a>
- <li><a href="#n40">Useful functions</a>
- <li><a href="#n41">Standard typemaps</a>
- <li><a href="#n42">Pointer handling</a>
- </ul>
- <li><a href="#n43">Turning a SWIG module into a Tcl Package.</a>
- <li><a href="#n44">Building new kinds of Tcl interfaces (in Tcl)</a>
- <ul>
- <li><a href="#n45">Shadow classes</a>
- </ul>
- </ul>
- <!-- INDEX -->
- <b>Caution: This chapter is under repair!</b>
- <p>
- This chapter discusses SWIG's support of Tcl. SWIG currently requires
- Tcl 8.0 or a later release. Earlier releases of SWIG supported Tcl 7.x, but
- this is no longer supported.
- <a name="n2"></a><H2>21.1 Preliminaries</H2>
- To build a Tcl module, run SWIG using the <tt>-tcl</tt> option :<p>
- <p>
- <blockquote><pre>$ swig -tcl example.i
- </pre></blockquote>
- If building a C++ extension, add the <tt>-c++</tt> option:
- <p>
- <blockquote><pre>$ swig -c++ -tcl example.i
- </pre></blockquote>
- <p>
- This creates a file <tt>example_wrap.c</tt> or
- <tt>example_wrap.cxx</tt> that contains all of the code needed to
- build a Tcl extension module. To finish building the module, you
- need to compile this file and link it with the rest of your program.
- <a name="n3"></a><H3>21.1.1 Getting the right header files</H3>
- In order to compile the wrapper code, the compiler needs the <tt>tcl.h</tt> header file.
- This file is usually contained in the directory
- <p>
- <blockquote><pre>/usr/local/include
- </pre></blockquote>
- Be aware that some Tcl versions install this header file with a version number attached to it. If
- this is the case, you should probably make a symbolic link so that <tt>tcl.h</tt> points to the correct
- header file.
- <a name="n4"></a><H3>21.1.2 Compiling a dynamic module</H3>
- The preferred approach to building an extension module is to compile it into
- a shared object file or DLL. To do this, you will need to compile your program
- using comands like this (shown for Linux):
- <p>
- <blockquote><pre>$ swig -tcl example.i
- $ gcc -c example.c
- $ gcc -c example_wrap.c -I/usr/local/include
- $ gcc -shared example.o example_wrap.o -o example.so
- </pre></blockquote>
- The exact commands for doing this vary from platform to platform.
- SWIG tries to guess the right options when it is installed. Therefore,
- you may want to start with one of the examples in the <tt>SWIG/Examples/tcl</tt>
- directory. If that doesn't work, you will need to read the man-pages for
- your compiler and linker to get the right set of options. You might also
- check the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl">SWIG Wiki</a> for
- additional information.
- <p>
- When linking the module, the name of the output file has to match the name
- of the module. If the name of your SWIG module is "<tt>example</tt>", the
- name of the corresponding object file should be
- "<tt>example.so</tt>".
- The name of the module is specified using the <tt>%module</tt> directive or the
- <tt> -module</tt> command line option.<p>
- <a name="n5"></a><H3>21.1.3 Static linking</H3>
- An alternative approach to dynamic linking is to rebuild the Tcl
- interpreter with your extension module added to it. In the past,
- this approach was sometimes necesssary due to limitations in dynamic loading
- support on certain machines. However, the situation has improved greatly
- over the last few years and you should not consider this approach
- unless there is really no other option.
- <p>
- The usual procedure for adding a new module to Tcl involves writing a
- special function <tt>Tcl_AppInit()</tt> and using it to initialize the interpreter and
- your module. With SWIG, the <tt>tclsh.i</tt> and <tt>wish.i</tt> library files
- can be used to rebuild the <tt>tclsh</tt> and <tt>wish</tt> interpreters respectively.
- For example:
- <p>
- <blockquote><pre>%module example
- extern int fact(int);
- extern int mod(int, int);
- extern double My_variable;
- %include tclsh.i // Include code for rebuilding tclsh
- </pre></blockquote>
- The <tt>tclsh.i</tt> library file includes supporting code that
- contains everything needed to rebuild tclsh. To rebuild the interpreter,
- you simply do something like this:
- <p>
- <blockquote><pre>$ swig -tcl example.i
- $ gcc example.c example_wrap.c \
- -Xlinker -export-dynamic \
- -DHAVE_CONFIG_H -I/usr/local/include/ \
- -L/usr/local/lib -ltcl -lm -ldl \
- -o mytclsh
- </pre></blockquote>
- You will need to supply the same libraries that were used to build Tcl the first
- time. This may include system libraries such as <tt>-lsocket</tt>, <tt>-lnsl</tt>,
- and <tt>-lpthread</tt>. If this actually works, the new version of Tcl
- should be identical to the default version except that your extension module will be
- a built-in part of the interpreter.
- <p>
- <b>Comment:</b> In practice, you should probably try to avoid static
- linking if possible. Some programmers may be inclined
- to use static linking in the interest of getting better performance.
- However, the performance gained by static linking tends to be rather
- minimal in most situations (and quite frankly not worth the extra
- hassle in the opinion of this author).
- <a name="n6"></a><H3>21.1.4 Using your module</H3>
- To use your module, simply use the Tcl <tt>load</tt> command. If
- all goes well, you will be able to this:
- <p>
- <blockquote><pre>$ tclsh
- % load ./example.so
- % fact 4
- 24
- %
- </pre></blockquote>
- A common error received by first-time users is the following:
- <blockquote>
- <pre>
- % load ./example.so
- couldn't find procedure Example_Init
- %
- </pre>
- </blockquote>
- This error is almost always caused when the name of the shared object file doesn't
- match the name of the module supplied using the SWIG <tt>%module</tt> directive.
- Double-check the interface to make sure the module name and the shared object
- file match. Another possible cause of this error is forgetting to link the SWIG-generated
- wrapper code with the rest of your application when creating the extension module.
- <p>
- Another common error is something similar to the following:
- <blockquote>
- <pre>
- % load ./example.so
- couldn't load file "./example.so": ./example.so: undefined symbol: fact
- %
- </pre>
- </blockquote>
- This error usually indicates that you forgot to include some object
- files or libraries in the linking of the shared library file. Make
- sure you compile both the SWIG wrapper file and your original program
- into a shared library file. Make sure you pass all of the required libraries
- to the linker.
- <p>
- Sometimes unresolved symbols occur because a wrapper has been created