C ++应用程序是否跨平台? [英] Are C++ applications cross-platform?

查看:198
本文介绍了C ++应用程序是否跨平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学到的第一件事是,C ++应用程序不在不同的操作系统上运行。最近,我读到基于Qt的C ++应用程序运行无处不在。那么,发生了什么事? C ++应用程序是否跨平台?

解决方案


  1. 如果我编译源代码,它将在任何地方运行。


  2. API / ABI兼容性。操作系统是否以代码可以理解的方式提供了组件的接口?


  3. 二进制兼容性。代码能否在目标主机上运行?




源代码兼容



C ++ 是定义结构,内存,文件可以如何读写的标准。

  #include< iostream> 
int main(int argc,char ** argv)
{
std :: cout< Hello World< std :: endl;
}

写入过程数据的代码(例如 grep awk sed )通常是跨平台的。



当你想与用户交互时,现代操作系统有一个GUI,这些不是跨平台的,并且导致为特定平台编写代码。



诸如 qt wxWidgets 之类的库具有多个平台的实现, code> qt 而不是 Windows iOS



这些匿名化库的问题是,他们利用平台X的一些特殊好处,平台。



例如 Windows 使用 WaitForMultipleObjects 函数,它允许您等待不同类型的事件发生,或UNIX上的 fork 函数,它允许您的进程的两个副本以明显的共享状态。在UI中,窗体外观和行为略有不同(例如,颜色选择器,最大化,最小化,跟踪窗口外面的鼠标,手势的行为)。



当你需要做的工作对你很重要时,你可能最终想要编写平台特定的代码来利用特定应用程序的优势。



C 库 sqlite 是广泛的跨平台代码,但其低级IO是平台特定的,保证数据库的完整性(数据真正写入磁盘)。



因此,如Qt库这样的工作,他们可能会产生不满意的结果,



API / ABI兼容性



UNIX和Windows的不同版本有一些形式的相容性。这些允许为一个版本的操作系统构建的二进制文件在其他版本的操作系统上运行。



在UNIX中,构建机器的选择定义了兼容性。您希望支持的最低操作系统版本应该是您的构建机器,并且它将生成与后续次要版本兼容的二进制文件,直到它们进行中断更改(弃用库)。



在Windows和Mac OS X上,您可以选择一个SDK,以允许您将一组操作系统定位到与更改相同的问题。



在Linux上,每个内核版本都是ABI与任何其他版本不兼容,并且内核模块需要为每个内核版本重新编译。



二进制兼容性



这是CPU理解代码的能力。这比你想象的更复杂,因为x64芯片可以运行x86代码(取决于操作系统的支持)。



通常一个C ++程序包装在里面一个容器(PE可执行文件,ELF格式),它由操作系统使用来解包代码和数据的部分并加载库。这使得最终的程序有二进制(代码类型)和API(容器的格式)不兼容的形式。



同样今天如果你编译一个x86 Windows应用程序在Visual Studio 2015上针对Windows 7),那么如果处理器没有SSE2指令(大约10年CPU),代码可能无法执行。



从PowerPC更改为x86,他们提供了一个仿真层,允许旧的PowerPC代码在x86平台上的仿真器中运行。



因此,一般二进制不兼容性是一个阴暗的区。可以产生识别无效指令(例如SSE2)的OS,并且在故障中模拟行为,这可以在新特征出来时被更新,并且保持代码运行,即使它是二进制不兼容的。 p>

即使您的平台无法运行某种形式的指令集,也可以仿真并兼容。


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 everywhere. So, what is going on? Are C++ applications cross-platform or not?

解决方案

  1. Source code compatible. If I compile the source code, will it run everywhere.

  2. API/ABI compatibility. Does the OS provide the interface to its components in a way that the code will understand?

  3. Binary compatibility. Is the code capable of running on the target host?

Source code compatible

C++ is a standard which defines how structures, memory, files can be read and written.

#include <iostream>
int main( int argc, char ** argv )
{
     std::cout << "Hello World" << std::endl;
}

Code written to process data (e.g. grep, awk, sed) is generally cross-platform.

When you want to interact with the user, modern operating systems have a GUI, these are not cross-platform, and cause code to be written for a specific platform.

Libraries such as qt or wxWidgets have implementations for multiple platforms and allow you to program for qt instead of Windows or iOS, with the result being compatible with both.

The problem with these anonymizing libraries, is they take some of the specific benefits of platform X away in the interest of uniformity across platforms.

Examples of this would be on Windows using the WaitForMultipleObjects function, which allows you to wait for different types of events to occur, or the fork function on UNIX, which allows two copies of your process to be running with significant shared state. In the UI, the forms look and behave slightly different (e.g. color-picker, maximize, minimize, the ability to track mouse outside of your window, the behaviour of gestures).

When the work you need to be done is important to you, then you may end up wanting to write platform specific code to leverage the advantages of the specific application.

The C library sqlite is broadly cross-platform code, but its low-level IO is platform specific, so it can make guarantees for database integrity (that the data is really written to disk).

So libraries such as Qt do work, they may produce results which are unsatisfactory, and you end up having to write native code.

API/ABI compatibility

Different releases of UNIX and Windows have some form of compatibility with each other. These allow a binary built for one version of the OS to run on other versions of the OS.

In UNIX the choice of your build machine defines the compatibility. The lowest OS revision you wish to support should be your build machine, and it will produce binaries compatible with subsequent minor versions until they make a breaking change (deprecate a library).

On Windows and Mac OS X, you choose an SDK which allows you to target a set of OS's with the same issues with breaking changes.

On Linux, each kernel revision is ABI incompatible with any other, and kernel modules need to be re-compiled for each kernel revision.

Binary compatibility

This is the ability of the CPU to understand the code. This is more complex than you might think, as the x64 chips, can be capable (depending on OS support) of running x86 code.

Typically a C++ program is packaged inside a container (PE executable, ELF format) which is used by the operating system to unpack the sections of code and data and to load libraries. This makes the final program have both binary (type of code) and API (format of the container) forms of incompatibilities.

Also today if you compile a x86 Windows Application (targeting Windows 7 on Visual Studio 2015), then the code may fail to execute if the processor does not have SSE2 instructions (about 10 years old CPU).

Finally when Apple changed from PowerPC to x86, they provided an emulation layer which allowed the old PowerPC code to run in an emulator on the x86 platform.

So in general binary incompatibility is a murky area. It would be possible to produce an OS which identified invalid instructions (e.g. SSE2) and in the fault, emulated the behaviour, this could be updated as new features come out, and keeps your code running, even though it is binary incompatible.

Even if your platform is incapable of running a form of instruction set, it could be emulated and behave compatibly.

这篇关于C ++应用程序是否跨平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