LINK_LIBRARY¶
This generator expression belongs to the Link Features family. See that section for related generator expressions.
Syntax¶
$<LINK_LIBRARY:feature,library-list>
Added in version 3.24.
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:
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:
DEFAULTThis feature corresponds to standard linking, essentially equivalent to using no feature at all. It is typically only used with the
LINK_LIBRARY_OVERRIDEandLINK_LIBRARY_OVERRIDE_<LIBRARY>target properties.WHOLE_ARCHIVEForce inclusion of all members of a static library when linked as a dependency of consuming Executables, Shared Libraries, and Module Libraries. This feature is only supported for the following platforms, with limitations as noted:
Linux.
All BSD variants.
SunOS.
All Apple variants. The library must be specified as a CMake target name, a library file name (such as
libfoo.a), or a library file path (such as/path/to/libfoo.a). Due to a limitation of the Apple linker, it cannot be specified as a plain library name likefoo, wherefoois not a CMake target.Windows. When using a MSVC or MSVC-like toolchain, the MSVC version must be greater than 1900.
Cygwin.
MSYS.
Note
Since Static Libraries are archives and not linked binaries, CMake records their link dependencies for transitive use when linking consuming binaries. Therefore
WHOLE_ARCHIVEdoes not cause a static library's objects to be included in other static libraries. Use Object Libraries for that.FRAMEWORKThis option tells the linker to search for the specified framework using the
-frameworklinker option. It can only be used on Apple platforms, and only with a linker that understands the option used (i.e. the linker provided with Xcode, or one compatible with it).The framework can be specified as a CMake framework target, a bare framework name, or a file path. If a target is given, that target must have the
FRAMEWORKtarget property set to true. For a file path, if it contains a directory part, that directory will be added as a framework search path.add_library(lib SHARED ...) target_link_libraries(lib PRIVATE "$<LINK_LIBRARY:FRAMEWORK,/path/to/my_framework>") # The constructed linker command line will contain: # -F/path/to -framework my_framework
File paths must conform to one of the following patterns (
*is a wildcard, and optional parts are shown as[...]):[/path/to/]FwName[.framework][/path/to/]FwName.framework/FwName[suffix][/path/to/]FwName.framework/Versions/*/FwName[suffix]
Note that CMake recognizes and automatically handles framework targets, even without using the
$<LINK_LIBRARY:FRAMEWORK,...>expression. The generator expression can still be used with a CMake target if the project wants to be explicit about it, but it is not required to do so. The linker command line may have some differences between using the generator expression or not, but the final result should be the same. On the other hand, if a file path is given, CMake will recognize some paths automatically, but not all cases. The project may want to use$<LINK_LIBRARY:FRAMEWORK,...>for file paths so that the expected behavior is clear.Added in version 3.25: The
FRAMEWORK_MULTI_CONFIG_POSTFIX_<CONFIG>target property as well as thesuffixof the framework library name are now supported by theFRAMEWORKfeatures.NEEDED_FRAMEWORKThis is similar to the
FRAMEWORKfeature, except it forces the linker to link with the framework even if no symbols are used from it. It uses the-needed_frameworkoption and has the same linker constraints asFRAMEWORK.REEXPORT_FRAMEWORKThis is similar to the
FRAMEWORKfeature, except it tells the linker that the framework should be available to clients linking to the library being created. It uses the-reexport_frameworkoption and has the same linker constraints asFRAMEWORK.WEAK_FRAMEWORKThis is similar to the
FRAMEWORKfeature, except it forces the linker to mark the framework and all references to it as weak imports. It uses the-weak_frameworkoption and has the same linker constraints asFRAMEWORK.NEEDED_LIBRARYThis is similar to the
NEEDED_FRAMEWORKfeature, except it is for use with non-framework targets or libraries. This feature is only supported on the following platforms:- Apple
Uses the
-needed_libraryor-needed-loption as appropriate, with the same linker constraints asNEEDED_FRAMEWORK.- Linux
Added in version 4.5.
Wraps the library with
--no-as-needed/--as-neededto force an entry in the binary'sDT_NEEDEDlist regardless of whether any symbols from the library are directly referenced. When the linker supports--push-state/--pop-state(GNU ld >= 2.25, gold, LLD, MOLD), the state-stacking form is used instead to correctly restore the pre-existing--as-neededstate rather than unconditionally enabling it after the library. This feature is only meaningful for shared libraries; applying it to a static library produces a developer warning and has no effect.
REEXPORT_LIBRARYThis is similar to the
REEXPORT_FRAMEWORKfeature, except it is for use with non-framework targets or libraries (Apple platforms only). It uses the-reexport_libraryor-reexport-loption as appropriate, and has the same linker constraints asREEXPORT_FRAMEWORK.WEAK_LIBRARYThis is similar to the
WEAK_FRAMEWORKfeature, except it is for use with non-framework targets or libraries (Apple platforms only). It uses the-weak_libraryor-weak-loption as appropriate, and has the same linker constraints asWEAK_FRAMEWORK.
Built-in and custom library features are defined in terms of the following variables:
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:
If the language-specific
CMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>_SUPPORTEDvariable is true, thefeaturemust be defined by the correspondingCMAKE_<LANG>_LINK_LIBRARY_USING_<FEATURE>variable.If no language-specific
featureis supported, then theCMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTEDvariable must be true and thefeaturemust be defined by the correspondingCMAKE_LINK_LIBRARY_USING_<FEATURE>variable.
The following limitations should be noted:
The
library-listcan specify CMake targets or libraries. Any CMake target of type OBJECT or INTERFACE 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 theLINK_LIBRARIES,INTERFACE_LINK_LIBRARIES, andINTERFACE_LINK_LIBRARIES_DIRECTtarget properties, and be specified intarget_link_libraries()andlink_libraries()commands.If a
$<LINK_LIBRARY:...>generator expression appears in theINTERFACE_LINK_LIBRARIESproperty of a target, it will be included in the imported target generated by ainstall(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 thelibrary-listshould be wrapped inTARGET_NAMEso 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:
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
LINK_LIBRARY_OVERRIDEandLINK_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-groupand--end-group, as supported by the GNUldlinker, use theLINK_GROUPgenerator expression instead.