add_library.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ... sourceN)
  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 library
  15. to be created. STATIC libraries are archives of object files for use
  16. when linking other targets. SHARED libraries are linked dynamically
  17. and loaded at runtime. MODULE libraries are plugins that are not
  18. 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 BUILD_SHARED_LIBS is true. For SHARED and MODULE libraries
  22. the POSITION_INDEPENDENT_CODE target property is set to TRUE
  23. automatically.
  24. By default the library file will be created in the build tree
  25. directory corresponding to the source tree directory in which the
  26. command was invoked. See documentation of the
  27. ARCHIVE_OUTPUT_DIRECTORY, LIBRARY_OUTPUT_DIRECTORY, and
  28. RUNTIME_OUTPUT_DIRECTORY target properties to change this location.
  29. See documentation of the OUTPUT_NAME target property to change the
  30. <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 EXCLUDE_FROM_ALL target
  33. property for details.
  34. The add_library command can also create IMPORTED library targets using
  35. this signature:
  36. ::
  37. add_library(<name> <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED
  38. [GLOBAL])
  39. An IMPORTED library target references a library file located outside
  40. the project. No rules are generated to build it. The target name has
  41. scope in the directory in which it is created and below, but the
  42. GLOBAL option extends visibility. It may be referenced like any
  43. target built within the project. IMPORTED libraries are useful for
  44. convenient reference from commands like target_link_libraries.
  45. Details about the imported library are specified by setting properties
  46. whose names begin in ``IMPORTED_``. The most important such property is
  47. IMPORTED_LOCATION (and its per-configuration version
  48. IMPORTED_LOCATION_<CONFIG>) which specifies the location of the main
  49. library file on disk. See documentation of the IMPORTED_* properties
  50. for more information.
  51. The signature
  52. ::
  53. add_library(<name> OBJECT <src>...)
  54. creates a special "object library" target. An object library compiles
  55. source files but does not archive or link their object files into a
  56. library. Instead other targets created by add_library or
  57. add_executable may reference the objects using an expression of the
  58. form $<TARGET_OBJECTS:objlib> as a source, where "objlib" is the
  59. object library name. For example:
  60. ::
  61. add_library(... $<TARGET_OBJECTS:objlib> ...)
  62. add_executable(... $<TARGET_OBJECTS:objlib> ...)
  63. will include objlib's object files in a library and an executable
  64. along with those compiled from their own sources. Object libraries
  65. may contain only sources (and headers) that compile to object files.
  66. They may contain custom commands generating such sources, but not
  67. PRE_BUILD, PRE_LINK, or POST_BUILD commands. Object libraries cannot
  68. be imported, exported, installed, or linked. Some native build
  69. systems may not like targets that have only object files, so consider
  70. adding at least one real source file to any target that references
  71. $<TARGET_OBJECTS:objlib>.
  72. The signature
  73. ::
  74. add_library(<name> ALIAS <target>)
  75. creates an alias, such that <name> can be used to refer to <target> in
  76. subsequent commands. The <name> does not appear in the generated
  77. buildsystem as a make target. The <target> may not be an IMPORTED
  78. target or an ALIAS. Alias targets can be used as linkable targets,
  79. targets to read properties from. They can also be tested for
  80. existance with the regular if(TARGET) subcommand. The <name> may not
  81. be used to modify properties of <target>, that is, it may not be used
  82. as the operand of set_property, set_target_properties,
  83. target_link_libraries etc. An ALIAS target may not be installed of
  84. exported.
  85. The signature
  86. ::
  87. add_library(<name> INTERFACE)
  88. creates an interface target. An interface target does not directly
  89. create build output, though it may have properties set on it and it
  90. may be installed, exported and imported. Typically the INTERFACE_*
  91. properties are populated on the interface target using the
  92. set_property(), target_link_libraries(), target_include_directories()
  93. and target_compile_defintions() commands, and then it is used as an
  94. argument to target_link_libraries() like any other target.