Search code examples
cmakeqt5.5

Qt5.5: How can I include Qt's qtpcre.lib and qtharfbuzzng.lib with CMake?


I've just upgraded one of our projects from Qt 5.3.1 to Qt 5.5. We are using a statically built Qt (we build it ourselves) with the win32-msvc2013 make spec.
After upgrading, I've received a couple of unresolved externals which I could backtrace to the following two libraries which already are in the lib directory after building Qt:

qtpcre(d).lib: Qt wrapper around the Perl Compatible Regular Expressions library
qtharfbuzzng(d).lib: Qt wrapper around the Harf Buzz NG Unicode Text shaping library

Manually adding these libraries (the Qt ones, not the original ones) to my Visual Studio project configuration resolves the linker errors, but our VS projects are generated using CMake (3.0.2 I think).

Is there a way to include these libraries using targets like Qt5::Core or something similar to the Qt Plugins? Or do I have to add them manually using FIND_LIBRARY or similar? So far I haven't found any CMake scripts related to those two libraries.


Solution

  • Origin

    First of all it's a Qt *Config.cmake code issue. You can take a look at these bugs (it's not about Windows exactly but quite the same):

    Workaround (simple, not effective)

    Link them manually:

    add_executable(foo foo.cpp)
    target_link_libraries(
        foo
        Qt5::Widgets
        "${_qt5Widgets_install_prefix}/lib/qtpcre.lib"
    )
    

    Workaround (complex, effective)

    Use INTERFACE_LINK_LIBRARIES property (in this case you don't have to link it to every target that use Qt and can keep using just Qt5::Widgets). Plus you can use generator expressions to switch between Debug and Release variants:

    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}"
    )
    

    Usage:

    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)
    

    Hunter

    Code is taken from Qt5Widgets_HunterPlugin module which is installed near the Qt5WidgetsConfig.cmake and loaded by it automatically. Same kind of workarounds for other platforms can be found there too (iOS, OSX, Linux, Visual Studio, MinGW).