1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Check if a C function can be linked
10 .. command:: check_function_exists
14 check_function_exists(<function> <variable>)
16 Checks that the ``<function>`` is provided by libraries on the system and store
17 the result in a ``<variable>``, which will be created as an internal
20 The following variables may be set before calling this macro to modify the
23 .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
25 .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
27 .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
29 .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
31 .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
33 .. include:: /module/CMAKE_REQUIRED_QUIET.txt
37 Prefer using :Module:`CheckSymbolExists` instead of this module,
38 for the following reasons:
40 * ``check_function_exists()`` can't detect functions that are inlined
41 in headers or specified as a macro.
43 * ``check_function_exists()`` can't detect anything in the 32-bit
44 versions of the Win32 API, because of a mismatch in calling conventions.
46 * ``check_function_exists()`` only verifies linking, it does not verify
47 that the function is declared in system headers.
48 #]=======================================================================]
52 macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
53 if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
54 set(MACRO_CHECK_FUNCTION_DEFINITIONS
55 "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
56 if(NOT CMAKE_REQUIRED_QUIET)
57 message(CHECK_START "Looking for ${FUNCTION}")
59 if(CMAKE_REQUIRED_LINK_OPTIONS)
60 set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
61 LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
63 set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
65 if(CMAKE_REQUIRED_LIBRARIES)
66 set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
67 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
69 set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
71 if(CMAKE_REQUIRED_INCLUDES)
72 set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
73 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
75 set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
78 if(CMAKE_C_COMPILER_LOADED)
79 set(_cfe_source CheckFunctionExists.c)
80 elseif(CMAKE_CXX_COMPILER_LOADED)
81 set(_cfe_source CheckFunctionExists.cxx)
83 message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
86 try_compile(${VARIABLE}
87 SOURCE_FROM_FILE "${_cfe_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
88 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
89 ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
90 ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
91 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
92 "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
97 set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
98 if(NOT CMAKE_REQUIRED_QUIET)
99 message(CHECK_PASS "found")
102 if(NOT CMAKE_REQUIRED_QUIET)
103 message(CHECK_FAIL "not found")
105 set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")