问题描述
最近我一直在与交叉编译使用GCC打转转,发现似乎是一个复杂的区域,工具链。
我不太明白这一点,因为我是在IM pression GCC下可以创建二进制机器code对大多数常见的结构,和所有的其他真正重要的是什么库与你联系什么可执行文件类型创建。
能不GCC做所有这些事情本身?与海湾合作委员会的一个版本,所有适当的库和正确的旗帜送到GCC,我可以生产用于在Windows x86机器PE可执行文件,然后创建一个嵌入式Linux MIPS设备ELF可执行文件,最后一个可执行文件的OSX的PowerPC机?
如果不是有人可以解释你将如何实现这一目标?
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.
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:
这篇关于关于GCC和交叉编译一般问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!