如果要在编译时添加例如 -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.
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
Contents
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:
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_SONAMEtarget property set, CMake may ask the linker to search for the library instead of using the full path (e.g./usr/lib/libfoo.sobecomes-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.sobecomes-lfoo), such as when a shared library is detected to have noSONAMEfield. See policyCMP0060for discussion of another case.If the library file is in a Mac OSX framework, the
Headersdirectory 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
.targetswill 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.
foobecomes-lfooorfoo.lib). -
A link flag: Item names starting with
-, but not-lor-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_FLAGStarget property to add link flags explicitly. The flags will then be placed at the toolchain-defined flag position in the link command. -
A
debug,optimized, orgeneralkeyword immediately followed by another<item>. The item following such a keyword will be used only for the corresponding build configuration. Thedebugkeyword corresponds to theDebugconfiguration (or to configurations named in theDEBUG_CONFIGURATIONSglobal property if it is set). Theoptimizedkeyword corresponds to all other configurations. Thegeneralkeyword 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
The PUBLIC, PRIVATE 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
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)¶
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文件名完全固定
本文介绍了如何使用CMake进行库的链接,包括全局链接库和针对特定目标的链接库的方法。详细解释了LINK_LIBRARIES与TARGET_LINK_LIBRARIES的区别及使用场景,并提供了配置示例。

5410

被折叠的 条评论
为什么被折叠?



