I created very simple GTK+ GUI with Glade, which includes one toggle button. I want to assign action to this button. I have a problem with getting the signal handler for the button to work. After compiling and running I get this message:
Gtk-WARNING **: Could not find signal handler 'on_togglebutton1_toggled'. Did you compile with -rdynamic?
My current C code looks like this:
#include <gtk/gtk.h>
#include <stdio.h>
#include "f_back.h"
#define UI_FILE "gui.ui"
GtkWidget * create_window( void )
{
GtkWidget * window;
GtkBuilder * builder;
GError * error = NULL;
builder = gtk_builder_new();
if( !gtk_builder_add_from_file( builder, UI_FILE, & error ) )
{
g_warning( "Can't load builder file: %s", error->message );
g_error_free( error );
}
gtk_builder_connect_signals( builder, NULL );
window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );
g_object_unref( builder );
return window;
}
int
main( int argc, char * argv[] )
{
GtkWidget * window;
gtk_init( & argc, & argv );
okno = create_window();
gtk_widget_show( window );
gtk_main()
return 0;
}
f_back.h:
#include <gtk/gtk.h>
void on_togglebutton1_toggled( GtkToggleButton * togglebutton, gpointer user_data );
f_back.c:
#include "f_back.h"
void on_togglebutton1_toggled( GtkToggleButton * togglebutton, gpointer user_data )
{
g_print( "Toggle button pressed\n" );
}
GTK+ UI:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Testowe</property>
<property name="window_position">center</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="row_homogeneous">True</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkImage" id="image1">
<property name="name">logo</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="yalign">0.10000000149011612</property>
<property name="pixbuf">logo_large.bmp</property>
<style>
<class name="logo"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="togglebutton1">
<property name="label" translatable="yes">LED ON/OFF</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<style>
<class name="grid"/>
</style>
</object>
</child>
</object>
</interface>
I've tried many options described on forums:
- compiling with -rdynamic,
- compiling with -export-dynamic,
- compiling with -lgmodule-2.0,
- adding gmodule-export-2.0 library.
- adding extern "C" before function declaration,
- using G_MODULE_EXPORT.
None of these solutions worked. I always get the same message.
My current Geany build commands:
g++ -Wall -c "%f" -l wiringPi
pkg-config --cflags gtk+-3.0 --libs gtk+-3.0 gmodule-export-2.0
-rdynamic -export-dynamic -lgmodule-2.0g++ -Wall -o "%e" "%f" -l wiringPi
pkg-config --cflags gtk+-3.0 --libs gtk+-3.0 gmodule-export-2.0
-rdynamic -export-dynamic -lgmodule-2.0
Does anybody knows what's wrong with my code?
extern "C"
andG_MODULE_EXPORT
at the same time. – andlabsgcc -o output_file main.o -pthread `pkg-config --cflags --libs gtk+-3.0` -export-dynamic
fixed the problem. – Noodles