PDA

View Full Version : [SOLVED] Problem with building GTK programs in Code::Blocks



scottptn
October 2nd, 2008, 02:47 PM
Hello,

Im using Ubuntu 8.04 . In Code::Blocks v8.02, i created a new GTK+ project. The main.c code was autogenerated and is as follows :



#include <stdlib.h>
#include <gtk/gtk.h>

static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;

dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}

int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;

/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "Hello World");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);

/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);

button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}



But when , i press Ctrl+F9 to build the project , i get the following errors :



Compiling: main.c
/home/scott/code/prjTestWin/main.c:2:21: error: gtk/gtk.h: No such file or directory
/home/scott/code/prjTestWin/main.c:4: error: expected ‘)’ before ‘*’ token
/home/scott/code/prjTestWin/main.c: In function ‘main’:
/home/scott/code/prjTestWin/main.c:16: error: ‘GtkWidget’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:16: error: (Each undeclared identifier is reported only once
/home/scott/code/prjTestWin/main.c:16: error: for each function it appears in.)
/home/scott/code/prjTestWin/main.c:16: error: ‘button’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:17: error: ‘win’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:18: error: ‘vbox’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:21: warning: implicit declaration of function ‘g_log_set_handler’
/home/scott/code/prjTestWin/main.c:21: error: ‘G_LOG_LEVEL_WARNING’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:21: error: ‘GLogFunc’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:21: error: expected ‘)’ before ‘gtk_false’
/home/scott/code/prjTestWin/main.c:22: warning: implicit declaration of function ‘gtk_init’
/home/scott/code/prjTestWin/main.c:23: error: ‘g_log_default_handler’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:26: warning: implicit declaration of function ‘gtk_window_new’
/home/scott/code/prjTestWin/main.c:26: error: ‘GTK_WINDOW_TOPLEVEL’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:27: warning: implicit declaration of function ‘gtk_container_set_border_width’
/home/scott/code/prjTestWin/main.c:27: warning: implicit declaration of function ‘GTK_CONTAINER’
/home/scott/code/prjTestWin/main.c:28: warning: implicit declaration of function ‘gtk_window_set_title’
/home/scott/code/prjTestWin/main.c:28: warning: implicit declaration of function ‘GTK_WINDOW’
/home/scott/code/prjTestWin/main.c:29: warning: implicit declaration of function ‘gtk_window_set_position’
/home/scott/code/prjTestWin/main.c:29: error: ‘GTK_WIN_POS_CENTER’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:30: warning: implicit declaration of function ‘gtk_widget_realize’
/home/scott/code/prjTestWin/main.c:31: warning: implicit declaration of function ‘g_signal_connect’
/home/scott/code/prjTestWin/main.c:31: error: ‘gtk_main_quit’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:34: warning: implicit declaration of function ‘gtk_vbox_new’
/home/scott/code/prjTestWin/main.c:34: error: ‘TRUE’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:35: warning: implicit declaration of function ‘gtk_container_add’
/home/scott/code/prjTestWin/main.c:37: warning: implicit declaration of function ‘gtk_button_new_from_stock’
/home/scott/code/prjTestWin/main.c:37: error: ‘GTK_STOCK_DIALOG_INFO’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:38: warning: implicit declaration of function ‘G_OBJECT’
/home/scott/code/prjTestWin/main.c:38: warning: implicit declaration of function ‘G_CALLBACK’
/home/scott/code/prjTestWin/main.c:38: error: ‘helloWorld’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:38: error: ‘gpointer’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:38: error: expected ‘)’ before ‘win’
/home/scott/code/prjTestWin/main.c:39: warning: implicit declaration of function ‘gtk_box_pack_start’
/home/scott/code/prjTestWin/main.c:39: warning: implicit declaration of function ‘GTK_BOX’
/home/scott/code/prjTestWin/main.c:41: error: ‘GTK_STOCK_CLOSE’ undeclared (first use in this function)
/home/scott/code/prjTestWin/main.c:46: warning: implicit declaration of function ‘gtk_widget_show_all’
/home/scott/code/prjTestWin/main.c:47: warning: implicit declaration of function ‘gtk_main’
Process terminated with status 1 (0 minutes, 0 seconds)
21 errors, 19 warnings


I can successfully compile and run gtk programs from the command line . Also, in Code::Blocks, i tried adding
`pkg-config --cflags gtk+-2.0 ` to compiler settings -> other options and
`pkg-config --libs gtk+-2.0 ` to linker settings->other linker options, but i still get the above errors.

Please help

Thanks.

scottptn
October 6th, 2008, 06:54 PM
I initially had libgtk1.2-dev installed . The problem got resolved after i installed libgtk2.0-dev.