问题集锦:使用CMake部署Qt应用程序:set_target_properties、get_target_property

357 阅读1分钟

组合使用的例子

# 给myTarget增加prop1属性,值为p1
set_target_properties(myTarget PROPERTIES prop1 p1 prop2 p2)
# 获取myTaraget的prop1属性,并赋值给ret
get_target_property(ret myTarget prop1)
message(${ret}) # 返回p1
get_target_property(windeployqt Qt5::qmake IMPORTED_LOCATION)

target的名字就是Qt5::qmake,不要被::迷惑,变量名字允许使用::

定义Qt5::qmake的地方

  • Qt\5.15.2\msvc2019\lib\cmake\Qt5Core\Qt5CoreConfigExtras.cmake
if(NOT DEFINED QT_DEFAULT_MAJOR_VERSION)
    set(QT_DEFAULT_MAJOR_VERSION 5)
endif()

if (NOT TARGET Qt5::qmake)
    add_executable(Qt5::qmake IMPORTED)

    set(imported_location "${_qt5Core_install_prefix}/bin/qmake.exe")
    _qt5_Core_check_file_exists(${imported_location})

    set_target_properties(Qt5::qmake PROPERTIES
        IMPORTED_LOCATION ${imported_location}
    )
endif()

这里就能看到IMPORTED_LOCATION的逻辑,我们看到是和_qt5Core_install_prefix拼接的,其实这个变量我们也能直接拿来用,但是是以_开头,一般习惯都认为是个私有变量,还是不使用为好

获取目录

get_filename_component