Hi there,
I have realized a Linux library fooLib that provides some functions to be used with alsa-lib data types. I want cmake to configure the compilation of the library and also to create a relocatable package that users of fooLib can import in their CMake project. For that purpose, I have thought to rely upon the FindALSA.cmake that I found in my Ubuntu 22.04 system (actually, I do not know which package installed it).
The key point is that the generated relocatable package should address the correct position of alsa-lib headers in the user machine, but I am failing to achieve this goal.
This is my simplified CMakeLists.txt file:
project(foo)
find_package(ALSA REQUIRED) # uses FindALSA.cmake
# For sake of brevity, assume that ALSA is found
add_library(fooLib SHARED foo.c) # my source code
target_include_directories(fooLib PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> # where my foo.h is
$<INSTALL_INTERFACE:include> # where my foo.h will be
${ALSA_INCLUDE_DIRS}) # this path is defined after calling find_package(ALSA)
target_link_libraries(fooLib PUBLIC
ALSA::ALSA) # this target is defined after calling find_package(ALSA)
install(TARGETS fooLib EXPORT fooLibTargets)
install(
EXPORT fooLibTargets
NAMESPACE fooLib::
FILE "fooLibTargets.cmake"
DESTINATION "lib/cmake/fooLib")
The generated fooLibTargets.cmake contains (among others) the following lines:
# Create imported target fooLib::fooLib
add_library(fooLib::fooLib SHARED IMPORTED)
set_target_properties(fooLib::fooLib PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;/usr/include"
)
This file is not relocatable because the alsa-lib headers path are assumed to be in ‘/usr/include’, which is correct for my machine but non necessarily for the users’ one. I need something that is relocatable (similarly to the above “${_IMPORT_PREFIX}/include”).
How can I overcome this issue and make CMake to generate a relocatable package that correctly handles the included directories in such situation?
And (possibly), which is the correct approach to generate a relocatable CMake package that does not suffer of this issue? Please note that the official tutorials do not cover this aspect.
Thank you in advance.