error in testfile GTK

This is a discussion on error in testfile GTK within the C Programming forums, part of the General Programming Boards category; Hello I finally got GTK installed. This is my code that i am trying to compile. If i compile it, ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    error in testfile GTK

    Hello

    I finally got GTK installed. This is my code that i am trying to compile.
    If i compile it, it doesnt give any errors.
    When i run it, the program gets in an infinite loop at gtk_init.
    I dont know how this can happen?

    Code:
    #include <gtk/gtk.h>
    
    int main (int argc, char *argv[]){
    
    	GtkWidget		*window;
    	
    	printf("test\n");
    	gtk_init (&argc, &argv);
    		printf("test\n");
    
    	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    	
    	gtk_widget_show( window);
    
    	gtk_main();
    
    	return 0;
    }
    I am compiling with : gcc -Wall -ansi -pedantic oef1.c -o oef1 `pkg-config --cflags --libs gtk+-2.0`


    Thank you

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Does this mean your other thread is fixed (regarding using pkg-config)?
    problem installing GTK, MAC
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    It seems that I recall that until you define a handler for destroy(), that is what happens and you must kill the process to stop it. If you have CMake available for your platform I wrote a simple Python script to jump-start these kinds of projects. Basically you run the script, answer a few simple questions and the app generates (in this case) GTK skeletal source along with a proper CMake makefile, and compiles the source into a working demo app which is just a little more that what you have. You can download the latest version of the script is here: JBCobb.net Post Topic CMake Wizard 2.3 (Maint. release) but if you just want the code it generates (well the GTK part anyways) this is what it does:

    Code:
    /*************************************************
     * File: gtktest2_main.cpp
     *************************************************/
    #include "gtktest2.hpp"
    #include <iostream>
    #include <string>
    using namespace std;
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <getopt.h>
    #include <gtk/gtk.h>
    
    #define CMDOPTS "hi:"
    
    static void destroy(GtkWidget *window, gpointer data)
    {
      gtk_main_quit();
    }
    
    static gboolean delete_event(GtkWidget *window, GdkEvent *event, gpointer data)
    {
    
      return FALSE;
    }
    
    
    void Usage()
    {
        cout << "Usage: Blah blah" << endl;
        exit(-1);
    }
    
    int main(int argc, char *argv[])
    {
       cout << "gtktest2_test 1.0" << endl;
       string inputFile = "";
       int nRC = 0;
       opterr = 0;
       int c;
       while((c = getopt(argc, argv, CMDOPTS)) != EOF)
       {
          switch(c)
          {
    	 case 'h':
    	 {
    	    Usage();
    	    break;
    	 }
    	 case 'i':
    	 {
    	    inputFile = optarg;
    	    break;
    	 }
          }
       }
       cout << "Testing: " << getLibName() << endl;
      GtkWidget *window, *label;
    
      gtk_init(&argc, &argv);
    
      window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    
      gtk_window_set_title(GTK_WINDOW(window), "Hello World");
    
      gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    
      gtk_widget_set_size_request(window, 200, 100);
    
      g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
      g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
    
      label = gtk_label_new("Hello world");
    
      gtk_label_set_selectable(GTK_LABEL(label), TRUE);
    
      gtk_container_add(GTK_CONTAINER(window), label);
    
      gtk_widget_show_all(window);
    
      gtk_main();
    
    
       return nRC;
    }
    and it generates this:
    Attached Images Attached Images  
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you guys for all the help. But i found my solution. I needed to update X11.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MinGW32 and gtk setup in Windows
    By Joelito in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-05-2009, 10:39 AM
  2. Replies: 1
    Last Post: 09-22-2008, 04:49 AM
  3. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 12:37 AM
  4. Replies: 1
    Last Post: 10-24-2005, 06:35 AM
  5. GTK Help
    By Cronkilla in forum C Programming
    Replies: 2
    Last Post: 08-24-2004, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21