[CMake] How to avoid the explicit library location when linking with imported library targets
Brad King
brad.king at kitware.com
Fri Sep 17 08:43:40 EDT 2010
On 09/17/2010 07:19 AM, Pere Mato Vila wrote:
> I am using the nice feature of exporting targets from one CMake
> project to another. This works really nicely since it avoids
> explicitly linking my executables and shared libraries with the
> dependent libraries of the imported library target. The problem
> is that imported library is linked with its full installation
> path using the value of property IMPORTED_LOCATION, which is
> generated when installing the exports. This implies that I
> cannot relocate the installation of the project that is
> exporting the targets. Is there a way to tell CMake to use only
> the library name for the imported library targets?
>
> In other words and with a little example
>
> add_library(bar SHARED IMPORTED)
> set_property(TARGET bar APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
> set_target_properties( bar PROPERTIES
> IMPORTED_LINK_INTERFACE_LIBRARIES_NOCONFIG "A;B;C"
> IMPORTED_LOCATION_NOCONFIG "/tmp/absolute/path/to/libbar.so"
The export() command produces target import properties with full path
because it is meant for exporting from a build tree. The install(EXPORT)
command is meant for install trees, and it does support relocation. It
sets the properties based on a path relative to the export file:
install(TARGETS mylib DESTINATION lib EXPORT mytargets)
install(EXPORT mytargets DESTINATION lib/myproj)
The "lib/myproj/mytargets-<config>.cmake" that gets installed does this:
# Compute the installation prefix relative to this file.
GET_FILENAME_COMPONENT(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
GET_FILENAME_COMPONENT(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
GET_FILENAME_COMPONENT(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
...
SET_TARGET_PROPERTIES(mylib PROPERTIES
...
IMPORTED_LOCATION_NOCONFIG "${_IMPORT_PREFIX}/lib/libmylib.a"
)
The whole install tree can be relocated to a new prefix and will still work.
-Brad
More information about the CMake
mailing list