Search code examples
cmakepkg-config

Pkg-config can't find glib-2.0 during CMake run


I got my hands on a small project with CMakeLists.txt already written and trying to build it on Ubuntu MATE 20.04. After installing libs via apt i got stuck with missing glib-2.0:

Make Error at /snap/cmake/1334/share/cmake-3.27/Modules/FindPkgConfig.cmake:607 (message):
  A required package was not found
Call Stack (most recent call first):
  /snap/cmake/1334/share/cmake-3.27/Modules/FindPkgConfig.cmake:829 (_pkg_check_modules_internal)
  CMakeLists.txt:9 (pkg_check_modules)

First i checked if it's installed with apt search libglib2.0 | grep installed (both regular and dev, just in case):

libglib2.0-0/focal-updates,focal-security,now 2.64.6-1~ubuntu20.04.6 amd64 [installed]
libglib2.0-bin/focal-updates,focal-security,now 2.64.6-1~ubuntu20.04.6 amd64 [installed]
libglib2.0-cil/focal,now 2.12.40-3 amd64 [installed]
libglib2.0-cil-dev/focal,now 2.12.40-3 amd64 [installed]
libglib2.0-data/focal-updates,focal-updates,focal-security,focal-security,now 2.64.6-1~ubuntu20.04.6 all [installed]
libglib2.0-dev/focal-updates,focal-security,now 2.64.6-1~ubuntu20.04.6 amd64 [installed]
libglib2.0-dev-bin/focal-updates,focal-security,now 2.64.6-1~ubuntu20.04.6 amd64 [installed]
libglib2.0-doc/focal-updates,focal-updates,focal-security,focal-security,now 2.64.6-1~ubuntu20.04.6 all [installed]
libglib2.0-tests/focal-updates,focal-security,now 2.64.6-1~ubuntu20.04.6 amd64 [installed]

pkg-config displays it with pkg-config --list-all | grep glib :

gio-unix-2.0                   GIO unix specific APIs - unix specific headers for glib I/O library
glib-2.0                       GLib - C Utility Library
dbus-c++-glib-1                libdbus-c++-glib - Native C++ bindings for D-Bus (Glib Mainloop)
dbus-glib-1                    dbus-glib - GLib integration for the free desktop message bus
gio-2.0                        GIO - glib I/O library
glib-sharp-2.0                 GLib - GLib

And CMakeLists.txt has lines to find it (i guess that's where it fails):

include(FindPkgConfig)
pkg_check_modules (GLIB REQUIRED IMPORTED_TARGET glib-2.0)
pkg_check_modules (DBUS REQUIRED IMPORTED_TARGET dbus-1)
pkg_check_modules (SQLITE3 REQUIRED IMPORTED_TARGET sqlite3)
pkg_check_modules (JSON-C REQUIRED IMPORTED_TARGET json-c)

I've tried asking ChatGPT and it recommended to pass the full path to GLIB_LIBRARY variable when executing CMake:

cmake -DGLIB_LIBRARY=/usr/lib/x86_64-linux-gnu/libglib-2.0.so

As you may guess it took me nowhere.

I'm pretty sure there's some configuring has to be done in CMakeLists.txt, so i'm going over CMake docs.

Will appreciate any clues/ideas.


Solution

  • I've decided to try and comment out some lines is CMake-related files. In toolchain file linux-x86_64.cmake i got rid of this line:

    set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
    

    I guess it messed up the behavior of pkg-config. After doing that CMake successfully found all mentioned libs.