user1497818
user1497818

Reputation: 375

How to use tcl apis in a c code

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 taken a very simple tcl code which contains one API which adds two numbers and prints the sum. Can anybody tell me how can i call this tcl code to get the sum. How can i write a c wrapper that will call this tcl code. Below is my sample tcl program that i am using :

#!/usr/bin/env tclsh8.5
proc add_two_nos { } {

set a 10

  set b 20

  set c [expr { $a + $b } ]

  puts " c is $c ......."

}

Upvotes: 1

Views: 1577

Answers (2)

user1497818
user1497818

Reputation: 375

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

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137567

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);
}

Upvotes: 4

Related Questions