0

I have written a library, built with cmake (currently using version 3.5.1 if that is important), and am now trying to use that library in another project, also built with cmake.

find_package finds the library just fine, and creates the exported targets, but unfortunately does not provide the expected <package>_INCLUDE_DIRS variable.

Unfortunately I can't post the library, but for example pugixml has the same problem:

mkdir pugi
cd pugi
git clone https://github.com/zeux/pugixml.git .
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=~/temp ..
make install

So far so good. However, trying to find_package it:

find_package(pugixml REQUIRED)
message(STATUS ${pugixml_FOUND})
message(STATUS ${pugixml_INCLUDE_DIRS})
cmake -DCMAKE_INSTALL_PREFIX=~/temp ..

shows that the expected pugixml_INCLUDE_DIRS variable is not set:

-- 1
--

I found that I can retrieve the include directory using a property on the target:

get_target_property(INCLUDE_DIRS pugixml INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS ${INCLUDE_DIRS})
-- /home/username/temp/include

but I have never seen a cmake file do this.

What is the proper method of retrieving the include directory of a package in cmake?

Alternatively, what needs to be done in the package's cmake file such that <package>_INCLUDE_DIRS is properly set by find_package?


Thanks for the tips thus far, I'm getting closer.

The problem turns out to be that I am creating an intermediary object library target, which cannot link to a library. The target_link_libraries is only present in the final binary, but that is understandably not enough for compilation of the object library to find the headers.

The question remains: how can I make the object library target aware of the include path of the found package?


The marked duplicate unfortunately only solves this problem for relatively new versions of cmake (3.12 or newer). For older versions, get_target_property or the associated generator expression seem to be the only way to do this.

zennehoy
  • 6,405
  • 28
  • 55
  • "... but I have never seen a cmake file do this." - This is because extracting if the include directories from the *IMPORTED* library target is **rarely needed**: linking with that target will automatically include these directories for compilation. – Tsyvarev Jan 29 '19 at 09:32
  • @Tsyvarev linking with the target how? Using `target_link_libraries(myProject pugixml)` still causes `fatal error: pugixml.hpp: No such file or directory`. – zennehoy Jan 29 '19 at 09:38
  • Yes, `target_link_libraries(myProject pugixml)` is a correct way for use IMPORTED library. Probably, the error is caused by other reason. If you want us to help you with the error, we need to see more code (`CMakeLists.txt`) and **exact** error message. – Tsyvarev Jan 29 '19 at 09:49
  • @Tsyvarev Please see the edit - the problem turns out to be how to add the include path of the found package to an object library target... – zennehoy Jan 29 '19 at 10:04
  • Then you probably want the same as this question: https://stackoverflow.com/questions/31290077/how-to-specify-imported-dependencies-of-an-object-library. Currently it has the only answer which tells that you may use `target_link_libraries` for OBJECT library, but only since CMake 3.12. – Tsyvarev Jan 29 '19 at 10:14

0 Answers0