Search code examples
c++cmakepkg-config

PkgConfig module: INCLUDE_DIRS not listing all directories


Short version (TL;DR)

Using CMake with the PkgConfig module I try to print all the include directories for a library I need for my project, let's call it thelibrary. I do this by printing the value of the THELIBRARY_INCLUDE_DIRS variable. The list of include directories is printed, but one directory is missing.

This really surprised it, as if I type the following on a terminal:

pkg-config thelibrary --cflags

then all the include directories are printed, including the one missing from THELIBRARY_INCLUDE_DIRS.

The same happens if I try to print the value of THELIBRARY_CFLAGS.

How is this possible? Where is the mistake? (Hoping it is a mistake of mine)

Details

Context

I am working on a C++ Cmake project which is compiled correctly under Mac OS but not under Linux (Ubuntu), and when investigating on the possible reasons I found the issue below.

Problem

I included the PkgConfig module in file CMakeLists.txt by writing:

find_package(PkgConfig REQUIRED)    

After that, in order to use the library, I set the PKG_CONFIG_PATH environment variable by writing:

set(ENV{PKG_CONFIG_PATH} "/usr/local/opt/thelibrary/pkgconfig:$ENV{PKG_CONFIG_PATH}")

Finally, I put this instruction to search the library.

pkg_check_modules(THELIBRARY thelibrary)

Finally, in order to debug, I have tried to print the list of included directories by writing:

MESSAGE( STATUS "THELIBRARY DIRS: " ${THELIBRARY_INCLUDE_DIRS} )

The problem is, it worked, in fact it printed a list of include directories, but the problem is that the most important one is missing. In fact, the output is something like:

/opt/auxlib1/include/opt/auxlib2/include ...

where auxlib1, auxlib2 etc. are auxiliary include directories I need to compile.

But I also expected the dir /opt/thelibrary/include in output, which is printed if I use the pkg-config command on the terminal as said at the beginning. How is it possible that only that directory is not printed?


Solution

  • Thanks to the suggestion by Tsyvarev (deleting CMakeCache.txt), I understood what the cause was, and since this may happen to other people, I hope this explanation helps.

    The reason why the directory /opt/thelibrary/include wasn't appearing in THELIBRARY_INCLUDE_DIRS is that file CMakeCache.txt contained information about a previous version of the library, whose header files were installed in a directory with a slightly different name, which was removed when that previous version was uninstalled.

    Since that directory does not exist anymore, then it doesn't appear in THELIBRARY_INCLUDE_DIRS, and the new directory does not appear simply because the file CmakeCache.txt was still pointing to the previous version.

    Solution: delete CmakeCache.txt and re-run cmake.