有关 GCC 和交叉编译的一般问题

发布于 2024-10-07 11:53:53 字数 304 浏览 0 评论 0原文

最近,我一直在尝试使用 GCC 进行交叉编译,并发现了一个看似复杂的区域:工具链。

我不太明白这一点,因为我的印象是 GCC 可以为大多数常见架构创建二进制机器代码,而其他真正重要的是您链接的库以及创建什么类型的可执行文件。

GCC 不能自己做所有这些事情吗?通过 GCC 的单个构建,将所有适当的库和正确的标志发送到 GCC,我可以为 Windows x86 机器生成 PE 可执行文件,然后为嵌入式 Linux MIPS 设备创建 ELF 可执行文件,最后为 OSX PowerPC 创建可执行文件机器? 如果没有,有人可以解释一下你将如何实现这一目标吗?

Recently I've been playing around with cross compiling using GCC and discovered what seems to be a complicated area, tool-chains.

I don't quite understand this as I was under the impression GCC can create binary machine code for most of the common architectures, and all that else really matters is what libraries you link with and what type of executable is created.

Can GCC not do all these things itself? With a single build of GCC, all the appropriate libraries and the correct flags sent to GCC, could I produce a PE executable for a Windows x86 machine, then create an ELF executable for an embedded Linux MIPS device and finally an executable for an OSX PowerPC machine?
If not can someone explain how you would achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走野 2024-10-14 11:53:53

通过一次构建 GCC,所有
适当的库和正确的
发送到 GCC 的标志,我可以制作一个
Windows x86 的 PE 可执行文件
机器,然后创建一个 ELF 可执行文件
对于嵌入式 Linux MIPS 设备和
最后是 OSX 的可执行文件
PowerPC 机?如果没有,有人可以
解释一下你将如何实现这一目标?

不会。GCC 的单个构建会为一个目标体系结构生成目标代码。您需要一个针对 Intel x86 的构建、一个针对 MIPS 的构建以及一个针对 PowerPC 的构建。然而,尽管您可以通过一次 GCC 调用将源代码构建为可执行文件,但编译器并不是您需要的唯一工具。在底层,它还使用汇编器 (as) 和链接器 (ld),并且这些需要针对目标架构和平台进行构建。通常 GCC 使用 GNU binutils 包中的这些工具的版本,因此您也需要为目标平台构建它。

您可以在此处了解有关构建交叉编译工具链的更多信息。

我不太明白这一点,因为我以前
海湾合作委员会可以创造的印象
大多数的二进制机器代码
通用架构

从 GCC 本身的源代码可以构建到针对各种体系结构的编译器的意义上来说,这是正确的,但您仍然需要单独的构建。


关于 -march,这不允许相同版本的 GCC 在平台之间切换。相反,它用于选择允许用于同一系列处理器的指令。例如,现代 x86 处理器支持的某些指令不受最早的 x86 处理器的支持,因为它们是后来引入的(例如 MMX 和 SSE 等扩展指令集)。当您传递 -march 时,GCC 会启用该处理器及其前身支持的所有操作码。引用GCC手册:

选择特定的 CPU 类型时
为此适当地安排事情
特定芯片,编译器不会
生成任何无法运行的代码
没有 -march=cpu-type 的 i386
正在使用的选项。

With a single build of GCC, all the
appropriate libraries and the correct
flags sent to GCC, could I produce a
PE executable for a Windows x86
machine, then create an ELF executable
for an embedded Linux MIPS device and
finally an executable for an OSX
PowerPC machine? If not can someone
explain how you would achieve this?

No. A single build of GCC produces object code for one target architecture. You would need a build targeting Intel x86, a build targeting MIPS, and a build targeting PowerPC. However, the compiler is not the only tool you need, despite the fact that you can build source code into an executable with a single invocation of GCC. Under the hood, it makes use of the assembler (as) and linker (ld) as well, and those need to be built for the target architecture and platform. Usually GCC uses the versions of these tools from the GNU binutils package, so you'd need to build that for the target platform too.

You can read more about building a cross-compiling toolchain here.

I don't quite understand this as I was
under the impression GCC can create
binary machine code for most of the
common architectures

This is true in the sense that the source code of GCC itself can be built into compilers that target various architectures, but you still require separate builds.


Regarding -march, this does not allow the same build of GCC to switch between platforms. Rather it's used to select the allowable instructions to use for the same family of processors. For example, some of the instructions supported by modern x86 processors weren't supported by the earliest x86 processors because they were introduced later on (such as extension instruction sets like MMX and SSE). When you pass -march, GCC enables all opcodes supported on that processor and its predecessors. To quote the GCC manual:

While picking a specific cpu-type will
schedule things appropriately for that
particular chip, the compiler will not
generate any code that does not run on
the i386 without the -march=cpu-type
option being used.

清晨说晚安 2024-10-14 11:53:53

如果您想尝试交叉编译,并且不想自己构建工具链,我建议您查看 CodeSourcery。他们有一个基于 GNU 的工具链,并且他们的免费“Lite”版本支持相当多的工具链架构。我已经将它用于 Linux/ARM 和 Android/ARM。

If you want to try cross-compiling, and don't want to build the toolchain yourself, I'd recommend looking at CodeSourcery. They have a GNU-based toolchain, and their free "Lite" version supports quite a few architectures. I've used it for Linux/ARM and Android/ARM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文