【亲测可行】QT实现cmake构建增加输出文件版本信息

步骤1:先准备资源文件VersionInfo.rc.in,放到CmakeLists.txt同一级目录下

# if defined(UNDER_CE)
#  include 
# else
#  include 
# endif
VS_VERSION_INFO VERSIONINFO
 FILEVERSION ${PROJECT_VERSION_MAJOR}, ${PROJECT_VERSION_MINOR}, ${PROJECT_VERSION_PATCH}
 PRODUCTVERSION ${PROJECT_VERSION_MAJOR}, ${PROJECT_VERSION_MINOR}, ${PROJECT_VERSION_PATCH}
 FILEFLAGSMASK 0x17L
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x0L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904b0"
        BEGIN
            VALUE "FileDescription", "MyLibrary Binary"
            VALUE "FileVersion", "${PROJECT_VERSION_MAJOR}, ${PROJECT_VERSION_MINOR}, ${PROJECT_VERSION_PATCH}"
            VALUE "InternalName", "MyLibrary"
            VALUE "LegalCopyright", "Copyright (C) 2019"
            VALUE "OriginalFilename", ""
            VALUE "ProductName", "MyLibrary"
            VALUE "ProductVersion", "${PROJECT_VERSION_MAJOR}, ${PROJECT_VERSION_MINOR}, ${PROJECT_VERSION_PATCH}"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1200
    END
END

步骤2:编写CMakeLists.txt

根据自身实际进行参考,不可无脑拷贝

cmake_minimum_required(VERSION 3.5)

set(CurVersion 6.22.0402.1)
project(QtDeviceMiddleCommonTestTool LANGUAGES CXX VERSION ${CurVersion})


set(OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/output")
set(INCLUDES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/includes")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs")


#..自定义脚本
#版本
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    message(STATUS "${CurVersion}123asd")
    set(MY_VERSIONINFO_RC "${CMAKE_CURRENT_SOURCE_DIR}/VersionInfo.rc")
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/VersionInfo.rc.in" "${MY_VERSIONINFO_RC}")
else()
    message(STATUS "${CurVersion}789hjk")
endif()


#..自定义脚本
#输出增加版本VersionInfo.rc
#${PROJECT_NAME}后记得加自己的源文件列表,add_executable本处已省略自身的头文件和源文件
add_executable("${PROJECT_NAME}" ${MY_VERSIONINFO_RC})


#...自定义脚本
#文件最末尾,Windows通过rc增加版本信息,Linux是增加版本号的软连接方式
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    message(STATUS ${CurVersion})

else()
    SET_TARGET_PROPERTIES("${PROJECT_NAME}${ProductNameSufffix}" PROPERTIES VERSION ${CurVersion} SOVERSION 1)
    message(STATUS ${CurVersion})
endif()

第3部:QT加载CMakelists生成对应工程

右键点击工程--》执行Cmake,会生成VersionInfo.rc,里面的宏也会被替换。

【亲测可行】QT实现cmake构建增加输出文件版本信息_第1张图片构建工程,查看效果

【亲测可行】QT实现cmake构建增加输出文件版本信息_第2张图片

 

 

 

你可能感兴趣的:(cmake,QT,c++)