try_compile.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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(<compileResultVar> PROJECT <projectName>
  11. SOURCE_DIR <srcdir>
  12. [BINARY_DIR <bindir>]
  13. [TARGET <targetName>]
  14. [LOG_DESCRIPTION <text>]
  15. [NO_CACHE]
  16. [NO_LOG]
  17. [CMAKE_FLAGS <flags>...]
  18. [OUTPUT_VARIABLE <var>])
  19. .. versionadded:: 3.25
  20. Try building a project. Build success returns ``TRUE`` and build failure
  21. returns ``FALSE`` in ``<compileResultVar>``.
  22. In this form, ``<srcdir>`` should contain a complete CMake project with a
  23. ``CMakeLists.txt`` file and all sources. The ``<bindir>`` and ``<srcdir>``
  24. will not be deleted after this command is run. Specify ``<targetName>`` to
  25. build a specific target instead of the ``all`` or ``ALL_BUILD`` target. See
  26. below for the meaning of other options.
  27. .. versionchanged:: 3.24
  28. CMake variables describing platform settings, and those listed by the
  29. :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable, are propagated
  30. into the project's build configuration. See policy :policy:`CMP0137`.
  31. Previously this was only done by the
  32. :ref:`source file <Try Compiling Source Files>` signature.
  33. .. versionadded:: 3.26
  34. This command records a
  35. :ref:`configure-log try_compile event <try_compile configure-log event>`
  36. if the ``NO_LOG`` option is not specified.
  37. This command supports an alternate signature for CMake older than 3.25.
  38. The signature above is recommended for clarity.
  39. .. code-block:: cmake
  40. try_compile(<compileResultVar> <bindir> <srcdir>
  41. <projectName> [<targetName>]
  42. [CMAKE_FLAGS <flags>...]
  43. [OUTPUT_VARIABLE <var>])
  44. .. _`Try Compiling Source Files`:
  45. Try Compiling Source Files
  46. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  47. .. code-block:: cmake
  48. try_compile(<compileResultVar>
  49. [SOURCES_TYPE <type>]
  50. <SOURCES <srcfile...> |
  51. SOURCE_FROM_CONTENT <name> <content> |
  52. SOURCE_FROM_VAR <name> <var> |
  53. SOURCE_FROM_FILE <name> <path> >...
  54. [LOG_DESCRIPTION <text>]
  55. [NO_CACHE]
  56. [NO_LOG]
  57. [CMAKE_FLAGS <flags>...]
  58. [COMPILE_DEFINITIONS <defs>...]
  59. [LINK_OPTIONS <options>...]
  60. [LINK_LIBRARIES <libs>...]
  61. [LINKER_LANGUAGE <lang>]
  62. [OUTPUT_VARIABLE <var>]
  63. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  64. [<LANG>_STANDARD <std>]
  65. [<LANG>_STANDARD_REQUIRED <bool>]
  66. [<LANG>_EXTENSIONS <bool>]
  67. )
  68. .. versionadded:: 3.25
  69. Try building an executable or static library from one or more source files
  70. (which one is determined by the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE`
  71. variable). Build success returns ``TRUE`` and build failure returns ``FALSE``
  72. in ``<compileResultVar>``.
  73. In this form, one or more source files must be provided. Additionally, one of
  74. ``SOURCES`` and/or ``SOURCE_FROM_*`` must precede other keywords.
  75. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is unset or is set to
  76. ``EXECUTABLE``, the sources must include a definition for ``main`` and CMake
  77. will create a ``CMakeLists.txt`` file to build the source(s) as an executable.
  78. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``STATIC_LIBRARY``,
  79. a static library will be built instead and no definition for ``main`` is
  80. required. For an executable, the generated ``CMakeLists.txt`` file would
  81. contain something like the following:
  82. .. code-block:: cmake
  83. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  84. include_directories(${INCLUDE_DIRECTORIES})
  85. link_directories(${LINK_DIRECTORIES})
  86. add_executable(cmTryCompileExec <srcfile>...)
  87. target_link_options(cmTryCompileExec PRIVATE <LINK_OPTIONS from caller>)
  88. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  89. CMake automatically generates, for each ``try_compile`` operation, a
  90. unique directory under ``${CMAKE_BINARY_DIR}/CMakeFiles/CMakeScratch``
  91. with an unspecified name. These directories are cleaned automatically unless
  92. :option:`--debug-trycompile <cmake --debug-trycompile>` is passed to :program:`cmake`.
  93. Such directories from previous runs are also unconditionally cleaned at the
  94. beginning of any :program:`cmake` execution.
  95. This command supports an alternate signature for CMake older than 3.25.
  96. The signature above is recommended for clarity.
  97. .. code-block:: cmake
  98. try_compile(<compileResultVar> <bindir> <srcfile|SOURCES srcfile...>
  99. [CMAKE_FLAGS <flags>...]
  100. [COMPILE_DEFINITIONS <defs>...]
  101. [LINK_OPTIONS <options>...]
  102. [LINK_LIBRARIES <libs>...]
  103. [OUTPUT_VARIABLE <var>]
  104. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  105. [<LANG>_STANDARD <std>]
  106. [<LANG>_STANDARD_REQUIRED <bool>]
  107. [<LANG>_EXTENSIONS <bool>]
  108. )
  109. In this version, ``try_compile`` will use ``<bindir>/CMakeFiles/CMakeTmp`` for
  110. its operation, and all such files will be cleaned automatically.
  111. For debugging, :option:`--debug-trycompile <cmake --debug-trycompile>` can be
  112. passed to :program:`cmake` to avoid this clean. However, multiple sequential
  113. ``try_compile`` operations, if given the same ``<bindir>``, will reuse this
  114. single output directory, such that you can only debug one such ``try_compile``
  115. call at a time. Use of the newer signature is recommended to simplify
  116. debugging of multiple ``try_compile`` operations.
  117. .. _`try_compile Options`:
  118. Options
  119. ^^^^^^^
  120. The options for the above signatures are:
  121. ``CMAKE_FLAGS <flags>...``
  122. Specify flags of the form :option:`-DVAR:TYPE=VALUE <cmake -D>` to be passed
  123. to the :manual:`cmake(1)` command-line used to drive the test build.
  124. The above example shows how values for variables
  125. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  126. are used.
  127. ``COMPILE_DEFINITIONS <defs>...``
  128. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  129. in the generated test project.
  130. ``COPY_FILE <fileName>``
  131. Copy the built executable or static library to the given ``<fileName>``.
  132. ``COPY_FILE_ERROR <var>``
  133. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  134. message encountered while trying to copy the file.
  135. ``LINK_LIBRARIES <libs>...``
  136. Specify libraries to be linked in the generated project.
  137. The list of libraries may refer to system libraries and to
  138. :ref:`Imported Targets <Imported Targets>` from the calling project.
  139. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  140. given to the ``CMAKE_FLAGS`` option will be ignored.
  141. .. versionadded:: 3.29
  142. Alias targets to imported libraries are also supported.
  143. ``LINK_OPTIONS <options>...``
  144. .. versionadded:: 3.14
  145. Specify link step options to pass to :command:`target_link_options` or to
  146. set the :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
  147. project, depending on the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
  148. ``LINKER_LANGUAGE <lang>``
  149. .. versionadded:: 3.29
  150. Specify the :prop_tgt:`LINKER_LANGUAGE` target property of the generated
  151. project. When using multiple source files with different languages, set
  152. this to the language of the source file containing the program entry point,
  153. e.g., ``main``.
  154. ``LOG_DESCRIPTION <text>``
  155. .. versionadded:: 3.26
  156. Specify a non-empty text description of the purpose of the check.
  157. This is recorded in the :manual:`cmake-configure-log(7)` entry.
  158. ``NO_CACHE``
  159. .. versionadded:: 3.25
  160. The result will be stored in a normal variable rather than a cache entry.
  161. The result variable is normally cached so that a simple pattern can be used
  162. to avoid repeating the test on subsequent executions of CMake:
  163. .. code-block:: cmake
  164. if(NOT DEFINED RESULTVAR)
  165. # ...(check-specific setup code)...
  166. try_compile(RESULTVAR ...)
  167. # ...(check-specific logging and cleanup code)...
  168. endif()
  169. If the guard variable and result variable are not the same (for example, if
  170. the test is part of a larger inspection), ``NO_CACHE`` may be useful to avoid
  171. leaking the intermediate result variable into the cache.
  172. ``NO_LOG``
  173. .. versionadded:: 3.26
  174. Do not record a :manual:`cmake-configure-log(7)` entry for this call.
  175. ``OUTPUT_VARIABLE <var>``
  176. Store the output from the build process in the given variable.
  177. ``SOURCE_FROM_CONTENT <name> <content>``
  178. .. versionadded:: 3.25
  179. Write ``<content>`` to a file named ``<name>`` in the operation directory.
  180. This can be used to bypass the need to separately write a source file when
  181. the contents of the file are dynamically specified. The specified ``<name>``
  182. is not allowed to contain path components.
  183. ``SOURCE_FROM_CONTENT`` may be specified multiple times.
  184. ``SOURCE_FROM_FILE <name> <path>``
  185. .. versionadded:: 3.25
  186. Copy ``<path>`` to a file named ``<name>`` in the operation directory. This
  187. can be used to consolidate files into the operation directory, which may be
  188. useful if a source which already exists (i.e. as a stand-alone file in a
  189. project's source repository) needs to refer to other file(s) created by
  190. ``SOURCE_FROM_*``. (Otherwise, ``SOURCES`` is usually more convenient.) The
  191. specified ``<name>`` is not allowed to contain path components.
  192. ``SOURCE_FROM_VAR <name> <var>``
  193. .. versionadded:: 3.25
  194. Write the contents of ``<var>`` to a file named ``<name>`` in the operation
  195. directory. This is the same as ``SOURCE_FROM_CONTENT``, but takes the
  196. contents from the specified CMake variable, rather than directly, which may
  197. be useful when passing arguments through a function which wraps
  198. ``try_compile``. The specified ``<name>`` is not allowed to contain path
  199. components.
  200. ``SOURCE_FROM_VAR`` may be specified multiple times.
  201. ``SOURCES_TYPE <type>``
  202. .. versionadded:: 3.28
  203. Sources may be classified using the ``SOURCES_TYPE`` argument. Once
  204. specified, all subsequent sources specified will be treated as that type
  205. until another ``SOURCES_TYPE`` is given. Available types are:
  206. ``NORMAL``
  207. Sources are not added to any ``FILE_SET`` in the generated project.
  208. ``CXX_MODULE``
  209. .. versionadded:: 3.28
  210. Sources are added to a ``FILE_SET`` of type ``CXX_MODULES`` in the
  211. generated project.
  212. The default type of sources is ``NORMAL``.
  213. ``<LANG>_STANDARD <std>``
  214. .. versionadded:: 3.8
  215. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  216. :prop_tgt:`OBJC_STANDARD`, :prop_tgt:`OBJCXX_STANDARD`,
  217. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  218. ``<LANG>_STANDARD_REQUIRED <bool>``
  219. .. versionadded:: 3.8
  220. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  221. :prop_tgt:`CXX_STANDARD_REQUIRED`, :prop_tgt:`OBJC_STANDARD_REQUIRED`,
  222. :prop_tgt:`OBJCXX_STANDARD_REQUIRED`,or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  223. target property of the generated project.
  224. ``<LANG>_EXTENSIONS <bool>``
  225. .. versionadded:: 3.8
  226. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  227. :prop_tgt:`OBJC_EXTENSIONS`, :prop_tgt:`OBJCXX_EXTENSIONS`,
  228. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  229. Other Behavior Settings
  230. ^^^^^^^^^^^^^^^^^^^^^^^
  231. .. versionadded:: 3.4
  232. If set, the following variables are passed in to the generated
  233. try_compile CMakeLists.txt to initialize compile target properties with
  234. default values:
  235. * :variable:`CMAKE_CUDA_RUNTIME_LIBRARY`
  236. * :variable:`CMAKE_ENABLE_EXPORTS`
  237. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  238. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  239. * :variable:`CMAKE_MSVC_RUNTIME_LIBRARY`
  240. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  241. * :variable:`CMAKE_WATCOM_RUNTIME_LIBRARY`
  242. If :policy:`CMP0056` is set to ``NEW``, then
  243. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  244. .. versionchanged:: 3.14
  245. If :policy:`CMP0083` is set to ``NEW``, then in order to obtain correct
  246. behavior at link time, the ``check_pie_supported()`` command from the
  247. :module:`CheckPIESupported` module must be called before using the
  248. ``try_compile`` command.
  249. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are propagated
  250. through to the generated test project.
  251. Set variable :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` to choose a build
  252. configuration:
  253. * For multi-config generators, this selects which configuration to build.
  254. * For single-config generators, this sets :variable:`CMAKE_BUILD_TYPE` in
  255. the test project.
  256. .. versionadded:: 3.6
  257. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  258. the type of target used for the source file signature.
  259. .. versionadded:: 3.6
  260. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  261. variables that must be propagated into the test project. This variable is
  262. meant for use only in toolchain files and is only honored by the
  263. ``try_compile()`` command for the source files form, not when given a whole
  264. project.
  265. .. versionchanged:: 3.8
  266. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  267. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  268. then the language standard variables are honored:
  269. * :variable:`CMAKE_C_STANDARD`
  270. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  271. * :variable:`CMAKE_C_EXTENSIONS`
  272. * :variable:`CMAKE_CXX_STANDARD`
  273. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  274. * :variable:`CMAKE_CXX_EXTENSIONS`
  275. * :variable:`CMAKE_OBJC_STANDARD`
  276. * :variable:`CMAKE_OBJC_STANDARD_REQUIRED`
  277. * :variable:`CMAKE_OBJC_EXTENSIONS`
  278. * :variable:`CMAKE_OBJCXX_STANDARD`
  279. * :variable:`CMAKE_OBJCXX_STANDARD_REQUIRED`
  280. * :variable:`CMAKE_OBJCXX_EXTENSIONS`
  281. * :variable:`CMAKE_CUDA_STANDARD`
  282. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  283. * :variable:`CMAKE_CUDA_EXTENSIONS`
  284. Their values are used to set the corresponding target properties in
  285. the generated project (unless overridden by an explicit option).
  286. .. versionchanged:: 3.14
  287. For the :generator:`Green Hills MULTI` generator, the GHS toolset and target
  288. system customization cache variables are also propagated into the test
  289. project.
  290. .. versionadded:: 3.24
  291. The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
  292. set to disable passing platform variables into the test project.
  293. .. versionadded:: 3.25
  294. If :policy:`CMP0141` is set to ``NEW``, one can use
  295. :variable:`CMAKE_MSVC_DEBUG_INFORMATION_FORMAT` to specify the MSVC debug
  296. information format.
  297. See Also
  298. ^^^^^^^^
  299. * :command:`try_run`