c++ - 通过 Bazel 在 macOS 上构建和运行 Qt 应用程序

标签 c++ macos qt5 bazel bazel-rules

我尝试使用 Bazel 5.0.0 在 macOS (10.15.7) 上构建和运行 Qt5 (5.15.2) 应用程序。 不幸的是,我遇到了一些问题。 构建部分似乎有效,但运行部分无效。

我使用 Homebrew 在我的机器上安装了 Qt5:

brew install qt@5
brew link qt@5

我改编了https://github.com/justbuchanan/bazel_rules_qt/根据我的需要。看这个PR .当我尝试运行时:

bazel run --cxxopt=-std=c++17 //tests/qt_resource:main

我收到运行时错误:

dyld: Symbol not found: __ZN10QByteArray6_emptyE

重现问题的步骤:

# brew install bazel # Install Bazel
# brew install qt@5  # Install Qt5
git clone https://github.com/Vertexwahn/bazel_rules_qt.git
cd bazel_rules_qt
git checkout add-macos-support
bazel build --cxxopt=-std=c++17 //... # should work
bazel run --cxxopt=-std=c++17 //tests/qt_resource:main # should give you the error message

尽管如此,使用 bazel build --cxxopt=-std=c++17//... 构建所有内容似乎都有效。 我不是 100% 确定链接选项 -F/usr/local/opt/qt5/Frameworks-framework QtCore 等是否正确。 也许有人可以证实这一点。

我是否使用了正确的链接选项?

对我来说,不太清楚 main 二进制文件需要什么依赖项。我尝试手动将 QtCore.framework 复制到 main 二进制文件的位置,但这不会更改错误消息。

main 二进制文件需要哪些文件?

如果我尝试在我的 main 二进制文件上运行 macdeployqt,我也会遇到一些错误。我在我的工作区根目录中执行 cd bazel-bin/tests/qt_resource 然后运行 ​​/usr/local/opt/qt5/bin/macdeployqt main:

ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
WARNING:
WARNING: Could not find any external Qt frameworks to deploy in "main"
WARNING: Perhaps macdeployqt was already used on "main" ?
WARNING: If so, you will need to rebuild "main" before trying again.
ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/strip: can't open file:  (No such file or directory)\n"
ERROR: ""

我希望 macdeployqt 能为我收集所有需要的资源。知道为什么这不起作用吗?

如何使用 macdeployqt 收集 main 二进制文件所需的所有依赖项?

如果我通过 lipo -create -output universall_app main 将我的 main 转换为应用程序,然后执行 /usr/local/opt/qt5/bin/macdeployqt universall_app我收到相同的错误消息。

CMake 方法

为了确保我的系统设置没有一般性问题,我尝试使用 CMake 构建 Qt5 应用程序:

git clone https://github.com/euler0/mini-cmake-qt.git
cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5 .
make -j

这会生成一个 example.app。 双击此应用程序包, 可以启动应用程序。 这适用于我的系统。

future 方向

似乎rules_apple可用于创建应用程序包。 我不确定是否需要将我的 Qt 应用程序二进制文件转换为应用程序包才能执行它。 可以使用 --sandbox_debug 来识别 Bazel 正在做什么,并使用 dtruss 来比较 CMake 版本的差异。 我目前不确定下一步要做什么,希望有一个简单的解决方案。我也很喜欢 Qt6 解决方案。

更新:备选答案

如果有人可以指出如何仅在 macOS 上使用 make 并安装了 Qt5 的 brew 来构建最小的 Qt 应用程序,或者告诉我链接器和编译器选项必须是什么样子,这也会很有帮助。

最佳答案

我按照您的步骤使用 Mac OSX 10.15.7、Qt(由自制软件安装)5.15.1 以及 bazel 4.2.2-homebrew 和 5.0.0-homebrew,最初我无法从 git 构建项目:

* 3fe5f6c -(4 周前)添加 macOS 支持 — Vertexwahn(HEAD -> add-macos-support, origin/add-macos-support)

