add_executable.rst 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. --------------------------------------------------------------------------
  34. ::
  35. add_executable(<name> IMPORTED [GLOBAL])
  36. An :ref:`IMPORTED executable target <Imported Targets>` references an
  37. executable file located outside the project. No rules are generated to
  38. build it, and the :prop_tgt:`IMPORTED` target property is ``True``. The
  39. target name has scope in the directory in which it is created and below, but
  40. the ``GLOBAL`` option extends visibility. It may be referenced like any
  41. target built within the project. ``IMPORTED`` executables are useful
  42. for convenient reference from commands like :command:`add_custom_command`.
  43. Details about the imported executable are specified by setting properties
  44. whose names begin in ``IMPORTED_``. The most important such property is
  45. :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
  46. :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
  47. the main executable file on disk. See documentation of the ``IMPORTED_*``
  48. properties for more information.
  49. --------------------------------------------------------------------------
  50. ::
  51. add_executable(<name> ALIAS <target>)
  52. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
  53. be used to refer to ``<target>`` in subsequent commands. The ``<name>``
  54. does not appear in the generated buildsystem as a make target. The
  55. ``<target>`` may not be an :ref:`Imported Target <Imported Targets>` or an
  56. ``ALIAS``. ``ALIAS`` targets can be used as targets to read properties
  57. from, executables for custom commands and custom targets. They can also be
  58. tested for existence with the regular :command:`if(TARGET)` subcommand.
  59. The ``<name>`` may not be used to modify properties of ``<target>``, that
  60. is, it may not be used as the operand of :command:`set_property`,
  61. :command:`set_target_properties`, :command:`target_link_libraries` etc.
  62. An ``ALIAS`` target may not be installed or exported.