1

I have a very light Linux OS that does not have any compiler on it. How can I install gcc or g++ on it? The target hardware is an armv7-a processor. Can I compile gcc on my x86 system and then install it on my armv7 Linux??

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You can. This is called cross-compilation. It is a very common method of compiling programs for different platforms. – Piotr Siupa Aug 13 '19 at 14:26
  • 2
    The other question - do you really need a compiler on that system? Or cross-compiling the needed binaries on some dev machine will suffice? – Eugene Sh. Aug 13 '19 at 14:33
  • You might be able to find a pre-built GCC for running on the target hardware, just as you presumably find the pre-built kernel and pre-built utilities for the target hardware. However, you do need quite a lot of space to do that — and space may be at a premium. – Jonathan Leffler Aug 13 '19 at 16:00

2 Answers2

3

Yes, you can compile your code on your x86 device and then export it to your arm device. But for this, you need a special compiler. This process is called "cross-compilation", where you compile a code for a special target device on an other device.

For arm devices, the one I used was the "arm-linux-gnueabihf-gcc" compiler.

Once installed, you can use it just like your casual gcc compiler. For example, on linux, it would be something like this :

$ arm-linux-gnueabihf-gcc -o your_program your_program.c

You then export the compiled output to your device, and it should work.

kokopelli
  • 208
  • 1
  • 7
  • The point being that you wouldn't usually use this technique to install the _compiler_ on the target system, but to instead just keep the compiler on your main dev machine then install your final build result to the target. Cut out the middle man entirely. – Lightness Races in Orbit Aug 13 '19 at 15:33
  • compiling compilers is common practice. Also, some Linux distributions ship compiler for their target system. It's not that unusual. – linuxUser123 Aug 14 '19 at 06:31
  • how install "arm-linux-gnueabihf-gcc" on fedora?? sudo dnf install arm-linux-gnueabihf-gcc do not work. – alireza sadeghpour Aug 14 '19 at 08:59
  • I think it's the "gcc-arm-linux-gnueabihf" package, not the "arm-linux-gnueabihf-gcc". – kokopelli Aug 14 '19 at 13:30
  • How if i want compile an kernel module for target platform? – alireza sadeghpour Aug 15 '19 at 08:07
  • For a kernel module, it's more tricky. You can still use cross compilation, but you need to give a link to the linux kernel folder in the compilation process, and the linux kernel must be of the same version as the one on your target device (if the kernel on your device is 4.17 and the one used to compile is 4.16, it won't work, and maybe even the configuration must be the same, but not sure about this last point). If you are using a makefile, the line to compile should be something like that: make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C M=$(shell pwd) – kokopelli Aug 15 '19 at 13:02
0

Cross-compiling may be the solution for you It allows you to compile executables for one architecture on a system of a different architecture. here is link http://www.landley.net/writing/docs/cross-compiling.html