Are C++ applications cross-platform?

前端 未结 6 1492
北荒
北荒 2020-12-07 15:35

One of the first things I learned as a student was that C++ applications don\'t run on different operating systems. Recently, I read that Qt based C++ applications run every

6条回答
  •  无人及你
    2020-12-07 16:16

    Yes. No. Maybe. What is cross-platform C++ code? Cross-platform C++ code is such a code that can be compiled under different operation systems without the need to be modified.

    That means, if you explicitly use any platform-dependant headers, your code is no longer cross-platform. Qt solves this problem in the following way: they provide wrappers for everything that is platform-specific. For example, imagine that you are using QFile to open/read/write a file. Your code looks like

    QFile file(filename);
    file.open(QFile::ReadOnly);
    //other stuff
    

    You can compile this code under any OS as long as you have a suitable compiler and Qt libraries for that OS. The code hidden under QFile will use the OS-appropriate file-handling functions, but that shouldn't concern you.

    Also, if you only use the standard library, your code can be compiled anywhere where a C++ compiler is present.

    The already-compiled applications, however, are not cross-platform in a way that, say, Java applications are - for instance, you can't compile an app for Windows and then run in in Linux, you will have to recompile your code under Linux instead.

提交回复
热议问题