try_compile.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. try_compile
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Try building some code.
  6. .. _`Try Compiling Whole Projects`:
  7. Try Compiling Whole Projects
  8. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. .. code-block:: cmake
  10. try_compile(<resultVar> PROJECT <projectName>
  11. SOURCE_DIR <srcdir>
  12. [BINARY_DIR <bindir>]
  13. [TARGET <targetName>]
  14. [CMAKE_FLAGS <flags>...]
  15. [OUTPUT_VARIABLE <var>])
  16. .. versionadded:: 3.25
  17. Try building a project. The success or failure of the ``try_compile``,
  18. i.e. ``TRUE`` or ``FALSE`` respectively, is returned in ``<resultVar>``.
  19. In this form, ``<srcdir>`` should contain a complete CMake project with a
  20. ``CMakeLists.txt`` file and all sources. The ``<bindir>`` and ``<srcdir>``
  21. will not be deleted after this command is run. Specify ``<targetName>`` to
  22. build a specific target instead of the ``all`` or ``ALL_BUILD`` target. See
  23. below for the meaning of other options.
  24. .. versionchanged:: 3.24
  25. CMake variables describing platform settings, and those listed by the
  26. :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable, are propagated
  27. into the project's build configuration. See policy :policy:`CMP0137`.
  28. Previously this was only done by the
  29. :ref:`source file <Try Compiling Source Files>` signature.
  30. This command also supports an alternate signature
  31. which was present in older versions of CMake:
  32. .. code-block:: cmake
  33. try_compile(<resultVar> <bindir> <srcdir>
  34. <projectName> [<targetName>] [CMAKE_FLAGS <flags>...]
  35. [OUTPUT_VARIABLE <var>])
  36. .. _`Try Compiling Source Files`:
  37. Try Compiling Source Files
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  39. .. code-block:: cmake
  40. try_compile(<resultVar>
  41. <SOURCES <srcfile...>] |
  42. SOURCE_FROM_ARG <name> <content>] |
  43. SOURCE_FROM_VAR <name> <var>] |
  44. SOURCE_FROM_FILE <name> <path> >...
  45. [CMAKE_FLAGS <flags>...]
  46. [COMPILE_DEFINITIONS <defs>...]
  47. [LINK_OPTIONS <options>...]
  48. [LINK_LIBRARIES <libs>...]
  49. [OUTPUT_VARIABLE <var>]
  50. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  51. [<LANG>_STANDARD <std>]
  52. [<LANG>_STANDARD_REQUIRED <bool>]
  53. [<LANG>_EXTENSIONS <bool>]
  54. )
  55. .. versionadded:: 3.25
  56. Try building an executable or static library from one or more source files
  57. (which one is determined by the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE`
  58. variable). The success or failure of the ``try_compile``, i.e. ``TRUE`` or
  59. ``FALSE`` respectively, is returned in ``<resultVar>``.
  60. In this form, one or more source files must be provided. Additionally, one of
  61. ``SOURCES`` and/or ``SOURCE_FROM_*`` must precede other keywords.
  62. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is unset or is set to
  63. ``EXECUTABLE``, the sources must include a definition for ``main`` and CMake
  64. will create a ``CMakeLists.txt`` file to build the source(s) as an executable.
  65. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``STATIC_LIBRARY``,
  66. a static library will be built instead and no definition for ``main`` is
  67. required. For an executable, the generated ``CMakeLists.txt`` file would
  68. contain something like the following:
  69. .. code-block:: cmake
  70. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  71. include_directories(${INCLUDE_DIRECTORIES})
  72. link_directories(${LINK_DIRECTORIES})
  73. add_executable(cmTryCompileExec <srcfile>...)
  74. target_link_options(cmTryCompileExec PRIVATE <LINK_OPTIONS from caller>)
  75. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  76. CMake will automatically generate a unique directory for each ``try_compile``
  77. operation in an unspecified location within the project's binary directory.
  78. These directories will be cleaned automatically unless
  79. :option:`--debug-trycompile <cmake --debug-trycompile>` is passed to ``cmake``.
  80. Such directories from previous runs are also unconditionally cleaned at the
  81. beginning of any ``cmake`` execution.
  82. This command also supports an alternate signature
  83. which was present in older versions of CMake:
  84. .. code-block:: cmake
  85. try_compile(<resultVar> <bindir> <srcfile|SOURCES srcfile...>
  86. [CMAKE_FLAGS <flags>...]
  87. [COMPILE_DEFINITIONS <defs>...]
  88. [LINK_OPTIONS <options>...]
  89. [LINK_LIBRARIES <libs>...]
  90. [OUTPUT_VARIABLE <var>]
  91. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  92. [<LANG>_STANDARD <std>]
  93. [<LANG>_STANDARD_REQUIRED <bool>]
  94. [<LANG>_EXTENSIONS <bool>]
  95. )
  96. In this version, ``try_compile`` will use ``<bindir>/CMakeFiles/CMakeTmp`` for
  97. its operation, and all such files will be cleaned automatically.
  98. For debugging, :option:`--debug-trycompile <cmake --debug-trycompile>` can be
  99. passed to ``cmake`` to avoid this clean. However, multiple sequential
  100. ``try_compile`` operations, if given the same ``<bindir>``, will reuse this
  101. single output directory, such that you can only debug one such ``try_compile``
  102. call at a time. Use of the newer signature is recommended to simplify
  103. debugging of multiple ``try_compile`` operations.
  104. The options are:
  105. ``CMAKE_FLAGS <flags>...``
  106. Specify flags of the form :option:`-DVAR:TYPE=VALUE <cmake -D>` to be passed
  107. to the :manual:`cmake(1)` command-line used to drive the test build.
  108. The above example shows how values for variables
  109. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  110. are used.
  111. ``COMPILE_DEFINITIONS <defs>...``
  112. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  113. in the generated test project.
  114. ``COPY_FILE <fileName>``
  115. Copy the built executable or static library to the given ``<fileName>``.
  116. ``COPY_FILE_ERROR <var>``
  117. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  118. message encountered while trying to copy the file.
  119. ``LINK_LIBRARIES <libs>...``
  120. Specify libraries to be linked in the generated project.
  121. The list of libraries may refer to system libraries and to
  122. :ref:`Imported Targets <Imported Targets>` from the calling project.
  123. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  124. given to the ``CMAKE_FLAGS`` option will be ignored.
  125. ``LINK_OPTIONS <options>...``
  126. .. versionadded:: 3.14
  127. Specify link step options to pass to :command:`target_link_options` or to
  128. set the :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
  129. project, depending on the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
  130. ``OUTPUT_VARIABLE <var>``
  131. Store the output from the build process in the given variable.
  132. ``SOURCE_FROM_ARG <name> <content>``
  133. .. versionadded:: 3.25
  134. Write ``<content>`` to a file named ``<name>`` in the operation directory.
  135. This can be used to bypass the need to separately write a source file when
  136. the contents of the file are dynamically specified. The specified ``<name>``
  137. is not allowed to contain path components.
  138. ``SOURCE_FROM_ARG`` may be specified multiple times.
  139. ``SOURCE_FROM_FILE <name> <path>``
  140. .. versionadded:: 3.25
  141. Copy ``<path>`` to a file named ``<name>`` in the operation directory. This
  142. can be used to consolidate files into the operation directory, which may be
  143. useful if a source which already exists (i.e. as a stand-alone file in a
  144. project's source repository) needs to refer to other file(s) created by
  145. ``SOURCE_FROM_*``. (Otherwise, ``SOURCES`` is usually more convenient.) The
  146. specified ``<name>`` is not allowed to contain path components.
  147. ``SOURCE_FROM_VAR <name> <content>``
  148. .. versionadded:: 3.25
  149. Write the contents of ``<var>`` to a file named ``<name>`` in the operation
  150. directory. This is the same as ``SOURCE_FROM_ARG``, but takes the contents
  151. from the specified CMake variable, rather than directly, which may be useful
  152. when passing arguments through a function which wraps ``try_compile``. The
  153. specified ``<name>`` is not allowed to contain path components.
  154. ``SOURCE_FROM_VAR`` may be specified multiple times.
  155. ``<LANG>_STANDARD <std>``
  156. .. versionadded:: 3.8
  157. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  158. :prop_tgt:`OBJC_STANDARD`, :prop_tgt:`OBJCXX_STANDARD`,
  159. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  160. ``<LANG>_STANDARD_REQUIRED <bool>``
  161. .. versionadded:: 3.8
  162. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  163. :prop_tgt:`CXX_STANDARD_REQUIRED`, :prop_tgt:`OBJC_STANDARD_REQUIRED`,
  164. :prop_tgt:`OBJCXX_STANDARD_REQUIRED`,or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  165. target property of the generated project.
  166. ``<LANG>_EXTENSIONS <bool>``
  167. .. versionadded:: 3.8
  168. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  169. :prop_tgt:`OBJC_EXTENSIONS`, :prop_tgt:`OBJCXX_EXTENSIONS`,
  170. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  171. Other Behavior Settings
  172. ^^^^^^^^^^^^^^^^^^^^^^^
  173. .. versionadded:: 3.4
  174. If set, the following variables are passed in to the generated
  175. try_compile CMakeLists.txt to initialize compile target properties with
  176. default values:
  177. * :variable:`CMAKE_CUDA_RUNTIME_LIBRARY`
  178. * :variable:`CMAKE_ENABLE_EXPORTS`
  179. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  180. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  181. * :variable:`CMAKE_MSVC_RUNTIME_LIBRARY`
  182. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  183. * :variable:`CMAKE_WATCOM_RUNTIME_LIBRARY`
  184. If :policy:`CMP0056` is set to ``NEW``, then
  185. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  186. .. versionchanged:: 3.14
  187. If :policy:`CMP0083` is set to ``NEW``, then in order to obtain correct
  188. behavior at link time, the ``check_pie_supported()`` command from the
  189. :module:`CheckPIESupported` module must be called before using the
  190. :command:`try_compile` command.
  191. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are propagated
  192. through to the generated test project.
  193. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  194. a build configuration.
  195. .. versionadded:: 3.6
  196. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  197. the type of target used for the source file signature.
  198. .. versionadded:: 3.6
  199. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  200. variables that must be propagated into the test project. This variable is
  201. meant for use only in toolchain files and is only honored by the
  202. ``try_compile()`` command for the source files form, not when given a whole
  203. project.
  204. .. versionchanged:: 3.8
  205. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  206. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  207. then the language standard variables are honored:
  208. * :variable:`CMAKE_C_STANDARD`
  209. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  210. * :variable:`CMAKE_C_EXTENSIONS`
  211. * :variable:`CMAKE_CXX_STANDARD`
  212. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  213. * :variable:`CMAKE_CXX_EXTENSIONS`
  214. * :variable:`CMAKE_OBJC_STANDARD`
  215. * :variable:`CMAKE_OBJC_STANDARD_REQUIRED`
  216. * :variable:`CMAKE_OBJC_EXTENSIONS`
  217. * :variable:`CMAKE_OBJCXX_STANDARD`
  218. * :variable:`CMAKE_OBJCXX_STANDARD_REQUIRED`
  219. * :variable:`CMAKE_OBJCXX_EXTENSIONS`
  220. * :variable:`CMAKE_CUDA_STANDARD`
  221. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  222. * :variable:`CMAKE_CUDA_EXTENSIONS`
  223. Their values are used to set the corresponding target properties in
  224. the generated project (unless overridden by an explicit option).
  225. .. versionchanged:: 3.14
  226. For the :generator:`Green Hills MULTI` generator the GHS toolset and target
  227. system customization cache variables are also propagated into the test project.
  228. .. versionadded:: 3.24
  229. The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
  230. set to disable passing platform variables into the test project.
  231. .. versionadded:: 3.25
  232. If :policy:`CMP0141` is set to ``NEW``, one can use
  233. :variable:`CMAKE_MSVC_DEBUG_INFORMATION_FORMAT` to specify MSVC debug
  234. information format.