Thread: GTK+ C++ compiler and signal handlers

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    7

    GTK+ C++ compiler and signal handlers

    So, I'm using GTK+ library and GLADE gui constructor, but I'm getting an error when i compile the code in C++, the GTK+ can't find my signal handlers but when i just modify my file names from *.cpp for *.c to codeblocks compile in C, the GTK finds my handlers.
    Other test that i made was put my handler directly in the main file and this didn't work too. Why GTK don't find my handlers?

    Compiler settings:
    Compiler -> other options:
    Code:
    `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
    Linker -> other options:
    Code:
    -export-dynamic
    main.cpp or main.c:
    Code:
    #include <gtk/gtk.h>
    #include "Callbacks.h"
    
    //this handler isn't found in C++
    void on_main_window_destroy(GtkWidget widget, gpointer data_user)
    {
        gtk_main_quit();
    }
    
    int main (int argc, char *argv[])
    {
        GtkBuilder *builder;
        GtkWidget *main_window;
    
        gtk_init(&argc,&argv);
    
        builder = gtk_builder_new();
        gtk_builder_add_from_file(builder,"GUI/main_gui.glade",NULL);
    
        main_window = GTK_WIDGET( gtk_builder_get_object(builder,"main_window"));
        gtk_builder_connect_signals(builder,NULL);
        g_object_unref(G_OBJECT(builder));
    
        gtk_widget_show(main_window);
        gtk_main();
    
        return 0;
    }
    Callbacks.h
    Code:
    #include <gtk/gtk.h>
    
    void on_main_window_destroy(GtkWidget widget, gpointer data_user);
    
    void on_open_menu_activate(GtkWidget widget, gpointer data_user);
    
    void on_save_menu_activate(GtkWidget widget, gpointer data_user);
    
    void on_information_menu_activate(GtkWidget widget, gpointer data_user);
    
    void on_quit_menu_activate(GtkWidget widget, gpointer data_user);
    Callbacks.c or cpp
    Code:
    #include "Callbacks.h"
    
    /*void on_main_window_destroy(GtkWidget widget, gpointer data_user)
    {
        gtk_main_quit(); removed to not duplicate definition(main.cpp)
    }*/
    
    //handlers that aren't found in C++ but are found in C
    void on_quit_menu_activate(GtkWidget widget, gpointer data_user)
    {
        gtk_main_quit();
    }
    
    void on_open_menu_activate(GtkWidget widget, gpointer data_user)
    {
        /*
         ... */
    }
    
    void on_save_menu_activate(GtkWidget widget, gpointer data_user)
    {
    
    }
    
    void on_information_menu_activate(GtkWidget widget, gpointer data_user)
    {
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    And there are no other errors?

    Have you tried using g++ directly?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    7
    Quote Originally Posted by MK27 View Post
    And there are no other errors?

    Have you tried using g++ directly?
    So i tried this command in the terminal:
    Code:
    g++ `pkg-config --cflags --libs gtk+-2.0` -o main  main.cpp Callbacks.cpp
    and got the same error:
    Code:
    (main:2784): Gtk-WARNING **: Could not find signal handler 'on_main_window_destroy'
    
    (main:2784): Gtk-WARNING **: Could not find signal handler 'on_information_menu_activate'
    
    (main:2784): Gtk-WARNING **: Could not find signal handler 'on_quit_menu_activate'
    
    (main:2784): Gtk-WARNING **: Could not find signal handler 'on_save_menu_activate'
    
    (main:2784): Gtk-WARNING **: Could not find signal handler 'on_open_menu_activate'
    Any suggestion?

    Thanks.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Maybe it's a C++ name mangling problem. Try declaring the signal handlers as extern "C" functions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    7
    Quote Originally Posted by brewbuck View Post
    Maybe it's a C++ name mangling problem. Try declaring the signal handlers as extern "C" functions.
    You are a genius, now it works. Thank you very much

  6. #6
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    I wonder if this is the solution to my problem; Linking C++ Static Libs, but I'm confused as to why the linker won't like a c++ app to a c++ static library.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    for static libraries in C++, they really need to be built with the same compiler as the code in which you're trying to use them. that way you can be (relatively) sure that the names are mangled in the same format. that is what makes it so difficult to use libraries built with G++ in VC++ and vice versa.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return Handlers
    By Laks in forum C Programming
    Replies: 0
    Last Post: 11-22-2009, 07:18 AM
  2. Return Handlers
    By Laks in forum C Programming
    Replies: 0
    Last Post: 11-22-2009, 07:17 AM
  3. storing handlers (templated classes)
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 11:16 AM
  4. uncaught exception handlers
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-26-2008, 08:50 AM
  5. Signal Handlers
    By MethodMan in forum Linux Programming
    Replies: 1
    Last Post: 03-05-2003, 12:20 AM