如何使用 CMake 生成 Windows DLL 版本信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6693100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Generate Windows DLL versioning information with CMake
提问by lellobot
I'm using CMake to build a shared library, however for the Windows DLL I need the versioning information, like:
我正在使用 CMake 构建一个共享库,但是对于 Windows DLL,我需要版本信息,例如:
- FileDescription
- FileVersion
- InternalName
- LegalCopyright
- OriginalFilename
- ProductName
- ProductVersion
- 文件描述
- 文件版本
- 内部名称
- 法律版权
- 原始文件名
- 产品名称
- 产品版本
So far, all I have are the VERSION and SOVERSION properties, but these don't seem to correlate to the FileVersion information I was expecting.
到目前为止,我所拥有的只是 VERSION 和 SOVERSION 属性,但这些似乎与我期望的 FileVersion 信息无关。
set(LIC_TARGET MySharedLib)
add_library(${LIC_TARGET} SHARED ${SOURCES} )
SET_TARGET_PROPERTIES(${LIC_TARGET}
PROPERTIES
VERSION ${MY_PRODUCT_NUMBER}.${MY_PRODUCT_VERSION}.${MY_BUILD_NUMBER}
SOVERSION ${MY_PRODUCT_NUMBER})
I've found manual methods(see example at the bottom) but would prefer to contain this within CMake.
我找到了手动方法(请参阅底部的示例),但更愿意将其包含在 CMake 中。
Help?
帮助?
采纳答案by DLRdave
You could use your CMake variable values in conjunction with a version.rc.infile and the configure_filecommand.
您可以将 CMake 变量值与version.rc.in文件和configure_file命令结合使用。
// version.rc.in
#define VER_FILEVERSION @MY_PRODUCT_NUMBER@,@MY_PRODUCT_VERSION@,@MY_BUILD_NUMBER@,0
#define VER_FILEVERSION_STR "@MY_PRODUCT_NUMBER@.@MY_PRODUCT_VERSION@.@[email protected]# CMakeLists.txt
set(MY_PRODUCT_NUMBER 3)
set(MY_PRODUCT_VERSION 5)
set(MY_BUILD_NUMBER 49)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in
${CMAKE_CURRENT_BINARY_DIR}/version.rc
@ONLY)
set(LIC_TARGET MySharedLib)
add_library(${LIC_TARGET} SHARED ${SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/version.rc)
# Alternatively you could simply include version.rc in another rc file
# if there already is one in one of the files in ${SOURCES}
"
#define VER_PRODUCTVERSION @MY_PRODUCT_NUMBER@,@MY_PRODUCT_VERSION@,@MY_BUILD_NUMBER@,0
#define VER_PRODUCTVERSION_STR "@MY_PRODUCT_NUMBER@.@MY_PRODUCT_VERSION@.@MY_BUILD_NUMBER@set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
"
//
// ...along with the rest of the file from your "manual methods" reference
And then, in your CMakeLists.txt file:
然后,在您的 CMakeLists.txt 文件中:
include(generate_product_version)
generate_product_version(
VersionFilesOutputVariable
NAME "My Great Project"
ICON ${PATH_TO_APPLICATION_ICON}
VERSION_MAJOR 1
VERSION_MINOR 3
VERSION_PATCH ${BUILD_COUNTER}
VERSION_REVISION ${BUILD_REVISION}
)
回答by hal
I'm had same problem and have automated version generation for my projects. You need three files from github:
我遇到了同样的问题,并且为我的项目自动生成了版本。你需要来自 github 的三个文件:
Put it in cmake subdirectory of your project and make sure to include it to CMAKE_MODULE_PATH like:
将其放在项目的 cmake 子目录中,并确保将其包含到 CMAKE_MODULE_PATH 中,例如:
add_executable(MyGreatProject ${your-target-sources} ${VersionFilesOutputVariable})
Then before add_executable() or add_library(SHARED) your target, use:
然后在 add_executable() 或 add_library(SHARED) 您的目标之前,使用:
#CMakeLists.txt
add_definitions(-DVER_COMPANYNAME_STR="MyCompany")
add_definitions(-DVER_FILEVERSION_STR="1,1,0.0")
# ...
# add all the other defines here
set(LIC_TARGET MySharedLib)
add_library(${LIC_TARGET} SHARED ${SOURCES} version.rc)
Full list of supported resource strings see in generate_product_version.cmake.
支持的资源字符串的完整列表参见generate_product_version.cmake。
VersionInfo.h and VersionResource.rc will be generated to cmake binaries folder. Variable VersionFilesOutputVariable will hold paths to these files. Just add this list to your target:
VersionInfo.h 和 VersionResource.rc 将生成到 cmake binaries 文件夹中。变量 VersionFilesOutputVariable 将保存这些文件的路径。只需将此列表添加到您的目标:
##代码##UPDATE: Corrected generate_product_version script parameters from VERSION_PATH to VERSION_PATCH.
更新:将 generate_product_version 脚本参数从 VERSION_PATH 更正为 VERSION_PATCH。
回答by Alf
There is an even easier way than the accepted answer. It does not involve transforming an input resource.rc.in. Simply create a generic version.rc file as described hereand then from your CMakeLists.txt do this:
有一种比公认的答案更简单的方法。它不涉及转换输入 resource.rc.in。只需按照此处所述创建一个通用 version.rc 文件,然后从您的 CMakeLists.txt 执行以下操作:
##代码##This has the added benefit that the defines are accessible from your source code as well, so you have programmatic access to your version.
这还有一个额外的好处,即也可以从您的源代码访问定义,因此您可以通过编程访问您的版本。