我是为操作系统还是处理器编码?
据说,通过使用 C/C++,人们可以编写在该平台上运行的“本机”程序。我对什么被认为是本机感到困惑 - 处理器架构还是操作系统版本?
例如:
我有一个32位处理器和Windows 7(32位),我编译并生成.exe文件。是否保证可以在任何 Windows 7 32 位上运行? (32/64 位机器上的 Win 7 32 位)
编辑1: 我在这里并不想只使用 Windows 操作系统。我的例子也可以扩展到Linux。例如,在 32 位处理器上运行的 32 位 Linux 操作系统上生成可执行文件(默认情况下 a.out
),然后在 64 位处理器上的 32 位 Linux 上运行它。
编辑2: 感谢您的回复,但我也打算使用标准库和函数 - 没有特定于操作系统的内容。只是 ANSI 或 ISO C++ 标准指定的一次。没有引用操作系统特定的窗口系统或其他库。 谢谢
It is said that by using C/C++, one can write 'native' programs - that run on the platform. I am confused about what is considered native - the processor architecture or the OS version?
For example:
I have a 32 bit processor and Windows 7 ( 32 bit ), and I compile and generate and .exe file. Is it guaranteed to run on any Windows 7 32 Bit? ( Win 7 32 bit on 32/64 Bit machines )
Edit1:
I did not intend only Windows OS here. My example can be extended to Linux also. For example, generating an executable ( by default a.out
) on a 32 bit Linux OS running on 32 bit processor, and then running it on a 32bit Linux on a 64 bit processor.
Edit2:
Thanks for the responses, but I also intended that I am using the standard libraries and functions - nothing OS Specific. Just the once specified by the ANSI or ISO C++ Standard. No references to OS specific windowing systems or other libraries.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
两个都;有点儿。
Windows 和 Linux 上的实际指令并没有真正的区别,因为它们是针对单个 CPU 架构 (x86) 编译的。
然而,二进制文件不仅仅是在裸硬件上运行的代码。例如,它还包含告诉操作系统如何加载可执行文件及其依赖项的信息。二进制文件以特定格式打包。该格式在不同的操作系统中可能不同。
除此之外,操作系统还向应用程序提供一些服务(通过系统调用和API)。操作系统提供的服务及其使用方式因操作系统而异。
这些原因导致了这样一个事实:大多数情况下,本机二进制文件取决于其编译所针对的操作系统和 CPU 架构。
回答更新的问题:
C++ 标准不需要任何有关编译目标的性质的信息。它只是指定源级别的兼容性要求。因此,如果您坚持使用标准库,您将能够使用相同的源代码在提供一致 C++ 实现的平台上进行编译。该标准没有提及任何有关二进制可移植性的内容。正如我上面提到的,操作系统提供的原始系统调用可能会有所不同,标准库的实际实现取决于操作系统提供这些系统调用的方式。
为了在 Linux 上运行 Windows 二进制文件,您需要使用某种模拟,例如 Wine,它可以理解 Windows 二进制格式并模拟应用程序的 Windows API。
Both; kind of.
The actual instructions don't really differ across Windows and Linux as they are compiled down for a single CPU architecture (x86).
However, a binary is more than just code that runs on bare hardware. For instance, it also contains information that tells the operating system how to load the executable and its dependencies. The binary is packaged in a specific format. This format can be different in different operating systems.
Besides that, the operating system provides some services to the applications (through system calls and APIs). The services that operating systems provide, and the way they can be used varies from an operating system to another.
These reasons contribute to the fact that most of the time a native binary depends on both OS and CPU architecture it's compiled for.
Answer to the updated question:
C++ Standard doesn't require anything about the nature of the compiled target. It just specifies compatibility requirements at the source level. Consequently, if you stick to the standard libraries, you'll be able to use the same source code to compile on platforms that offer a conforming C++ implementation. The standard doesn't say anything about binary portability. As I mentioned above, the primitive system calls that operating systems provide can vary and the actual implementation of the standard library depends on the way those system calls are provided by the OS.
In order to run a Windows binary on Linux, you need to use some sort of emulation like Wine which understands Windows binary format and simulates Windows API for applications.
1) 处理器架构(加上静态或动态的目标库)
2) 是
32 位 Windows 应用程序将在 Windows 64 位平台上运行 哇。
1) The processor architecture (plus the targeted libraries static or dynamic)
2) Yes
A 32bit windows application will run on a Windows 64bit platform as WOW.
如果您的(Windows)编译器的目标体系结构是x86(32位),那么它可以在任何32位和64位Windows 7上运行。但如果是x86-64,则它只能在64位Windows 7上运行。
If your (windows) compiler's target architecture is x86 (32-bit) then it can run on any 32 bit and 64 bit Windows 7. But if its x86-64, it will only run on 64 bit Windows 7.
为了具体回答标题,您需要为两者编写代码。
可执行文件包含特定于处理器的机器代码,以及操作系统特定的有关如何加载/执行程序的大量元数据。
该代码还可能(通常确实)包含对操作系统定义的函数的调用。因此,虽然它只是任何兼容 CPU 都能理解的完全普通的机器代码,但它尝试调用仅存在于 Windows 上的代码。
所以“本土”实际上意味着两者。您可以为特定操作系统(以及所有兼容操作系统)和特定 CPU(以及所有兼容 CPU)进行编码。
对于 Windows,您通常会针对特定版本的 Windows,然后该程序将在该版本以及未来版本的 Windows 上运行。
对于运行 Windows(和您的程序)的处理器,可执行文件包含 x86 机器代码,可以在任何 x86 CPU 上执行,无论是来自 Intel、AMD、Via 还是其他制造商多年来兼容的处理器。
To answer the title specifically, you code for both.
The executable contains machine code, which is specific to the processor, and a lot of metadata for the OS on how to load/execute the program, which is specific to the OS.
The code may also (and typically does) contain calls into functions defined by the OS. And so, while it is just perfectly ordinary machine code that any compatible CPU will understand, it attempts to call code that only exists on Windows.
So "native" really means both. You code for the specific OS (and all compatible OS'es) and that specific CPU (and all compatible CPUs).
In the case of Windows, you typically target a specific version of Windows, and the program will then work on that, and future versions of Windows.
for the processor on which Windows (and your program) runs, the executable contains x86 machine code, which can be executed on any x86 CPU, whether it is from Intel, AMD, Via or whoever else have made compatible processors over the years.
在无法看到您的代码的情况下,只有您可以告诉我们您正在为 32 位平台还是 64 位平台编码 - 例如,如果您将指针转换为 32 位 int,然后返回到指针,那么您正在编码对于 32 位机器,而如果您使用诸如
int_ptr
这样的类型,则无论您的代码是针对 32 位机器还是 64 位机器编译,您都是安全的。同样,为 Windows 桌面进行编码时,您的编码可以采用计算机的字节顺序。如您的示例所示,如果您为 32 位 Windows 7 编译该代码,那么它也将在 64 位 Windows 7 上运行。如果您使用 Windows 7 功能,则它不会在更早的版本上运行版本。微软非常擅长向后兼容,所以它可能会运行在更高的版本上。
Without being able to see your code, only you can tell us whether you're coding for 32-bit or 64-bit platform - for example, if you reinterpret_cast a pointer into a 32 bit int then back to a pointer, you are coding for 32-bit, whereas if you use a type such as
int_ptr
you are safe whether your code is compiled for 32 or 64 bit machines. Similarly, coding for Windows desktops your coding can assume the machine's endianess.If, as in your example, you compile that code for 32-bit Windows 7, then it will also run on 64 bit Windows 7. If you use Windows 7 features, it won't run on earlier versions. Microsoft are very good at backward compatibility, so it probably will run on later versions.
简短的回答:不。
更长:当您编译“本机代码”时,您是针对特定的处理器架构进行编译的; MIPS、ARM、x86、68k、Sparc 等。这些架构的字长可以为 8、16、32 和 64(也有例外)。此外,这些架构还可以进行一代又一代的扩展,例如 MMX、SSE、SSE2、Neon 等。
您还需要考虑操作系统、可以使用哪些库以及不同的调用约定。
所以,没有任何保证。但是,如果您在 Windows 7 上使用 MSVC 进行编译,它几乎可以保证在 Windows 7 上运行。我认为它目前仅适用于 x86。
Short answer: No.
Longer: When you compile "native code", you compile for a specific processor architecture; MIPS, ARM, x86, 68k, Sparc and so on. These architectures can have a wordlength of 8, 16, 32 and 64 (there are exceptions). Also these architectures can have extensions from generation to generation like MMX, SSE, SSE2, Neon and such.
Also you need to consider the operating system and what libraries you can use, and different calling conventions.
So, there's no guarantee. But if you compile with MSVC on Windows 7 it's almost guaranteed to run on Windows 7. I think it only exist for x86 at the moment.