如何修复“无法加载 Qt 平台插件“xcb"?在“"中即使它被发现了.在 fixup_bundle 宏之后?

2022-01-19 00:00:00 qt shared-libraries qml cmake c++

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

I'm trying to setting up a standalone binary archive (.tar.gz) that can run on most Linux distros (ex. Blender). I'm still not familiar with CMake. As far as I know, all the dependencies can be resolved at the install step with fixup_bundle macro. And I assume that the install directory should become a standalone app that can be copied and run on other computers without Qt installed? I'm not sure about the role of CPack here.

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

My Qt installation path is /home/<user>/Qt5.12.2/5.12.2/gcc_64/qmake. I've followed some answers and have copied platform/libqxcb.so and libQt5XcbQpa.so.5 into the install directory. In order to test the standalone package, I change ~/Qt5.12.2 into ~/qt. And this is the error message when the executable run:

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 窗口,但会附带一堆错误信息:

I've also tried qt.conf and set the Prefix and Plugin path to ./ but that didn't work. One interesting thing I found though is that when I set Plugins = /home/<user>/qt/5.12.2/gcc_64/plugins, a little Qt window shows, but comes with a bunch of error messages:

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找到了一些信息,虽然我不确定这是不是真正的原因.

Then, I found some information by testing the two libqxcb.so with ldd, although I'm not sure this is the actual cause.

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

ldd ~/qt/5.12.2/gcc_64/plugins/platforms/libqxcb.so shows that the original libqxcb.so links the libraries that come with the Qt installation:

        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 库,这不是我的项目所基于的:

ldd <path_to_project>/build/install/platforms/libqxcb.so shows it links to the system Qt library, which is not the one my project built against on:

./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 文件,但它不起作用.

Update01:

This is my qt.conf file, but it doesn't work.

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

但是,此脚本在我的编译计算机上运行,??但在另一台计算机上却不行:

However, this script works on my compiling computer but not on the other computer:

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

我的捆绑包内容:

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

libpluginsqml 完全从 QTDIR 复制而来(大约 500 MB),因此应该没有遗漏任何库或插件.

lib, plugins, and qml are fully copied from QTDIR (about 500 MB), so there should be no library or plugin missed.

推荐答案

我遇到了一个非常相似的问题,同样的错误信息.首先,通过开启调试一些

I ran into a very similar problem with the same error message. First, debug some by turning on

导出 QT_DEBUG_PLUGINS=1

export QT_DEBUG_PLUGINS=1

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

and rerun the application. For me this revealed the following:

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

"Cannot load library /home/.../miniconda3/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxkbcommon-x11.so.0: cannot open shared object file: No such file or directory)"

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

"Cannot load library /home/.../miniconda3/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (libxkbcommon-x11.so.0: cannot open shared object file: No such file or directory)"

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

Indeed, I was missing libxkbcommon-x11.so.0 and libxkbcommon-x11.so.0. Next, check your architecture using dpkg from the linux command line. (For me, the command "arch" gave a different and unhelpful result)

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

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

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

I then googled "libxkbcommon-x11.so.0 ubuntu 18.04 amd64", and likewise for libxkbcommon-x11.so.0, which yields those packages on packages.ubuntu.com. That told me, in retrospect unsurprisingly, I'm missing packages called libxkbcommon-x11-0 and libxkbcommon-0, and that installing those packages will include the needed files, but the dev versions will not. Then the solution:

sudo apt-get 更新

sudo apt-get update

sudo apt-get install libxkbcommon0

sudo apt-get install libxkbcommon0

sudo apt-get install libxkbcommon-x11-0

sudo apt-get install libxkbcommon-x11-0

相关文章