Search code examples
c++cmakeshared-librariesheader-files

How to export dependency include dir in cmake?


I have 3 projects.

my.exe, pcl.lib and flann.lib.

my.exe uses pcl.lib and pcl.lib uses flann.lib.

In one of pcl.lib's header file, it includes flann.h, so for my.exe, it needs to find_package both pcl and flann to include the header files from both 2 libraries. This is not perfect because I don't want my.exe to know the existance of flann, at least in cmake find_package.

One solution is to use INTERFACE in pcl project for flann:

target_link_libraries(pcl INTERFACE flann)

But the problem is that pcl is a complicated project and target_link_libraries(pcl ... has been used elsewhere and I got this error in cmake:

The keyword signature for target_link_libraries has already been used with the target "pcl". All uses of target_link_libraries with a target must be either all-keyword or all-plain.

target_link_libraries(pcl has been used without INTERFACE, PUBLIC or PRIVATE keywords already so I can't use target_link_libraries(pcl with INTERFACE.

So in this case, How can set the include directories as INTERFACE?


Solution

  • One solution would be to introduce an interface library as intermediary target like this:

    # both pcl and flann must be known at this point
    add_library(pcl_if INTERFACE)
    target_link_libraries(pcl_if INTERFACE pcl flann)
    

    In the CMakeLists.txt where my is defined:

    target_link_libraries(my PUBLIC pcl_if)
    

    Quoting from the doc page linked to above:

    An interface library created with the above signature has no source files itself and is not included as a target in the generated buildsystem.

    It does carry the interface properties of pcl and flann, however.