How does CMake use pkg-config to find packages?

12,049

Solution 1

In case someone else stumbles across this like I did:

  • Hack in some MESSAGE calls in Modules/FindPkgConfig.cmake. In particular, before the execute_process call(s) if you want to dump how pkg-config is being invoked.

  • Now, like me, you might be astonished that your MESSAGE's never happen! What I learned is that if cmake invokes pkg-config for a particular package while checking dependencies for some other package earlier in the timeline, then the version it finds is cached in CMakeCache.txt which has session persistence. This means that if you later come and update that package (because some other dependency check is failing) then CMakeCache.txt still holds the old value and so despite you pulling your hair out, cmake errors out because the version or whatever isn't right.

  • Simply put, delete CMakeCache.txt if you update any packages as part of trying to get through cmake errors.

Solution 2

You may display the content of your variables using the message command and call cmake with:

cmake --debug-output ...

Or you may edit the file CMakeCache.txt.

I also suggest that you include the following line so that you see what's going on while you're compiling.

set (CMAKE_VERBOSE_MAKEFILE true)
Share:
12,049
Kao Dome
Author by

Kao Dome

Updated on June 04, 2022

Comments

  • Kao Dome
    Kao Dome almost 2 years

    I'm trying to build a package which uses CMake for the building system (i.e. libebur128), which relies on pkg-config to find the required packages in the system.

    After building the required libraries I find that CMake is able to find one of them but not the others, although they were built in the same manner.

    How can I know what is wrong? Is there any way to output the tests it's doing or something on how it's using pkg-config to locate the packages? Because manually using pkg-config works every time locating all necessary packages.

    Here is one of the CMake scripts to locate something, libmpg123 in this case:

    find_package(PkgConfig)
    pkg_check_modules(PC_MPG123 QUIET libmpg123)
    
    find_path(MPG123_INCLUDE_DIR mpg123.h
              HINTS ${PC_MPG123_INCLUDEDIR} ${PC_MPG123_INCLUDE_DIRS})
    find_library(MPG123_LIBRARY NAMES mpg123 mpg123-0
              HINTS ${PC_MPG123_LIBDIR} ${PC_MPG123_LIBRARY_DIRS})
    
    set(MPG123_LIBRARIES ${MPG123_LIBRARY})
    set(MPG123_INCLUDE_DIRS ${MPG123_INCLUDE_DIR})
    
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(MPG123 DEFAULT_MSG MPG123_LIBRARY MPG123_INCLUDE_DIR)
    mark_as_advanced(MPG123_INCLUDE_DIR MPG123_LIBRARY)
    

    Note: I'm cross compiling for Windows in case that matters, I'm using the MinGW-w64 toolchain and I have adjusted pkg-config paths to look where it should.