2017-04-15 113 views
0

我想为CMakeLists文件中的项目添加生成后事件。 此后生成事件必须将Qt库放在我的可执行文件附近。 我用add_custom_command做到这一点:通过CMake生成共享库名称

set(libraryFileName ${QtDir}/bin/${packageName}.dll) 

# Copy qt library after build 
add_custom_command(
    TARGET ${target} POST_BUILD    #Path to cmake executable file 
    COMMAND "${CMAKE_COMMAND}" -E   #CMake in command mode 
     copy        #Copy command 
     "${libraryFileName}"    #Path to the file 
     "$<TARGET_FILE_DIR:${target}>"  #Where to copy 
    COMMENT "Copying to output directory") 

的主要问题是如何正确生成libraryFileName每一个系统?我的意思是,我的灵魂工作适用于Windows,但对其他系统类型而言,我猜它会失败。有什么办法可以获得共享库的扩展而不是硬编码吗?

回答

0

我找到了解决方案。但它可能只适用于Qt。

# Get package location 
get_target_property(location ${qtVersion}::${shortPackageName} LOCATION) 

# Copy qt library after build 
add_custom_command(
    TARGET ${target} POST_BUILD    #Path to cmake executable file 
    COMMAND "${CMAKE_COMMAND}" -E   #CMake in command mode 
     copy        #Copy command 
     "${location}"      #Path to the file 
     "$<TARGET_FILE_DIR:${target}>"  #Where to copy 
    COMMENT "Copying ${packageName}...") 
+0

难道你不能只使用'$ '作为'copy'的源位置吗? – Florian

+0

只记得还有一个[策略CMP0026](https://cmake.org/cmake/help/latest/policy/CMP0026.html),它将“禁止使用LOCATION属性构建目标”,并且会给出CMake新版本中的警告/错误。 – Florian

0

您可以添加平台检查器并根据该检查设置变量。

message(STATUS "Current CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}") 

if("${CMAKE_SYSTEM_NAME}" STREQUAL "WIN98") 
    set(libraryFileName ${QtDir}/bin/${packageName}.dll) 
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "LINUX") 
    set(libraryFileName ${QtDir}/bin/${packageName}.so) 
else 
    message(ERROR "The system ${CMAKE_SYSTEM_NAME} is not supported.") 
endif() 
+0

如何使用['CMAKE_SHARED_LIBRARY_SUFFIX'](https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LIBRARY_SUFFIX.html)获得更通用的方法? – Florian