在c程序中包括tk.h和tcl.h

发布于 2024-09-05 09:32:01 字数 1009 浏览 1 评论 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

有人可以告诉我如何纠正这些错误吗?请帮忙...

i am working on an ubuntu system. My aim is to basically make an IDE in C language using GUI tools from TCL/TK. I installed tcl 8.4, tk8.4, tcl8.4-dev, tk8.4-dev and have the tk.h and tcl.h headers file in my system. But, when I am running a basic hello world program it's showing a hell lot of errors.

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

Some of the errors are

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

Can anyone please tell me how can I rectify these errors? Please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-09-12 09:32:03

你在那里写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_DoOneEventTk_MainLoop 也是一个包装器),但我不知道是什么到目前为止的证据对你来说是最好的。

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

Are you writing Tcl or C there? Confusion over that is what's causing all those errors.

Assuming you're just writing Tcl to pop up a Tk GUI that does something, you make a file called hello.tcl with this contents:

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

Then you run it with this:

wish hello.tcl

To run this from within a C program, you need to do more work.

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

The string literal, broken up over several lines, ought to be fairly recognizable from before. You might want to use Tcl_EvalFile instead to bring in the script to run from another file, because writing all those backslashes for quoting gets tedious. There are also alternatives to Tk_MainLoop, all of which involve Tcl_DoOneEvent somewhere (Tk_MainLoop is a wrapper round that too) but I can't tell what's best for you there on the evidence so far.

Compile the above code, linking against both libtk and libtcl in that order. I can't remember if you have to explicitly link against the X11 library too, or whether linking against Tk will be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文