I try to write GUI programs with gnome-builder and try to make the layout with Glade. I made a tutorial on Programmer's Notes.
C
#include <gtk/gtk.h>
int main(int argc, char *argv[]){
GError *err = NULL;
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "window_main.ui", &err);
if (err != NULL) {
fprintf (stderr, "Unable to read file: %s\n", err->message);
g_error_free(err);
return 1;
}
window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
if (window == NULL || !GTK_IS_WINDOW(window)) {
fprintf (stderr, "Unable to get window. (window == NULL || window != GtkWindow)\n");
return 1;
}
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
void on_window_main_destroy(GtkWidget *widget, gpointer user_data){
gtk_main_quit();
}
Glade
<?xml version=1.0 encoding="UFT-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window_main">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Test Window</property>
<property name="default_width">640</property>
<property name="default_height">480</property>
<signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
<child>
<placeholder/>
</child>
</object>
</interface>
The .glade file is located in the src/ direction like the .c file.
When I run the code, following error message returns
Unable to read file: Failed to open file “./window_main.ui”: No such file or directory
I don't know where I should put the glade file, because when I compiled my self with
gcc main.c -Wall 'pkg-config --cflags --libs gtk+-3.0' -export-dynamic
and run it with ./a.out
the window opens without any error.