0

I am trying to build a simple pdf reader with Qt6 and poppler-qt6. I installed poppler-qt6 from the website with their installation instructions and with "checkinstall" and the package is being recognized by my compiler; however when I run my code it tells me all calls to the Poppler class are of undefined reference. I read online that this I caused by the linker, but I could not figure out how to fix the issue. I am running into this problem with CLion with CMake.

/usr/bin/ld: CMakeFiles/PDF_Viewer.dir/mainwindow.cpp.o: in function `MainWindow::MainWindow(QWidget*)':
/home/maximilian/C-Programming/PDF-Viewer/mainwindow.cpp:20: undefined reference to `Poppler::Document::load(QString const&, QByteArray const&, QByteArray const&)'
/usr/bin/ld: /home/maximilian/C-Programming/PDF-Viewer/mainwindow.cpp:21: undefined reference to `Poppler::Document::page(int) const'
/usr/bin/ld: /home/maximilian/C-Programming/PDF-Viewer/mainwindow.cpp:22: undefined reference to `Poppler::Page::renderToImage(double, double, int, int, int, int, Poppler::Page::Rotation) const'
/usr/bin/ld: CMakeFiles/PDF_Viewer.dir/mainwindow.cpp.o: in function `std::default_delete<Poppler::Document>::operator()(Poppler::Document*) const':
/usr/include/c++/12/bits/unique_ptr.h:95: undefined reference to `Poppler::Document::~Document()'
/usr/bin/ld: CMakeFiles/PDF_Viewer.dir/mainwindow.cpp.o: in function `std::default_delete<Poppler::Page>::operator()(Poppler::Page*) const':
/usr/include/c++/12/bits/unique_ptr.h:95: undefined reference to `Poppler::Page::~Page()'

I can get rid of the undefined reference error by adding /usr/local/include/poppler/qt6/poppler-qt6.h (location of the poppler-qt6 install) into my CMakeLists.txt file, which breakes the compile while compiling the "poppler-annotation.h" file, because it was unable to include <QtCore/QDateTime>. The library does show up in the "external libraries/header search paths".

How do I tell CMake where to look for the poppler library to build and link it correctly?

Below is my CMakeLists.txt file.

cmake_minimum_required(VERSION 3.24)
project(PDF_Viewer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)


find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

add_executable(PDF_Viewer main.cpp mainwindow.cpp mainwindow.h mainwindow.ui)
target_link_libraries(PDF_Viewer
        Qt::Core
        Qt::Gui
        Qt::Widgets
        /usr/local/include/poppler/qt6/poppler-qt6.h
        )
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • 2
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) *(If not, please provide details to distinguish your question from that one, and remove details that are not necessary to reproduce the error so we can better focus on what is relevant.)* – JaMiT Mar 25 '23 at 17:29
  • 1
    It does explain why the error occurs, thank you. I am just not able to translate my knowledge of why it does not work to how to make it work. I do not understand how to tell cmake where to look for the needed poppler library. – Mettigel4_1 Mar 25 '23 at 17:48
  • 1
    Your specification of the `/usr/local/include/poppler/qt6/poppler-qt6.h` **header** is a mess. "Undefined reference" error is fixed by linking with a proper **library** (on Linux it is a `.a` or `.so` file), and a **header** file is NEVER a **library**. The command `target_link_libraries` works with **libraries**, so specifying a header in it is simply wrong. – Tsyvarev Mar 25 '23 at 20:42

1 Answers1

1

The package provides poppler-qt6.pc file, which is consumed by pkg-config utility.

In CMake its usage could be

include(FindPkgConfig)
pkg_check_modules(POPPLER_QT6 REQUIRED IMPORTED_TARGET poppler-qt6)
...
target_link_libraries(PDF_Viewer PkgConfig::POPPLER_QT6)

More information about using pkg-config in CMake could be found in that question.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153