c++cmakeglibconanpkg-config

PkgConfig can't find 'glib-2.0' when trying to build with CMake


I am trying to compile a program that's written in C++ by using CMake.

The contents of the CMakeLists.txt file it the code came with are:

cmake_minimum_required(VERSION 3.21)
project(<redacted> C CXX)

set(CMAKE_CXX_STANDARD 23)

add_executable(<redacted> src/main.cpp
        src/<redacted>.h
        src/<redacted>.h src/<redacted>.cpp
        src/<redacted>.h
        src/<redacted>.h src/<redacted>.cpp
        src/<redacted>.h src/<redacted>.cpp
        src/<redacted>.h
        src/<redacted>.h src/<redacted>.c
        src/<redacted>.h)

find_package(PkgConfig REQUIRED)

pkg_search_module(GLIB REQUIRED glib-2.0)


include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})

target_link_libraries(<redacted> ${GLIB_LIBRARIES} gcrypt)

When I run the command: cmake . -G "MinGW Makefiles" && make in w64devkit (Or in Visual Studio, or VSCode), I get the following error:

-- Found PkgConfig: C:/Users/Jonathon/Downloads/pkg-config-lite-0.28-1_bin-win32/pkg-config-lite-0.28-1/bin/pkg-config.exe (found version "0.28")
-- Checking for module 'glib-2.0'
--   No package 'glib-2.0' found
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:634 (message):
  The following required packages were not found:

   - glib-2.0

Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:862 (_pkg_check_modules_internal)
  CMakeLists.txt:28 (PKG_CHECK_MODULES)


-- Configuring incomplete, errors occurred!

There is a similar post here but that was on linux, and I am on Windows. Also, the solution which was found was removing a line from CMakeLists.txt, but that line isn't present in my CMakeLists.txt file.

Any help would be highly appreciated. Thanks


[Update]

I have tried installing glib by using conan, using this as the conanfile.txt:

[requires]
glib/2.78.3

[generators]
CMakeDeps
CMakeToolchain

Then, when I type the command conan install . --output-folder=build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake && cmake --build (as suggested here because I was getting an 'ERROR: Conanfile not found' when I just used conan install . --output-folder=build && cmake --build ), this is the output I get:

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows


======== Computing dependency graph ========
Graph root
    conanfile.txt: C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\conanfile.txt
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916 - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2 - Cache
Build requirements
    autoconf/2.71#f9307992909d7fb3df459340f1932809 - Cache
    automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50 - Cache
    m4/1.4.19#b38ced39a01e31fef5435bc634461fd2 - Cache
    meson/1.2.2#04bdfb85d665c82b08a3510aee3ffd19 - Cache
    msys2/cci.latest#5a31efa2bde593541fd5ac3bcc50c01c - Cache
    ninja/1.11.1#77587f8c8318662ac8e5a7867eb4be21 - Cache
    pkgconf/2.0.3#f996677e96e61e6552d85e83756c328b - Cache
Resolved version ranges
    zlib/[>=1.2.11 <2]: zlib/1.3.1

======== Computing necessary packages ========
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4:67bfcb7b4b78262b9d05495e479dcd92f747316b#cea9c0f0dfa41e83aebd5d39c5a5cbf3 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01:750e9c8cf560085c96c0fa42531c45c18af8be50#29d02404508c39a0803c3bee40426924 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916:7bfde258ff4f62f75668d0896dbddedaa7480a0f#37e6c892e9418389fcf21227e9c1ae6b - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b:55052839d1d508208c0cd4c7594a7001c70287d1#bea6a5629dc31a80b4d489e5ef163ec4 - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd:7bfde258ff4f62f75668d0896dbddedaa7480a0f#9ef92719f5c05dca2f0dbb46f50d3f8d - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695:1bbffc30d8479a890917ef6674ab5e5f3c5b2036#d6a7c0162d1450645a9d56832ca25ae6 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2:7bfde258ff4f62f75668d0896dbddedaa7480a0f#20d10b761ec15eed7a1d61c86bc7415a - Cache
Build requirements
Skipped binaries
    autoconf/2.71, automake/1.16.5, m4/1.4.19, meson/1.2.2, msys2/cci.latest, ninja/1.11.1, pkgconf/2.0.3

