在CMake中安装时如何使用版本号重命名/修改目标文件?

时间:2019-07-16 11:07:44

标签: cmake install version lib kdevelop

我正在使用CMake-3.13.4和KDevelop-5.2.1。

我有一个最顶部的CMakeLists.txt,它定义了目标的版本号。 看起来像:

// MARK: - Empty
struct Root: Codable {
    let attendence: [Attendence]
}

// MARK: - Attendence
struct Attendence: Codable {
    let name, date, subject:String
    let status: Int


    enum CodingKeys: String, CodingKey {
        case name = "student__first_name"
        case subject = "subject__name"
        case date = "attendance_date"
        case status
    }

}

我希望CMake自动将版本字符串附加到目标的文件名。所以我的代码如下:

set( PROJECT_VERSION_MAJOR 1 )
set( PROJECT_VERSION_MINOR 4 )
set( PROJECT_VERSION_PATCH 7 )
...
add_executable( mytarget main.cpp XXX.cpp ... )
target_link_libraries( mytarget "stdc++fs" ${CMAKE_THREAD_LIBS_INIT} ... )
install( TARGETS mytarget RUNTIME DESTINATION . )

但这不起作用。

有没有可以用CMake完成的方法? 我最后想要的文件名为“ mytarget-1.4.7”。

2 个答案:

答案 0 :(得分:1)

您正在寻找属性OUTPUT_NAME

add_executable( mytarget main.cpp XXX.cpp ... )
target_link_libraries( mytarget "stdc++fs" ${CMAKE_THREAD_LIBS_INIT} ... )
set_target_properties( mytarget PROPERTIES OUTPUT_NAME "mytarget-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" ) 
install( TARGETS mytarget RUNTIME DESTINATION . )

答案 1 :(得分:0)

我通过修改以下代码获得了它:

add_executable( mytarget-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} 
 main.cpp XXX.cpp ... )

target_link_libraries( mytarget-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} "stdc++fs" ${CMAKE_THREAD_LIBS_INIT} ... )

install( TARGETS mytarget-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} RUNTIME DESTINATION . )