简体   繁体   中英

How does CMake's export know what version the target is?

I am trying to make a library. My CMakeLists.txt looks something like this:

add_library(Lib STATIC)
set_target_properties(Lib PROPERTIES
    VERSION 0.1)
export(TARGETS Lib
    FILE ${BIN_DIR}/LibConfig.cmake)

When an outside project uses this library, CMake shows the error:

CMake Error at CMakeLists.txt:10 (find_package):
  Could not find a configuration file for package "Lib" that is
  compatible with requested version "0.1".

  The following configuration files were considered but not accepted:

some directory/Lib/bin/LibConfig.cmake, version: unknown

Looking at the generated LibConfig.cmake, nothing is wrong except that it doesn't carry the version information.

To allow CMake determining the version of your library, you need to create a LibConfigVersion.cmake file.

An example can be found in the CMake documentation: generating package files

For your library, this results in something like this:

add_library(Lib STATIC)
set_target_properties(Lib PROPERTIES VERSION 0.1)
export(TARGETS LibFILE ${BIN_DIR}/LibConfig.cmake)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(${BIN_DIR}/LibConfigVersion.cmake
                                 VERSION 0.1
                                 COMPATIBILITY SameMajorVersion)

Instead of providing the version information multiple times, you may set it once in the project command:

project(Lib VERSION 0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM