try_compile.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. try_compile
  2. -----------
  3. Try building some code.
  4. ::
  5. try_compile(RESULT_VAR <bindir> <srcdir>
  6. <projectName> [targetName] [CMAKE_FLAGS flags...]
  7. [OUTPUT_VARIABLE <var>])
  8. Try building a project. In this form, srcdir should contain a
  9. complete CMake project with a CMakeLists.txt file and all sources.
  10. The bindir and srcdir will not be deleted after this command is run.
  11. Specify targetName to build a specific target instead of the 'all' or
  12. 'ALL_BUILD' target.
  13. ::
  14. try_compile(RESULT_VAR <bindir> <srcfile|SOURCES srcfile...>
  15. [CMAKE_FLAGS flags...]
  16. [COMPILE_DEFINITIONS flags...]
  17. [LINK_LIBRARIES libs...]
  18. [OUTPUT_VARIABLE <var>]
  19. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]])
  20. Try building an executable from one or more source files. In this
  21. form the user need only supply one or more source files that include a
  22. definition for 'main'. CMake will create a CMakeLists.txt file to
  23. build the source(s) as an executable. Specify COPY_FILE to get a copy
  24. of the linked executable at the given fileName and optionally
  25. COPY_FILE_ERROR to capture any error.
  26. In this version all files in bindir/CMakeFiles/CMakeTmp will be
  27. cleaned automatically. For debugging, --debug-trycompile can be
  28. passed to cmake to avoid this clean. However, multiple sequential
  29. try_compile operations reuse this single output directory. If you use
  30. --debug-trycompile, you can only debug one try_compile call at a time.
  31. The recommended procedure is to protect all try_compile calls in your
  32. project by ``if(NOT DEFINED RESULT_VAR)`` logic, configure with cmake
  33. all the way through once, then delete the cache entry associated with
  34. the try_compile call of interest, and then re-run cmake again with
  35. --debug-trycompile.
  36. Some extra flags that can be included are, INCLUDE_DIRECTORIES,
  37. LINK_DIRECTORIES, and LINK_LIBRARIES. COMPILE_DEFINITIONS are
  38. -Ddefinition that will be passed to the compile line.
  39. The srcfile signature also accepts a LINK_LIBRARIES argument which may
  40. contain a list of libraries or IMPORTED targets which will be linked
  41. to in the generated project. If LINK_LIBRARIES is specified as a
  42. parameter to try_compile, then any LINK_LIBRARIES passed as
  43. CMAKE_FLAGS will be ignored.
  44. try_compile creates a CMakeList.txt file on the fly that looks like
  45. this:
  46. ::
  47. add_definitions( <expanded COMPILE_DEFINITIONS from calling cmake>)
  48. include_directories(${INCLUDE_DIRECTORIES})
  49. link_directories(${LINK_DIRECTORIES})
  50. add_executable(cmTryCompileExec sources)
  51. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  52. In both versions of the command, if OUTPUT_VARIABLE is specified, then
  53. the output from the build process is stored in the given variable.
  54. The success or failure of the try_compile, i.e. TRUE or FALSE
  55. respectively, is returned in RESULT_VAR. CMAKE_FLAGS can be used to
  56. pass -DVAR:TYPE=VALUE flags to the cmake that is run during the build.
  57. Set variable CMAKE_TRY_COMPILE_CONFIGURATION to choose a build
  58. configuration.