try_compile.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. try_compile
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Try building some code.
  6. Try Compiling Whole Projects
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. .. code-block:: cmake
  9. try_compile(RESULT_VAR <bindir> <srcdir>
  10. <projectName> [<targetName>] [CMAKE_FLAGS <flags>...]
  11. [OUTPUT_VARIABLE <var>])
  12. Try building a project. The success or failure of the ``try_compile``,
  13. i.e. ``TRUE`` or ``FALSE`` respectively, is returned in ``RESULT_VAR``.
  14. In this form, ``<srcdir>`` should contain a complete CMake project with a
  15. ``CMakeLists.txt`` file and all sources. The ``<bindir>`` and ``<srcdir>``
  16. will not be deleted after this command is run. Specify ``<targetName>`` to
  17. build a specific target instead of the ``all`` or ``ALL_BUILD`` target. See
  18. below for the meaning of other options.
  19. Try Compiling Source Files
  20. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  21. .. code-block:: cmake
  22. try_compile(RESULT_VAR <bindir> <srcfile|SOURCES srcfile...>
  23. [CMAKE_FLAGS <flags>...]
  24. [COMPILE_DEFINITIONS <defs>...]
  25. [LINK_OPTIONS <options>...]
  26. [LINK_LIBRARIES <libs>...]
  27. [OUTPUT_VARIABLE <var>]
  28. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  29. [<LANG>_STANDARD <std>]
  30. [<LANG>_STANDARD_REQUIRED <bool>]
  31. [<LANG>_EXTENSIONS <bool>]
  32. )
  33. Try building an executable from one or more source files. The success or
  34. failure of the ``try_compile``, i.e. ``TRUE`` or ``FALSE`` respectively, is
  35. returned in ``RESULT_VAR``.
  36. In this form the user need only supply one or more source files that include a
  37. definition for ``main``. CMake will create a ``CMakeLists.txt`` file to build
  38. the source(s) as an executable that looks something like this:
  39. .. code-block:: cmake
  40. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  41. include_directories(${INCLUDE_DIRECTORIES})
  42. link_directories(${LINK_DIRECTORIES})
  43. add_executable(cmTryCompileExec <srcfile>...)
  44. target_link_options(cmTryCompileExec PRIVATE <LINK_OPTIONS from caller>)
  45. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  46. The options are:
  47. ``CMAKE_FLAGS <flags>...``
  48. Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
  49. the ``cmake`` command-line used to drive the test build.
  50. The above example shows how values for variables
  51. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  52. are used.
  53. ``COMPILE_DEFINITIONS <defs>...``
  54. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  55. in the generated test project.
  56. ``COPY_FILE <fileName>``
  57. Copy the linked executable to the given ``<fileName>``.
  58. ``COPY_FILE_ERROR <var>``
  59. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  60. message encountered while trying to copy the file.
  61. ``LINK_LIBRARIES <libs>...``
  62. Specify libraries to be linked in the generated project.
  63. The list of libraries may refer to system libraries and to
  64. :ref:`Imported Targets <Imported Targets>` from the calling project.
  65. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  66. given to the ``CMAKE_FLAGS`` option will be ignored.
  67. ``LINK_OPTIONS <options>...``
  68. Specify link step options to pass to :command:`target_link_options` or
  69. to :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
  70. project, depending of the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
  71. ``OUTPUT_VARIABLE <var>``
  72. Store the output from the build process the given variable.
  73. ``<LANG>_STANDARD <std>``
  74. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  75. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  76. ``<LANG>_STANDARD_REQUIRED <bool>``
  77. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  78. :prop_tgt:`CXX_STANDARD_REQUIRED`, or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  79. target property of the generated project.
  80. ``<LANG>_EXTENSIONS <bool>``
  81. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  82. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  83. In this version all files in ``<bindir>/CMakeFiles/CMakeTmp`` will be
  84. cleaned automatically. For debugging, ``--debug-trycompile`` can be
  85. passed to ``cmake`` to avoid this clean. However, multiple sequential
  86. ``try_compile`` operations reuse this single output directory. If you use
  87. ``--debug-trycompile``, you can only debug one ``try_compile`` call at a time.
  88. The recommended procedure is to protect all ``try_compile`` calls in your
  89. project by ``if(NOT DEFINED RESULT_VAR)`` logic, configure with cmake
  90. all the way through once, then delete the cache entry associated with
  91. the try_compile call of interest, and then re-run cmake again with
  92. ``--debug-trycompile``.
  93. Other Behavior Settings
  94. ^^^^^^^^^^^^^^^^^^^^^^^
  95. If set, the following variables are passed in to the generated
  96. try_compile CMakeLists.txt to initialize compile target properties with
  97. default values:
  98. * :variable:`CMAKE_ENABLE_EXPORTS`
  99. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  100. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  101. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  102. If :policy:`CMP0056` is set to ``NEW``, then
  103. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  104. If :policy:`CMP0083` is set to ``NEW``, then in order to obtain correct
  105. behavior at link time, the ``check_pie_supported()`` command from the
  106. :module:`CheckPIESupported` module must be called before using the
  107. :command:`try_compile` command.
  108. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are set in the
  109. generated project.
  110. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  111. a build configuration.
  112. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  113. the type of target used for the source file signature.
  114. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  115. variables that must be propagated into the test project. This variable is
  116. meant for use only in toolchain files.
  117. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  118. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  119. then the language standard variables are honored:
  120. * :variable:`CMAKE_C_STANDARD`
  121. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  122. * :variable:`CMAKE_C_EXTENSIONS`
  123. * :variable:`CMAKE_CXX_STANDARD`
  124. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  125. * :variable:`CMAKE_CXX_EXTENSIONS`
  126. * :variable:`CMAKE_CUDA_STANDARD`
  127. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  128. * :variable:`CMAKE_CUDA_EXTENSIONS`
  129. Their values are used to set the corresponding target properties in
  130. the generated project (unless overridden by an explicit option).
  131. For the :generator:`Green Hills MULTI` generator the GHS toolset and target
  132. system customization cache variables are also propagated into the test project.