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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL that calls C++ code 1

Status
Not open for further replies.

UchihaItachi

IS-IT--Management
Jun 23, 2010
7
ES
Hi,

I'm new here and I'm trying to get tcl script's three variables, pass them as arguments to a function in c + + code.

Help please. Thanks.
 
I am not sure if I understand your request correctly. As I I understand, this is what you are trying to do.

C/C++ code:

void somefunc(int argc, char* argv[])
{
int i = 1;
while(i < argc)
{
cout << "Arg " << i << ":" << *argv << endl;
i++;
}
}
int main(int argc, char *argv[])
{
if (argc >= 4)
{
cout << "Entered " << argc << " arguments." << endl;
somefunc(argc, argv);
}
else
{
cout << "Need 3 or more arguments" << endl;
return 1;
}
return 0;
}

Script code:

#!usr/bin/expect --

puts "Sending three arguments to C/C++ app..."

if {[catch {exec /path/to/exe/cpp_app.exe a1 a2 a3}]} {
if {[lindex $::errorCode 2] eq "1"} {
puts "cpp_app.exe error: [lindex $::errorCode 2]"
} else {
# Something else went wrong
}
}

puts "All is well...hopefully."


Hope this helps.


 
Thanks for your help. My question is like sending tcl Three variables obtained, a code c + +.

I will try to prove your answer.
 
Few key notes about my first response...

Typo fix:
cout << "Arg " << i << ":" << *argv << endl;
The line above should be:
cout << "Arg " << i << ":" << argv << endl;

Script code is in Expect lanugage. You maybe writing in TCL but idea is the same.

Line below can send script defined variables to C/C++ app w/ modification from:
if {[catch {exec /path/to/exe/cpp_app.exe a1 a2 a3}]}...
to:
if {[catch {exec /path/to/exe/cpp_app.exe $a1 $a2 $a3}]}...

For example, these variables are defined prior to executing line above:
set a1 "Hello"
set a2 "there"
set a3 "UchihaItachi!!!"

Good luck.




 
Hello,

ExpectTheUnexpected, this part:

if ([catch (exec / path / to / exe / cpp_app.exe $ a1 $ a2 $ a3)]) (
if ([lIndex $:: errorCode 2] eq "1") (
puts "cpp_app.exe error: [lIndex $:: errorCode 2]
Else ()
# Something else went wrong
)
)

I put in the Tcl script and run it gives me error. I put here: / path / to / exe / cpp_app.exe, the road where I have the code in c + +.

A doubt while I run the TCL script I have to compile the code in c + +?

Greetings.
 

It looks like you have incorrect syntax. Your code is wrong!

Code:
if ([catch (exec / path / to / exe / cpp_app.exe $ a1 $ a2 $ a3)]) (    
if ([lIndex $:: errorCode 2] eq "1") (
  puts "cpp_app.exe error: [lIndex $:: errorCode 2]
  Else ()
    # Something else went wrong    
  )
)

What language is this? You MUST follow the syntax defined by, in this case, Expect/TCL language.

Please look at the code syntax above carefully. I will paste same code below again. Take care not to replace BRACKETS "{" with PARENTHESES "(" as well as there are no spaces in PATH. Furthermore, "$ a" does NOT equal "$a" - no space!


Correct code syntax:

Code:
set a1 "Hello"
set a2 "there"
set a3 "UchihaItachi!!!"

if {[catch {exec /path/to/exe/cpp_app.exe $a1 $a2 $a3}]} {
  if {[lindex $::errorCode 2] eq "1"} {
    puts "cpp_app.exe error: [lindex $::errorCode 2]"
  } else {
      # Something else went wrong
  }
}
puts "All is well...hopefully."

Please note, I am not familiar with TCL language but I think this code SHOULD work under TCL as well!!!



 
You have to compile C/C++ code. You have to include iostream header. I had to install C++ compiler using yum.

I ran above script code with TCL interpreter with no problems.

Good luck
 
First thank you for the assistance. I compiled the code c + + what I said, I run the Tcl script. But failed to Tcl script variables are seen in c++ code.

Thank you.
 
Hmm. I think I understand your concern. Change the code as follows:

Code:
set a1 "Hello"
set a2 "there"
set a3 "UchihaItachi!!!"

set status [catch {exec /path/to/exe/cpp_app.exe "\$a1" $a1 "\$a2" $a2 "\$a3" $a3} result]

puts "$result"
if {$status} { puts "cpp_app error: $status}

Notice now there are 6 arguments to cpp_app.exe. For example, "\$a1" is sent literally. You can associate this with $a1 variable. You don't have to do it but it is an aid for you.

In cpp_app.exe argv[1] would be "$a1". argv[2] will be "Hello" and so on. Run this new code. Does this help?

Thanks



 
Thank you very much for your great help. Now everything is running correctly.

Thank you for taking your time to resolve my doubt.

I will keep working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top