pkg-config cannot find gtk+-3.0

sdgfsdh picture sdgfsdh · Apr 25, 2018 · Viewed 15.1k times · Source

I am trying to use libui-node to build a Node.js project.

$ yarn add libui-node

This gives an error:

node-gyp configure build Package gtk+-3.0 was not found in the pkg-config search path. Perhaps you should add the directory containing `gtk+-3.0.pc' to the PKG_CONFIG_PATH environment variable No package 'gtk+-3.0' found gyp: Call to 'pkg-config gtk+-3.0 --cflags-only-I | sed s/-I//g' returned exit status 0 while in binding.gyp. while trying to load binding.gyp gyp ERR! configure error ...

So then I follow the instructions:

$ pkg-config gtk+-3.0 --cflags-only-I | sed s/-I//g 

Package gtk+-3.0 was not found in the pkg-config search path. Perhaps you should add the directory containing `gtk+-3.0.pc' to the PKG_CONFIG_PATH environment variable No package 'gtk+-3.0' found

However, I have already installed gtk+-3.0 with this command:

$  sudo apt-get install build-essential libgtk-3-dev

I am on Ubuntu 17.10.

My PKG_CONFIG_PATH:

$ echo $PKG_CONFIG_PATH

/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:

How should I configure my system so that it can find this library?

Answer

liberforce picture liberforce · Apr 26, 2018

EDIT:

Showing the default locations where pkg-config looks for .pc files is more easily done with pkg-config --variable pc_path pkg-config as stated by @BrettHale in this SO answer. This uses a special virtual pkg-config package to expose pkg-config configuration. This is easier than parsing the debug logs or using strace (which saved my day more than once), but the point was more on teaching how to get information when we don't know where to look.

Original answer:

You shouldn't have to set PKG_CONFIG_PATH. Usually the paths your distro uses match the ones pkg-config will look into by default.

pkg-config looks for the .pc associated with GTK+ 3. As you have installed the libgtk-3-dev development package, you find in it the .pc files it provides using:

$ dpkg -L libgtk-3-dev | grep '\.pc'
/usr/lib/x86_64-linux-gnu/pkgconfig/gdk-wayland-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-unix-print-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gdk-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-wayland-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gdk-x11-3.0.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-x11-3.0.pc

Those results are for my Ubuntu 14.04 system, but on Ubuntu 17.10 for amd64, the file has not moved, it's still:

/usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-3.0.pc

Now the file is named gtk+-3.0.pc, so the name of the module as expected by pkg-config is that name without the .pc extension, which is gtk+-3.0. This helps making sure you didn't make a typo in the module name.

For example,

pkg-config --modversion gtk+3.0

would tell you it can't find gtk+3.0 and that you should change PKG_CONFIG_PATH, but in fact the real problem is that it's the wrong module name as there's a missing - character.

Now, we will run pkg-config in its default configuration, without customized PKG_CONFIG_PATH. This will check your system's default behavior, with pkg-config looking only in its default paths:

unset PKG_CONFIG_PATH
pkg-config --modversion gtk+-3.0

If this returns the version of GTK+, you're done. If you still have the error message saying it's not found though, then you may check where pkg-config looks by default in the debug logs. Just add the --debug option:

pkg-config --debug --modversion gtk+-3.0

This returns a quite verbose log of where it detects the .pc files. Here a the first few lines on may Ubuntu 14.04 system:

Option --debug seen
Option --modversion seen
Error printing enabled by default due to use of --version, --libs, --cflags, --libs-only-l, --libs-only-L, --libs-only-other, --cflags-only-I, --cflags-only-other or --list. Value of --silence-errors: 0
Error printing enabled
Adding virtual 'pkg-config' package to list of known packages
Cannot open directory '/usr/local/lib/x86_64-linux-gnu/pkgconfig' in package search path: No such file or directory
Cannot open directory '/usr/local/lib/pkgconfig' in package search path: No such file or directory
Cannot open directory '/usr/local/share/pkgconfig' in package search path: No such file or directory
Scanning directory '/usr/lib/x86_64-linux-gnu/pkgconfig'
[...]

Notice the lines starting with Cannot open directory and Scanning directory. They tell you where pkg-config is looking. Let's only display that:

$ pkg-config --debug --modversion gtk+-3.0 2>&1 | egrep "(Cannot open|Scanning) directory"
Cannot open directory '/usr/local/lib/x86_64-linux-gnu/pkgconfig' in package search path: No such file or directory
Cannot open directory '/usr/local/lib/pkgconfig' in package search path: No such file or directory
Cannot open directory '/usr/local/share/pkgconfig' in package search path: No such file or directory
Scanning directory '/usr/lib/x86_64-linux-gnu/pkgconfig'
Scanning directory '/usr/lib/pkgconfig'
Scanning directory '/usr/share/pkgconfig'

Now you have all the locations that are searched for. Those are the same in my 14.04 and in Ubuntu 17.04 (I checked that in a docker container). Some of those directories exist, others don't. You will notice that /usr/lib/x86_64-linux-gnu/pkgconfig is there for me, so /usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-3.0.pc is found.

If it's not there for you, then yes, you may add it to PKG_CONFIG_PATH:

export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig

This should now return the version of the GTK+ library detected by pkg-config:

pkg-config --modversion gtk+-3.0