c++ - tcl script executed directly with tclsh is having different behavior than sourcing on tclsh - Stack Overflow

I have a cpp executable which prints messages on console. when i try to execute this with tclsh, i get

I have a cpp executable which prints messages on console. when i try to execute this with tclsh, i get different behavior than sourcing on tcl console.

C++ code

#include<iostream>
int main()
{
  std::cout << "Testing c++ cout from tcl sehll" << std::endl;
  return 0;
}

run.tcl :

  exec ./a.out

C++ message skipped/suppressed when i use "tclsh run.tcl". Alternatively if I launch tcl console first with tclsh and then 'source run.tcl' message gets printed.

tclsh
% source run.tcl
Testing c++ cout from tcl sehll

I have a cpp executable which prints messages on console. when i try to execute this with tclsh, i get different behavior than sourcing on tcl console.

C++ code

#include<iostream>
int main()
{
  std::cout << "Testing c++ cout from tcl sehll" << std::endl;
  return 0;
}

run.tcl :

  exec ./a.out

C++ message skipped/suppressed when i use "tclsh run.tcl". Alternatively if I launch tcl console first with tclsh and then 'source run.tcl' message gets printed.

tclsh
% source run.tcl
Testing c++ cout from tcl sehll
Share Improve this question edited Mar 26 at 10:12 user2890240 asked Mar 26 at 9:32 user2890240user2890240 114 bronze badges 3
  • Does it really suppress the output? Doesn't it just give you an interactive tclsh session? (Note that tclsh does not complain about unknown arguments - like "-s" - but seems to just ignore them.) – molbdnilo Commented Mar 26 at 9:47
  • To be more concrete, try tclsh run.tcl instead. – molbdnilo Commented Mar 26 at 9:58
  • Thanks molbdnilo, updated the question. – user2890240 Commented Mar 26 at 10:12
Add a comment  | 

2 Answers 2

Reset to default 5

From the exec documentation (my emphasis):

If standard output has not been redirected then the exec command returns the standard output from the last command in the pipeline [...]

And from source:

The return value from source is the return value of the last command executed in the script".

The return value is what gets printed when you run interactively.
You need to print that value in your tcl script if you want to see it, otherwise it just gets dicarded:

puts [exec ./a.out]

Using catch we can get the message.

set res [catch {exec ./a.out} msg ]
puts "$msg"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744155246a4560828.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信