[cmake-developers] Unknown Imported & Global libraries

Brad King brad.king at kitware.com
Mon Jan 9 14:55:48 EST 2017


On 12/21/2016 07:12 AM, Florent Castelli wrote:
> find_package(foo)
> if(NOT FOO_FOUND)
>   add_library(foo STATIC foo.cpp)
> endif()

Instead do

  find_package(foo)
  if(NOT FOO_FOUND)
    add_subdirectory(bundled_foo)
  endif()

so that the imported targets are visible.  Inside the bundled_foo you can build
the target and then crate an ALIAS library whose name matches what the imported
target would have been.

> make imported libraries global by default

The reason they are locally scoped is that find_package() may load
different, possibly conflicting, external packages in separate directories.
The find_package() call should be made at the highest level that contains
anything that directly references the target.  See above example.

An alternative is:

  find_package(foo)
  add_library(Foo INTERFACE)
  target_link_libraries(Foo PUBLIC FOO::FOO)

That will make a globally visible Foo target that when used forwards all
usage requirements from the imported target.

-Brad



More information about the cmake-developers mailing list