add_executable.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. add_executable
  2. --------------
  3. .. only:: html
  4. .. contents::
  5. Add an executable to the project using the specified source files.
  6. Normal Executables
  7. ^^^^^^^^^^^^^^^^^^
  8. .. signature::
  9. add_executable(<name> <options>... <sources>...)
  10. :target: normal
  11. Add an :ref:`executable <Executables>` target called ``<name>`` to
  12. be built from the source files listed in the command invocation.
  13. The options are:
  14. ``WIN32``
  15. Set the :prop_tgt:`WIN32_EXECUTABLE` target property automatically.
  16. See documentation of that target property for details.
  17. ``MACOSX_BUNDLE``
  18. Set the :prop_tgt:`MACOSX_BUNDLE` target property automatically.
  19. See documentation of that target property for details.
  20. ``EXCLUDE_FROM_ALL``
  21. Set the :prop_tgt:`EXCLUDE_FROM_ALL` target property automatically.
  22. See documentation of that target property for details.
  23. The ``<name>`` corresponds to the logical target name and must be globally
  24. unique within a project. The actual file name of the executable built is
  25. constructed based on conventions of the native platform (such as
  26. ``<name>.exe`` or just ``<name>``).
  27. .. versionadded:: 3.1
  28. Source arguments to ``add_executable`` may use "generator expressions" with
  29. the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  30. manual for available expressions.
  31. .. versionadded:: 3.11
  32. The source files can be omitted if they are added later using
  33. :command:`target_sources`.
  34. By default the executable file will be created in the build tree
  35. directory corresponding to the source tree directory in which the
  36. command was invoked. See documentation of the
  37. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target property to change this
  38. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target property
  39. to change the ``<name>`` part of the final file name.
  40. See the :manual:`cmake-buildsystem(7)` manual for more on defining
  41. buildsystem properties.
  42. See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
  43. pre-processed, and you want to have the original sources reachable from
  44. within IDE.
  45. Imported Executables
  46. ^^^^^^^^^^^^^^^^^^^^
  47. .. signature::
  48. add_executable(<name> IMPORTED [GLOBAL])
  49. :target: IMPORTED
  50. Add an :ref:`IMPORTED executable target <Imported Targets>` to reference
  51. an executable file located outside the project. The target name may be
  52. referenced like any target built within the project, except that by
  53. default it is visible only in the directory in which it is created,
  54. and below.
  55. The options are:
  56. ``GLOBAL``
  57. Make the target name globally visible.
  58. No rules are generated to build imported targets, and the :prop_tgt:`IMPORTED`
  59. target property is ``True``. Imported executables are useful for convenient
  60. reference from commands like :command:`add_custom_command`.
  61. Details about the imported executable are specified by setting properties
  62. whose names begin in ``IMPORTED_``. The most important such property is
  63. :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
  64. :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
  65. the main executable file on disk. See documentation of the ``IMPORTED_*``
  66. properties for more information.
  67. Alias Executables
  68. ^^^^^^^^^^^^^^^^^
  69. .. signature::
  70. add_executable(<name> ALIAS <target>)
  71. :target: ALIAS
  72. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
  73. be used to refer to ``<target>`` in subsequent commands. The ``<name>``
  74. does not appear in the generated buildsystem as a make target. The
  75. ``<target>`` may not be an ``ALIAS``.
  76. .. versionadded:: 3.11
  77. An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  78. .. versionadded:: 3.18
  79. An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
  80. scoped to the directory in which it is created and subdirectories.
  81. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  82. alias is global or not.
  83. ``ALIAS`` targets can be used as targets to read properties
  84. from, executables for custom commands and custom targets. They can also be
  85. tested for existence with the regular :command:`if(TARGET)` subcommand.
  86. The ``<name>`` may not be used to modify properties of ``<target>``, that
  87. is, it may not be used as the operand of :command:`set_property`,
  88. :command:`set_target_properties`, :command:`target_link_libraries` etc.
  89. An ``ALIAS`` target may not be installed or exported.
  90. See Also
  91. ^^^^^^^^
  92. * :command:`add_library`