1
0

add_executable.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 source files can be omitted
  14. here if they are added later using :command:`target_sources`.) The
  15. ``<name>`` corresponds to the logical target name and must be globally
  16. unique within a project. The actual file name of the executable built is
  17. constructed based on conventions of the native platform (such as
  18. ``<name>.exe`` or just ``<name>``).
  19. By default the executable file will be created in the build tree
  20. directory corresponding to the source tree directory in which the
  21. command was invoked. See documentation of the
  22. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target property to change this
  23. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target property
  24. to change the ``<name>`` part of the final file name.
  25. If ``WIN32`` is given the property :prop_tgt:`WIN32_EXECUTABLE` will be
  26. set on the target created. See documentation of that target property for
  27. details.
  28. If ``MACOSX_BUNDLE`` is given the corresponding property will be set on
  29. the created target. See documentation of the :prop_tgt:`MACOSX_BUNDLE`
  30. target property for details.
  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_executable`` 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. See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
  39. pre-processed, and you want to have the original sources reachable from
  40. within IDE.
  41. Imported Executables
  42. ^^^^^^^^^^^^^^^^^^^^
  43. .. code-block:: cmake
  44. add_executable(<name> IMPORTED [GLOBAL])
  45. An :ref:`IMPORTED executable target <Imported Targets>` references an
  46. executable file located outside the project. No rules are generated to
  47. build it, and the :prop_tgt:`IMPORTED` target property is ``True``. The
  48. target name has scope in the directory in which it is created and below, but
  49. the ``GLOBAL`` option extends visibility. It may be referenced like any
  50. target built within the project. ``IMPORTED`` executables are useful
  51. for convenient reference from commands like :command:`add_custom_command`.
  52. Details about the imported executable are specified by setting properties
  53. whose names begin in ``IMPORTED_``. The most important such property is
  54. :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
  55. :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
  56. the main executable file on disk. See documentation of the ``IMPORTED_*``
  57. properties for more information.
  58. Alias Executables
  59. ^^^^^^^^^^^^^^^^^
  60. .. code-block:: cmake
  61. add_executable(<name> ALIAS <target>)
  62. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
  63. be used to refer to ``<target>`` in subsequent commands. The ``<name>``
  64. does not appear in the generated buildsystem as a make target. The
  65. ``<target>`` may not be an ``ALIAS``.
  66. An ``ALIAS`` to a non-``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  67. has scope in the directory in which the alias is created and below.
  68. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  69. alias is global or not.
  70. ``ALIAS`` targets can be used as targets to read properties
  71. from, executables for custom commands and custom targets. They can also be
  72. tested for existence with the regular :command:`if(TARGET)` subcommand.
  73. The ``<name>`` may not be used to modify properties of ``<target>``, that
  74. is, it may not be used as the operand of :command:`set_property`,
  75. :command:`set_target_properties`, :command:`target_link_libraries` etc.
  76. An ``ALIAS`` target may not be installed or exported.