Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling C function from TCL

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
CA
Hi guys,

Thanks alot for your answers to my previous questions. They were really helpful. I have another question regarding calling C functions from tcl procedures. For example:

proc sendData {} {
set a 12
set b 8
sendToC $a $b ------ //sendToC is a C procedure which I
am trying to call.
...
}

Is this possible.? (I know I have written it incorrectly.)
 
No problem at all.

You can think of the Tcl interpreter as parsing a command in 3 steps, in the following order:[ol][li]Determine word boundaries based on white-space characters and quoting.[/li][li]Perform variable, command, and backslash substitution where permitted (i.e., not inside a [tt]{}[/tt] quoted word).[/li][li]Use the first word as the command to execute, passing the remaining words as arguments to the command.[/li][/ol]Applying those steps to your example, Tcl would determine that there were three words. Tcl performs variable substitution on the second and third words. Tcl then calls the sendToC command, passing it the 2 arguments. So, when your sendToC command executes, all it sees are the values "12" and "8" as arguments. It has no idea that these values came from variable substitution; that was already handled by the Tcl interpreter itself.

Now it's also possible to pass the name of a variable as an argument. For example:

Code:
sendToC a b

In a case like this, you could code the command to actually modify the values of those variables if you wanted. But let's not get into that in this post. Learn to walk before running. :)

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hi Mr. Jones,

say what if you have a function called cFunc in your c code which you want to call from your TCL code, more precisely proc sendData (which would pass 8 and 12 as the arguments to the C function In the c code.)
Basically what I am asking is how can I call a function in my c code from a procedure in my TCL code.
Please help me out here.
 
Several different options, depending on whether or not you have existing C code. There's lots of good information on this topic on the Tcl'ers Wiki, I'd suggest starting with the page "How to write C-coded extensions for Tcl," If you have only a few little snippets of C code that you'd like to include (for example, for performace reasons), you might like to check out "CriTcl builds C extensions on-the-fly," And if you have an existing library of C/C++ code, by far the easiest way to make that available as Tcl commands is with SWIG,
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hi Mr. Jones,

Basically what I am trying to do is just send one or two variables for example a=1 and b=2 defined in my tcl procedure to my c function. In essence what should happen

proc tclP {} {
set a 1
set b 2
cFunc $a $b ---------->
-------> cFunc is the c function in my c code which would just accept these values 1 and 2 and do something.

What do you recommand the easiest way to do this. If not too much trouble can you give me some more helpful websites.
Thanks alot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top