[CMake] How to avoid the explicit library location when linking with imported library targets

Ryan Pavlik rpavlik at iastate.edu
Fri Sep 17 09:32:00 EDT 2010


On Fri, Sep 17, 2010 at 7:43 AM, Brad King <brad.king at kitware.com> wrote:

> 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
>

Yes, however, transitive link dependencies are stored/set in the installed
targets file with full paths to the location on the original build machine.
If I install the export target for a static libfoo, which links to
/home/rpavlik/libbar.a, the targets file includes that full path.  I'm not
sure what the "right" solution is - in my case, I was bundling all the
dependencies so it would have been reasonable OK to just link to the name of
the library.  However, I can envision possibly wanting to run a find_package
script in there, etc.

Ryan

-- 
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University

rpavlik at iastate.edu
http://academic.cleardefinition.com
Internal VRAC/HCI Site: http://tinyurl.com/rpavlik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100917/3128b0bb/attachment.htm>


More information about the CMake mailing list