TCL library return string from C ++ function

I have the following code

int redirect_test(ClientData pClientData, Tcl_Interp *pInterp, int argc, char *argv[])
{
    std::string result_str =  "This is the output from the method";
    Tcl_SetObjResult(pInterp, Tcl_NewStringObj(result_str.c_str(), -1));
    return true;
}

      

However, when I try to use this method like this

% set m [ redirect_test]
This is the output from the method
% puts $m
can't read "m": no such variable

      

How do I return values ​​from TCL functions?

+3


source to share


1 answer


The problem is this:

return true;

      

Tcl command implementations indicate success by returning the constant C TCL_OK

(= 0) and indicating errors with TCL_ERROR

(= 1). (There are other more specific result codes, but you do not recommend using them if you are not sure what they mean.) true

Is converted to 1

using C ++ bool

β†’ int

cast operator TCL_ERROR

, which leads to the failure of the team (and your line result - this message error).



The fixation is trivial. Use this instead:

return TCL_OK;

      

+2


source







All Articles