QTP0003
创建 Qt 库时需考虑 BUILD_SHARED_LIBS 的值。
该策略自 Qt 6.7 起引入。该策略会影响使用Qt CMake API(如qt_add_library()、qt_add_plugin()、qt_add_qml_module())创建的库的默认类型。
如果将该策略设置为OLD ,则选定的默认库类型将与 Qt 构建类型保持一致,即shared 或static 。
如果策略设置为NEW ,则根据BUILD_SHARED_LIBS的值(如果已设置)来选择库类型。如果未设置BUILD_SHARED_LIBS ,则默认库类型将回退到 Qt 构建类型。
例如,尽管BUILD_SHARED_LIBS 已设置为ON ,但以下代码仍会将 Qt 构建类型作为MyLib 目标的默认库类型:
set(BUILD_SHARED_LIBS ON)
...
qt_add_library(MyLib sourcefile.h sourcefile.cpp)如果您在调用qt_add_library()之前将 QTP0003 设置为NEW ,则BUILD_SHARED_LIBS 将影响库的默认类型,此时共享库将采用MyLib 。
set(BUILD_SHARED_LIBS ON)
...
qt_policy(SET QTP0003 NEW)
qt_add_library(MyLib sourcefile.h sourcefile.cpp)© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.