Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for "Cross-Platform" Development with Qt

Tags:

qt

qt-creator

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?

like image 235
Jan Hettich Avatar asked Jan 29 '11 20:01

Jan Hettich


People also ask

Is Qt really cross-platform?

No, no. Qt does not emulate anything, QT ships with it's own renderer if thats what you mean. While it might not look perfectly like the OS it is being run on it will however look the same on all supported OSs.

Is Qt good for development?

Companies have been using Qt as a software development framework for more than 20 years. Over these years, the framework has been improved many times. Qt was used in different industries and tested under various conditions which means that it provides stability that cannot be expected from any young frameworks.

Is Qt good for desktop development?

Its built on a hardware accelerated scene-graph allowing for smooth compositing and animation, QML is an easy to use means of creating your UI and connecting it to C++ (especially post-C++11) is trivially easy allowing for good solid high performance UI's.

Is Qt used in industry?

Qt is often used in a combination with other technologies. Qt is used by tens of thousands of companies across 70+ industries and in all regions of the world. Qt is often used to target one or multiple of the following platforms: Desktop: Windows, Linux/X11 and Mac OS X.


2 Answers

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.

like image 104
Shinnok Avatar answered Sep 22 '22 15:09

Shinnok


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.

like image 20
ismail Avatar answered Sep 25 '22 15:09

ismail