add_executable.rst 3.4 KB

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