======== Installing packages ========
bzip2/1.0.8: Already installed! (1 of 7)
zlib/1.3.1: Already installed! (2 of 7)
libiconv/1.17: Already installed! (3 of 7)
libffi/3.4.4: Already installed! (4 of 7)
pcre2/10.42: Already installed! (5 of 7)
pcre2/10.42: Appending PATH environment variable: C:\Users\Jonathon\.conan2\p\pcre2e09694aa47719\p\bin
libgettext/0.22: Already installed! (6 of 7)
glib/2.78.3: Already installed! (7 of 7)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: zlib/1.3.1, bzip2/1.0.8, pcre2/10.42, libiconv/1.17, libgettext/0.22
WARN: deprecated:     'cpp_info.build_modules' used in: bzip2/1.0.8
WARN: deprecated:     'env_info' used in: pcre2/10.42, libiconv/1.17, bzip2/1.0.8, glib/2.78.3

======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(glib)
    target_link_libraries(... glib::glib)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: Preset 'conan-default' added to CMakePresets.json. Invoke it manually using 'cmake --preset conan-default' if using CMake>=3.23
conanfile.txt: If your CMake version is not compatible with CMakePresets (<3.23) call cmake like: 'cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build\conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW'
conanfile.txt: CMakeToolchain generated: CMakePresets.json
conanfile.txt: CMakeToolchain generated: ..\CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- Checking for one of the modules 'glib-2.0'
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:927 (message):
  None of the required 'glib-2.0' found
Call Stack (most recent call first):
  CMakeLists.txt:20 (pkg_search_module)


-- Configuring incomplete, errors occurred!

Now, glib version 2.78.3 is installed, because that's the latest version available from conan. The earliest version available from conan is 2.65.0, so when I tried changing the conanfile to try and get version 2.0.0, it didn't work. Also, version 2.0.0 is from 2002 and I highly doubt that the project I'm trying to compile was using that because it was made last year.

When I went back to the CMakeLists.txt file and changed this line: pkg_search_module(GLIB REQUIRED glib-2.0) to pkg_search_module(GLIB REQUIRED glib-2.78.3), then ran the conan install . --output-folder=build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake && cmake --build command again, I get this output:

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows


======== Computing dependency graph ========
Graph root
    conanfile.txt: C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\conanfile.txt
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916 - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2 - Cache
Build requirements
    autoconf/2.71#f9307992909d7fb3df459340f1932809 - Cache
    automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50 - Cache
    m4/1.4.19#b38ced39a01e31fef5435bc634461fd2 - Cache
    meson/1.2.2#04bdfb85d665c82b08a3510aee3ffd19 - Cache
    msys2/cci.latest#5a31efa2bde593541fd5ac3bcc50c01c - Cache
    ninja/1.11.1#77587f8c8318662ac8e5a7867eb4be21 - Cache
    pkgconf/2.0.3#f996677e96e61e6552d85e83756c328b - Cache
Resolved version ranges
    zlib/[>=1.2.11 <2]: zlib/1.3.1

