“跨平台”最佳实践使用 Qt 进行开发
根据qt.nokia.com,Qt 是一个“跨平台应用程序和 UI 框架”,允许您“编写一次代码即可针对多个平台”。 Qt SDK 是一个“完整的开发环境”,包含“在一次安装中使用 Qt 构建跨平台应用程序所需的工具”。 Qt Creator 是一个“跨平台 IDE”,“在 Windows、Linux/X11 和 Mac OS X 桌面操作系统上运行,并允许开发人员为多个桌面和移动设备平台创建应用程序”。
这些网站简介中没有明确提及“交叉编译”这个神奇的词。尽管如此,天真的读者可能会推断,您可以为您用于开发的任何主机系统下载 Qt SDK(包括 Qt Creator),创建一个项目,并编写一些代码,您可以轻松 为 Windows、Linux、Mac 等生成可执行文件。通过“轻松”,我想到的是在构建设置对话框中勾选一些复选框,然后按“构建”按钮。
我仍在寻找那些复选框!同时,我在这里和其他地方找到了各种关于安装交叉编译器、安装额外的二进制文件、重写 qmake 文件等的帖子。从市场营销来看,我有点期望交叉编译已经得到完全和直接的支持IDE 和 SDK 工具的“开箱即用”安装。我错过了一些明显的东西吗?
如果没有,我有可用于所有三种操作系统的开发机器。我应该在所有三个平台上安装 Qt Creator 吗?如果我这样做,我是否可以期望能够将我使用 Qt Creator 为 Windows 开发的 Qt 项目(或者可能只是源代码),将其复制到我的 Mac 或 Linux 机器上,并构建它是否使用适用于该平台的 Qt Creator 版本,而没有遇到一些重大问题?与在单个开发主机上安装交叉编译工具相比,这甚至可能是使用 Qt 为多个平台创建可执行文件的最佳实践吗?
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."
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.
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Qt 的口号是:
考虑到这一点,Qt 并没有正式提供任何开箱即用的解决方案来将 Qt 应用程序从特定平台交叉编译到其他不同平台。虽然绝对可行,但如果投入大量工作和时间,Qt 良好的常见做法会建议您直接在目标平台上构建 Qt 应用程序。这意味着,在 Windows 机器上为应用程序构建 Windows 目标,在 Mac 机器上构建 Mac 目标,在 Linux 机器上构建 Linux 目标,您猜对了(:-))。
在我看来,“一次编写,到处编译”是一个精心挑选的词语组合。否则,口号可能是“编写一次,编译所有内容”。
回到最初的问题,您甚至不需要为每个目标平台使用不同的物理机,因为在当今良好的虚拟化解决方案时代,您可以轻松设置几个虚拟机并在不同的平台上构建应用程序来自同一台物理机器。
至于您的问题没有直接回答的其他问题:
是的。小问题包括在所有三个主要平台(Windows、Linux、Mac)上正确显示应用程序可执行文件/二进制文件图标或 Mac Dock 高级集成或托盘栏两者之间的集成问题。通过将特定于平台的代码正确封装在#define 等编译器指令中,可以在不破坏代码的跨平台特性的情况下处理这些问题。我还建议您对应用程序所需的每个其他平台特定代码执行此操作,或者如果您将广泛使用特定于平台的代码,则将相关平台特定代码的整个块分成特定于每个平台的多个动态加载库(或共享库),但是导出相同的抽象通用接口并根据需要加载/链接到它们。
您应该尽可能使用 Qt SDK(Qt Creator 或命令行工具)来构建应用程序,因为像 qmake 这样的工具将承担在 Makefile 中手动处理 .moc 文件的负担。但如果由于多种原因而变得不可能,例如。就像您的公司实施 Visual Studio 开发(跨平台吧?),有很多关于如何使用 MS Visual Studio、GNU autotools 或 CMake 等构建系统处理基于 Qt 的构建的教程。
虽然我建议坚持使用 qmake,它是一个很好的“make makefile”生成器,并且可以轻松适应您的应用程序构建正确所需的任何特定于平台的 hack,而不是使用更适合特定于平台的 hack 的构建系统然后尝试满足 Qt 对这些构建系统的需求。毕竟,如果您出于跨平台原因在 Qt 中开发应用程序,那么 Qt 应该是您应用程序的主要框架,而不是您可能使用的特定于平台的 api/代码或第三方库。
我希望我已经说得足够清楚并且有帮助。
PS:我也欢迎对我在评论中所写内容提出建议或修复/添加。
Qt's tagline is:
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:
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.
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: I am also welcome to suggestions or fixes/additions regarding what i wrote in the comments.
您可以使用
MinGW
在 Linux 上针对 Windows 进行交叉编译。由于技术问题,无法交叉编译到 Mac。从 GUI 中执行此操作没有简单的方法,但是 这里是一个关于在 Windows 上交叉编译 QtWebKit 的好方法。这可以应用于任何 Qt 项目。You can use
MinGW
to cross-compile on Linux for Windows. Cross-compiling to Mac is not possible due to technical problems. There is no easy mean to do this from GUI but here is a nice how-to on cross compiling QtWebKit for Windows. That can be applied to any Qt project.我认为没有交叉编译配置。
关于整个项目的可移植性,我会说“几乎”,因为我发现了一些恼人的故障。尽管如此,您的源代码将是 100% 可移植的,并且在另一个平台上重新编译只需要 .pro 中的一些技巧。
我所做的就是只使用一个平台(Linux),并且时不时地使用我的 Windows 机器和我的 Mac Mini 进行编译。我通常最终遇到的只是一些关于在哪里找到外部库或包含文件的指令。另外,在 Mac 上工作时,我必须复制应用程序包中的外部文件,以便程序可以找到它们。
I don't think there is a cross-compile configuration.
About the portability of the full project I'd say "almost" because I found a few annoying glitches. None the less your source code will be 100% portable and recompiling on another platform will just require some tricks in the .pro.
What I do is working with just one platform (Linux) and every now and then compiling using my Windows machine and my Mac Mini. What I normally end up fighting with are just a few directives about where to find external libraries or include files. Also when working on Mac I've to copy external files in the app bundle so that the program can find them.
上次我使用 Qt 是几年前的事了,你必须在你想要二进制文件的每个平台上进行编译。当然你可以交叉编译,但我认为这取决于你来设置这样的东西。
也许其他人会提供更好的问题,但我认为您不会遗漏任何内容。
Last time I was using Qt, and it was years ago, you had to compile on every platform you wanted a binary for. Of course you could cross-compile but I think that it'd be up to you to set up such a thing.
Maybe someone else will provide a better question, but I don't think you are missing anything.
我不了解Linux系统。但是,要开发针对 Mac OS X 和 Windows 的 Qt 应用程序,您需要为它们分别配备一台开发机器。两者没有交叉编译。
Qt 是跨平台的,因为相同的源文件可以为不同平台构建应用程序。有了所有可用的虚拟机,一台(物理)机器设置来开发和构建多个目标应该不会有太大问题。事实上,这正是我当前针对 Mac 和 Windows 的项目的工作方式。
I don't know about Linux systems. But for developing Qt app targeting Mac OS X and Windows, you need a development machine for each of them. There's no cross-compiling for the two.
Qt is cross-platform in the sense that the same source files can build app for different platforms. With all the VMs available, a one (physical) machine setup to develop and build multiple targets shouldn't be too much of problem. In fact, that's exactly how I am working on my current project which targets both Mac and Windows.
交叉编译通常用于在主机 Linux 系统上开发软件并针对目标 Linux 系统进行编译。多年来,我使用运行在 x86 和 X11 (KDE) 上的 Linux 系统来交叉编译我们的 Qt 应用程序,以便运行 Qt Embedded 的嵌入式 ARM 系统。如果您开发移动应用程序,则可能会进行交叉编译。当然,你可以在Windows系统上进行交叉编译。您真正需要的只是交叉编译器。由于 gcc 在源代码中可用,因此 gcc 交叉编译器非常常见。要使用 Qt 执行交叉编译,您需要 qmake 的 mkspec 来指定要使用的编译器。
你的最后一段表明你可能无法进行这种交叉编译——你可以在你的 Windows 机器上为 Linux 或可能为 Mac 做它,并且你可能可以在 Linux 上为 Windows 做它,如果你'您可以在 Windows 上使用 mingw 版本的 Qt,但您永远无法在 Linux 或 Mac 上为 Windows 编译基于 Microsoft 编译器的应用程序。
底线——按照你指示的去做。在每个目标平台上获取源代码并在那里进行编译。
Cross-compiling is typically used for developing software on a host Linux system and compiling for a target Linux system. For years I used a Linux system running on an x86 with X11 (KDE) to cross compile our Qt application for an embedded ARM system running Qt Embedded. If you do mobile apps, you're likely cross-compiling. You can cross compile on a Windows system, of course. All you really need is the cross compiler. Since gcc is available in source code, gcc cross-compilers are quite common. To perform cross compilation with Qt, you need a mkspec for qmake that specifies what compilers to use.
Your last paragraph indicates you're probably not going to be able to do this kind of cross compiling--you can do it on your Windows machine for Linux or probably for Mac, and you can probably do it on Linux for Windows if you're using the mingw version of Qt on Windows, but you'll never compile a Microsoft-compiler-based application on Linux or Mac for Windows.
Bottom line--do what you indicated. Get your source on each target platform and compile it there.