编译器如何进行交叉编译到不同的操作系统和架构?

huangapple go评论65阅读模式
英文:

How can a compiler cross-compile to a different OS and architecture?

问题

我对Go语言(自1.5版本起)具有内置的交叉编译选项非常感兴趣。但是如何编译适用于不同操作系统和架构的程序呢?这意味着需要了解目标机器的语言和平台,并可能需要相应的行为。

英文:

I'm very intrigued by the fact that Go (since v1.5) has in-built cross compilation options.
But how is it possible to compile for a different OS and architecture?
I mean that would require knowing (and probably behaving like) the target machine language and platform.

答案1

得分: 4

是的,Go编译器需要了解目标操作系统的工作原理,但它不需要像目标操作系统一样运行,因为Go编译器不会运行编译后的可执行二进制文件,它只需要生成它。

所有Go工具需要知道的是不同操作系统的二进制格式以及操作系统和架构的详细信息(例如指令集、字长、字节序、对齐方式、可用寄存器等;更多信息请参考此处)。而这些知识已经内置在Go工具中。

英文:

> I mean that would require knowing (and probably behaving like) the target machine language and platform.

Yes, the Go compiler has to know how the target operating system works, but it doesn't need to behave like the target OS, as the Go compiler will not run the compiled executable binary, it just needs to produce it.

All the Go tools need to know is the binary formats of the different Operating Systems, and OS and architectural details (such as the instruction set, word size, endianness, alignment, available registers etc.; more info on this). And this knowledge is built into the Go tools.

huangapple
  • 本文由 发表于 2016年11月11日 19:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/40546840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定