All I want is just a simple dialog to select a file for processing. I didn't use C lang for a while, and I can't find a good working example.
code:
#include <gtk/gtk.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
GtkFileChooserNative *native;
native = gtk_file_chooser_native_new ("Open File", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
return 0;
}
I compile it with this command:
gcc `pkg-config gtk+-3.0 --cflags` `pkg-config gtk+-3.0 --libs` -o out dialog.c
I am having segmentation fault on gtk_file_chooser_native_new ()
Maybe strace will help:
You need to call gtk_init
(before any other GTK function), or have your own application class and call g_application_run. And your main
should be int main(int argc, char**argv)
as usual.
The following program does not segfault (on Linux/Debian/Sid, GTK is 3.22.7)
#include <gtk/gtk.h>
#include <stdio.h>
int main (int argc, char **argv) {
GtkFileChooserNative *native = NULL;
gtk_init (&argc, &argv);
native = gtk_file_chooser_native_new ("Open File", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
guint res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (native));
if (res == GTK_RESPONSE_ACCEPT) {
char *filename;
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
filename = gtk_file_chooser_get_filename (chooser);
printf ("should open %s\n", filename);
g_free (filename);
}
/// in a real application perhaps you want: gtk_main ();
return 0;
}
and does show a dialog. Compile that using
gcc -Wall -g $(pkg-config gtk+-3.0 --cflags) \
$(pkg-config gtk+-3.0 --libs) \
-o out dialog.c
and use the gdb
debugger when debugging.