add_executable.rst 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. add_executable
  2. --------------
  3. Add an executable to the project using the specified source files.
  4. ::
  5. add_executable(<name> [WIN32] [MACOSX_BUNDLE]
  6. [EXCLUDE_FROM_ALL]
  7. source1 [source2 ...])
  8. Adds an executable target called ``<name>`` to be built from the source
  9. files 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 executable built is constructed based on
  12. conventions of the native platform (such as ``<name>.exe`` or just
  13. ``<name>``).
  14. By default the executable file will be created in the build tree
  15. directory corresponding to the source tree directory in which the
  16. command was invoked. See documentation of the
  17. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target property to change this
  18. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target property
  19. to change the ``<name>`` part of the final file name.
  20. If ``WIN32`` is given the property :prop_tgt:`WIN32_EXECUTABLE` will be
  21. set on the target created. See documentation of that target property for
  22. details.
  23. If ``MACOSX_BUNDLE`` is given the corresponding property will be set on
  24. the created target. See documentation of the :prop_tgt:`MACOSX_BUNDLE`
  25. target property for details.
  26. If ``EXCLUDE_FROM_ALL`` is given the corresponding property will be set on
  27. the created target. See documentation of the :prop_tgt:`EXCLUDE_FROM_ALL`
  28. target property for details.
  29. Source arguments to ``add_executable`` may use "generator expressions" with
  30. the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  31. manual for available expressions. See the :manual:`cmake-buildsystem(7)`
  32. manual for more on defining buildsystem properties.
  33. See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
  34. pre-processed, and you want to have the original sources reachable from
  35. within IDE.
  36. --------------------------------------------------------------------------
  37. ::
  38. add_executable(<name> IMPORTED [GLOBAL])
  39. An :ref:`IMPORTED executable target <Imported Targets>` references an
  40. executable file located outside the project. No rules are generated to
  41. build it, and the :prop_tgt:`IMPORTED` target property is ``True``. The
  42. target name has scope in the directory in which it is created and below, but
  43. the ``GLOBAL`` option extends visibility. It may be referenced like any
  44. target built within the project. ``IMPORTED`` executables are useful
  45. for convenient reference from commands like :command:`add_custom_command`.
  46. Details about the imported executable are specified by setting properties
  47. whose names begin in ``IMPORTED_``. The most important such property is
  48. :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
  49. :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
  50. the main executable file on disk. See documentation of the ``IMPORTED_*``
  51. properties for more information.
  52. --------------------------------------------------------------------------
  53. ::
  54. add_executable(<name> ALIAS <target>)
  55. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
  56. be used to refer to ``<target>`` in subsequent commands. The ``<name>``
  57. does not appear in the generated buildsystem as a make target. The
  58. ``<target>`` may not be an :ref:`Imported Target <Imported Targets>` or an
  59. ``ALIAS``. ``ALIAS`` targets can be used as targets to read properties
  60. from, executables for custom commands and custom targets. They can also be
  61. tested for existence with the regular :command:`if(TARGET)` subcommand.
  62. The ``<name>`` may not be used to modify properties of ``<target>``, that
  63. is, it may not be used as the operand of :command:`set_property`,
  64. :command:`set_target_properties`, :command:`target_link_libraries` etc.
  65. An ``ALIAS`` target may not be installed or exported.