Cmake Find_library

CMAKEFINDLIBRARYPREFIXES ¶ Prefixes to prepend when looking for libraries. This specifies what prefixes to add to library names when the findlibrary command looks for libraries. On UNIX systems this is typically lib, meaning that when trying to find the foo library it will look for libfoo. You can add different directories to findlibrary. To use this library call cmake by cmake -DFOOPREFIX=/some/path. CMake is a cross-platform, open-source build system. CMake is part of a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. This is the CMake build script that Android Studio generates for your Hello-cmake project; CMakelist.txt directs compiler and linker to produce libnative-lib.so from your C/C source code. LibNatiove-lib.so is loaded by your project's Java code at run time. Semicolon-separated list of directories specifying a search path for the findlibrary command. By default it is empty, it is intended to be set by the project.

The qibuild CMake framework contains several -config.cmake fileswhen upstream Find-.cmake files are not correct or missing.

For instance, the canonical FindFoo.cmake looks like:

There is a lot of boilerplate code here, and it’s not easyto know the names of the variables (is it FOO_INCLUDE_DIR or FOO_INCLUDE_DIRS ?)

For qibuild, we use the concept of PREFIX and exported variables will alwaysbe ${${PREFIX}_INCLUDE_DIRS} and ${${PREFIX}_LIBRARIES}

Thus, foo-config.cmake can simply be rewritten as:

Note that the exported variables will always beall upper-case, will contain no version number and will havethe plural form.

  • FOO_INCLUDE_DIRS
  • FOO_LIBRARIES

Also note that FOO_LIBRARIES will equal either:

if flib() was called with no specific argumentor:

if both flib(FOOOPTIMIZED...) and flib(FOODEBUG...)have been called.

So this variable can be used directly in the target_link_libraries() call

clean¶

Arguments:
  • prefix – The prefix of the variables to clean

Cleanup variables related to a library/executable/source-only libraryUse this at the start of the ${prefix}-config.cmake file

fpath¶

Arguments:
  • prefix – The prefix of the exported variables. Must match the argument of clean() and export_lib() (or export_header for a header-only library) calls.
  • path – The path of one of the headers inside the include directory.
  • args (remaining) – The remaining arguments will be passed to find_path

Search for an include directory

A small example, assuming /usr/local/include/foo/foo.hexists.

If you use:

FOO_INCLUDE_DIRS will equal /usr/local/include, so you willhave to use

Whereas if you use

FOO_INCLUDE_DIRS will equal usr/local/include/foo, so youwill have to use

flib¶

Arguments:
  • prefix – The prefix of the exported variables. Must match the argument of clean() and export() calls.
  • name – The name of the library
  • DEBUG – find a library that will be used for a debug build
  • OPTIMIZED – find a library that will be used for an optimized build
  • args (remaining) – The remaining arguments will be passed to find_library

Search for a library

Cmake find_library debug

If the library has a different name in debug and in release,you should use:

If the library has different names, you should call flib() just once

