How to use tcl apis in a c code

后端 未结 2 1758
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 10:42

I want to use some of the functionalities(APIs) of my tcl code in another \"c\" code file. But i am not getting how to do that especiallly how to link them. For that i have take

相关标签:
2条回答
  • To evaluate a script from C code, use Tcl_Eval() or one of its close relatives. In order to use that API, you need to link in the Tcl library, initialize the Tcl library and create an interpreter to hold the execution context. Plus you really ought to do some work to retrieve the result and print it out (printing script errors out is particularly important, as that helps a lot with debugging!)

    Thus, you get something like this:

    #include <tcl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv) {
        Tcl_Interp *interp;
        int code;
        char *result;
    
        Tcl_FindExecutable(argv[0]);
        interp = Tcl_CreateInterp();
        code = Tcl_Eval(interp, "source myscript.tcl; add_two_nos");
    
        /* Retrieve the result... */
        result = Tcl_GetString(Tcl_GetObjResult(interp));
    
        /* Check for error! If an error, message is result. */
        if (code == TCL_ERROR) {
            fprintf(stderr, "ERROR in script: %s\n", result);
            exit(1);
        }
    
        /* Print (normal) result if non-empty; we'll skip handling encodings for now */
        if (strlen(result)) {
            printf("%s\n", result);
        }
    
        /* Clean up */
        Tcl_DeleteInterp(interp);
        exit(0);
    }
    
    0 讨论(0)
  • 2021-01-24 11:29

    I think i have sloved it out. You were correct. The problem was with the include method that i was using. I have the files tcl.h, tclDecls.h and tclPlatDecls.h included in the c code but these files were not existing in the path /usr/include so i was copying these files to that directory, may be it was not a proper way to do. Finally i have not copied those files to /usr/include and gave the include path while compiling. I have created executable and it is givingthe proper result on terminal. Thanks for your help.

    Here is the exact c code i am using :

    #include <tcl.h>
    #include <tclDecls.h>
    #include <tclPlatDecls.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main (int argc, char **argv) {
    Tcl_Interp *interp;
    int code;
    char *result;
    
    printf("inside main function \n");
    //    Tcl_InitStubs(interp, "8.5", 0);
    Tcl_FindExecutable(argv[0]);
    interp = Tcl_CreateInterp();
    code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
    
    /* Retrieve the result... */
    result = Tcl_GetString(Tcl_GetObjResult(interp));
    
    /* Check for error! If an error, message is result. */
    if (code == TCL_ERROR) {
        fprintf(stderr, "ERROR in script: %s\n", result);
        exit(1);
    }
    
    /* Print (normal) result if non-empty; we'll skip handling encodings for now */
    if (strlen(result)) {
        printf("%s\n", result);
    }
    
    /* Clean up */
    Tcl_DeleteInterp(interp);
    exit(0);
    

    }

    And to compile this code and to generate executable file i am using below command :

    gcc simple_addition_wrapper_new.c -I/usr/include/tcl8.5/ -ltcl8.5 -o simple_addition_op
    

    I have executed the file simple_addition_op and got below result which was proper

    inside main function 
    c is 30 .......
    

    My special thanks to Donal Fellows and Johannes

    0 讨论(0)
提交回复
热议问题