Why can't I build a “hello world” for glib?

前端 未结 5 1451
走了就别回头了
走了就别回头了 2021-01-30 13:26

So here\'s the world\'s simplest glib program:

#include 

I try to compile it with gcc test.c and I get:



        
相关标签:
5条回答
  • 2021-01-30 13:30

    glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.

    Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.

    The canonical way to do what you are trying is

    % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
    

    Note the back-ticks, which tell the shell to run the pkg-config command "in-place".

    0 讨论(0)
  • 2021-01-30 13:33
    > > The canonical way to do what you are trying is
    
    > % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
    

    Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:

    gcc -Wall -o test `pkg-config --cflags glib-2.0` test.c `pkg-config --libs glib-2.0`
    

    It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.

    0 讨论(0)
  • 2021-01-30 13:37

    I am using glib.h as well- Do this to compile all your glib.h programs :)

    gcc `pkg-config --cflags --libs glib-2.0` filename.c
    

    Make sure to surround pkg-config --cflags --libs glib-2.0 with back-quotes which you find under tilde (left most key on the querty keyboard).

    Thank me later .. :P

    0 讨论(0)
  • 2021-01-30 13:41

    As @chris said use pkg-config.

    glibconfig.h is missing 
    

    it’s because this file is not in the /usr/include/glib-2.0, but in /usr/lib/glib-2.0. So you have to include also this /usr/lib path or copy the file to the /include/glib-2.0

    0 讨论(0)
  • 2021-01-30 13:52

    apt-get build-dep is your friend -- nobody can remember all the packages you need to build something.

    0 讨论(0)
提交回复
热议问题