如何 rename/revise 在 CMake 中安装带有版本号的目标文件?

How to rename/revise target file with version number on installing in CMake?

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

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

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 自动将版本字符串附加到目标的文件名。所以我编码如下:

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

但是没用。

有没有办法用 CMake 来完成? 我最终要的文件名是"mytarget-1.4.7".

我是通过修改代码得到的,如下:

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 . )

您正在寻找 属性 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 . )