c++ - 如何修复 'Could not load the Qt platform plugin "xcb“在 ""中,即使它已被发现。”在fixup_bundle宏之后?

标签 c++ qt cmake qml shared-libraries

我正在尝试设置一个可以在大多数 Linux 发行版(例如 Blender)上运行的独立二进制存档 (.tar.gz)。我对 CMake 还不太熟悉。据我所知,所有依赖项都可以在安装步骤中通过 fixup_bundle 解决。宏。我假设安装目录应该成为一个独立的应用程序,可以在没有安装 Qt 的其他计算机上复制和运行?我不确定 CPack 在这里的作用。

我尝试过的

我的Qt安装路径是/home/<user>/Qt5.12.2/5.12.2/gcc_64/qmake 。我遵循了一些答案并复制了 platform/libqxcb.solibQt5XcbQpa.so.5进入安装目录。为了测试独立包,我更改了 ~/Qt5.12.2进入~/qt 。这是可执行文件运行时的错误消息:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

[1]    25965 abort (core dumped)  ./<executable_name>

我也尝试过qt.conf并将前缀和插件路径设置为 ./但这没有用。但我发现的一件有趣的事情是,当我设置 Plugins = /home/<user>/qt/5.12.2/gcc_64/plugins 时,显示一个小 Qt 窗口,但附带一堆错误消息:

qrc:/main.qml:4:1: module "QtQuick.Dialogs" is not installed
qrc:/main.qml:1:1: module "QtQuick" is not installed
qrc:/main.qml:3:1: module "QtQuick.Controls" is not installed
......
qrc:/main.qml:3:1: module "QtQuick.Controls" is not installed
qrc:/main.qml:5:1: module "QtQuick.Controls.Styles" is not installed
qrc:/main.qml:2:1: module "QtQuick.Layouts" is not installed

然后通过ldd测试两个libqxcb.so,找到了一些信息,尽管我不确定这就是真正的原因。

ldd ~/qt/5.12.2/gcc_64/plugins/platforms/libqxcb.so显示原始 libqxcb.so 链接了 Qt 安装附带的库:

        libQt5XcbQpa.so.5 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libQt5XcbQpa.so.5 (0x00007ff8936d7000)
        libQt5Gui.so.5 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libQt5Gui.so.5 (0x00007ff892d64000)
        libQt5DBus.so.5 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5 (0x00007ff892ad8000)
        libQt5Core.so.5 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libQt5Core.so.5 (0x00007ff892343000)
......
        libicui18n.so.56 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libicui18n.so.56 (0x00007ff8914ee000)
        libicuuc.so.56 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libicuuc.so.56 (0x00007ff891136000)
        libicudata.so.56 => /home/giokka/qt/5.12.2/gcc_64/plugins/platforms/../../lib/libicudata.so.56 (0x00007ff88f751000)
......

ldd <path_to_project>/build/install/platforms/libqxcb.so显示它链接到系统 Qt 库,这不是我的项目构建的库:

