Cannot link with UNKNOWN IMPORTED library if GLOBAL was used
Note: This is the same bug as already described on the mailing-list and on StackOverflow in 2016.
An IMPORTED
library which is neither marked as STATIC
or SHARED
but instead as UNKNOWN
can be linked to another target via target_link_libraries
(in the same scope).
...
add_library( ImportedLib::NonGlobal UNKNOWN IMPORTED )
...
add_executable( AnotherTarget main.cpp )
target_link_library( AnotherTarget PRIVATE ImportedLib::NonGlobal )
This works fine unless one also used GLOBAL
when creating the IMPORTED
library.
...
add_library( ImportedLib::Global UNKNOWN IMPORTED GLOBAL)
...
add_executable( AnotherTarget main.cpp )
target_link_library( AnotherTarget PRIVATE ImportedLib::Global )
Error:
CMake Error at CMakeLists.txt:10 (target_link_libraries):
Target "ImportedLib::Global" of type UNKNOWN_LIBRARY may not be linked into
another target. One may link only to STATIC or SHARED libraries, or to
executables with the ENABLE_EXPORTS property set.
It does not make sense that adding the GLOBAL
keyword prevents successful linking while without it works just fine.