CheckCXXSymbolExists.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCXXSymbolExists
  5. --------------------
  6. Check if a symbol exists as a function, variable, or macro in ``C++``.
  7. .. command:: check_cxx_symbol_exists
  8. .. code-block:: cmake
  9. check_cxx_symbol_exists(<symbol> <files> <variable>)
  10. Check that the ``<symbol>`` is available after including given header
  11. ``<files>`` and store the result in a ``<variable>``. Specify the list of
  12. files in one argument as a semicolon-separated list.
  13. ``check_cxx_symbol_exists()`` can be used to check for symbols as seen by
  14. the C++ compiler, as opposed to :command:`check_symbol_exists`, which always
  15. uses the ``C`` compiler.
  16. If the header files define the symbol as a macro it is considered
  17. available and assumed to work. If the header files declare the symbol
  18. as a function or variable then the symbol must also be available for
  19. linking. If the symbol is a type, enum value, or C++ template it will
  20. not be recognized: consider using the :module:`CheckTypeSize`
  21. or :module:`CheckCXXSourceCompiles` module instead.
  22. .. note::
  23. This command is unreliable when ``<symbol>`` is (potentially) an overloaded
  24. function. Since there is no reliable way to predict whether a given function
  25. in the system environment may be defined as an overloaded function or may be
  26. an overloaded function on other systems or will become so in the future, it
  27. is generally advised to use the :module:`CheckCXXSourceCompiles` module for
  28. checking any function symbol (unless somehow you surely know the checked
  29. function is not overloaded on other systems or will not be so in the
  30. future).
  31. The following variables may be set before calling this macro to modify
  32. the way the check is run:
  33. ``CMAKE_REQUIRED_FLAGS``
  34. string of compile command line flags.
  35. ``CMAKE_REQUIRED_DEFINITIONS``
  36. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  37. ``CMAKE_REQUIRED_INCLUDES``
  38. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  39. the compiler.
  40. ``CMAKE_REQUIRED_LINK_OPTIONS``
  41. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  42. ``CMAKE_REQUIRED_LIBRARIES``
  43. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  44. command. See policy :policy:`CMP0075`.
  45. ``CMAKE_REQUIRED_QUIET``
  46. execute quietly without messages.
  47. For example:
  48. .. code-block:: cmake
  49. include(CheckCXXSymbolExists)
  50. # Check for macro SEEK_SET
  51. check_cxx_symbol_exists(SEEK_SET "cstdio" HAVE_SEEK_SET)
  52. # Check for function std::fopen
  53. check_cxx_symbol_exists(std::fopen "cstdio" HAVE_STD_FOPEN)
  54. #]=======================================================================]
  55. include_guard(GLOBAL)
  56. include(CheckSymbolExists)
  57. macro(CHECK_CXX_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  58. __CHECK_SYMBOL_EXISTS_IMPL("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.cxx" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  59. endmacro()