“跨平台"最佳实践用 Qt 开发 [英] Best Practices for "Cross-Platform" Development with Qt

查看:41
本文介绍了“跨平台"最佳实践用 Qt 开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 qt.nokia.com,Qt 是一个跨平台应用程序和 UI 框架",允许您编写一次代码以针对多个平台".Qt SDK 是一个完整的开发环境",包含在一次安装中使用 Qt 构建跨平台应用程序所需的工具".Qt Creator 是一个跨平台 IDE",可在 Windows、Linux/X11 和 Mac OS X 桌面操作系统上运行,并允许开发人员为多个桌面和移动设备平台创建应用程序."

According to qt.nokia.com, Qt is a "cross-platform application and UI framework", allowing you to "write code once to target multiple platforms". The Qt SDK is a "complete development environment" containing "the tools you need to build cross-platform applications with Qt in a single install". Qt Creator is a "cross-platform IDE" that "runs on Windows, Linux/X11, and Mac OS X desktop operating systems, and allows developers to create applications for multiple desktop and mobile device platforms."

那些网站简介中没有明确提到交叉编译"这个神奇的词.然而,天真的读者可能会推断出您可以为用于开发的任何主机系统下载 Qt SDK(包括 Qt Creator),创建项目,并编写一些您可以轻松轻松地编写的代码.em> 生成适用于 Windows、Linux、Mac 等的可执行文件.通过轻松"我想到的是在构建设置对话框中勾选一些复选框,然后按下构建"按钮.

The magic words "cross compilation" are not mentioned explicitly those website blurbs. Nevertheless, a naive reader might be forgiven for inferring that you can download the Qt SDK (including Qt Creator) for whatever host system you are using for development, create a project, and write some code from which you could easily generate executables for Windows, Linux, Mac, etc. By "easily" I have in mind something like ticking some checkboxes in a build settings dialog, and pressing the "Build" button.

我还在寻找那些复选框!同时,我在这里和其他地方找到了很多关于安装交叉编译器、安装额外的二进制文件、重写你的 qmake 文件等的帖子.从市场营销来看,我有点期望交叉编译已经得到了完全和直接的支持IDE 和 SDK 工具的开箱即用"安装.我是否遗漏了一些明显的东西?

I'm still looking for those checkboxes! Meanwhile, I have found various posts, here and elsewhere, about installing a cross-compiler, installing additional binaries, rewriting your qmake file, etc. From the marketing, I sort of expected that cross-compilation would already be fully and directly supported with an "out-of-the-box" installation of the IDE and SDK tools. Am I missing something obvious?

如果没有,我的开发机器可用于所有三种操作系统.我应该在所有三个平台上都安装 Qt Creator 吗?如果我这样做,我是否可以期望能够采用我使用 Qt Creator 为 Windows 开发的 Qt 项目(或者可能只是源代码),将其复制到我的 Mac 或 Linux 机器上,然后构建它是否在该平台上使用 Qt Creator 版本,而不会遇到一些重大问题?与在单个开发主机上安装交叉编译工具相比,这可能是使用 Qt 为多个平台创建可执行文件的最佳实践吗?

If not, I have development machines available with all three operating systems. Should I just install Qt Creator on all three platforms? If I do that, can I expect to be able to take a Qt project (or maybe just the source code) that I have developed using Qt Creator for, say, Windows, copy it over to my Mac or Linux machine, and build it there using the version of Qt Creator for that platform, without running into some major issues? Might that even be the best practice for using Qt to create executables for mutiple platforms, vs. installing cross-compilation tools on a single development host?

推荐答案

Qt 的标语是:

一次编写,到处编译.

考虑到这一点,Qt 并未正式提供任何开箱即用的解决方案,用于将 Qt 应用程序从特定平台交叉编译到其他不同平台.虽然绝对可行,但如果投入大量工作和时间,Qt 良好的惯例会建议您直接在目标平台上构建 Qt 应用程序.这意味着,为您的应用程序构建 Windows 目标,在一台 Windows 机器上,在 Mac 机器上构建 Mac 目标,在 Linux 机器上构建 Linux 目标,您猜对了 (:-)).

With this in mind, Qt doesn't officially offer any out of the box solution for cross compiling Qt applications from a specific platform to other different platforms. Though definitely do-able, if much work and time is invested, Qt good common practice would suggest you to build your Qt application directly on the targeted platform. That means, build your the Windows target for your application an a Windows machine, your Mac target on a Mac machine and Linux target on a, you guessed right( :-) ), a Linux machine.

这就是一次编写,随处编译"的方式,在我看来,这是一个非常好的词组合.否则,标签行可能是一次编写,为所有内容编译".