======== Computing necessary packages ========
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4:67bfcb7b4b78262b9d05495e479dcd92f747316b#cea9c0f0dfa41e83aebd5d39c5a5cbf3 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01:750e9c8cf560085c96c0fa42531c45c18af8be50#29d02404508c39a0803c3bee40426924 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916:7bfde258ff4f62f75668d0896dbddedaa7480a0f#37e6c892e9418389fcf21227e9c1ae6b - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b:55052839d1d508208c0cd4c7594a7001c70287d1#bea6a5629dc31a80b4d489e5ef163ec4 - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd:7bfde258ff4f62f75668d0896dbddedaa7480a0f#9ef92719f5c05dca2f0dbb46f50d3f8d - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695:1bbffc30d8479a890917ef6674ab5e5f3c5b2036#d6a7c0162d1450645a9d56832ca25ae6 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2:7bfde258ff4f62f75668d0896dbddedaa7480a0f#20d10b761ec15eed7a1d61c86bc7415a - Cache
Build requirements
Skipped binaries
    autoconf/2.71, automake/1.16.5, m4/1.4.19, meson/1.2.2, msys2/cci.latest, ninja/1.11.1, pkgconf/2.0.3

======== Installing packages ========
bzip2/1.0.8: Already installed! (1 of 7)
zlib/1.3.1: Already installed! (2 of 7)
libiconv/1.17: Already installed! (3 of 7)
libffi/3.4.4: Already installed! (4 of 7)
pcre2/10.42: Already installed! (5 of 7)
pcre2/10.42: Appending PATH environment variable: C:\Users\Jonathon\.conan2\p\pcre2e09694aa47719\p\bin
libgettext/0.22: Already installed! (6 of 7)
glib/2.78.3: Already installed! (7 of 7)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: bzip2/1.0.8, libiconv/1.17, libgettext/0.22, zlib/1.3.1, pcre2/10.42
WARN: deprecated:     'cpp_info.build_modules' used in: bzip2/1.0.8
WARN: deprecated:     'env_info' used in: glib/2.78.3, bzip2/1.0.8, libiconv/1.17, pcre2/10.42

======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(glib)
    target_link_libraries(... glib::glib)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: Preset 'conan-default' added to CMakePresets.json. Invoke it manually using 'cmake --preset conan-default' if using CMake>=3.23
conanfile.txt: If your CMake version is not compatible with CMakePresets (<3.23) call cmake like: 'cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build\conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW'
conanfile.txt: CMakeToolchain generated: CMakePresets.json
conanfile.txt: CMakeToolchain generated: ..\CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- Checking for one of the modules 'glib-2.0'
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:927 (message):
  None of the required 'glib-2.0' found
Call Stack (most recent call first):
  CMakeLists.txt:20 (pkg_search_module)


-- Configuring incomplete, errors occurred!

C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build>cd ..

C:\Users\Jonathon\source\tlsdump-main\tlsdump-main>conan install . --output-folder=build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake && cmake --build

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows


======== Computing dependency graph ========
Graph root
    conanfile.txt: C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\conanfile.txt
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916 - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2 - Cache
Build requirements
    autoconf/2.71#f9307992909d7fb3df459340f1932809 - Cache
    automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50 - Cache
    m4/1.4.19#b38ced39a01e31fef5435bc634461fd2 - Cache
    meson/1.2.2#04bdfb85d665c82b08a3510aee3ffd19 - Cache
    msys2/cci.latest#5a31efa2bde593541fd5ac3bcc50c01c - Cache
    ninja/1.11.1#77587f8c8318662ac8e5a7867eb4be21 - Cache
    pkgconf/2.0.3#f996677e96e61e6552d85e83756c328b - Cache
Resolved version ranges
    zlib/[>=1.2.11 <2]: zlib/1.3.1

