英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论