起源
首先这是一个Qt*Config.cmake
代码问题。您可以查看这些错误(这与 Windows 不完全一样,但完全相同):
解决方法(简单,无效)
手动链接它们:
add_executable(foo foo.cpp)
target_link_libraries(
foo
Qt5::Widgets
"${_qt5Widgets_install_prefix}/lib/qtpcre.lib"
)
解决方法(复杂,有效)
使用INTERFACE_LINK_LIBRARIES属性(在这种情况下,您不必将它链接到使用 Qt 的每个目标,并且可以继续使用 Qt5::Widgets)。另外,您可以使用生成器表达式在 Debug 和 Release 变体之间切换:
get_target_property(
linked_libs
Qt5::Widgets
INTERFACE_LINK_LIBRARIES
)
set(debug "${_qt5Widgets_install_prefix}/lib/qtpcred.lib")
set(nondebug "${_qt5Widgets_install_prefix}/lib/qtpcre.lib")
set(debug_gen_expr "$<$<CONFIG:Debug>:${debug}>")
set(nondebug_gen_expr "$<$<NOT:$<CONFIG:Debug>>:${release}>")
set(gen_expr "${debug_gen_expr};${nondebug_gen_expr}")
set_target_properties(
Qt5::Widgets
PROPERTIES
INTERFACE_LINK_LIBRARIES "${gen_expr};${linked_libs}"
)
用法:
add_executable(foo foo.cpp)
add_executable(boo boo.cpp)
# no need to link qtpcred.lib manually
target_link_libraries(foo PUBLIC Qt5::Widgets)
# will be linked to target `boo` automatically too
target_link_libraries(boo PUBLIC Qt5::Widgets)
猎人
代码取自Qt5Widgets_HunterPlugin模块,该模块安装在附近Qt5WidgetsConfig.cmake
并由它自动加载。其他平台也可以找到相同的解决方法(iOS、OSX、Linux、Visual Studio、MinGW)。