×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!
  • Students Click Here

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Calling C function from TCL

Calling C function from TCL

Calling C function from TCL

(OP)
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.)

RE: Calling C function from TCL

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

RE: Calling C function from TCL

(OP)
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.

RE: Calling C function from TCL

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

RE: Calling C function from TCL

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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