General questions about GCC and cross compiling

后端 未结 2 1951
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 03:19

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

相关标签:
2条回答
  • 2021-01-04 03:43

    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.

    0 讨论(0)
  • 2021-01-04 03:53

    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.

    0 讨论(0)
提交回复
热议问题