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

Global Variable from C++ to Tcl

4 views
Skip to first unread message

Sathya

unread,
Oct 31, 2006, 7:09:59 PM10/31/06
to
Hello Everybody

I am faced with a small problem.

I have a c++ program, which sources a tcl file at run-time.

I wish to define a global variable in the c++ program and see if I can
access it in the Tcl function call without having to explicitly define
it as a result.

i.e.

proc myfunc {} {
global myvar # defined in C++ code
set myvar 0.0
}

On returning the variable myvar in C++ contains the value that is set.

Can some one please explain how to do this ?

Thanks
Sathya

Ralf Fassel

unread,
Nov 1, 2006, 4:48:47 AM11/1/06
to
* "Sathya" <sathya.kri...@gmail.com>

| proc myfunc {} {
| global myvar # defined in C++ code
| set myvar 0.0
| }
|
| On returning the variable myvar in C++ contains the value that is set.
|
| Can some one please explain how to do this ?

For 'int' and 'double' variables, check

http://www.tcl.tk/man/tcl8.4/TclLib/LinkVar.htm

R'

Sathya

unread,
Nov 1, 2006, 11:22:35 AM11/1/06
to
Hi Ralf

Thanks. This is very useful.

Are there any extensions for regular array and named-array objects as
well ?

Thanks

Regards
Sathya Krishnamurthy

Ralf Fassel

unread,
Nov 1, 2006, 11:34:01 AM11/1/06
to
* "Sathya" <sathya.kri...@gmail.com>
| [Tcl_LinkVar()]

| Are there any extensions for regular array and named-array objects
| as well ?

None that I know of. The concept of C/C++ arrays and TCL arrays is
completely different.

However, setting a trace on a TCL-array which updates the C-array
accordingly should work if the TCL array key is integer.

I haven't checked whether
int array[10];
Tcl_Linkvar( &array[0], "array(0)")
Tcl_Linkvar( &array[1], "array(1)")
...
would work.

R'

Uwe Klein

unread,
Nov 1, 2006, 11:47:48 AM11/1/06
to
Sathya wrote:
> Hi Ralf
>
> Thanks. This is very useful.
>
> Are there any extensions for regular array and named-array objects as
> well ?

You may want to have a look into "vector" from blt.
those can be accessed as commands or in array style on the tcl side
and provide a c api.

http://manpages.debian.net/cgi-bin/display_man.cgi?id=1ffccf1c8389489e8bf47aacffaabe5d&format=plain

uwe

Sathya

unread,
Nov 1, 2006, 12:46:32 PM11/1/06
to
Hi Ralf and Uwe

Thanks for your recommendations. I will try out some of these and get
back to you.

Thanks again
Sathya

0 new messages