add_library.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. add_library
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Add a library to the project using the specified source files.
  6. Normal Libraries
  7. ^^^^^^^^^^^^^^^^
  8. ::
  9. add_library(<name> [STATIC | SHARED | MODULE]
  10. [EXCLUDE_FROM_ALL]
  11. source1 [source2 ...])
  12. Adds a library target called ``<name>`` to be built from the source files
  13. listed in the command invocation. The ``<name>`` corresponds to the
  14. logical target name and must be globally unique within a project. The
  15. actual file name of the library built is constructed based on
  16. conventions of the native platform (such as ``lib<name>.a`` or
  17. ``<name>.lib``).
  18. ``STATIC``, ``SHARED``, or ``MODULE`` may be given to specify the type of
  19. library to be created. ``STATIC`` libraries are archives of object files
  20. for use when linking other targets. ``SHARED`` libraries are linked
  21. dynamically and loaded at runtime. ``MODULE`` libraries are plugins that
  22. are not linked into other targets but may be loaded dynamically at runtime
  23. using dlopen-like functionality. If no type is given explicitly the
  24. type is ``STATIC`` or ``SHARED`` based on whether the current value of the
  25. variable :variable:`BUILD_SHARED_LIBS` is ``ON``. For ``SHARED`` and
  26. ``MODULE`` libraries the :prop_tgt:`POSITION_INDEPENDENT_CODE` target
  27. property is set to ``ON`` automatically.
  28. A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`
  29. target property to create an OS X Framework.
  30. By default the library file will be created in the build tree directory
  31. corresponding to the source tree directory in which the command was
  32. invoked. See documentation of the :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY`,
  33. :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY`, and
  34. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target properties to change this
  35. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target
  36. property to change the ``<name>`` part of the final file name.
  37. If ``EXCLUDE_FROM_ALL`` is given the corresponding property will be set on
  38. the created target. See documentation of the :prop_tgt:`EXCLUDE_FROM_ALL`
  39. target property for details.
  40. Source arguments to ``add_library`` may use "generator expressions" with
  41. the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  42. manual for available expressions. See the :manual:`cmake-buildsystem(7)`
  43. manual for more on defining buildsystem properties.
  44. Imported Libraries
  45. ^^^^^^^^^^^^^^^^^^
  46. ::
  47. add_library(<name> <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED
  48. [GLOBAL])
  49. An :ref:`IMPORTED library target <Imported Targets>` references a library
  50. file located outside the project. No rules are generated to build it, and
  51. the :prop_tgt:`IMPORTED` target property is ``True``. The target name has
  52. scope in the directory in which it is created and below, but the ``GLOBAL``
  53. option extends visibility. It may be referenced like any target built
  54. within the project. ``IMPORTED`` libraries are useful for convenient
  55. reference from commands like :command:`target_link_libraries`. Details
  56. about the imported library are specified by setting properties whose names
  57. begin in ``IMPORTED_`` and ``INTERFACE_``. The most important such
  58. property is :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration
  59. variant :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the
  60. location of the main library file on disk. See documentation of the
  61. ``IMPORTED_*`` and ``INTERFACE_*`` properties for more information.
  62. Object Libraries
  63. ^^^^^^^^^^^^^^^^
  64. ::
  65. add_library(<name> OBJECT <src>...)
  66. Creates an :ref:`Object Library <Object Libraries>`. An object library
  67. compiles source files but does not archive or link their object files into a
  68. library. Instead other targets created by :command:`add_library` or
  69. :command:`add_executable` may reference the objects using an expression of the
  70. form ``$<TARGET_OBJECTS:objlib>`` as a source, where ``objlib`` is the
  71. object library name. For example:
  72. .. code-block:: cmake
  73. add_library(... $<TARGET_OBJECTS:objlib> ...)
  74. add_executable(... $<TARGET_OBJECTS:objlib> ...)
  75. will include objlib's object files in a library and an executable
  76. along with those compiled from their own sources. Object libraries
  77. may contain only sources that compile, header files, and other files
  78. that would not affect linking of a normal library (e.g. ``.txt``).
  79. They may contain custom commands generating such sources, but not
  80. ``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Object libraries
  81. cannot be imported, exported, installed, or linked. Some native build
  82. systems may not like targets that have only object files, so consider
  83. adding at least one real source file to any target that references
  84. ``$<TARGET_OBJECTS:objlib>``.
  85. Alias Libraries
  86. ^^^^^^^^^^^^^^^
  87. ::
  88. add_library(<name> ALIAS <target>)
  89. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
  90. used to refer to ``<target>`` in subsequent commands. The ``<name>`` does
  91. not appear in the generatedbuildsystem as a make target. The ``<target>``
  92. may not be an :ref:`Imported Target <Imported Targets>` or an ``ALIAS``.
  93. ``ALIAS`` targets can be used as linkable targets and as targets to
  94. read properties from. They can also be tested for existance with the
  95. regular :command:`if(TARGET)` subcommand. The ``<name>`` may not be used
  96. to modify properties of ``<target>``, that is, it may not be used as the
  97. operand of :command:`set_property`, :command:`set_target_properties`,
  98. :command:`target_link_libraries` etc. An ``ALIAS`` target may not be
  99. installed or exported.
  100. Interface Libraries
  101. ^^^^^^^^^^^^^^^^^^^
  102. ::
  103. add_library(<name> INTERFACE [IMPORTED [GLOBAL]])
  104. Creates an :ref:`Interface Library <Interface Libraries>`. An ``INTERFACE``
  105. library target does not directly create build output, though it may
  106. have properties set on it and it may be installed, exported and
  107. imported. Typically the ``INTERFACE_*`` properties are populated on
  108. the interface target using the commands:
  109. * :command:`set_property`,
  110. * :command:`target_link_libraries(INTERFACE)`,
  111. * :command:`target_include_directories(INTERFACE)`,
  112. * :command:`target_compile_options(INTERFACE)`,
  113. * :command:`target_compile_definitions(INTERFACE)`, and
  114. * :command:`target_sources(INTERFACE)`,
  115. and then it is used as an argument to :command:`target_link_libraries`
  116. like any other target.
  117. An ``INTERFACE`` :ref:`Imported Target <Imported Targets>` may also be
  118. created with this signature. An ``IMPORTED`` library target references a
  119. library defined outside the project. The target name has scope in the
  120. directory in which it is created and below, but the ``GLOBAL`` option
  121. extends visibility. It may be referenced like any target built within
  122. the project. ``IMPORTED`` libraries are useful for convenient reference
  123. from commands like :command:`target_link_libraries`.