# ALIAS and (not) globally visible targets

**URL:** https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305
**Category:** Usage
**Created:** [August 19, 2022, 6:16pm UTC](https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305 "2022-08-19T18:16:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)
#### Post date: [August 19, 2022, 6:16pm UTC](https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305/1 "2022-08-19T18:16:07Z")

</div>

I’m getting an error with CMake 3.16.3 that is not failing in 3.24. The code is working around a legacy construct of created imported targets when the package already has imported targets. So rather than create an imported target, I’m creating an alias target:

```cmake
if(TARGET ${_target})
    add_library(My::target ALIAS ${_target})
endif()

```

As noted, CMake 3.24 does what I want. However, CMake 3.16.3 gives this error:

```auto
#5 2.423 CMake Error at cmake/Modules/FindFoo.cmake:195 (add_library):
#5 2.423 add_library cannot create ALIAS target "My::target" because target
#5 2.423 "Foo::target" is imported but not globally visible.

```

I found this note on [Imported Targets](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#imported-targets):

> The scope of the definition of an [`IMPORTED`](https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED) target is the directory where it was defined. It may be accessed and used from subdirectories, but not from parent directories or sibling directories. The scope is similar to the scope of a cmake variable.
> 
> It is also possible to define a `GLOBAL` [`IMPORTED`](https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED) target which is accessible globally in the buildsystem.

I must confess, I don’t really know what that means; What is the directory of my cmake/Modules files? Is it the top-level CMakeLists.txt where it is included?

I tested the [IMPORTED\_GLOBAL](https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED_GLOBAL.html) property of the imported target, and it is FALSE

Package `Foo` is not setting the GLOBAL property when it creates the IMPORT target.

This error seems to suggest that I can’t do what I want, but it does work in later CMake. Is there a work-around so it isn’t an error in older CMake?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [August 22, 2022, 5:37pm UTC](https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305/2 "2022-08-22T17:37:01Z")

</div>

I tried that with pugixml when it changed its target names. Here’s the result that ended up working:

> <https://github.com/zeux/pugixml/blob/master/scripts/pugixml-config.cmake.in#L5-12>

---

<div class="post-metadata">

### Author: ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)
#### Post date: [August 22, 2022, 5:55pm UTC](https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305/3 "2022-08-22T17:55:08Z")

</div>

That’s a lot simpler than where I was headed. I extracted the properties from the “real” target and added them to my faux target.

The properties I found I needed to get working included:

- INTERFACE\_COMPILE\_DEFINITIONS
- INTERFACE\_COMPILE\_FEATURES
- INTERFACE\_INCLUDE\_DIRECTORIES
- INTERFACE\_LINK\_LIBRARIES
- IMPORTED\_LOCATION
- IMPORTED\_SONAME

If it works like I think (hope) it does, by just creating the “indirection” of link libraries on the “real” target, all those properties will be pulled from the underlying target?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [August 22, 2022, 6:50pm UTC](https://discourse.cmake.org/t/alias-and-not-globally-visible-targets/6305/4 "2022-08-22T18:50:10Z")

</div>

`INTERFACE` linking can be thought of as “if you link me, link this too”. So it acts like an `ALIAS` for compatibility purposes.
