1try_run
2-------
3
4.. only:: html
5
6   .. contents::
7
8Try compiling and then running some code.
9
10Try Compiling and Running Source Files
11^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12
13.. code-block:: cmake
14
15  try_run(<runResultVar> <compileResultVar>
16          <bindir> <srcfile> [CMAKE_FLAGS <flags>...]
17          [COMPILE_DEFINITIONS <defs>...]
18          [LINK_OPTIONS <options>...]
19          [LINK_LIBRARIES <libs>...]
20          [COMPILE_OUTPUT_VARIABLE <var>]
21          [RUN_OUTPUT_VARIABLE <var>]
22          [OUTPUT_VARIABLE <var>]
23          [WORKING_DIRECTORY <var>]
24          [ARGS <args>...])
25
26Try compiling a ``<srcfile>``.  Returns ``TRUE`` or ``FALSE`` for success
27or failure in ``<compileResultVar>``.  If the compile succeeded, runs the
28executable and returns its exit code in ``<runResultVar>``.  If the
29executable was built, but failed to run, then ``<runResultVar>`` will be
30set to ``FAILED_TO_RUN``.  See the :command:`try_compile` command for
31information on how the test project is constructed to build the source file.
32
33.. versionadded:: 3.14
34  The names of the result variables ``<runResultVar>`` and
35  ``<compileResultVar>`` are defined by the user.  Previously, they had
36  fixed names ``RUN_RESULT_VAR`` and ``COMPILE_RESULT_VAR``.
37
38The options are:
39
40``CMAKE_FLAGS <flags>...``
41  Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
42  the ``cmake`` command-line used to drive the test build.
43  The example in :command:`try_compile` shows how values for variables
44  ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
45  are used.
46
47``COMPILE_DEFINITIONS <defs>...``
48  Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
49  in the generated test project.
50
51``COMPILE_OUTPUT_VARIABLE <var>``
52  Report the compile step build output in a given variable.
53
54``LINK_LIBRARIES <libs>...``
55  .. versionadded:: 3.2
56
57  Specify libraries to be linked in the generated project.
58  The list of libraries may refer to system libraries and to
59  :ref:`Imported Targets <Imported Targets>` from the calling project.
60
61  If this option is specified, any ``-DLINK_LIBRARIES=...`` value
62  given to the ``CMAKE_FLAGS`` option will be ignored.
63
64``LINK_OPTIONS <options>...``
65  .. versionadded:: 3.14
66
67  Specify link step options to pass to :command:`target_link_options` in the
68  generated project.
69
70``OUTPUT_VARIABLE <var>``
71  Report the compile build output and the output from running the executable
72  in the given variable.  This option exists for legacy reasons.  Prefer
73  ``COMPILE_OUTPUT_VARIABLE`` and ``RUN_OUTPUT_VARIABLE`` instead.
74
75``RUN_OUTPUT_VARIABLE <var>``
76  Report the output from running the executable in a given variable.
77
78``WORKING_DIRECTORY <var>``
79  .. versionadded:: 3.20
80
81  Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
82  specified, the executable will run in ``<bindir>``.
83
84Other Behavior Settings
85^^^^^^^^^^^^^^^^^^^^^^^
86
87Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
88a build configuration.
89
90Behavior when Cross Compiling
91^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92
93.. versionadded:: 3.3
94  Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled
95  binaries.
96
97When cross compiling, the executable compiled in the first step
98usually cannot be run on the build host.  The ``try_run`` command checks
99the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
100cross-compiling mode.  If that is the case, it will still try to compile
101the executable, but it will not try to run the executable unless the
102:variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set.  Instead it
103will create cache variables which must be filled by the user or by
104presetting them in some CMake script file to the values the executable
105would have produced if it had been run on its actual target platform.
106These cache entries are:
107
108``<runResultVar>``
109  Exit code if the executable were to be run on the target platform.
110
111``<runResultVar>__TRYRUN_OUTPUT``
112  Output from stdout and stderr if the executable were to be run on
113  the target platform.  This is created only if the
114  ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
115
116In order to make cross compiling your project easier, use ``try_run``
117only if really required.  If you use ``try_run``, use the
118``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
119required.  Using them will require that when cross-compiling, the cache
120variables will have to be set manually to the output of the executable.
121You can also "guard" the calls to ``try_run`` with an :command:`if`
122block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
123provide an easy-to-preset alternative for this case.
124