.. cmake-manual-description: CMake Buildsystem Reference
cmake-buildsystem(7)
********************
.. only:: html or latex
.. contents::
Introduction
============
A CMake-based buildsystem is organized as a set of high-level logical
targets. Each target corresponds to an executable or library, or
is a custom target containing custom commands. Dependencies between the
targets are expressed in the buildsystem to determine the build order
and the rules for regeneration in response to change.
Binary Targets
==============
Executables and libraries are defined using the :command:`add_library`
and :command:`add_executable` commands. The resulting binary files have
appropriate prefixes, suffixes and extensions for the platform targeted.
Dependencies between binary targets are expressed using the
:command:`target_link_libraries` command:
.. code-block:: cmake
add_library(archive archive.cpp zip.cpp lzma.cpp)
add_executable(zipapp zipapp.cpp)
target_link_libraries(zipapp archive)
``archive`` is defined as a static library -- an archive containing objects
compiled from ``archive.cpp``, ``zip.cpp``, and ``lzma.cpp``. ``zipapp``
is defined as an executable formed by compiling and linking ``zipapp.cpp``.
When linking the ``zipapp`` executable, the ``archive`` static library is
linked in.
Binary Library Types
--------------------
By default, the :command:`add_library` command defines a static library,
unless a type is specified. A type may be specified when using the command:
.. code-block:: cmake
add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
.. code-block:: cmake
add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)
The :variable:`BUILD_SHARED_LIBS` variable may be enabled to change the
behavior of :command:`add_library` to build shared libraries by default.
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 :command:`target_link_libraries` command.
It is a type which is loaded as a plugin using runtime techniques.
.. code-block:: cmake
add_library(archive MODULE 7z.cpp)
The ``OBJECT`` library type is also not linked to. It defines a non-archival
collection of object files resulting from compiling the given source files.
The object files collection can be used as source inputs to other targets:
.. code-block:: cmake
add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
add_library(archiveExtras STATIC $<TARGET_OBJECTS:archive> extras.cpp)
add_executable(test_exe $<TARGET_OBJECTS:archive> test.cpp)
``OBJECT`` libraries may only be used locally as sources in a buildsystem --
they may not be installed, exported, or used in the right hand side of
:command:`target_link_libraries`. They also may not be used as the ``TARGET``
in a use of the :command:`add_custom_command(TARGET)` command signature.
Commands such as :command:`add_custom_command`, which generates rules to be
run at build time can transparently use an :prop_tgt:`EXECUTABLE <TYPE>`
target as a ``COMMAND`` executable. The buildsystem rules will ensure that
the executable is built before attempting to run the command.
Build Specification and Usage Requirements
==========================================
The :command:`target_include_directories`, :command:`target_compile_definitions`
and :command:`target_compile_options` commands specify the build specifications
and the usage requirements of binary targets. The commands populate the
:prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_DEFINITIONS` and
:prop_tgt:`COMPILE_OPTIONS` target properties respectively, and/or the
:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`, :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`
and :prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties.
Each of the commands has a ``PRIVATE``, ``PUBLIC`` and ``INTERFACE`` mode. The
``PRIVATE`` mode populates only the non-``INTERFACE_`` variant of the target
property and the ``INTERFACE`` mode populates only the ``INTERFACE_`` variants.
The ``PUBLIC`` mode populates both variants of the repective target property.
Each command may be invoked with multiple uses of each keyword:
.. code-block:: cmake
target_compile_definitions(archive
PRIVATE BUILDING_WITH_LZMA
INTERFACE USING_ARCHIVE_LIB
)
Note that usage requirements are not designed as a way to make downstreams
use particular :prop_tgt:`COMPILE_OPTIONS` or
:prop_tgt:`COMPILE_DEFINITIONS` etc for convenience only. The contents of
the properties must be **requirements**, not merely recommendations or
convenience.
Target Properties
-----------------
The contents of the :prop_tgt:`INCLUDE_DIRECTORIES`,
:prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS` target
properties are used appropriately when compiling the source files of a
binary target.
Entries in the :prop_tgt:`INCLUDE_DIRECTORIES` are added to the compile line
with ``-I`` or ``-isystem`` prefixes and in the order of appearance in the
property value.
Entries in the :prop_tgt:`COMPILE_DEFINITIONS` are prefixed with ``-D`` or
``/D`` and added to the compile line in an unspecified order. The
:prop_tgt:`DEFINE_SYMBOL` target property is also added as a compile
definition as a special convenience case for ``SHARED`` and ``MODULE``
library targets.
Entries in the :prop_tgt:`COMPILE_OPTIONS` are escaped for the shell and added
in the order of appearance in the property value. Several compile options have
special separate handling, such as :prop_tgt:`POSITION_INDEPENDENT_CODE`.
The contents of the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
:prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` and
:prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties are
*Usage Requirements* -- they specify content which consumers
must use to correctly compile and link with the target they appear on.
For any binary target, the contents of each ``INTERFACE_`` property on
each target specified in a :command:`target_link_libraries` command is
consumed:
.. code-block:: cmake
set(srcs archive.cpp zip.cpp)
if (LZMA_FOUND)
list(APPEND srcs lzma.cpp)
endif()
add_library(archive SHARED ${srcs})
if (LZMA_FOUND)
# The archive library sources are compiled with -DBUILDING_WITH_LZMA
target_compile_definitions(archive PRIVATE BUILDING_WITH_LZMA)
endif()
target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
add_executable(consumer)
# Link consumer to archive and consume its usage requirements. The consumer
# executable sources are compiled with -DUSING_ARCHIVE_LIB.
target_link_libraries(consumer archive)
Because it is common to require that the source directory and corresponding
build directory are added to the :prop_tgt:`INCLUDE_DIRECTORIES`, the
:variable:`CMAKE_INCLUDE_CURRENT_DIR` variable can be enabled to conveniently
add the corresponding directories to the :prop_tgt:`INCLUDE_DIRECTORIES` of
all targets. The variable :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE`
can be enabled to add the corresponding directories to the
:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of all targets. This makes use of
targets in multiple different directories convenient through use of the
:command:`target_link_libraries` command.
.. _`Target Usage Requirements`:
Transitive Usage Requirements
-----------------------------
The usage requirements of a target can transitively propagate to dependents.
The :command:`target_link_libraries` command has ``PRIVATE``,
``INTERFACE`` and ``PUBLIC`` keywords to control the propagation.
.. code-block:: cmake
add_library(archive archive.cpp)
target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
add_library(serialization serialization.cpp)
target_compile_definitions(serialization INTERFACE USING_SERIALIZATION_LIB)
add_libra
没有合适的资源?快使用搜索试试~ 我知道了~
cmake-3.0.0-win32-x86.zip
共2000个文件
txt:959个
html:932个
rst:31个
需积分: 5 0 下载量 126 浏览量
2024-02-01
01:14:21
上传
评论
收藏 13.77MB ZIP 举报
温馨提示
cmake-3.0.0-win32-x86
资源推荐
资源详情
资源评论
收起资源包目录
cmake-3.0.0-win32-x86.zip (2000个子文件)
ctest.1 18KB
cmake.1 14KB
ccmake.1 9KB
cpack.1 9KB
cmake-gui.1 7KB
cmake-modules.7 355KB
cmake-commands.7 219KB
cmake-properties.7 109KB
cmake-variables.7 84KB
cmake-policies.7 54KB
cmake-buildsystem.7 34KB
cmake-developer.7 30KB
cmake-packages.7 25KB
cmake-language.7 18KB
cmake-generators.7 10KB
cmake-generator-expressions.7 9KB
cmake-qt.7 8KB
cmake-toolchains.7 7KB
CheckForPthreads.c 739B
CMakeCCompilerABI.c 600B
CheckFunctionExists.c 403B
CheckVariableExists.c 383B
main.c 261B
CMakeTestGNU.c 184B
VerifyC.c 70B
my_module_.c 26B
mymodule_.c 25B
CMakeTestWatcomVersion.c 21B
cmake 5KB
COPYING 3KB
COPYING 1KB
COPYING 1KB
cpack 2KB
CMakeCXXCompilerABI.cpp 470B
basic.css 8KB
default.css 4KB
pygments.css 4KB
cmake.css 229B
ctest 3KB
libeay32.dll 1.12MB
msvcr90.dll 641KB
msvcp90.dll 556KB
ssleay32.dll 264KB
msvcm90.dll 220KB
cmake-mode.el 14KB
cmake-gui.exe 8.62MB
ctest.exe 3.52MB
cmake.exe 3.13MB
cpack.exe 3.07MB
cmcldeps.exe 153KB
ajax-loader.gif 673B
cmCPluginAPI.h 12KB
CMakeCompilerABI.h 1KB
genindex.html 365KB
cmake-buildsystem.7.html 103KB
3.0.0.html 67KB
cmake-developer.7.html 64KB
cmake-packages.7.html 58KB
cmake-language.7.html 50KB
cmake-variables.7.html 43KB
cmake-properties.7.html 38KB
ctest.1.html 35KB
install.html 34KB
find_package.html 34KB
cmake-modules.7.html 29KB
cmake-qt.7.html 27KB
cmake.1.html 26KB
cmake-generator-expressions.7.html 25KB
cmake-toolchains.7.html 23KB
CPack.html 22KB
CPackRPM.html 22KB
FindQt4.html 21KB
ExternalProject.html 21KB
cpack.1.html 20KB
ccmake.1.html 20KB
target_link_libraries.html 20KB
CPackComponent.html 19KB
add_library.html 19KB
FindCUDA.html 19KB
cmake-commands.7.html 19KB
ExternalData.html 19KB
if.html 18KB
GenerateExportHeader.html 17KB
cmake-gui.1.html 17KB
CPackDeb.html 17KB
CPackWIX.html 17KB
cmake-policies.7.html 15KB
file.html 14KB
FindBoost.html 14KB
FeatureSummary.html 14KB
find_library.html 14KB
find_path.html 14KB
UseJava.html 13KB
CPackNSIS.html 13KB
find_file.html 13KB
CMakePackageConfigHelpers.html 13KB
find_program.html 13KB
BundleUtilities.html 13KB
add_custom_command.html 12KB
project.html 12KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
程序员Chino的日记
- 粉丝: 2779
- 资源: 3万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功