Create relocatable package with proper autogenerated config cmake

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Create relocatable package with proper autogenerated config cmake
I have created cmake target, say A, and want to install it and create a Config file, so that the installed package could be relocatable. My code is:

install(EXPORT ${PROJECT_NAME}Targets
    FILE ${PROJECT_NAME}Targets.cmake
Here, I am having a problem with the proper destination. I want the Config file to be installed where ${CMAKE_INSTALL_PREFIX} points to. But when I put ${CMAKE_INSTALL_PREFIX} at ???, my resulting ATargets.cmake file contains the line:

set(_IMPORT_PREFIX "C:/Libraries/...")

which is the actual value of ${CMAKE_INSTALL_PREFIX}. This _IMPORT_PREFIX is later prepended to the parameters of set_target_properties() command inside the auto-generated ATargets.cmake, resulting in hard coded paths, valid only on the installation system.

I tried to use some generator expressions like <$IMPORT_PREFIX> in place of ???, but this gave me an error at cmake generation. I also tried to omit DESTINATION which in my opinion should place the file in the location relative to ${CMAKE_INSTALL_PREFIX}, but cmake complained about it too.

I got a suggesion on StackOverflow to use . (a dot) as my DESTINATION. This worked partially, in the way that I no longer have absolute _IMPORT_PREFIX. Unfortunately, the produced paths are still invalid: The generated ATargets.cmake contains:

get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)

and the second line above is not necessary, it trims the path too much by one folder.

Do you have any ideas how to solve it?

Best regards,


Powered by

Please keep messages on-topic and check the CMake FAQ at:

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support:
CMake Consulting:
CMake Training Courses:

Visit other Kitware open-source projects at

Follow this link to subscribe/unsubscribe: