cmakeqt5

Cmake Qt5 | undefined reference to `QPrinter::QPrinter(QPrinter::PrinterMode)


I am preparing cmake build for qt application. where I am using following structure ..

libMyApp which uses

SET(QT5_MODULES Widgets PrintSupport Network XmlPatterns)
FIND_PACKAGE(Qt5 REQUIRED COMPONENTS ${QT5_MODULES})

and

TARGET_LINK_LIBRARIES(
    ${TARGET_NAME}
    Qt5::Widgets
    Qt5::PrintSupport 
    Qt5::Network
    Qt5::XmlPatterns
)

libMyApp build successfully and generated libMyApp.a (static library)

Now I am using this library in my application MyApp which uses

SET(QT5_MODULES Widgets PrintSupport XmlPatterns)
FIND_PACKAGE(Qt5 REQUIRED COMPONENTS ${QT5_MODULES})

TARGET_LINK_LIBRARIES(
    ${TARGET_NAME}
    Qt5::Widgets
    Qt5::PrintSupport 
    Qt5::XmlPatterns
    ${CODE_LIB_FILES}
)

${CODE_LIB_FILES} is list holding path of libMyApp.a MyApp builds successfully and at last shows linking error

undefined reference to `QPrinter::QPrinter(QPrinter::PrinterMode)

Same thing happening with xml also

undefined reference to `QDomNode::isElement() const'

can anyone point out what is wrong here ?

or any sample program depicting same scenario with printsupport and xmlpatterns module


Solution

  • The order of the entries in the function TARGET_LINK_LIBRARIES() is important. The libraries with no dependecies shall be mentioned last which are typically some standard libraries or external libraries, in this example the Qt5 Libs.

    An example:

    Then the call of the function shall be the following:

    TARGET_LINK_LIBRARIES(
        ${TARGET_NAME}   # Name of the app
        "Lib_A"
        "Lib_B"
        "Lib_std"        # Last entries: Std Libs, external Libs, ...
    )
    

    In this application I assumed that ${CODE_LIB_FILES}=libMyApp.a has some dependencies to the Qt5-Libs so it would be plausible to move this entry above the Qt5-Libs.

    SET(QT5_MODULES Widgets PrintSupport XmlPatterns)
    FIND_PACKAGE(Qt5 REQUIRED COMPONENTS ${QT5_MODULES})
    
    TARGET_LINK_LIBRARIES(
        ${TARGET_NAME}
        ${CODE_LIB_FILES}       # <<< Moved this entry up
        Qt5::Widgets
        Qt5::PrintSupport 
        Qt5::XmlPatterns
    )