0

I'm trying to compile a project utilizing QT and LibXMl++. I'm having trouble messing around with the cmake file. I'm getting undefined references to libxml++ while compiling my code.

Here is the snippet in the cmake file pertaining to libXML++:

#libxml++
find_package (PkgConfig REQUIRED)
find_package (LibXml2 REQUIRED)
pkg_check_modules (LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-5.0)
target_link_libraries(PkgConfig::LIBXMLXX)
link_directories("/usr/lib64/")
include_directories("/usr/include/libxml++-5.0/")

The libXML++ source is located at: /usr/include/libxml++-5.0/libxml++, and I see the .so file located at /usr/lib64/libxml++5.0.so

Main.cpp:

#include "downloader.h"
#include <libxml++/libxml++.h>
#include <iostream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    try {
        xmlpp::DomParser parser;
        parser.set_validate();

    }
    catch(const std::exception& ex) {
        std::cerr << "Exception caught: " << ex.what() << std::endl;
        return EXIT_FAILURE;
    }

    Downloader d;
    d.doDownload();

    return a.exec();
}

Compiler output:

main.cpp:11: error: undefined reference to `xmlpp::DomParser::DomParser()'
main.cpp:12: undefined reference to `xmlpp::Parser::set_validate(bool)'
main.cpp:14: undefined reference to `xmlpp::DomParser::~DomParser()'
collect2: error: ld returned 1 exit status

Compiled in QT Creator 9.0.2

wohlstad
  • 12,661
  • 10
  • 26
  • 39
ryan714
  • 25
  • 6
  • Your `target_link_libraries` is wrong, probably needs to be `target_link_libraries(myexe,PkgConfig::LIBXMLXX)`, see e.g. [this](https://stackoverflow.com/questions/63749077/c-cmake-cant-find-libxml). Also, you should **not** use explicit paths, `pkg_check_modules ` finds them, see e.g. [this](https://stackoverflow.com/questions/70273010/include-pkg-config-cflags-libs-gtk-2-0-in-cmake). – n. m. could be an AI Mar 30 '23 at 05:23
  • ``target_link_libraries(qtProject, PkgConfig::LIBXMLXX)`` gets me ``error: Cannot specify link libraries for target "qtProject," which is not built by this project. `` – ryan714 Mar 30 '23 at 05:40
  • Besides the question but anyway: you are aware of [Qt's XML module](https://doc.qt.io/qt-6/qtxml-module.html)? If you're already using Qt, that would be the obvious choice. – Friedrich Mar 30 '23 at 05:51
  • ``target_link_libraries(qtProject PRIVATE PkgConfig::LIBXMLXX)`` made it work! – ryan714 Mar 30 '23 at 05:54
  • 1
    I actually need libxml2's ability to parse html, QT doesn't have a html parsing module. – ryan714 Mar 30 '23 at 05:55

1 Answers1

1

My test code fully compiles! Here is my full cmake file:


project(qtProject VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

find_package(Qt6Network REQUIRED)


#libxml++
include_directories("/usr/include/libxml++-5.0/")
find_package (PkgConfig REQUIRED)
find_package (LibXml2 REQUIRED)
pkg_check_modules (LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-5.0)

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        downloader.cpp
        mainwindow.h
        downloader.h
        mainwindow.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

    qt_add_executable(qtProject
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )

# Define target properties for Android with Qt 6 as:
#    set_property(TARGET qtProject APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(qtProject SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(qtProject
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(qtProject PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt6::Network)
target_link_libraries(qtProject PRIVATE PkgConfig::LIBXMLXX)

set_target_properties(qtProject PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

install(TARGETS qtProject
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(qtProject)
endif()
ryan714
  • 25
  • 6