Linux下Poppler-qt6未定义引用错误:CMake正确链接方案问询
问题:Qt6 + Poppler-Qt6 链接器未定义引用错误
我用Qt6和poppler-qt6搭建简易PDF阅读器,已经通过官网说明和checkinstall完成poppler-qt6安装,编译器能识别头文件,但运行代码时所有Poppler类调用都提示未定义引用,使用CLion+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()'
我曾尝试在CMakeLists.txt里添加/usr/local/include/poppler/qt6/poppler-qt6.h来解决错误,但编译poppler-annotation.h时出现无法包含<QtCore/QDateTime>的问题,且该库已经在"external libraries/header search paths"中显示。
当前CMakeLists.txt
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 )
请问如何正确配置CMake以找到并链接poppler库?
解决方案
核心问题是CMake配置错误:你把头文件路径加到了target_link_libraries(该命令用于链接库文件,而非指定头文件),且未正确找到Poppler-Qt6的库文件。按以下步骤修改CMakeLists.txt:
用pkg-config辅助查找Poppler-Qt6
CMake可借助pkg-config自动识别poppler-qt6的头文件路径、库文件路径及依赖项,避免手动配置出错。分离头文件路径与库链接逻辑
用target_include_directories指定头文件搜索路径,target_link_libraries仅用于链接库文件。
修改后的CMakeLists.txt示例:
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) # 查找Qt6组件 find_package(Qt6 COMPONENTS Core Gui Widgets REQUIRED) # 启用pkg-config并查找poppler-qt6 find_package(PkgConfig REQUIRED) # 若手动安装到/usr/local,需先指定pkgconfig路径 set(PKG_CONFIG_PATH "/usr/local/lib/pkgconfig" ${PKG_CONFIG_PATH}) pkg_check_modules(POPPLER_QT6 REQUIRED poppler-qt6) # 添加可执行文件 add_executable(PDF_Viewer main.cpp mainwindow.cpp mainwindow.h mainwindow.ui) # 设置头文件搜索路径 target_include_directories(PDF_Viewer PRIVATE ${POPPLER_QT6_INCLUDE_DIRS}) # 链接依赖库 target_link_libraries(PDF_Viewer Qt::Core Qt::Gui Qt::Widgets ${POPPLER_QT6_LIBRARIES} ) # 传递poppler的编译选项 target_compile_options(PDF_Viewer PRIVATE ${POPPLER_QT6_CFLAGS_OTHER})
额外说明
- 若
pkg_check_modules仍找不到poppler-qt6,先在终端执行pkg-config --cflags --libs poppler-qt6,确认输出的路径是否正确,若输出为空,检查/usr/local/lib/pkgconfig下是否存在poppler-qt6.pc文件。 - 之前出现的
<QtCore/QDateTime>找不到问题,是因为Poppler头文件依赖Qt,但原配置未让CMake感知到Qt的依赖,用pkg-config会自动处理跨库依赖,解决该问题。
内容的提问来源于stack exchange,提问作者Mettigel4_1
相关产品推荐
相关产品推荐

