Pregunta

// gcc -o 0 $(pkg-config --cflags --libs gtk+-2.0) 1.c
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
    GFile *f1 = NULL;
    f1 = g_file_new_for_path ("/home/user/1.txt");
    g_printf ("File loaded successfully.\n");
    return 0;
}

When I run this program it causes segmentation fault at g_file_new_for_path ()(whether or not /home/user/1.txt exists).
Have I miswrote the code? Or is it a bug for my system?

P. S. : My system is Arch Linux, and GLib version is 2.28.8-1.

¿Fue útil?

Solución

You need to call g_type_init() before using g_file_new_for_path -- as per this thread. After that, the program works.

Otros consejos

Firs of all you should use

// gcc -o 0 $(pkg-config --cflags --libs gio-2.0) 1.c    
#include <gio/gio.h>

instead of

// gcc -o 0 $(pkg-config --cflags --libs gtk+-2.0) 1.c    
#include <gtk/gtk.h>

Then you should g_type_init() before of g_file_new_for_path(...).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top