简体   繁体   English

在Windows和Linux下开发的C ++程序之间的区别

[英]The difference between a program in C++ developed under Windows and Linux

What's the difference between a program developed in C++ under Windows and Linux? 在Windows和Linux下用C ++开发的程序有什么区别?

Why can't a program developed under Windows in C++ be used under Linux? 为什么不能在Linux下使用在C ++下用Windows开发的程序?

Native programs are not compatible because Windows has a completely different set of API's than Linux, for one. 原生程序不兼容,因为Windows拥有与Linux完全不同的一组API。 As others have mentioned, each platform uses a different executable format as well. 正如其他人所提到的,每个平台也使用不同的可执行格式。 Also both platforms have their own set of libraries that programs will be linked against and/or share. 此外,两个平台都有自己的一组库,程序将链接和/或共享。 For example, a Windows program will typically be developed in Visual Studio using windows-specific libraries such as MFC , Win32 API, etc. These libraries are not available in linux, so the program will not even compile unless care is taken to make sure cross-platform libraries (such as QT) are used. 例如,Windows程序通常使用特定于Windows的库(如MFCWin32 API等)在Visual Studio中开发。这些库在linux中不可用,因此除非注意确保交叉,否则程序甚至不会编译-platform库(如QT)被使用。

If you are careful, however, you can use cross-platform libraries in your code and you can get the same program to compile under both platforms. 但是,如果您小心,可以在代码中使用跨平台库,并且可以在两个平台下编译相同的程序。 For such a program you would need to carefully put any platform-specific details (file system locations, etc) in their own files. 对于这样的程序,您需要仔细地将任何特定于平台的详细信息(文件系统位置等)放在他们自己的文件中。 Then, you would need to setup the proper #define statements and/or makefile directives to ensure the proper files are included in the build for each platform. 然后,您需要设置正确的#define语句和/或makefile指令,以确保每个平台的构建中包含正确的文件。

Of course, if you use a "cross-platform" language such as Java or Python, and do not use any platform-specific code in your implementation, then your program can run under both environments. 当然,如果您使用“跨平台”语言(如Java或Python),并且在实现中不使用任何特定于平台的代码,那么您的程序可以在两种环境下运行。

Note Although the executable formats are different, some programs developed on Windows can be executed under Linux using an emulator called WINE . 注意虽然可执行格式不同,但在Windows上开发的某些程序可以使用名为WINE的模拟器在Linux下执行。

  • Windows and Linux use different container formats to hold the executable code ( PE vs ELF ). Windows和Linux使用不同的容器格式来保存可执行代码( PE vs ELF )。
  • Windows and Linux have completely different APIs (except for trivial programs that only use the CRT and STL ) Windows和Linux有完全不同的API(除了仅使用CRT和STL的简单程序)
  • Windows and Linux have a completely different directory structure Windows和Linux具有完全不同的目录结构

You could write a program that can use either set of APIs (for example, using Qt ), and that can handle either directory structure, but you still won't be able to run the same file on both operating systems because of the differing container formats. 您可以编写一个程序,可以使用任何一组API(例如,使用Qt ),并且可以处理任一目录结构,但由于容器不同,您仍然无法在两个操作系统上运行相同的文件格式。

This can be solved by using Wine . 这可以通过使用Wine来解决。

In a nutshell, 简而言之,

