CheckLibraryExists — CMake 4.1.4 Documentation

# CheckLibraryExists

This module provides a command to check whether a C library exists.

Load this module in a CMake project with:

    include(CheckLibraryExists)

## Commands

This module provides the following command:

check_library_exists

  Checks once whether a specified library exists and a given C function is available:

      check_library_exists(<library> <function> <location> <variable>)

  This command attempts to link a test executable that uses the specified C `<function>` to verify that it is provided by either a system or user-provided `<library>`.

  The arguments are:

  `<library>`

    The name of the library, a full path to a library file, or an Imported Target.

  `<function>`

    The name of a function that should be available in the system or user-provided library `<library>`.

  `<location>`

    The directory containing the library file. It is added to the link search path during the check. If this is an empty string, only the default library search paths are used.

  `<variable>`

    The name of the variable in which to store the check result. This variable will be created as an internal cache variable.

  Note

  This command is intended for performing basic sanity checks to verify that a library provides the expected functionality, or that the correct library is being located. However, it only verifies that a function symbol can be linked successfully - it does not ensure that the function is declared in library headers, nor can it detect functions that are inlined or defined as preprocessor macros. For more robust detection of function availability, prefer using `CheckSymbolExists` or `CheckSourceCompiles`.

  Variables Affecting the Check

  The following variables may be set before calling this command to modify the way the check is run:

  `CMAKE_REQUIRED_FLAGS`

    A space-separated string of additional flags to pass to the compiler. A semicolon-separated list will not work. The contents of `CMAKE_<LANG>_FLAGS` and its associated configuration-specific `CMAKE_<LANG>_FLAGS_<CONFIG>` variables are automatically prepended to the compiler command before the contents of this variable.

  `CMAKE_REQUIRED_DEFINITIONS`

    A semicolon-separated list of compiler definitions, each of the form `-DFOO` or `-DFOO=bar`. A definition for the name specified by the result variable argument of the check command is also added automatically.

  `CMAKE_REQUIRED_LINK_OPTIONS`

    Added in version 3.14.

    A semicolon-separated list of options to add to the link command (see `try_compile()` for further details).

  `CMAKE_REQUIRED_LIBRARIES`

    A semicolon-separated list of libraries to add to the link command. These can be the names of system libraries, or they can be Imported Targets (see `try_compile()` for further details).

  `CMAKE_REQUIRED_LINK_DIRECTORIES`

    Added in version 3.31.

    A semicolon-separated list of library search paths to pass to the linker (see `try_compile()` for further details).

  `CMAKE_REQUIRED_QUIET`

    Added in version 3.1.

    If this variable evaluates to a boolean true value, all status messages associated with the check will be suppressed.

## Examples

Checking if the `curl` library exists in the default paths and has the `curl_easy_perform()` function:

    include(CheckLibraryExists)
    check_library_exists(curl curl_easy_perform "" HAVE_LIBRARY_CURL)

To check if library exists in specific non-standard location and has a specified function:

    include(CheckLibraryExists)
    check_library_exists(curl curl_easy_perform "/opt/curl/lib" HAVE_LIBRARY_CURL)

Also Imported Targets (for example, from the `find_package()` call) can be used:

    find_package(CURL)

    # ...

    if(TARGET CURL::libcurl)
      include(CheckLibraryExists)
      check_library_exists(CURL::libcurl curl_easy_perform "" HAVE_LIBRARY_CURL)
    endif()

## See Also

- The `CheckSymbolExists` module to check whether a C symbol exists.