2012-12-13 29 views
1

我想在另一個「c」代碼文件中使用我的tcl代碼的一些功能(API)。但我不明白如何做到這一點,特別是如何鏈接它們。爲此,我採取了非常簡單的tcl代碼,其中包含一個添加兩個數字並打印總和的API。任何人都可以告訴我如何調用這個tcl代碼來獲得總和。我如何編寫一個將調用此tcl代碼的c包裝器。下面是我用我的樣品TCL程序:如何在c代碼中使用tcl apis

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

set a 10 

    set b 20 

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

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

} 
+0

對不起。我現在已經接受了幾個對我非常有幫助的回覆。 – user1497818

+0

任何人都可以請回復這個。這對我來說非常緊迫 – user1497818

回答

4

從C代碼評估一個腳本,使用Tcl_Eval()或其近親屬之一。爲了使用該API,您需要鏈接到Tcl庫initialize the Tcl librarycreate an interpreter以保存執行上下文。另外,你真的應該做一些工作來檢索結果,並打印出來

因此,你得到這樣的事情(打印腳本錯誤出來就顯得尤爲重要,因爲它幫助很多調試!):

#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

首先感謝您的回覆。我會盡快嘗試並回復您。 – user1497818

+0

我在嘗試下面的錯誤時得到了:simple_addition_wrapper_new.c :(。text + 0x12):未定義對'Tcl_FindExecutable'的引用 simple_addition_wrapper_new.c :(.text + 0x17):未定義對'Tcl_CreateInterp'的引用 simple_addition_wrapper_new (.text + 0x2f):undefined reference to'Tcl_Eval' simple_addition_wrapper_new.c :(.text + 0x3f):未定義對'Tcl_GetObjResult'的引用 simple_addition_wrapper_new.c :(.text + 0x47) Tcl_GetString' simple_addition_wrapper_new.c :(。text + 0x9f):未定義引用'Tcl_DeleteInterp' – user1497818

+0

可能是什麼問題? – user1497818

0

我想我已經解決了它。你是對的。問題在於我使用的包含方法。我在c代碼中包含tcl.h,tclDecls.h和tclPlatDecls.h文件,但這些文件不在路徑/ usr/include中,因此我將這些文件複製到該目錄,可能不是合適的辦法。最後,我還沒有將這些文件複製到/ usr/include,並在編譯時給出了包含路徑。我創建了可執行文件,它在終端上給出了正確的結果。謝謝你的幫助。

這裏是確切的C代碼我使用:

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

}

並編輯成代碼,並生成可執行文件我用下面的命令:

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

我已經執行文件simple_addition_op,並得到了正確的結果

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

我特別感謝Donal Fellows和Johannes

+0

在主代碼中,我希望結果爲30.但是現在它以字符數組的形式出現。我們可以使用Tcl_GetInt嗎?如果是,如何使用它來獲得完整的整數作爲返回值(這裏是30) – user1497818