Furthermore, even if there were a tool to convert between PE and ELF, the program instructions necessary to interface with the operating system are completely different between Windows and Linux. 此外,即使有一个在PE和ELF之间进行转换的工具,与Windows操作系统连接所需的程序指令在Windows和Linux之间也完全不同。 Only the most restricted computation-only code (that only does calculations and doesn't interact with the operating system at all) could be ported between systems without special action. 只有最受限制的仅计算代码(只进行计算并且根本不与操作系统交互)可以在系统之间移植而无需特殊操作。 However, this is rarely done. 但是,很少这样做。

I believe some versions of Linux allow you to directly load device drivers designed for Windows without recompiling. 我相信某些版本的Linux允许您直接加载专为Windows设计的设备驱动程序而无需重新编译。 However, this is an extremely special purpose application and the technique is not generally used. 然而,这是一种非常特殊的用途,并且通常不使用该技术。

Each operating system defines an API. 每个操作系统都定义了一个API。 If you code to call the Win32 API , it won't be there on Linux. 如果您编写代码来调用Win32 API ,它将不会出现在Linux上。 If you code to the POSIX API, it won't jump right out at you in Windows. 如果您编写POSIX API代码,它将不会在Windows中向您跳转。

To learn more about this, download a significant open source program (for example, Perl or Python ) and see how its 'configure' script makes arrangements to compile in either place. 要了解更多相关信息,请下载一个重要的开源程序(例如, PerlPython ),并查看其“configure”脚本如何安排在任何一个地方进行编译。

When a C++ program is compiled on a platform, it ultimately changes into a form that machine can understand (ie Machine code). 当在平台上编译C ++程序时,它最终会变成机器可以理解的形式(即机器代码)。 Under the hood, the program uses system calls to perform privileged action. 在引擎盖下,程序使用系统调用来执行特权操作。 These system calls are implemented via methods or APIs. 这些系统调用通过方法或API实现。 These methods differ from platform to platform. 这些方法因平台而异。 Hence on every platform, the compiled code is different. 因此,在每个平台上,编译的代码都是不同的。 There are many cross-compilers available if you want to compile your code for a different platform. 如果要为不同的平台编译代码,可以使用许多交叉编译器。

C++ is itself portable. C ++本身就是可移植的。 But some C++ libraries are not. 但是有些C ++库不是。 If a C++ program uses some libraries, which are not portable, then this program is not portable. 如果C ++程序使用某些不可移植的库,则该程序不可移植。

For example, a C++ program uses MFC to draw the GUI stuff, because MFC is supported only in Windows, so this C++ program cannot be compiled or run on Linux directly. 例如,C ++程序使用MFC绘制GUI内容,因为MFC仅在Windows中受支持,因此无法在Linux上直接编译或运行此C ++程序。

There are two major reasons. 有两个主要原因。

Theoretically , the same program (source code) for some languages like C, can run on both Windows and Linux. 从理论上讲 ,某些语言(如C语言)的相同程序(源代码)可以在Windows和Linux上运行。 But the compilation only differs; 但汇编只有不同; this means you have to compile the same source code file for each platform. 这意味着您必须为每个平台编译相同的源代码文件。

But actually, each operating system has a different set of APIs. 但实际上,每个操作系统都有一组不同的API。 And different techniques to get job done faster... Which usually attract developers to use them. 以及更快地完成工作的不同技术......这通常会吸引开发人员使用它们。 And they don't stick to standards, so they lose portability. 而且他们不遵守标准,因此他们失去了便携性。

This was for native programs... Anyway, there are the Java and Python languages... They are truly cross-platform, but you have to sacrifice speed for sake of portability. 这是针对原生程序的...无论如何,有JavaPython语言......它们是真正的跨平台,但为了便携性,你必须牺牲速度。

This is a large topic. 这是一个很大的话题。

  1. First, Windows and Linux aren't binary comparable. 首先,Windows和Linux不具有二进制可比性。 This means that even the simplest of programs will not be recognized from one machine to the other. 这意味着即使是最简单的程序也无法从一台机器识别到另一台机器。 This is why interpreted languages like PHP , Perl , Python and Java are becoming so popular, but even these don't all support the same set of features on each platform. 这就是PHPPerlPythonJava等解释性语言变得如此受欢迎的原因,但即使这些语言并不都支持每个平台上的相同功能集。

  2. Library dependence / OS support: Any significantly complicated program will need to access the system is some way, and many of the features available on one system are not available on the other. 库依赖/操作系统支持:任何显着复杂的程序都需要以某种方式访问​​系统,并且在一个系统上可用的许多功能在另一个系统上不可用。 There are a million examples; 有一百万个例子; just look on so for the Linux equivalent of blank or Windows equivalent of blank. 只是看看Linux等效的空白或Windows等效的空白。 Moving beyond OS support applications are built mostly on top of libraries of functions and some of those are just not available on both systems. 超越OS支持应用程序主要是在函数库之上构建的,其中一些在两个系统上都不可用。

Not to be overly pedantic, but developing a program is different than building it and executing it. 不要过于迂腐,但开发一个程序不同于构建和执行它。 In many cases, a program written on one operating system can be built and compiled to execute on another. 在许多情况下,可以构建和编译在一个操作系统上编写的程序以在另一个操作系统上执行。 Other programs, as others have pointed out, rely on certain functionality provided only by a particular OS or libraries resident only on that OS. 正如其他人所指出的,其他程序依赖于仅由特定操作系统或仅驻留在该操作系统上的库提供的某些功能。 As a result, it must be built and run on that OS. 因此,必须在该操作系统上构建并运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM