add_library.rst 6.4 KB

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