Embedding Tcl in C/C++ Applications
Presented At:
The Tcl2K Conference
Austin, Texas
9:00am, February 15, 2000
|
|
Instructor:
D. Richard Hipp
drh@hwaci.com
http://www.hwaci.com/drh/
704.948.4565
|
Copies of these notes, example source code, and other
resources related to this tutorial are available online at
http://www.hwaci.com/tcl2k/
$Id: index.html 11708 2007-02-12 23:01:19Z shyouhei $ |
Tutorial Outline
- Introduction
- Building It Yourself
- "Hello, World!" using Tcl
- Tcl scripts as C strings
- Adding new Tcl commands
- A tour of the Tcl API
- Tcl initialization scripts
- Adding Tk
- Tools Survey
- Mktclapp
- "Hello World" using mktclapp
- Adding C code
- Other Features
- Invoking Tcl from C
- Running mktclapp directly
- Real-world examples
- Summary
Embedding Tcl in C/C++ Applications
- You know how to program in Tcl/Tk
- You know how to program in C/C++
- This tutorial is about how to do both at the same time.
Why Mix C With Tcl/Tk?
- Use C for the things C is good at and Tcl for the things
Tcl is good at.
- Generate standalone executables.
- Eliminate the need to install Tcl/Tk.
- Prevent problems when the wrong version of Tcl/Tk is installed.
- Prevent end users from changing the source code.
- Keeps users from creating new bugs.
- Protects proprietary code.
- Use Tcl/Tk as a portability layer for a large C program
- Use Tcl as a testing interface
Why Mix C With Tcl/Tk?
"Use C for the things C is good at and use Tcl/Tk for the things
Tcl/Tk is good at."
C is good at:
- Speed
- Complex data structures
- Computation
- Interacting with hardware
- Byte-by-byte data analysis
|
|
Tcl/Tk is good at:
- Building a user interface
- Manipulation of strings
- Portability
- Opening sockets
- Handling events
|
Programming Models
Mainstream Tcl Programming Model:
|
|
Embedded Tcl Programming Model:
|
- Add bits of C code to a large Tcl program
|
|
- Add bits of Tcl code to a large C program
|
- Main Tcl script loads extensions written in C
|
|
- Main C procedure invokes the Tcl interpreter
|
- Tcl/Tk is a programming language
|
|
|

Most of the Tcl2K conference is about
|
|

This tutorial is about
|
"Hello, World!" Using The Tcl Library
#include <tcl.h> |
|
 |
|
Always include <tcl.h> |
int main(int argc, char **argv){
Tcl_Interp *interp; |
| | | |
interp = Tcl_CreateInterp(); |
|
 |
|
Create a new Tcl interpreter |
Tcl_Eval(interp, "puts {Hello, World!}"); |
|
 |
|
Execute a Tcl command. |
return 0;
} |
| | | |
Compiling "Hello, World!"
Unix:
$ gcc hello.c -ltcl -lm -ldl
$ ./a.out
Hello, World!
Windows using Cygwin:
C:> gcc hello.c -ltcl80 -lm
C:> a.exe
Hello, World!
Windows using Mingw32:
C:> gcc -mno-cygwin hello.c -ltcl82 -lm
 |
Also works with VC++ |
Where Does -ltcl Come From On Unix?
Build it yourself using these steps:
- Get tcl8.2.2.tar.gz from Scriptics
- zcat tcl8.2.2.tar.gz | tar vx
- ./configure --disable-shared
- Move libtcl8.2.a to your lib directory.
- Copy ../generic/tcl.h into /usr/include.
What Other Libraries Are Required For Unix?
- The sequence of -l options after -ltcl
varies from system to system
- Observe what libraries the TCL makefile inserts when
it is building tclsh
- Examples in this talk are for RedHat Linux 6.0 for Intel
How To Compile Under Unix Without Installing Tcl
Specify the *.a file directly:
$ gcc -I../tcl8.2.2/generic hello.c \
../tcl8.2.2/unix/libtcl8.2.a -lm -ldl
$ strip a.out
$ ./a.out
Hello, World!
Or, tell the C compiler where to look for *.a files:
$ gcc -I../tcl8.2.2/generic hello.c \
-L../tcl8.2.2/unix -ltcl -lm -ldl
$ strip a.out
$ ./a.out
Hello, World!
 |
The -I../tcl8.2.2 argument
tells the compiler where to
find <tcl.h>. |
What's "Cygwin"?
- An implementation of GCC/G++ and all development tools
for Windows95/98/NT/2000
- Available for free download at
http://sourceware.cygnus.com/cygwin/
- Also available shrink-wrapped at your local software retailer or
online at
http://www.cygnus.com/cygwin/index.html
- Programs compiled using Cygwin require a special
DLL (cygwin1.dll) that provides a POSIX system API
- Cygwin1.dll cannot be shipped with proprietary programs
without purchasing a license from Cygnus.
- Mingw32 is the same compiler as Cygwin, but generates
binaries that do not use cygwin1.dll
Where Does -ltcl82 Come From On Windows?
Build it like this:
- Get tcl82.lib and tcl82.dll from Scriptics.
- nm tcl82.lib | grep 'T _' | sed 's/.* T _//' >>tcl82.def
- dlltool --def tcl82.def --dllname tcl82.dll --output-lib libtcl82.a
- Move libtcl82.a to the lib directory and tcl82.dll
to the bin directory.
Where Does Your Code Go?
#include <tcl.h>
int main(int argc, char **argv){
Tcl_Interp *interp;
interp = Tcl_CreateInterp(); |
| | | |
/* Your application code goes here */ |
|
 |
|
Insert C code here to do whatever it is your program is
suppose to do |
return 0;
} |
| | | |
Building A Simple TCLSH
#include <tcl.h>
int main(int argc, char **argv){
Tcl_Interp *interp;
char *z;
char zLine[2000];
interp = Tcl_CreateInterp(); |
| | | |
while( fgets(zLine,sizeof(zLine),stdin) ){ |
|
 |
|
Get one line of input |
Tcl_Eval(interp, zLine); |
|
 |
|
Execute the input as Tcl. |
z = Tcl_GetStringResult(interp);
if( z[0] ){
printf("厚 |