./platforms/libqxcb.so: /lib64/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by ./platforms/libqxcb.so)
./platforms/libqxcb.so: /lib64/libQt5Gui.so.5: version `Qt_5_PRIVATE_API' not found (required by ./platforms/libqxcb.so)
        libQt5XcbQpa.so.5 => /lib64/libQt5XcbQpa.so.5 (0x00007f1d8ea75000)
        libQt5Gui.so.5 => /lib64/libQt5Gui.so.5 (0x00007f1d8e41e000)
        libQt5DBus.so.5 => /lib64/libQt5DBus.so.5 (0x00007f1d8e382000)
        libQt5Core.so.5 => /lib64/libQt5Core.so.5 (0x00007f1d8de62000)
......
        libicui18n.so.63 => /lib64/libicui18n.so.63 (0x00007f1d8cf37000)
        libicuuc.so.63 => /lib64/libicuuc.so.63 (0x00007f1d8cd64000)
        libicudata.so.63 => /lib64/libicudata.so.63 (0x00007f1d8afd0000)
......

源代码

CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(OpenGLUnderQML LANGUAGES CXX)

set(CMAKE_PREFIX_PATH "$ENV{HOME}/Qt5.12.2/5.12.2/gcc_64/lib/cmake")
set(qt_lib_path "$ENV{HOME}/Qt5.12.2/5.12.2/gcc_64")

list(APPEND qt_modules
    Core
    Gui
    Quick
    DBus
)

foreach(module ${qt_modules})
    list(APPEND qt_libs "Qt5::${module}")
endforeach()

include(GNUInstallDirs)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")


find_package(Qt5 COMPONENTS ${qt_modules} REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

include_directories(include/)

list(APPEND headers
    include/Scene.hpp
    include/Renderer.hpp
    include/VertexArray.hpp
    include/VertexBuffer.hpp
    include/VertexLayout.hpp
    include/IndexBuffer.hpp
    include/Shader.hpp
)

list(APPEND qrc
    qml/qml.qrc
    res/fonts.qrc
    res/shaders.qrc
)

add_executable(${PROJECT_NAME}
    src/main.cpp
    src/Scene.cpp
    src/Renderer.cpp
    src/VertexArray.cpp
    src/VertexBuffer.cpp
    src/VertexLayout.cpp
    src/IndexBuffer.cpp
    src/Shader.cpp
    ${headers}
    ${qrc}
)

target_link_libraries(${PROJECT_NAME}
    PUBLIC
        ${qt_libs}
)


file(RELATIVE_PATH _rel "${CMAKE_INSTALL_PREFIX}/install" "${CMAKE_INSTALL_PREFIX}")
set(_rpath "\$ORIGIN/${_rel}")
file(TO_NATIVE_PATH "${_rpath}/install" app_RPATH)

set_target_properties(${PROJECT_NAME}
    PROPERTIES
        SKIP_BUILD_RPATH OFF
        BUILD_WITH_INSTALL_RPATH OFF
        INSTALL_RPATH ${app_RPATH}
        INSTALL_RPATH_USE_LINK_PATH ON
)

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_BINARY_DIR}/install)
install(
    CODE "
include(BundleUtilities)
fixup_bundle(\"${CMAKE_BINARY_DIR}/install/${PROJECT_NAME}\" \"\" \"\")
"
    DESTINATION ${CMAKE_BINARY_DIR}/install
    COMPONENT Runtime
)

install(FILES "$<TARGET_FILE:Qt5::QXcbIntegrationPlugin>" DESTINATION ${CMAKE_BINARY_DIR}/install/platforms)

qt.conf

[Paths]
Prefix = ./
Plugins = /home/giokka/qt/5.12.2/gcc_64/plugins

更新01:

这是我的qt.conf文件,但它不起作用。

[Paths]
Prefix = .
Libraries = lib
Qml2Imports = qml
Plugins = plugins

但是,这个脚本在我的编译计算机上有效,但在另一台计算机上无效:

export LD_LIBRARY_PATH=`pwd`/lib
export QML_IMPORT_PATH=`pwd`/qml
export QML2_IMPORT_PATH=`pwd`/qml
export QT_QPA_PLATFORM_PLUGIN_PATH=`pwd`/plugins/platforms
./OpenGLUnderQML

我的 bundle 内容:

lib
OpenGLUnderQML (the executable)
plugins
qml
qt.conf
startapp.sh (the script above)

lib , plugins ,和qml完全从 QTDIR 复制(约 500 MB),因此不应遗漏任何库或插件。

最佳答案

我遇到了一个非常相似的问题,并且有相同的错误消息。 首先,通过打开调试一些

export QT_DEBUG_PLUGINS=1

并重新运行应用程序。对我来说,这揭示了以下内容:

“无法加载库/home/.../miniconda3/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxkbcommon-x11.so.0:无法打开共享对象文件:没有这样的文件或目录)”

“无法加载库/home/.../miniconda3/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxkbcommon-x11.so.0: 无法打开共享对象文件:没有这样的文件或目录)”

确实,我缺少 libxkbcommon-x11.so.0 和 libxkbcommon-x11.so.0。接下来,从 Linux 命令行使用 dpkg 检查您的体系结构。 (对我来说,命令“arch”给出了不同且无用的结果)

dpkg --print-architecture #result for me: amd64

然后我用 google 搜索“libxkbcommon-x11.so.0 ubuntu 18.04 amd64”,同样搜索 libxkbcommon-x11.so.0,它在packages.ubuntu.com 上生成这些软件包。这告诉我,回想起来,毫不奇怪,我缺少名为 libxkbcommon-x11-0 和 libxkbcommon-0 的软件包,并且安装这些软件包将包含所需的文件,但开发版本不会。那么解决办法:

sudo apt-get update

sudo apt-get install libxkbcommon0

sudo apt-get install libxkbcommon-x11-0

关于c++ - 如何修复 'Could not load the Qt platform plugin "xcb“在 ""中,即使它已被发现。”在fixup_bundle宏之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57362015/

相关文章:

c++ - 为什么 CMake 找不到 GTest(Google 测试)?

c++ - 在 Visual Studio 2010 的 CMake 中定义宏 "disallow copy and assign"

c++ - cmake 如何在 Visual Studio 中激活 "Inherit from parent"链接选项

C++请求成员,非类类型

python - 我们可以使用映射来搜索而不是二分搜索吗?

c++ - Qt 库组件可以调用 qDebug() 或类似函数吗?

c++ - Qt QHBoxLayout 问题?

c++ - 为什么我需要在 C++ 中的所有文件中包含预编译头文件?

c++ - c++代码中的意外输出,未获得预期结果

c++ - 在 Qt 中绘制在标签的顶部,而不是在它的后面