简体   繁体   English

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

[英]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: 我的CMakeLists.txt看起来像这样:

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将显示错误:

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. 查看生成的LibConfig.cmake,除了它不携带版本信息外,没有什么错。

To allow CMake determining the version of your library, you need to create a LibConfigVersion.cmake file. 为了允许CMake确定库的版本,您需要创建一个LibConfigVersion.cmake文件。

An example can be found in the CMake documentation: generating package files 可以在CMake文档中找到一个示例: 生成软件包文件

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命令中设置一次,而不是多次提供版本信息:

project(Lib VERSION 0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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