windows Windows和Linux下开发的C++程序的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2349827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between a program in C++ developed under Windows and Linux
提问by skydoor
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?
为什么Windows下用C++开发的程序不能在Linux下使用?
采纳答案by SLaks
- Windows and Linux use different container formats to hold the executable code (PEvs ELF).
- Windows and Linux have completely different APIs (except for trivial programs that only use the CRT and STL)
- Windows and Linux have a completely different directory structure
- Windows 和 Linux 使用不同的容器格式来保存可执行代码(PE与ELF)。
- Windows 和 Linux 具有完全不同的 API(除了仅使用 CRT 和STL 的琐碎程序)
- 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来解决。
回答by Justin Ethier
Native programs are not compatible because Windows has a completely different set of API's than Linux, for one. 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 有一套与 Linux 完全不同的 API,一方面。正如其他人所提到的,每个平台也使用不同的可执行格式。此外,这两个平台都有自己的一组库,程序将针对这些库进行链接和/或共享。例如,一个Windows程序通常会使用Visual Studio Windows专用库,比如开发MFC
,Win32
API等,这些库不是在Linux中提供,所以程序甚至不会编译除非小心,以确保跨平台使用库(如 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,并且在您的实现中不使用任何特定于平台的代码,那么您的程序可以在这两种环境下运行。
NoteAlthough the executable formats are different, some programs developed on Windows can be executed under Linux using an emulator called WINE.
注意尽管可执行格式不同,但某些在 Windows 上开发的程序可以使用名为WINE的模拟器在 Linux 下执行。
回答by Greg Hewgill
In a nutshell,
简而言之,
- Windows runs PE format executables
- Linux runs ELF format executables
- Windows 运行PE 格式的可执行文件
- Linux 运行ELF 格式的可执行文件
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. 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.
而且,即使有工具可以在PE和ELF之间进行转换,Windows和Linux之间与操作系统接口所需的程序指令也完全不同。只有最受限制的仅计算代码(仅进行计算并且根本不与操作系统交互)可以在系统之间移植而无需特殊操作。但是,很少这样做。
I believe some versions of Linux allow you to directly load device drivers designed for Windows without recompiling. However, this is an extremely special purpose application and the technique is not generally used.
我相信某些版本的 Linux 允许您直接加载为 Windows 设计的设备驱动程序,而无需重新编译。然而,这是一个非常特殊的用途,该技术并不普遍使用。
回答by bmargulies
Each operating system defines an API. If you code to call the Win32 API, it won't be there on Linux. If you code to the POSIXAPI, it won't jump right out at you in Windows.
每个操作系统都定义了一个 API。如果您编写代码来调用Win32 API,则它不会出现在 Linux 上。如果您对POSIXAPI 进行编码,它不会在 Windows 中直接出现在您身上。
To learn more about this, download a significant open source program (for example, Perlor Python) and see how its 'configure' script makes arrangements to compile in either place.
要了解更多相关信息,请下载一个重要的开源程序(例如,Perl或Python)并查看其“配置”脚本如何安排在任一位置进行编译。
回答by Bharat Singh
When a C++ program is compiled on a platform, it ultimately changes into a form that machine can understand (i.e. Machine code). Under the hood, the program uses system calls to perform privileged action. These system calls are implemented via methods or APIs. 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++程序在一个平台上编译时,它最终会变成一种机器可以理解的形式(即机器码)。在幕后,该程序使用系统调用来执行特权操作。这些系统调用是通过方法或 API 实现的。这些方法因平台而异。因此,在每个平台上,编译的代码都是不同的。如果您想为不同的平台编译代码,有许多交叉编译器可用。
回答by Yin Zhu
C++ is itself portable. But some C++ libraries are not. If a C++ program uses some libraries, which are not portable, then this program is not portable.
C++ 本身是可移植的。但有些 C++ 库不是。如果一个 C++ 程序使用了一些不可移植的库,那么这个程序是不可移植的。
For example, a C++ program uses MFCto 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中支持,所以这个C++程序不能直接在Linux上编译或运行。
回答by Radian
There are two major reasons.
有两个主要原因。
Theoretically, the same program (source code) for some languages like C, can run on both Windows and Linux. But the compilation only differs; this means you have to compile the same source code file for each platform.
理论上,某些语言(如 C)的相同程序(源代码)可以在 Windows 和 Linux 上运行。但编译只是不同;这意味着您必须为每个平台编译相同的源代码文件。
But actually, each operating system has a different set of APIs. 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.
但实际上,每个操作系统都有一组不同的 API。还有不同的技术可以更快地完成工作......通常会吸引开发人员使用它们。而且它们不遵守标准,因此失去了便携性。
This was for native programs... Anyway, there are the Javaand Pythonlanguages... They are truly cross-platform, but you have to sacrifice speed for sake of portability.
这是针对本机程序的......无论如何,有Java和Python语言......它们确实是跨平台的,但你必须为了可移植性而牺牲速度。
回答by rerun
This is a large topic.
这是一个很大的话题。
First, Windows and Linux aren't binary comparable. 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, Pythonand Javaare becoming so popular, but even these don't all support the same set of features on each platform.
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. 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.
回答by sizzzzlerz
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.
不要过于迂腐,但开发程序与构建和执行程序不同。在许多情况下,在一个操作系统上编写的程序可以构建和编译以在另一个操作系统上执行。正如其他人指出的那样,其他程序依赖于仅由特定操作系统或仅驻留在该操作系统上的库提供的某些功能。因此,它必须在该操作系统上构建和运行。