If you want to link with several libraries in one step (for instancefoo-bar depends on foo-core but you just want to doqi_use_lib(..FOO), you must call flib() several times:

fprogram¶

Arguments:
  • prefix – Prefix of the variables to export. Must match the calls to clean() and export_bin() calls.

Search for an executable

export_lib¶

Arguments:
  • prefix – The prefix of the exported variables

Export the variables related to a library

Use this at the end of the ${prefix}-config.cmakefind_package_handle_standard_args will be called to makesure ${prefix}_LIBRARIES and ${prefix}_INCLUDE_DIRS havebeen found.

export_lib_pkgconfig¶

Helper function to use with pkgconfig.

Usage, assuming foo-1.0.pc is somewherein PKG_CONFIG_PATH

export_bin¶

Arguments:
  • prefix – The prefix of the variables to export

Export the variables related to an executable

Use at the end of foo-config.cmake

Here, FOO_EXECUTABLE will be set to‘/path/to/foo.exe’, and FOO_EXECUTABLE_DEBUGto ‘path/to/foo_d.exe’

export_header¶

Arguments:
  • prefix – The prefix of the variables to export

Export the variables related to an header-onlylibrary

Use at the end of foo-config.cmake

This is the reference page for cmake_find_package generator.Go to Integrations/CMake if you want to learn how to integrate your project or recipes with CMake.

The cmake_find_package generator creates a file for each requirement specified in the conanfile.

The name of the files follow the pattern Find<PKG-NAME>.cmake. So for the asio/1.14.0 package,a Findasio.cmake file will be generated.

Cmake Specify Library Location

Variables in Find<PKG-NAME>.cmake¶

Cmake Find_library Usage

Being <PKG-NAME> the package name used in the reference (by default) or the one declared in cpp_info.name or incpp_info.names['cmake_find_package'] if specified:

NAMEVALUE
<PKG-NAME>_FOUNDSet to 1
<PKG-NAME>_VERSIONPackage version
<PKG-NAME>_INCLUDE_DIRSContaining all the include directories of the package
<PKG-NAME>_INCLUDESSame as the XXX_INCLUDE_DIRS
<PKG-NAME>_DEFINITIONSDefinitions of the library
<PKG-NAME>_LIBSLibrary paths to link
<PKG-NAME>_LIBRARIESSame as <PKG-NAME>_LIBS
<PKG-NAME>_BUILD_MODULESList of CMake module files with functionalities for consumers
<PKG-NAME>_SYSTEM_LIBSSystem libraries to link
<PKG-NAME>_FRAMEWORKSFramework names to do a find_library()
<PKG-NAME>_FRAMEWORKS_FOUNDFound frameworks to link with after find_library()
<PKG-NAME>_FRAMEWORK_DIRSFramework directories to perform the find_library() of <PKG-NAME>_FRAMEWORKS

This file uses <PKG-NAME>_BUILD_MODULES values to include the files using the include(…) CMake directive after the targets arecreated. This makes functions or utilities exported by the package available for consumers just by setting find_package(<PKG-NAME>) in theCMakeLists.txt.

Moreover, this also adjusts CMAKE_MODULE_PATH and CMAKE_PREFIX_PATH to the values declared by the package in cpp_info.buildirs, somodules in those directories can be found.

Targets in Find<PKG-NAME>.cmake¶

A target named <PKG-NAME>::<PKG-NAME> target is generated with the following properties adjusted:

  • INTERFACE_INCLUDE_DIRECTORIES: Containing all the include directories of the package.
  • INTERFACE_LINK_LIBRARIES: Library paths to link.
  • INTERFACE_COMPILE_DEFINITIONS: Definitions of the library.

The targets are transitive. So, if your project depends on a packages A and B, and at the same timeA depends on C, the A target will contain automatically the properties of the C dependency, soin your CMakeLists.txt file you only need to find_package(A) and find_package(B).

Components¶

If a recipe uses components, the targets generated will be <PKG-NAME>::<COMP-NAME> with the following properties adjusted:

  • INTERFACE_INCLUDE_DIRECTORIES: Containing all the include directories of the component.
  • INTERFACE_LINK_DIRECTORIES: Containing all the lib directories of the component.
  • INTERFACE_LINK_LIBRARIES: Containing the targets to link the component to (includes component’s libraries and dependencies).
  • INTERFACE_COMPILE_DEFINITIONS: Containing the definitions of the component.
  • INTERFACE_COMPILE_OPTIONS: Containing the compile options of the component.

Moreover, a global target <PKG-NAME>::<PKG-NAME> will be declared with the following properties adjusted:

  • INTERFACE_LINK_LIBRARIES: Containing all the component targets to link the global target to (includes package’s components only).

Important

Name conflicts: If the name of the global target is the same for different packages, Conan will aggregate into this global targetall the components from all those different packages. This means that this global target will contain information coming from differentpackages. For the components themselves, a name conflict will result in one of them being inaccessible without further notice.