8 Try compiling and then running some code.
10 Try Compiling and Running Source Files
11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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>]
26 Try compiling a ``<srcfile>``. Returns ``TRUE`` or ``FALSE`` for success
27 or failure in ``<compileResultVar>``. If the compile succeeded, runs the
28 executable and returns its exit code in ``<runResultVar>``. If the
29 executable was built, but failed to run, then ``<runResultVar>`` will be
30 set to ``FAILED_TO_RUN``. See the :command:`try_compile` command for
31 information on how the test project is constructed to build the source file.
35 ``CMAKE_FLAGS <flags>...``
36 Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
37 the ``cmake`` command-line used to drive the test build.
38 The example in :command:`try_compile` shows how values for variables
39 ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
42 ``COMPILE_DEFINITIONS <defs>...``
43 Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
44 in the generated test project.
46 ``COMPILE_OUTPUT_VARIABLE <var>``
47 Report the compile step build output in a given variable.
49 ``LINK_LIBRARIES <libs>...``
52 Specify libraries to be linked in the generated project.
53 The list of libraries may refer to system libraries and to
54 :ref:`Imported Targets <Imported Targets>` from the calling project.
56 If this option is specified, any ``-DLINK_LIBRARIES=...`` value
57 given to the ``CMAKE_FLAGS`` option will be ignored.
59 ``LINK_OPTIONS <options>...``
60 .. versionadded:: 3.14
62 Specify link step options to pass to :command:`target_link_options` in the
65 ``OUTPUT_VARIABLE <var>``
66 Report the compile build output and the output from running the executable
67 in the given variable. This option exists for legacy reasons. Prefer
68 ``COMPILE_OUTPUT_VARIABLE`` and ``RUN_OUTPUT_VARIABLE`` instead.
70 ``RUN_OUTPUT_VARIABLE <var>``
71 Report the output from running the executable in a given variable.
73 ``WORKING_DIRECTORY <var>``
74 .. versionadded:: 3.20
76 Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
77 specified, the executable will run in ``<bindir>``.
79 Other Behavior Settings
80 ^^^^^^^^^^^^^^^^^^^^^^^
82 Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
83 a build configuration.
85 Behavior when Cross Compiling
86 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
89 Use ``CMAKE_CROSSCOMPILING_EMULATOR`` when running cross-compiled
92 When cross compiling, the executable compiled in the first step
93 usually cannot be run on the build host. The ``try_run`` command checks
94 the :variable:`CMAKE_CROSSCOMPILING` variable to detect whether CMake is in
95 cross-compiling mode. If that is the case, it will still try to compile
96 the executable, but it will not try to run the executable unless the
97 :variable:`CMAKE_CROSSCOMPILING_EMULATOR` variable is set. Instead it
98 will create cache variables which must be filled by the user or by
99 presetting them in some CMake script file to the values the executable
100 would have produced if it had been run on its actual target platform.
101 These cache entries are:
104 Exit code if the executable were to be run on the target platform.
106 ``<runResultVar>__TRYRUN_OUTPUT``
107 Output from stdout and stderr if the executable were to be run on
108 the target platform. This is created only if the
109 ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` option was used.
111 In order to make cross compiling your project easier, use ``try_run``
112 only if really required. If you use ``try_run``, use the
113 ``RUN_OUTPUT_VARIABLE`` or ``OUTPUT_VARIABLE`` options only if really
114 required. Using them will require that when cross-compiling, the cache
115 variables will have to be set manually to the output of the executable.
116 You can also "guard" the calls to ``try_run`` with an :command:`if`
117 block checking the :variable:`CMAKE_CROSSCOMPILING` variable and
118 provide an easy-to-preset alternative for this case.