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

Calling C functions from Tcl

0 views
Skip to first unread message

Aleksandar Matijaca

unread,
Oct 29, 1998, 3:00:00 AM10/29/98
to
Hi there,

I am a relative newbie to Tcl, so please bear with me.
I am doing some development in Tcl on a Solaris box.
I would like to call a function that was written in C and is
residing in a dynamic library - something.so for example.

If my function is called f() and it looks like this:

int f() {

return 5;

}

How do I "call" it from Tcl? There don't seem to be any
realy clear answers...

Thanks, Alex.

Tom Poindexter

unread,
Oct 30, 1998, 3:00:00 AM10/30/98
to
In article <36390806...@polyorb.com>,
Aleksandar Matijaca <ale...@polyorb.com> wrote:

>I am a relative newbie to Tcl, so please bear with me.

Welcome aboard!

>I am doing some development in Tcl on a Solaris box.
>I would like to call a function that was written in C and is
>residing in a dynamic library - something.so for example.

>How do I "call" it from Tcl? There don't seem to be any
>realy clear answers...

The easiest for something like this is to use SWIG, which can
generate a Tcl wrapper for you in short order. www.swig.org
Much documenation and tutuorial material exists for SWIG.

The traditional, manual method, is to write your own interface,
which is pretty simple. All commands have four arguments: the
interp struct, a clientdata pointer, a count of the number of
arguments (ie. argc), and a pointer to an array of arguments (ie.
argv[]). The argc/argv combination is very similar to what you
would get if you coded your program for command line shell arguments.

Your job is to parse the arguments in a meaniful way, perform the unique
steps of your extension, set a Tcl return value, and return with
TCL_OK if everything went well.

How does all of this look? The best teacher is Tcl itself. All of
the built-in commands are implemented this way. Look over the code
in the Tcl source in the files 'tclCmd??.c'. Any of the large number of
Tcl extensions also can be used as examples. And extension writing
also is explained in various books, including Ousterhout's, Welch's, etc.

--
Tom Poindexter
tpoi...@nyx.net
http://www.nyx.net/~tpoindex/

0 new messages