简体   繁体   English

C如何跨平台?

[英]How is C cross platform?

C language is ultimately compiled to machine specific assembly language. C语言最终被编译成机器特定的汇编语言。 Then how can C be cross platform if every processor has its own assembly language syntax?那么如果每个处理器都有自己的汇编语言语法,C 怎么能跨平台呢? If I write an operating system kernel in C then how can I make it run across a variety of processors?如果我用 C 编写操作系统内核,那么如何让它在各种处理器上运行?

C is portable at the source code level. C 在源代码级别是可移植的。 This means that you can recompile your program in the platform of choice and have your application running there.这意味着您可以在选择的平台上重新编译您的程序并让您的应用程序在那里运行。

You can find the history of C in the wikipedia , for example.例如,您可以在 wikipedia 中找到C历史 At that time, programming was carried out in assembly for each specific machine, so having a high-level (by that time) programming language was a bless: it sped up development and also made possible to port programs from one machine to a new one just by porting the compiler (the C compiler could later be written in C itself, again speeding up the porting process, but that's another story).当时,编程是针对每台特定机器在汇编中进行的,因此拥有高级(到那时)编程语言是一种福气:它加快了开发速度,还可以将程序从一台机器移植到新机器上只需移植编译器(C 编译器以后可以用 C 本身编写,再次加快移植过程,但那是另一回事了)。

Specifically, the idea of porting UNIX between operating systems appeared when they had UNIX running in the PDP-7, and the PDP-11 appeared.具体来说,当在 PDP-7 和 PDP-11 中运行 UNIX 时,就出现了在操作系统之间移植 UNIX 的想法。 Finally, even UNIX's kernel was rewritten in C, so it made the operating system really popular because of the easy porting: there was only the need to write a small amount of assembly for very specific parts of the kernel, so you could have UNIX up and running in a new machine shortly, at least compared to other operating systems of that time.最后,即使是 UNIX 的内核也是用 C 重写的,因此它使操作系统非常受欢迎,因为它易于移植:只需要为内核的特定部分编写少量汇编代码,就可以让 UNIX 运行起来并且很快就会在新机器上运行,至少与当时的其他操作系统相比是这样。

The language C itself is cross-platform, because you don't directly run C code on machines. C 语言本身是跨平台的,因为您不会直接在机器上运行 C 代码。 The C source code is compiled to assembly, and assembly is the platform specific code. C 源代码被编译为汇编,汇编是特定于平台的代码。 The only non cross-platform part are the compilers and the resulting assembly.唯一的非跨平台部分是编译器和生成的程序集。

In essence, you can use the same C code, with different compilers, to create the specific assembly needed.本质上,您可以使用具有不同编译器的相同 C 代码来创建所需的特定程序集。 Since you use the same C source code, its deemed cross-platform.由于您使用相同的 C 源代码,因此它被视为跨平台。

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

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