add_executable.rst 3.9 KB

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