C – Include tk.h and tcl.h in the C program

Include tk.h and tcl.h in the C program… here is a solution to the problem.

Include tk.h and tcl.h in the C program

I’m using an ubuntu system. My goal is to basically make an IDE in C using TCL/TK’s GUI tools. I have tcl 8.4, tk8.4, tcl8.4-dev, tk8.4-dev installed and have tk.h and tcl.h header files on my system. However, when I run a basic hello world program, it shows a large number 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 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 tell me how to correct these errors? Please help….


This is not an effective program at all. What you’re trying to do is embed Tcl and Tk into your C application. Read the relevant sections of Tcl/Tk books or study the Tcl wiki (e.g. 1).

To run a Tcl or Tk command, you must initialize the Tcl_Interp correctly. So at least you have to initialize the Tcl library and create an interpreter. Then for Tk, you will need to initialize the library and run the event loop. This is discussed in the documentation for Tcl_AppInit, and the tclAppInit.c file in the Tcl source code (or tkAppInit.c in Tk) shows you how to set up your application. Typically, you would use the provided tkAppInit file as “main” and put custom application initialization into a Tcl_AppInit function called from the Tcl or Tk main functions.

It is unwise to call the Tk function from C. Define scripts and write Tk bits in Tcl. Even Tk itself uses Tcl scripts (from library/*.tcl) to create standard dialogs, etc.

Related Problems and Solutions