That's way "Write once, compile everywhere", in my opinion is a very well chosen combination of words. Otherwise the tag line might have been, "Write once, compile for everything".

回到最初的问题,您甚至不需要针对每个目标平台使用不同的物理机,因为在当今良好的虚拟化解决方案的时代,您可以轻松设置几个虚拟机并构建您的应用程序在同一物理机的不同平台上.

Going back to the original issue, you don't even need different physical machines for each of the targeted platforms, since in this day and age of good Virtualization solutions you can easily set up a couple of virtual machines and build you app on different platforms from the same physical machine.

至于您的问题没有直接回答的其他问题:

As for the other questions not directly answered from your question:

我应该在上面安装 Qt Creator三个平台?如果我这样做,可以我希望能够接受 Qt项目(或者可能只是源代码)我使用 Qt 开发的创建者,比如说,Windows,复制它到我的 Mac 或 Linux 机器上,以及使用 Qt 版本在那里构建它该平台的创建者,没有遇到一些重大问题?

Should I just install Qt Creator on all three platforms? If I do that, can I expect to be able to take a Qt project (or maybe just the source code) that I have developed using Qt Creator for, say, Windows, copy it over to my Mac or Linux machine, and build it there using the version of Qt Creator for that platform, without running into some major issues?

是的.次要问题是让您的应用程序在所有三个主要平台(Windows、Linux、Mac)或 Mac Dock 高级集成或托盘栏上正确执行应用程序/二进制图标在两者之间的集成问题.通过将特定于平台的代码正确封装在编译器指令(如 #define for eg)中,可以在不破坏代码的跨平台特性的情况下处理这些问题.我还建议对您的应用程序需要的每个其他平台特定代码执行此操作,或者如果您将广泛使用平台特定代码,将相关平台特定代码的整个块分成几个特定于每个平台的动态加载库(或共享库),但是导出相同的抽象通用接口并根据需要加载/链接到它们.

Yes. Minor issues are things like getting your applications executable/binaries icons right on all three major platforms(Windows, Linux, Mac) or Mac Dock advanced integration or tray bar Integration problems between the two. These issues can be handled without breaking the cross platform characteristic of your code, by properly encapsulating your platform specific code in compiler directives like #define for eg. I also recommend doing so for every other platform specific code that your application requires or if you will make extensive use of platform specific code, separating entire blocks of related platform specific code into several dynamical loaded libraries(or shared libraries) specific to each platform but exporting the same abstract generic interface and loading/linking to them as needed.

这可能是最好的做法使用 Qt 创建可执行文件多平台,与安装单个交叉编译工具开发主机?

Might that even be the best practice for using Qt to create executables for mutiple platforms, vs. installing cross-compilation tools on a single development host?

您应该尽可能使用 Qt SDK(Qt Creator 或命令行工具)来构建您的应用程序,因为像 qmake 这样的工具将承担在您的 Makefile 中手动处理 .moc 文件的负担.但是,如果由于多种原因而变得不可能,例如.就像你的公司强加 Visual Studio 开发(跨平台吧?),有很多关于如何使用 MS Visual Studio、GNU autotools 或 CMake 等构建系统处理基于 Qt 的构建的教程.虽然我建议坚持使用 qmake,它是一个很好的make makefile"生成器,并且可以轻松适应您可能需要正确构建应用程序的任何平台特定黑客,而不是使用更适合平台特定黑客的构建系统然后尝试满足 Qt 对这些构建系统的需求.毕竟,如果您出于跨平台的原因在 Qt 中开发应用程序,那么 Qt 应该是您应用程序的主要框架,而不是您可能使用的特定于平台的 API/代码或第三方库.

You should use the Qt SDK(Qt Creator or the command line tools) for building your application wherever possible since tools like qmake will take the burden of handling .moc files manually in your Makefiles. But if that becomes impossible for several whatever reasons, for eg. like your company imposing Visual Studio development(cross platform huh?), there are many tutorials out there on how to handle Qt based builds using build systems like MS Visual Studio, GNU autotools or CMake. Though i would recommend sticking with qmake, which is a good "make makefile" generator and easily adaptable to whatever platform specific hacks you might need for your application to build right, rather then using a build system which is more comfortable to the platform specific hacks and then trying to accommodate for Qt's needs for those build systems. After all, if you develop your application in Qt for cross platform reasons, then Qt should be the primary framework for your application and not the platform specific api/code or third party libraries you might use.

我希望我已经足够清楚并且有帮助.

I hope i have been clear enough and helpful.

PS:我也欢迎就我在评论中写的内容提出建议或修复/补充.

PS: I am also welcome to suggestions or fixes/additions regarding what i wrote in the comments.

这篇关于“跨平台"最佳实践用 Qt 开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