cmake学习笔记(三)cmake编译系统

1、介绍

基于CMake的构建系统被组织为一组高级逻辑目标。每个目标对应于一个可执行文件或库,或者是一个包含自定义命令的自定义目标。目标之间的依赖关系在buildsystem中表示,以确定生成顺序和响应更改的重新生成规则。

2、二进制目标

可执行文件和库是使用 add_executable()add_library()命令定义的。生成的二进制文件具有针对目标平台的适当前缀、后缀和扩展名。二进制目标之间的依赖关系使用target_link_libraries()命令表示:

add_library(archive archive.cpp zip.cpp lzma.cpp)
add_executable(zipapp zipapp.cpp)
target_link_libraries(zipapp archive)

archive 文件被定义为一个静态库——一个包含从中编译的对象的archive 文件archive .cpp, zip.cpp,和lzma.cpp。 zipapp被定义为通过编译和链接Zipap.cpp形成的可执行文件. 链接zipapp可执行文件时,将链接archive静态库。

2.1、二进制可执行文件

add_executable()命令定义可执行目标:

add_executable(mytool mytool.cpp)

诸如 add_custom_command()之类的命令可以透明地将 EXECUTABLE目标用作命令(COMMAND)可执行文件,该命令生成在生成时运行的规则。buildsystem规则将确保在尝试运行命令之前生成可执行文件。

2.2、二进制库类型

2.2.1、普通库

By default, the add_library() command defines a STATIC library, unless a type is specified. A type may be specified when using the command:

默认情况下,add_library()命令定义静态库,除非指定了类型。使用命令时可以指定类型:

add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)

默认情况下,可以启用BUILD_SHARED_LIBS 变量来更改add_library()的行为以生成共享库。

In the context of the buildsystem definition as a whole, it is largely irrelevant whether particular libraries are SHARED or STATIC -- the commands, dependency specifications and other APIs work similarly regardless of the library type. The MODULE library type is dissimilar in that it is generally not linked to -- it is not used in the right-hand-side of the target_link_libraries() command. It is a type which is loaded as a plugin using runtime techniques. If the library does not export any unmanaged symbols (e.g. Windows resource DLL, C++/CLI DLL), it is required that the library not be a SHARED library because CMake expects SHARED libraries to export at least one symbol.

在整个buildsystem定义的上下文中,特定的库是共享的还是静态的在很大程度上是无关的——不管库的类型如何,命令、依赖规范和其他API的工作方式都是相似的。MODULE库类型的不同之处在于它通常不链接到—它不用于target_link_libraries()命令的右侧。它是一种使用运行时技术作为插件加载的类型。如果库不输出任何非托管符号(例如Windows资源DLL、C++ + CLI DLL),则要求库不是共享库,因为CMake 希望共享库至少导出一个符号。

add_library(archive MODULE 7z.cpp)

2.2.2 、Apple Frameworks

A SHARED library may be marked with the FRAMEWORK target property to create an macOS or iOS Framework Bundle. A library with the FRAMEWORK target property should also set the FRAMEWORK_VERSION target property. This property is typically set to the value of "A" by macOS conventions. The MACOSX_FRAMEWORK_IDENTIFIER sets CFBundleIdentifier key and it uniquely identifies the bundle.

共享库可以用FRAMEWORK target属性标记以创建macOS或iOS框架包。具有FRAMEWORK target属性的库还应设置FRAMEWORK_VERSION target属性。此属性通常由macOS约定设置为值“A”。MACOSX_FRAMEWORK_IDENTIFIER 设置CFBundleIdentifier 键,它唯一地标识包。

add_library(MyFramework SHARED MyFramework.cpp)
set_target_properties(MyFramework PROPERTIES
  FRAMEWORK TRUE
  FRAMEWORK_VERSION A # Version "A" is macOS convention
  MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework
)

3、对象库

The OBJECT library type defines a non-archival collection of object files resulting from compiling the given source files. The object files collection may be used as source inputs to other targets by using the syntax $<TARGET_OBJECTS:name>. This is a generator expression that can be used to supply the OBJECT library content to other targets:

参考:https://cmake.org/cmake/help/v3.20/manual/cmake-buildsystem.7.html#binary-library-types

猜你喜欢

转载自blog.csdn.net/juluwangriyue/article/details/114795540