2
// 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.

5frame
  • 101
  • 1
  • 8
  • Can you run it through GDB and append a backtrace to your question? – GWW Aug 29 '11 at 14:03
  • Program received signal SIGSEGV, Segmentation fault. 0x00000000 in ?? () (gdb) backtrace #0 0x00000000 in ?? () – 5frame Aug 29 '11 at 15:02
  • You need to make sure you have the `-g` flag specified when compiling (and it's easier if you disable optimizations) – GWW Aug 29 '11 at 15:06

2 Answers2

3

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

michel-slm
  • 9,438
  • 3
  • 32
  • 31
2

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(...).