Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Write Qt code once and compile it on many platforms



  • Hi all,

    Here's a statement I have some question about. I'm not sure I understood it completely right.

    "Because of Qt's broad applicability, you really can write your code once and just recompile it on a different platform in order to have it work out of the
    box
    ."

    What does it exactly mean?

    For example I write a program by Qt using pure code (C++). It works well on my Windows machine.

    Now I want to use that program on other platforms too, such as: Linux, Unix, iOS, Android and so on.
    What is the normal way to accomplish this please?
    Is what the writer said not just re-compiling the program using different kits so that we can obtain proper executable files for those other platforms? I mean the use of kits.

    Thanks.



  • @tomy said in Write Qt code once and compile it on many platforms:

    Is what the writer said not just re-compiling the program using different kits so that we can obtain proper executable files for those other platforms?

    Correct!


  • Lifetime Qt Champion

    @tomy Easiest way is to build directly on the other platform. For example if you want to build for Linux, then do it on Linux (install Qt on a Linux machine and build like you do on Windows). For iOS you need a Mac with XCode, there is no other way. For Android you need to install Android NDK on your Windows machine and Qt for Android. Check Qt documentation for Android and iOS support.
    Building on Windows for Linux is in theory possible, but it is way easier to do it on Linux directly.



  • @VRonin
    So if we aim at having our program for some other platform too, we just add its kit to our Qt Creator and re-compile the code by that kit. Right?

    @jsulm,
    Got it.

    Thanks to both of you.
    But one subtle point here, I think for platforms like iOS and Android which are mostly running on smartphones, it's better to write code using QML not Qt.


  • Lifetime Qt Champion

    @tomy Well, QML is better for modern UIs yes :-) But you still can write the logic in C++, in fact you will have to as QML has limited functionality.



  • @tomy said in Write Qt code once and compile it on many platforms:

    So if we aim at having our program for some other platform too, we just add its kit to our Qt Creator and re-compile the code by that kit. Right?

    Yes, but. As @jsulm correctly pointed out cross compiling (i.e. compile on one operating system for another) can become complicated very fast, especially on windows host.

    The fastest way is to set up a clear build process (qmake or CMake are perfect for this) and set up a CI system with different hosts compiling for each target.



  • @VRonin

    (qmake or CMake are perfect for this) and set up a CI system with different hosts compiling for each target.

    Will you explain it a little more, or simpler.
    What do you mean by a CI, then, please?


  • Lifetime Qt Champion

    @tomy

    What do you mean by a CI, then, please?

    A Continuous Integration system, like Jenkins



  • CI = Continuous Integration

    For open source programs you can use for free Appveyor for Windows and Travis for Linux and OSX. From the Ubuntu host you can also cross compile for android. I never tested the OSX image compiling for iOS but should be doable.
    For the chat example we set up a bare bones CI system to make sure that the code compiles everywhere but those platforms allow you to also run unit tests and automate the deployment

    Qt itself uses a a CI system called Coin



  • I've worked on projects where the target Operating System was RedHat Linux, another developer also working on the same system was using a Windows laptop and Windows 7 to develop and test on, the source code did not have any differences in, however we did have different project files to build for each platform.

    This was a few years ago using Qt 5.6



  • @SPlatten said in Write Qt code once and compile it on many platforms:

    however we did have different project files to build for each platform.

    This should not be necessary, a few ifs in the project file (either qmake or cmake) should be more than enough to have 1 project for every platform.

    Visual Studio merges the source structure of the IDE and the build system so sometimes it's just less of a hassle to have a separate vcproj file however it's not advisable for big projects. some careful configuration of CMake allows you to provide VS developers with perfectly usable project files



  • Sorry, my error, we didn't have multiple project files but we did have conditions in the same file for different operating system builds.



  • @VRonin

    The Wikipedia page was not clear enough for me; rather baffling actually! I read the introduction of this page for that topic. It seems that CI is for sharing projects mostly. A sharing project or some integration to build a better app was not in question. I don't know why you pointed to that. I look forward to seeing your helpful intention about that and will talk about it more afterwards.

    But as a plan B, what do you think of this please:

    • Using Qt Creator on Windows for Windows programs
    • If there's an interesting program on Windows, I simply copy its code and paste on the Linux Qt Creator and compile it there to have its Linux version.
    • Building Android projects using the Arm7 kit on Windows for Android apps using QML.
    • For other Oses, like iOS, since there isn't any MacOSX machine to my hands, I have to bow out of that purpose until I get such a machine and then on that will go for producing iOS apps.

  • Lifetime Qt Champion

    @tomy Your plan B is perfectly fine.

    A CI system however, automates that. You develop on your Windows machine, check in a new revision into the version control system and the CI automatically builds the Linux, mac, Android, iOS version for you.

    Regards


  • Banned

    This post is deleted!


  • @aha_1980

    You develop on your Windows machine, check in a new revision into the version control system and the CI automatically builds the Linux, mac, Android, iOS version for you.

    Wow, that would be great if it were that easy as it seems. :)

    A couple of questions:
    1- Can it work as good as the plan B?
    2- Are you sure using a CI system, say, on Windows, it's feasible to create apps for iOS too? (I mean, without a real Apple machine)


  • Lifetime Qt Champion

    @tomy You would probably set up multiple servers on different operating systems. But that could be in a computing center, so you would not have to care for the hardware.

    Probably you can buy such things as on-demand service, I've never searched but I'm sure such service exists.



  • @aha_1980

    Thank you. I suppose I'd better get back to the plan B. ;)


Log in to reply