blob: 44e4e0438844c336bef60400f3a864bf3008f1d0 [file] [edit]
LINK_LIBRARY
------------
.. versionadded:: 3.24
.. |genex-family| replace::
:ref:`genex-family-boolean-link-language-generator-expression`
.. include:: ../manual/include/GENEX_FAMILY.rst
Synopsis
^^^^^^^^
.. parsed-literal::
`$<LINK_LIBRARY:feature,library-list> <Syntax_>`_
Syntax
^^^^^^
.. code-block:: cmake
$<LINK_LIBRARY:feature,library-list>
Specify a set of libraries to link to a target, along with a ``feature`` which
provides details about *how* they should be linked. For example:
.. code-block:: cmake
add_library(lib1 STATIC ...)
add_library(lib2 ...)
target_link_libraries(lib2 PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,lib1>")
This specifies that ``lib2`` should link to ``lib1`` and use the
``WHOLE_ARCHIVE`` feature when doing so.
Feature names are case-sensitive and may only contain letters, numbers and
underscores. Feature names defined in all uppercase are reserved for CMake's
own built-in features. The pre-defined built-in library features are:
.. include:: ../variable/include/LINK_LIBRARY_PREDEFINED_FEATURES.rst
Built-in and custom library features are defined in terms of the following
variables:
* :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
* :variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>`
* :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED`
* :variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>`
The value used for each of these variables is the value as set at the end of the
directory scope in which the target was created. The usage is as follows:
1. If the language-specific
:variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED` variable
is true, the ``feature`` must be defined by the corresponding
:variable:`CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>` variable.
2. If no language-specific ``feature`` is supported, then the
:variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED` variable must be
true and the ``feature`` must be defined by the corresponding
:variable:`CMAKE_LINK_LIBRARY_USING_<FEATURE>` variable.
The following limitations should be noted:
* The ``library-list`` can specify CMake targets or libraries.
Any CMake target of type :ref:`OBJECT <Object Libraries>`
or :ref:`INTERFACE <Interface Libraries>` will ignore the feature aspect
of the expression and instead be linked in the standard way.
* The ``$<LINK_LIBRARY:...>`` generator expression can only be used to
specify link libraries. In practice, this means it can appear in the
:prop_tgt:`LINK_LIBRARIES`, :prop_tgt:`INTERFACE_LINK_LIBRARIES`, and
:prop_tgt:`INTERFACE_LINK_LIBRARIES_DIRECT` target properties, and be
specified in :command:`target_link_libraries` and :command:`link_libraries`
commands.
* If a ``$<LINK_LIBRARY:...>`` generator expression appears in the
:prop_tgt:`INTERFACE_LINK_LIBRARIES` property of a target, it will be
included in the imported target generated by a :command:`install(EXPORT)`
command. It is the responsibility of the environment consuming this
import to define the link feature used by this expression. Any CMake
target named in the ``library-list`` should be wrapped in
:genex:`TARGET_NAME` so that its name is namespaced on export; otherwise it
is exported verbatim and will not resolve as an imported target.
* Each target or library involved in the link step must have at most only
one kind of library feature. The absence of a feature is also incompatible
with all other features. For example:
.. code-block:: cmake
add_library(lib1 ...)
add_library(lib2 ...)
add_library(lib3 ...)
# lib1 will be associated with feature1
target_link_libraries(lib2 PUBLIC "$<LINK_LIBRARY:feature1,lib1>")
# lib1 is being linked with no feature here. This conflicts with the
# use of feature1 in the line above and would result in an error.
target_link_libraries(lib3 PRIVATE lib1 lib2)
Where it isn't possible to use the same feature throughout a build for a
given target or library, the :prop_tgt:`LINK_LIBRARY_OVERRIDE` and
:prop_tgt:`LINK_LIBRARY_OVERRIDE_<LIBRARY>` target properties can be
used to resolve such incompatibilities.
* The ``$<LINK_LIBRARY:...>`` generator expression does not guarantee
that the list of specified targets and libraries will be kept grouped
together. To manage constructs like ``--start-group`` and ``--end-group``,
as supported by the GNU ``ld`` linker, use the :genex:`LINK_GROUP`
generator expression instead.