[cmake-developers] Local target aliases

Brad King brad.king at kitware.com
Mon Dec 12 12:02:42 EST 2016


On 12/12/2016 11:35 AM, Egor Pugin wrote:
> So, the original proposal is to make ALIAS & OBJECT targets local. And
> explicit GLOBAL keyword will make them global again.

An OBJECT library is just like any other target as far as the generated
build system is concerned, so it needs a globally unique name like any
other.  The purpose of ALIAS targets is this:

```
# some/subdir/CMakeLists.txt
add_library(lib1 lib1.cpp)
install(TARGETS lib1 EXPORT lib1Export ${dest_args})
install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})
add_library(Upstream::lib1 ALIAS lib1)
```

Then other code in the project can use the name `Upstream::lib1` as
if it had imported that target (e.g. for test trees that may build
inside the project or externally).  The other code is not necessarily
in the same directory, so the global visibility of ALIAS targets is
an intentional design decision.  Since the aliased target has a globally
unique name, so would the ALIAS if it is only used to prefix a namespace.

If your trees are so huge or import so many other third-party projects
that name collisions are a problem, then I suggest breaking it down
using ExternalProject.

-Brad


More information about the cmake-developers mailing list