======== Computing necessary packages ========
Requirements
    bzip2/1.0.8#457c272f7da34cb9c67456dd217d36c4:67bfcb7b4b78262b9d05495e479dcd92f747316b#cea9c0f0dfa41e83aebd5d39c5a5cbf3 - Cache
    glib/2.78.3#0cd1865c8603d90b3bc5858065e16d01:750e9c8cf560085c96c0fa42531c45c18af8be50#29d02404508c39a0803c3bee40426924 - Cache
    libffi/3.4.4#35eb63842b505824b70aedc1baefc916:7bfde258ff4f62f75668d0896dbddedaa7480a0f#37e6c892e9418389fcf21227e9c1ae6b - Cache
    libgettext/0.22#2c87563d7a69544dd9379f038aca3b0b:55052839d1d508208c0cd4c7594a7001c70287d1#bea6a5629dc31a80b4d489e5ef163ec4 - Cache
    libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd:7bfde258ff4f62f75668d0896dbddedaa7480a0f#9ef92719f5c05dca2f0dbb46f50d3f8d - Cache
    pcre2/10.42#a7a2c122056510509a7525c83d6a6695:1bbffc30d8479a890917ef6674ab5e5f3c5b2036#d6a7c0162d1450645a9d56832ca25ae6 - Cache
    zlib/1.3.1#f52e03ae3d251dec704634230cd806a2:7bfde258ff4f62f75668d0896dbddedaa7480a0f#20d10b761ec15eed7a1d61c86bc7415a - Cache
Build requirements
Skipped binaries
    autoconf/2.71, automake/1.16.5, m4/1.4.19, meson/1.2.2, msys2/cci.latest, ninja/1.11.1, pkgconf/2.0.3

======== Installing packages ========
bzip2/1.0.8: Already installed! (1 of 7)
zlib/1.3.1: Already installed! (2 of 7)
libiconv/1.17: Already installed! (3 of 7)
libffi/3.4.4: Already installed! (4 of 7)
pcre2/10.42: Already installed! (5 of 7)
pcre2/10.42: Appending PATH environment variable: C:\Users\Jonathon\.conan2\p\pcre2e09694aa47719\p\bin
libgettext/0.22: Already installed! (6 of 7)
glib/2.78.3: Already installed! (7 of 7)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: pcre2/10.42, libiconv/1.17, bzip2/1.0.8, zlib/1.3.1, libgettext/0.22
WARN: deprecated:     'cpp_info.build_modules' used in: bzip2/1.0.8
WARN: deprecated:     'env_info' used in: pcre2/10.42, bzip2/1.0.8, glib/2.78.3, libiconv/1.17

======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(glib)
    target_link_libraries(... glib::glib)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: Preset 'conan-default' added to CMakePresets.json. Invoke it manually using 'cmake --preset conan-default' if using CMake>=3.23
conanfile.txt: If your CMake version is not compatible with CMakePresets (<3.23) call cmake like: 'cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=C:\Users\Jonathon\source\tlsdump-main\tlsdump-main\build\conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW'
conanfile.txt: CMakeToolchain generated: CMakePresets.json
conanfile.txt: CMakeToolchain generated: ..\CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- Checking for one of the modules 'glib-2.78.3'
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:927 (message):
  None of the required 'glib-2.78.3' found
Call Stack (most recent call first):
  CMakeLists.txt:20 (pkg_search_module)


-- Configuring incomplete, errors occurred!

And this really doesn't make sense to me, because when it's installing packages, it clearly shows this line in bright green: glib/2.78.3: Already installed! (7 of 7), then shortly after, it still shows:

-- Checking for one of the modules 'glib-2.78.3'
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/FindPkgConfig.cmake:927 (message):
  None of the required 'glib-2.78.3' found

So I really have no idea what's going on. glib is clearly installed but the cmake pkgconfig still isn't finding it for some reason. What am I doing wrong?


Solution

  • Summarizing my explanation in the comments:

    It should be possible to set PKG_CONFIG_PATH environment variable to point it to the glib installation. pkg-config looks for libraries in directories specified in this variable (and some others).

    But instead of doing manual configuration, I'd rather recommend using MSYS2. Use it to install all tools you need: pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-pkgconf mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-glib2.

    Open MSYS2 terminal and compile inside of it (to have the correct env variables set). Verify that the terminal prompt says UCRT64 in magenta text (What are MSYS2 environments? How do I pick one?).

    You can try pkg-config --libs --cflags glib-2.0 and see it finds the library.