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.