Tcl/Tk call procedure in C

Tcl/Tk call procedure in C

Post by Kelvi » Sat, 13 Nov 1999 04:00:00



Hi,
I am new in Tcl/Tk. I would like to know the approach to use Tcl/Tk to call
up some procedure written in C. How am i supppose to pass parameters to the
C procedure and how the results generated by the C procedure send back to
the Tcl/Tk. Please advice.

------------------  Posted via CNET Linux Help  ------------------
                    http://www.searchlinux.com

 
 
 

Tcl/Tk call procedure in C

Post by R.K.A » Sat, 13 Nov 1999 04:00:00



> Hi,
> I am new in Tcl/Tk. I would like to know the approach to use Tcl/Tk to call
> up some procedure written in C. How am i supppose to pass parameters to the
> C procedure and how the results generated by the C procedure send back to
> the Tcl/Tk. Please advice.

If you're working with C/Tcl combos you might want to take a look at
http://www.hwaci.com/sw/mktclapp/ but post your question in
comp.lang.tcl instead.

K.
--
Step by step guide to TrueType fonts under RedHat6:
http://home.powertech.no/rkaa/linux.html#ttf

 
 
 

Tcl/Tk call procedure in C

Post by Robert Helle » Sun, 14 Nov 1999 04:00:00



  In a message on Fri, 12 Nov 1999 07:32:24 GMT, wrote :

K> Hi,
K> I am new in Tcl/Tk. I would like to know the approach to use Tcl/Tk to call
K> up some procedure written in C. How am i supppose to pass parameters to the
K> C procedure and how the results generated by the C procedure send back to
K> the Tcl/Tk. Please advice.

Basically, you write a new Tcl 'command'.

Say you have a C function Foo with a prototype like this:

double Foo (double x, double y);

in a file named foo.c

You write a wrapper function like this in a file named foowrap.c:

#include <tcl.h>

double Foo (double x, double y);

int MyTclCWrapFoo (ClientData /*clientData*/, Tcl_Interp *interp,
                  int argc, char *argv[])
{
    double x,y,resultval;

    /* Make sure the argument list is the proper length */
    if (argc != 3) {
        Tcl_SetResult(interp, "Wrong # args. Foo X Y", TCL_STATIC);
        return TCL_ERROR;
    }
    /* Convert two arguments to doubles */
    if (Tcl_GetDouble(interp,argv[1],&x) == TCL_ERROR) return TCL_ERROR;
    if (Tcl_GetDouble(interp,argv[2],&y) == TCL_ERROR) return TCL_ERROR;
    /* Execute your C function */
    resultval = Foo(x,y);
    /* Pass the result back to Tcl */
    Tcl_PrintDouble(interp, resultval, interp->result);
    return TCL_OK;

Quote:}

Then you need to grab a copy of /usr/lib/tcl8.0/tclAppInit.c (or
/usr/lib/tk8.0/tkAppInit.c) and add in a line like:

    Tcl_CreateCommand(interp, "Foo", MyTclCWrapFoo, (ClientData) NULL,
                        NULL);

    Just after the comment:

    /*
     * Call Tcl_CreateCommand for application-specific commands, if
     * they weren't already created by the init procedures called above.
     */

   Now compile your C files:

cc -g -Wall foo.c -c -o foo.o
cc -g -Wall -I/usr/include foowrap.c -c -o foowrap.o
cc -g -Wall -I/usr/include tclAppInit.c -c -o tclAppInit.o
And link it:
cc -g -Wall -o mytclsh tclAppInit.o foowrap.o foo.o -L/usr/lib -ltcl8.0 -ldl  -lm

Now the program mytclsh has a new function 'Foo', which implements your
C function 'Foo'.

It is also possible to create a shared library with your C code in it.
If you have a large body of C (or C++) code, you might want to get SWIG,
a package that will construct all of the Tcl interface code for you.
SWIG can be downloaded from
'http://www.cs.utah.edu/~beazley/SWIG/swig.html'.

Feel free to visit my web site (http://www.deepsoft.com/) and download
either the Role Playing DB package or the Home Librarian package.  Both
packages are built as Tcl/Tk extensions (both are C++, but the principle
is the same for plain C).

K>
K> ------------------  Posted via CNET Linux Help  ------------------
K>                     http://www.searchlinux.com
K>          

--
                                     \/


http://www.deepsoft.com              /\FidoNet:    1:321/153

 
 
 

1. RPM/Tcl-Tk Question: Right Upgrade Procedure?

I think I may be screwing up somehow as I attempt to upgrade the
Tk-Tcl packages in RH 4.2 to the latest versions (8.1-1, I think).

Using Glint, should I be doing an INSTALL, or an UPGRADE?  Or does it
matter?  I used INSTALL and ended up with what appears to be both the
new and the old packages installed. This manifests itself when I
attempt to start FileRunner, which fails to run and reports problems
executing the initialization script.

Anyone else run into this? Suggestions?

TIA,
TL
"There's a lot to learn
 from wasting time."   -Mr. Neil Young

2. Where is xpmroot

3. Bash calls TCL, TCL calls Bash, 2nd Bash never reads input

4. chmod question

5. TK/tcl gurus: wrong version of Tk loaded (3.6): need 4.x?

6. LINUX will not install right on WD 516MB HD?

7. doubt in calling C functions in tcl/tk

8. POP3 client

9. CALL FOR AUTHORS - Tcl/Tk

10. Linking Problems (C calling Tcl/Tk)

11. Linux Tcl/Tk diffs in netcom.com: ~ftp/pub/yggdrasil/misc/tcl-diffs

12. Plans for Tcl/Tk8.4 version of tksh (ksh93 + Tcl/Tk)?

13. Tk/TCL for RS6000/AIX ?