Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you again! I can't tell you how much I and my company appreciate what you've done! I love this place!..."

Geography

Where in the world do Tek-Tips members come from?

Calling C function from TCL

shussai2 (Programmer)
25 Sep 03 10:02
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.)
AviaTraining (Instructor)
25 Sep 03 11:57
No problem at all.

You can think of the Tcl interpreter as parsing a command in 3 steps, in the following order:
  1. Determine word boundaries based on white-space characters and quoting.
  2. Perform variable, command, and backslash substitution where permitted (i.e., not inside a {} quoted word).
  3. Use the first word as the command to execute, passing the remaining words as arguments to the command.
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:

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, www.avia-training.com
  866-TCL-HELP (866-825-4357) US Toll free
  415-643-8692 Voice
  415-643-8697 Fax

shussai2 (Programmer)
25 Sep 03 12:49
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.
AviaTraining (Instructor)
25 Sep 03 13:01
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, http://wiki.tcl.tk. I'd suggest starting with the page "How to write C-coded extensions for Tcl," http://wiki.tcl.tk/2334. 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," http://wiki.tcl.tk/2516. 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, http://wiki.tcl.tk/719.

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

shussai2 (Programmer)
25 Sep 03 13:08
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close