这是我构建时得到的结果:

 % bazel build --cxxopt=-std=c++17  //...
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed 14 targets (0 packages loaded, 0 targets configured).
 INFO: Found 14 targets...
 ERROR: /Users/home/Git/my_repo/bazel_rules_qt/tests/qt_qml/BUILD:4:10: Compiling tests/qt_qml/main.cc failed: (Aborted): wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG '-std=c++11' ... (remaining 38 argument(s) skipped)

 Use --sandbox_debug to see verbose messages from the sandbox
 tests/qt_qml/main.cc:1:10: fatal error: 'QtQml/QQmlApplicationEngine' file not found
 #include <QtQml/QQmlApplicationEngine>
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1 error generated.
 Error in child process '/usr/bin/xcrun'. 1
 INFO: Elapsed time: 0,594s, Critical Path: 0,32s
 INFO: 3 processes: 3 internal.
 FAILED: Build did NOT complete successfully

在 qt.BUILD 中处理标题和包含路径之后:

 diff --git a/qt.BUILD b/qt.BUILD
 index 517c8db..8f110b5 100644
 --- a/qt.BUILD
 +++ b/qt.BUILD
 @@ -28,11 +28,12 @@ QT_LIBRARIES = [
      cc_library(
          name = "qt_%s_osx" % name,
          # When being on Windows this glob will be empty
 -        hdrs = glob(["%s/**" % include_folder], allow_empty = True),
 +        hdrs = glob(["include/%s/**" % include_folder], allow_empty = True),
          includes = ["."],
          linkopts = ["-F/usr/local/opt/qt5/lib"] + [
              "-framework %s" % library_name.replace("5", "") # macOS qt libs do not contain a 5 - e.g. instead of Qt5Core the lib is called QtCore
              ],
 +        strip_include_prefix= "include"
          # Available from Bazel 4.0.0
          # target_compatible_with = ["@platforms//os:osx"],
      )

我可以构建并运行该项目:

 % bazel build --cxxopt=-std=c++17  //...
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed 14 targets (1 packages loaded, 7422 targets configured).
 INFO: Found 14 targets...
 INFO: Elapsed time: 11,761s, Critical Path: 7,23s
 INFO: 3 processes: 1 internal, 2 darwin-sandbox.
 INFO: Build completed successfully, 3 total actions
 % bazel run --cxxopt=-std=c++17 //tests/qt_resource:main
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed target //tests/qt_resource:main (0 packages loaded, 0 targets configured).
 INFO: Found 1 target...
 Target //tests/qt_resource:main up-to-date:
 bazel-bin/tests/qt_resource/main
 INFO: Elapsed time: 3,657s, Critical Path: 0,00s
 INFO: 1 process: 1 internal.
 INFO: Build completed successfully, 1 total action
 INFO: Build completed successfully, 1 total action
 opened resource file
 file1

与您的问题有关链接器选项是否正确 -F/usr/local/opt/qt5/Frameworks -framework QtCore

是的,它们是正确的,您也可以使用 -F/usr/local/opt/qt5/lib(因为您已经在 qt.BUILD 中使用)作为 Frameworks 文件夹下的所有文件是指向 lib 文件夹的链接。

使用 macdeployqtlipo 即使在测试成功运行后,我也得到了与 OP 中相同的结果。

关于c++ - 通过 Bazel 在 macOS 上构建和运行 Qt 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71040975/

相关文章:

macos - MAMP 持续无响应故障排除

qt - 如何创建尊重 Material 样式颜色的自定义 QML 控件?

c++ - 如何从 qml Keys.onPressed 调用 qt keyPressEvent(QKeyEvent *event)

c++ - 如何缓存 Ruby VALUE 值以供以后重用?

c++ - xcode 将 dylib 复制到带有延迟加载的包中

c++ - const 引用类成员是否会延长临时对象的生命周期?

xcode - 如何在 Xcode 4 中使用 dylib 文件创建工作框架

c - 如何使用 clang 在 OS X 上使用 c11 标准库进行编译?

c++ - Qt Creator 2.6.1 + Qt 5 + C++11 + MSVC2010 编译器

C# DeflateStream 使用 C++ 解压缩