0

我正在开发一个 ubuntu 系统。我的目标是基本上使用 TCL/TK 的 GUI 工具用 C 语言制作 IDE。我安装了 tcl 8.4、tk8.4、tcl8.4-dev、tk8.4-dev 并在我的系统中有 tk.h 和 tcl.h 头文件。但是,当我运行一个基本的 hello world 程序时,它会显示很多错误。

#include "tk.h"
#include "stdio.h"
void hello() {
     puts("Hello C++/Tk!");
}
int main(int, char *argv[])
{
     init(argv[0]);
     button(".b") -text("Say Hello") -command(hello);
     pack(".b") -padx(20) -pady(6);
}

一些错误是

tkDecls.h:644: error: expected declaration specifiers before ‘EXTERN’

/usr/include/libio.h:488: error: expected ‘)’ before ‘*’ token

In file included from tk.h:1559,
                 from new1.c:1:
tkDecls.h:1196: error: storage class specified for parameter ‘TkStubs’
tkDecls.h:1201: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token

/usr/include/stdio.h:145: error: storage class specified for parameter ‘stdin’

tk.h:1273: error: declaration for parameter ‘Tk_PhotoHandle’ but no such parameter

谁能告诉我如何纠正这些错误?请帮忙...

4

1 回答 1

2

你在那里写Tcl或C?对此的困惑是导致所有这些错误的原因。

假设你只是编写 Tcl 来弹出一个 Tk GUI 来做某事,你创建一个hello.tcl用以下内容调用的文件:

package require Tk
proc hello {} {
    puts "Hello C++/Tk!"
}
button .b -text "Say Hello" -command hello
pack .b -padx 20 -pady 6

然后你用这个运行它:

wish hello.tcl

要在 C 程序中运行它,您需要做更多的工作。

#include <tcl.h>
#include <tk.h>
int main(int argc, char **argv) {
    Tcl_Interp *interp;

    Tcl_FindExecutable(argv[0]);
    interp = Tcl_CreateInterp();
    Tcl_Eval(interp,
        "package require Tk\n"
        "proc hello {} {\n"
            "puts \"Hello C++/Tk!\"\n"
        "}\n"
        "button .b -text \"Say Hello\" -command hello\n"
        "pack .b -padx 20 -pady 6\n");
    Tk_MainLoop();
    Tcl_DeleteInterp(interp);
    return 0;
}

字符串文字,分成几行,应该从以前开始就可以识别了。您可能希望使用Tcl_EvalFile引入脚本以从另一个文件运行,因为编写所有这些反斜杠以进行引用会很乏味。也有替代方案Tk_MainLoop,所有这些都涉及Tcl_DoOneEvent某个地方(Tk_MainLoop也是一个包装器),但到目前为止,我无法告诉你什么最适合你。

编译上面的代码,按顺序链接 libtk 和 libtcl 。我不记得您是否也必须明确链接到 X11 库,或者链接到 Tk 是否就足够了。

于 2010-09-06T08:39:07.900 回答