CMake的导出如何知道目标是哪个版本?

时间:2018-08-06 01:22:42

标签: c++ build cmake

我正试图做一个图书馆。我的CMakeLists.txt看起来像这样:

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

当外部项目使用此库时,CMake显示错误:

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

查看生成的LibConfig.cmake,除了它不包含版本信息外,没有什么错。

1 个答案:

答案 0 :(得分:1)

要允许CMake确定库的版本,您需要创建一个LibConfigVersion.cmake文件。

可以在CMake文档中找到一个示例:generating package files

对于您的图书馆,结果如下:

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)

您可以在project命令中将其设置一次,而不是多次提供版本信息:

project(Lib VERSION 0.1)