I am new at GTK programming and I am facing the following issue. I can compile the code without any warnings or errors however when I execute the binary I get this massage and the button does not work. The error looks like this:
(project:9686): Gtk-WARNING **: 16:26:31.359: Could not find signal handler ‘on_button_clicked’. Did you compile with -rdynamic?
That is the code:
#include <gtk/gtk.h> static const gchar *interface = "<interface>" " <object class="GtkWindow" id="main-window">" " <signal name="destroy" handler="gtk_main_quit"/>" " <child>" " <object class="GtkButton" id="my-button">" " <property name="label">Hallo, Welt!</property>" " <signal name="clicked" handler="on_button_clicked"/>" " </object>" " </child>" " </object>" "</interface>"; G_MODULE_EXPORT void on_button_clicked (GtkWidget *w, gpointer d) { g_print ("Hallo, Welt!n"); } int main (int argc, char *argv[]) { GtkBuilder *builder; GError *error = NULL; GtkWidget *window; gtk_init (&argc, &argv); builder = gtk_builder_new (); gtk_builder_add_from_string (builder, interface, -1, &error); gtk_builder_connect_signals (builder, NULL); window = GTK_WIDGET(gtk_builder_get_object (builder, "main-window")); gtk_widget_show_all (window); gtk_main (); return 0; }
I am working at a Linux machine and I compiled in the terminal with the following command:
g++ -Wall -std=c++0x project.cpp `pkg-config --cflags --libs gtk+-3.0 gmodule-2.0` -o project
I tried to compile with:
- -rdynamic
- -Wl,–export-dynamic
- -lgmodule-2.0
pkg-config --cflags --libs gtk+-3.0 gmodule-no-export-2.0
but nothing worked.
Advertisement
Answer
It’s hard to know for sure, but I would guess the problem is that you are compiling using a C++ compiler whereas your example is written in C. The C++ compiler is “mangling” the names of your exported symbols. If you aren’t writing the rest of your program in C++, then I would recommend compiling with gcc -std=c99
instead of g++ -std=c++0x
.
On the other hand, if the rest of your program does need to be written in C++, then you will need to prefix every symbol that you want to export for GtkBuilder (which does not know about C++ name mangling) with extern "C"
, or place them all together inside one or more extern "C" { ... }
blocks. This will prevent the name from being mangled, so then GtkBuilder will be able to find it.
To check whether this is the case, try running nm -g | grep on_button_clicked
(without any “demangle” option) on your compiled file. If the name is mangled, you will see something like __Z17on_button_clicked13BLAH14BLAH
whereas if it is not, then you will just see _on_button_clicked
.