cmake中链接系统标准库

本文介绍了如何使用CMake进行库的链接,包括全局链接库和针对特定目标的链接库的方法。详细解释了LINK_LIBRARIES与TARGET_LINK_LIBRARIES的区别及使用场景,并提供了配置示例。

如果要在编译时添加例如 -lpthread -lmath这类通用的库

只需直接在CMakeLists.txt中添加

LINK_LIBRARIES(标准库名称)

TARGET_LINK_LIBRARIES(编译目标名称target 标准库名称)

其中标准库名称就是-l后面的库名称,如 math,pthread等

编译目标名称就是ADD_EXECUTABLE的第一个参数


如果为所有target统一指定编译时要链接的库用LINK_LIBRARIES

为每个target单独指定编译时要链接的库用TARGET_LINK_LIBRARIES



注意

1 用LINK_LIBRARIES时这行一定要写在ADD_EXECUTABLE前面,参照官网的文档

https://cmake.org/cmake/help/v3.8/command/link_libraries.html?highlight=link_libraries#command:link_libraries

link_libraries

Link libraries to all targets added later.

link_libraries([item1 [item2 [...]]]
               [[debug|optimized|general] <item>] ...)

Specify libraries or flags to use when linking any targets created later in the current directory or below by commands such as add_executable() oradd_library(). See the target_link_libraries() command for meaning of arguments.

Note

 

The target_link_libraries() command should be preferred whenever possible. Library dependencies are chained automatically, so directory-wide specification of link libraries is rarely needed.


https://cmake.org/cmake/help/v3.8/command/target_link_libraries.html?highlight=link_libraries#command:target_link_libraries

target_link_libraries

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target’s dependencies affect compilation of its own sources.

Overview

This command has several signatures as detailed in subsections below. All of them have the general form:

target_link_libraries(<target> ... <item>... ...)

The named <target> must have been created in the current directory by a command such as add_executable() or add_library(). Repeated calls for the same <target> append items in the order called. Each <item> may be:

  • A library target name: The generated link line will have the full path to the linkable library file associated with the target. The buildsystem will have a dependency to re-link <target> if the library file changes.

    The named target must be created by add_library() within the project or as an IMPORTED library. If it is created within the project an ordering dependency will automatically be added in the build system to make sure the named library target is up-to-date before the <target> links.

    If an imported library has the IMPORTED_NO_SONAME target property set, CMake may ask the linker to search for the library instead of using the full path (e.g. /usr/lib/libfoo.so becomes -lfoo).

  • A full path to a library file: The generated link line will normally preserve the full path to the file. The buildsystem will have a dependency to re-link<target> if the library file changes.

    There are some cases where CMake may ask the linker to search for the library (e.g. /usr/lib/libfoo.so becomes -lfoo), such as when a shared library is detected to have no SONAME field. See policy CMP0060 for discussion of another case.

    If the library file is in a Mac OSX framework, the Headers directory of the framework will also be processed as a usage requirement. This has the same effect as passing the framework directory as an include directory.

    On Visual Studio Generators for VS 2010 and above, library files ending in .targets will be treated as MSBuild targets files and imported into generated project files. This is not supported by other generators.

  • A plain library name: The generated link line will ask the linker to search for the library (e.g. foo becomes -lfoo or foo.lib).

  • A link flag: Item names starting with -, but not -l or -framework, are treated as linker flags. Note that such flags will be treated like any other library link item for purposes of transitive dependencies, so they are generally safe to specify only as private link items that will not propagate to dependents.

    Link flags specified here are inserted into the link command in the same place as the link libraries. This might not be correct, depending on the linker. Use the LINK_FLAGS target property to add link flags explicitly. The flags will then be placed at the toolchain-defined flag position in the link command.

  • debugoptimized, or general keyword immediately followed by another <item>. The item following such a keyword will be used only for the corresponding build configuration. The debug keyword corresponds to the Debug configuration (or to configurations named in theDEBUG_CONFIGURATIONS global property if it is set). The optimized keyword corresponds to all other configurations. The general keyword corresponds to all configurations, and is purely optional. Higher granularity may be achieved for per-configuration rules by creating and linking to IMPORTED library targets.

Items containing ::, such as Foo::Bar, are assumed to be IMPORTED or ALIAS library target names and will cause an error if no such target exists. See policy CMP0028.

Arguments to target_link_libraries may use “generator expressions” with the syntax $<...>. Note however, that generator expressions will not be used in OLD handling of CMP0003 or CMP0004. See the cmake-generator-expressions(7) manual for available expressions. See the cmake-buildsystem(7) manual for more on defining buildsystem properties.

Libraries for a Target and/or its Dependents

target_link_libraries(<target>
                      <PRIVATE|PUBLIC|INTERFACE> <item>...
                     [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)

The PUBLICPRIVATE and INTERFACE keywords can be used to specify both the link dependencies and the link interface in one command. Libraries and targets following PUBLIC are linked to, and are made part of the link interface. Libraries and targets following PRIVATE are linked to, but are not made part of the link interface. Libraries following INTERFACE are appended to the link interface and are not used for linking <target>.

Libraries for both a Target and its Dependents

target_link_libraries(<target> <item>...)

Library dependencies are transitive by default with this signature. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too. This transitive “link interface” is stored in the INTERFACE_LINK_LIBRARIES target property and may be overridden by setting the property directly. When CMP0022 is not set to NEW, transitive linking is built in but may be overridden by theLINK_INTERFACE_LIBRARIES property. Calls to other signatures of this command may set the property making any libraries linked exclusively by this signature private.

Libraries for a Target and/or its Dependents (Legacy)

target_link_libraries(<target>
                      <LINK_PRIVATE|LINK_PUBLIC> <lib>...
                     [<LINK_PRIVATE|LINK_PUBLIC> <lib>...]...)

The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both the link dependencies and the link interface in one command.

This signature is for compatibility only. Prefer the PUBLIC or PRIVATE keywords instead.

Libraries and targets following LINK_PUBLIC are linked to, and are made part of the INTERFACE_LINK_LIBRARIES. If policy CMP0022 is not NEW, they are also made part of the LINK_INTERFACE_LIBRARIES. Libraries and targets following LINK_PRIVATE are linked to, but are not made part of theINTERFACE_LINK_LIBRARIES (or LINK_INTERFACE_LIBRARIES).


2 CMake的命令不区分大小写,但CMakeLists.txt文件名完全固定

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值