c++tclcouttclsh

tcl script executed directly with tclsh is having different behavior than sourcing on tclsh


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

Solution

  • 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]