[CMake] using IMPORTED targets in multiple projects within one CMake tree

Brad King brad.king at kitware.com
Mon May 11 13:26:17 EDT 2009


Alexander Neundorf wrote:
> On Tuesday 05 May 2009, Alexandre.Feblot at thomsonreuters.com wrote:
>> I saw this too.
>> IMHO, it's a bug. If it's a feature, I don't see the point.
> 
> I think it's a feature that imported targets are not global. Brad explained 
> this in a mail not too long ago, but I don't remember the details.

It is a feature.  Generally imported targets are created by third-party
package configuration files loaded by find_package.  Anything defined by
these outside files needs to have directory-local scope just like a
variable.  Different subdirectories of your project could import the
same outside project with different versions or configuration settings
which change how the targets are defined.

I suggest creating a helper module that defines the imported target,
and including it in any subdirectory that needs it.

BTW, there was a bug in 2.6.3 and below that caused transitive linking
to an imported target from another directory to fail:

   # A/CMakeLists.txt
   add_library(xxx SHARED IMPORTED)
   add_library(mylib ...)
   target_link_libraries(mylib xxx)

   # B/CMakeLists.txt
   add_executable(myexe ...)
   target_link_libraries(myexe mylib)

The 'myexe' target should link to 'xxx' transitively through 'mylib', but
this failed in 2.6.3 and below.  It works with 2.6.4.

-Brad


More information about the CMake mailing list