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
)
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.