1add_executable
2--------------
3
4.. only:: html
5
6  .. contents::
7
8Add an executable to the project using the specified source files.
9
10Normal Executables
11^^^^^^^^^^^^^^^^^^
12
13.. code-block:: cmake
14
15  add_executable(<name> [WIN32] [MACOSX_BUNDLE]
16                 [EXCLUDE_FROM_ALL]
17                 [source1] [source2 ...])
18
19Adds an executable target called ``<name>`` to be built from the source
20files listed in the command invocation.  The
21``<name>`` corresponds to the logical target name and must be globally
22unique within a project.  The actual file name of the executable built is
23constructed based on conventions of the native platform (such as
24``<name>.exe`` or just ``<name>``).
25
26.. versionadded:: 3.1
27  Source arguments to ``add_executable`` may use "generator expressions" with
28  the syntax ``$<...>``.  See the :manual:`cmake-generator-expressions(7)`
29  manual for available expressions.
30
31.. versionadded:: 3.11
32  The source files can be omitted if they are added later using
33  :command:`target_sources`.
34
35By default the executable file will be created in the build tree
36directory corresponding to the source tree directory in which the
37command was invoked.  See documentation of the
38:prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target property to change this
39location.  See documentation of the :prop_tgt:`OUTPUT_NAME` target property
40to change the ``<name>`` part of the final file name.
41
42If ``WIN32`` is given the property :prop_tgt:`WIN32_EXECUTABLE` will be
43set on the target created.  See documentation of that target property for
44details.
45
46If ``MACOSX_BUNDLE`` is given the corresponding property will be set on
47the created target.  See documentation of the :prop_tgt:`MACOSX_BUNDLE`
48target property for details.
49
50If ``EXCLUDE_FROM_ALL`` is given the corresponding property will be set on
51the created target.  See documentation of the :prop_tgt:`EXCLUDE_FROM_ALL`
52target property for details.
53
54See the :manual:`cmake-buildsystem(7)` manual for more on defining
55buildsystem properties.
56
57See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
58pre-processed, and you want to have the original sources reachable from
59within IDE.
60
61Imported Executables
62^^^^^^^^^^^^^^^^^^^^
63
64.. code-block:: cmake
65
66  add_executable(<name> IMPORTED [GLOBAL])
67
68An :ref:`IMPORTED executable target <Imported Targets>` references an
69executable file located outside the project.  No rules are generated to
70build it, and the :prop_tgt:`IMPORTED` target property is ``True``.  The
71target name has scope in the directory in which it is created and below, but
72the ``GLOBAL`` option extends visibility.  It may be referenced like any
73target built within the project.  ``IMPORTED`` executables are useful
74for convenient reference from commands like :command:`add_custom_command`.
75Details about the imported executable are specified by setting properties
76whose names begin in ``IMPORTED_``.  The most important such property is
77:prop_tgt:`IMPORTED_LOCATION` (and its per-configuration version
78:prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the location of
79the main executable file on disk.  See documentation of the ``IMPORTED_*``
80properties for more information.
81
82Alias Executables
83^^^^^^^^^^^^^^^^^
84
85.. code-block:: cmake
86
87  add_executable(<name> ALIAS <target>)
88
89Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can
90be used to refer to ``<target>`` in subsequent commands.  The ``<name>``
91does not appear in the generated buildsystem as a make target.  The
92``<target>`` may not be an ``ALIAS``.
93
94.. versionadded:: 3.11
95  An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
96
97.. versionadded:: 3.18
98  An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
99  scoped to the directory in which it is created and subdirectories.
100  The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
101  alias is global or not.
102
103``ALIAS`` targets can be used as targets to read properties
104from, executables for custom commands and custom targets.  They can also be
105tested for existence with the regular :command:`if(TARGET)` subcommand.
106The ``<name>`` may not be used to modify properties of ``<target>``, that
107is, it may not be used as the operand of :command:`set_property`,
108:command:`set_target_properties`, :command:`target_link_libraries` etc.
109An ``ALIAS`` target may not be installed or exported.
110