【问题标题】:qt is not linked correctly when building with cmake and conan使用 cmake 和 conan 构建时 qt 未正确链接
【发布时间】:2022-01-14 09:42:12
【问题描述】:

我正在学习 C++ 和 Qt,并开始尝试使用 QT 构建应用程序。

我使用 cmake 构建,conan 用于包管理器。 通过关注Qt guide for Conan,我得到了一个构建,但是在执行它时,我得到了:

#include <QApplication>



int main(int argc, char **argv) {
    QApplication app (argc, argv);
    return app.exec();
}
matteo@MacBook-Pro-de-matteo build % export QT_DEBUG_PLUGINS=1
matteo@MacBook-Pro-de-matteo build % ./bin/myapp          
QFactoryLoader::QFactoryLoader() ignoring "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3" since plugins are disabled in static builds
qt.qpa.plugin: Could not find the Qt platform plugin "cocoa" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

zsh: abort      ./bin/myapp

信息:

matteo@MacBook-Pro-de-matteo everywhere % conan -v
Conan version 1.44.0

matteo@MacBook-Pro-de-matteo everywhere % cmake --version
cmake version 3.22.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

conanfile.txt:

[requires]
qtbase/6.2.2@qt/everywhere
qt-conan-common/6.2@qt/everywhere
qtdeclarative/6.2.2@qt/everywhere


[generators]
cmake

通过执行以下命令(MacOS)构建库:

conan install ./conanfile.txt --build=missing --profile=conan/qtprofiles/macos-universal-clang --update --generator=VirtualBuildEnv -r qt

我已经将macos-universal-clang的编译器版本改为13,使其与编译器版本匹配,否则无法编译:

macos-universal-clang:

[settings]
    os=Macos
    os.version=11.0
    arch=x86_64
    compiler=apple-clang
    compiler.version=13.0
    compiler.libcxx=libc++
    build_type=RelWithDebInfo
[options]
qtbase:release=yes
qtbase:shared=yes
qtbase:nomake=examples;tests
qtbase:force_debug_info=yes
qtbase:separate_debug_info=yes
qtbase:framework=yes
qtbase:cmake_args_qtbase='-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DODBC_ROOT=/usr/local/opt/libiodbc'
*:cmake_args_leaf_module=""
[build_requires]
[env]

编译时找到库:

matteo@MacBook-Pro-de-matteo myxconfig % ./activate.sh                   
matteo@MacBook-Pro-de-matteo myxconfig % cmake -S . -B out/build  
-- Conan: Adjusting output directories
-- Conan: Using cmake global configuration
-- Conan: Adjusting default RPATHs Conan policies
-- Conan: Adjusting language standard
-- Current conanbuildinfo.cmake directory: .../out/build
-- Configuring done
-- Generating done
-- Build files have been written to: ..../out/build

cd out/build

matteo@MacBook-Pro-de-matteo build % make                   

[100%] Built target myxconfig
matteo@MacBook-Pro-de-matteo myxconfig % echo $?
0

怎么了?

更新

我通过将可可插件静态链接到可执行文件中使其工作:
#include <QApplication>
#include <QtPlugin>
Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin)

int main(int argc, char **argv) {
    QApplication app (argc, argv);
    return app.exec();
}

这是我的灵感来源:https://github.com/siavashk/qt5-static-hello-world

不过,作为一名新生,我仍然在问自己:

  • 如果运行的操作系统安装了 lib,如何避免捆绑它并使其正常工作?
  • 关于 Qt 所谓的“插件”的简短说明(参考文档)会很棒:)

【问题讨论】:

    标签: c++ macos qt cmake conan


    【解决方案1】:

    此消息不是关于链接,而是关于 Qt 运行时无法找到文件:

    qt.qpa.plugin:在“”中找不到Qt平台插件“cocoa”

    任一设置环境变量:

    QT_QPA_PLATFORM_PLUGIN_PATH=/home/me/.conan/data/qt/5.12.0/repo/stable/package/f3d861c7053b683d8cf8fcd66b11dc1dee351153/plugins
    

    QT_PLUGIN_PATH=/home/me/.conan/data/qt/5.12.0/repo/stable/package/f3d861c7053b683d8cf8fcd66b11dc1dee351153/plugins
    

    根据经验,其中一个应该可以根据 Qt 构建工作,但请注意您的本地系统路径。以上来自 Linux 项目,我们为其他平台创建了类似的项目。因此,我们的安装程序会创建启动脚本文件start.sh,如下所示:

    QT_PLUGIN_PATH=/home/me/.conan/data/qt/5.12.0/repo/stable/package/f3d861c7053b683d8cf8fcd66b11dc1dee351153/plugins
    /home/me/progPath/bin/qtProgram
    

    请注意,以上不是规则,可能取决于安装设置。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2019-10-30
      • 1970-01-01
      • 2015-03-06
      • 2022-07-15
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2018-02-01
      相关资源
      最近更新 更多