chinmay
chinmay

Reputation: 361

`fatal error: glib.h: No such file or directory` when compiling with gcc

I tried installing gtk and glib on Linux using:

sudo apt-get install libglib2.0-dev

and

sudo apt-get install libgtk2.0-dev

...but when building with gcc I still get this error:

fatal error: glib.h: No such file or directory
   16 | #include <glib.h>
      |          ^~~~~~~~
compilation terminated.

I then tried locating the glib.h file using locate glib.h, but it does not return any path for this file.

Here is the run and output of locate glib.h. Notice that none of the findings are glib.h:

$ locate glib.h
/home/asus/Qt5.8.0/5.8/Src/qtbase/src/3rdparty/libjpeg/jpeglib.h
/home/asus/Qt5.8.0/5.8/Src/qtimageformats/src/3rdparty/jasper/src/libjasper/jpg/jpg_jpeglib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/base/message_loop/message_pump_glib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/third_party/harfbuzz-ng/src/hb-glib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/third_party/libjpeg/jpeglib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/third_party/libjpeg_turbo/jpeglib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/third_party/pdfium/third_party/libjpeg/jpeglib.h
/home/asus/Qt5.8.0/5.8/Src/qtwebengine/src/3rdparty/chromium/ui/events/platform/x11/x11_event_source_glib.h
/usr/include/reglib/reglib.h
/usr/src/linux-headers-4.8.0-36-generic/include/config/blk/dev/bsglib.h

I know I have glib.h somewhere on my system now because I just installed glib with sudo apt-get install libglib2.0-dev, so how do I make locate find it?

And, once I find it, how do I make gcc find it when building?


Update: using the answers provided here and here, I used sudo updatedb and then locate glib.h again, and found it at /usr/include/glib-2.0/glib.h. I included that directory in my gcc build command with -I/usr/include/glib-2.0 and got past the glib.h error.

Now now I get a new error:

/usr/include/glib-2.0/glib/gtypes.h:32:10: fatal error: glibconfig.h: No such file or directory
   32 | #include <glibconfig.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

How do I solve this new glibconfig.h error?

I think it is related to glib also.

Update: I got the answer: using pkg-config --cflags --libs gtk+-2.0 solves this.

Upvotes: 16

Views: 62577

Answers (3)

Gabriel Staples
Gabriel Staples

Reputation: 52449

For those of you, such as myself, stumbling upon this problem and needing the full solution:

How to install locate and glib, and then tell gcc where to find glib.h and glibconfig.h

Tested and works on Ubuntu 22.04.

For context, I am building QEMU for PIC32 by following these instructions plus my own modifications as I figure it out.

In this answer, I will only address the parts pertinent to my answer title above.

This answer solves the following two errors you may encounter when building on Linux wth gcc:

fatal error: glib.h: No such file or directory
   16 | #include <glib.h>
      |          ^~~~~~~~
compilation terminated.

and:

/usr/include/glib-2.0/glib/gtypes.h:32:10: fatal error: glibconfig.h: No such file or directory
   32 | #include <glibconfig.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

Steps:

  1. Install locate and glib:

    Install locate.

    sudo apt update
    sudo apt install plocate
    

    Reference: This answer to Ask Ubuntu: How to install the locate command?

    Install glib.

    sudo apt install libglib2.0-dev
    
  2. Update the locate database so it can find any file just added to your system:

    First, since we installed glib after installing locate, we need to update the locate database:

    sudo updatedb
    

    References:

    1. This other answer to Ask Ubuntu: How to install the locate command?
    2. Answer by @alk
  3. Find the location of glib.h and glibconfig.h using the locate command:

    Note that locate is a generic tool that can find any file on your system, not just build files.

    locate glib.h
    locate glibconfig.h
    

    For me, the last line of the output from locate glibconfig.h was:

    /usr/lib/x86_64-linux-gnu/glib-2.0/include/glibconfig.h
    
  4. Tell gcc where to find glib.h and glibconfig.h:

    We need to pass the following options to gcc to tell it where to find these files. We could use the -I "include directory" option, but since these are system files included by angle brackets as #include <glib.h> instead of by quotes as #include "glib.h", it's better to use the -isystem "include system directory" as follows:

    -isystem/usr/include/glib-2.0
    

    For the glibconfig.h file, let's get a more generic path.

    uname -m on my system outputs x86_64, so a more generic include path option would be:

    -isystem/usr/lib/$(uname -m)-linux-gnu/glib-2.0/include
    

    So, the full set of additional options to pass to gcc would be:

    -isystem/usr/include/glib-2.0 -isystem/usr/lib/$(uname -m)-linux-gnu/glib-2.0/include
    

    References:

    1. Answer by @0___________
    2. My own knowledge.
    3. Chats with GitHub Copilot.
  5. If passing these options to a make command, however, you can do it via the CFLAGS environment variable like this.

    In the command below, I've also added time to the beginning to time how long it takes to build, -j "$(nproc)" to parallelize the build across all available processors, and -Wno-error to disable converting warnings to errors, for my situation specifically, so it will build.

    Here's my final make command:

    time make CFLAGS="-Wno-error -isystem/usr/include/glib-2.0 -isystem/usr/lib/$(uname -m)-linux-gnu/glib-2.0/include" -j "$(nproc)"
    

    Now, it builds and doesn't give me the errors about missing glib headers.

    Fixed errors:

    fatal error: glib.h: No such file or directory
    fatal error: glibconfig.h: No such file or directory
    

    References:

    1. Append compile flags to CFLAGS and CXXFLAGS during configuration/make
    2. My own knowledge.
    3. Chats with GitHub Copilot.

Done!

See also

  1. My answer: How to compile my own glibc C standard library from source and use it?
  2. My tutorial: Modern instructions to build and run QEMU for PIC32

Upvotes: 0

alk
alk

Reputation: 70883

but locate glib.h does not return any path for glib

You might want to update locate's database (by running updatedb), or wait until tomorrow ... ;-)

Upvotes: 5

0___________
0___________

Reputation: 67476

Add -I/usr/include/glib-2.0 to your compiler command line (check the path of course). You will need to amend your makefile probably

Upvotes: 8

Related Questions