tags:

views:

77

answers:

1

hi, 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...

A: 

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.

Donal Fellows