try_compile.rst 5.7 KB

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