开发者

Best Practices for "Cross-Platform" Development with 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?


Qt's tagline is:

Write once, compile everywhere.

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:

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?

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.

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?

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.


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