add_executable.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. .. code-block:: cmake
  9. add_executable(<name> [WIN32] [MACOSX_BUNDLE]
  10. [EXCLUDE_FROM_ALL]
  11. [source1] [source2 ...])
  12. Adds an executable target called ``<name>`` to be built from the source
  13. files listed in the command invocation. The
  14. ``<name>`` corresponds to the logical target name and must be globally
  15. unique within a project. The actual file name of the executable built is
  16. constructed based on conventions of the native platform (such as
  17. ``<name>.exe`` or just ``<name>``).
  18. .. versionadded:: 3.1
  19. Source arguments to ``add_executable`` may use "generator expressions" with
  20. the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  21. manual for available expressions.
  22. .. versionadded:: 3.11
  23. The source files can be omitted if they are added later using
  24. :command:`target_sources`.
  25. By default the executable file will be created in the build tree
  26. directory corresponding to the source tree directory in which the
  27. command was invoked. See documentation of the
  28. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target property to change this
  29. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target property
  30. to change the ``<name>`` part of the final file name.
  31. If ``WIN32`` is given the property :prop_tgt:`WIN32_EXECUTABLE` will be
  32. set on the target created. See documentation of that target property for
  33. details.
  34. If ``MACOSX_BUNDLE`` is given the corresponding property will be set on
  35. the created target. See documentation of the :prop_tgt:`MACOSX_BUNDLE`
  36. target property for details.
  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. 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. .. code-block:: cmake
  48. add_executable(<name> IMPORTED [GLOBAL])
  49. An :ref:`IMPORTED executable target <Imported Targets>` references an
  50. executable file located outside the project. No rules are generated to
  51. build it, and the :prop_tgt:`IMPORTED` target property is ``True``. The
  52. target name has scope in the directory in which it is created and below, but
  53. the ``GLOBAL`` option extends visibility. It may be referenced like any
  54. target built within the project. ``IMPORTED`` executables are useful
  55. for convenient reference from commands like :command:`add_custom_command`.
  56. Details about the imported executable are specified by setting properties
  57. whose names begin in ``IMPORTED_``. The most important such property is
  58. :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
  59. :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
  60. the main executable file on disk. See documentation of the ``IMPORTED_*``
  61. properties for more information.
  62. Alias Executables
  63. ^^^^^^^^^^^^^^^^^
  64. .. code-block:: cmake
  65. add_executable(<name> ALIAS <target>)
  66. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
  67. be used to refer to ``<target>`` in subsequent commands. The ``<name>``
  68. does not appear in the generated buildsystem as a make target. The
  69. ``<target>`` may not be an ``ALIAS``.
  70. .. versionadded:: 3.11
  71. An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  72. .. versionadded:: 3.18
  73. An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
  74. scoped to the directory in which it is created and subdirectories.
  75. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  76. alias is global or not.
  77. ``ALIAS`` targets can be used as targets to read properties
  78. from, executables for custom commands and custom targets. They can also be
  79. tested for existence with the regular :command:`if(TARGET)` subcommand.
  80. The ``<name>`` may not be used to modify properties of ``<target>``, that
  81. is, it may not be used as the operand of :command:`set_property`,
  82. :command:`set_target_properties`, :command:`target_link_libraries` etc.
  83. An ``ALIAS`` target may not be installed or exported.