Cross Compiler

What is Compiler

The compiler is a program that translates source code into object code. The compiler derives its name from the way it works, looking at the entire piece of source code and collecting and reorganizing the instructions. For example, there is a FORTRAN compiler for PCs and another for Apple Macintosh computers.

Why do we need a compiler to execute the program?

Because computers can’t understand the source code directly. It will understand only object-level code.

Source codes are human-readable format but the system cannot understand them.

So, the compiler is intermediate between human-readable format and machine-readable format.

Cross Compiler

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a Windows 7 PC but generates code that runs on an Android smartphone is a cross compiler. A cross compiler is necessary to compile code for multiple platforms from one development host. Direct compilation on the target platform might be infeasible, for example on embedded systems with limited computing resources. A cross compiler is necessary to compile for multiple platforms from one machine. A platform could be infeasible for a compiler to run on, such as for the microcontroller of an embedded system because those systems contain no operating system.

How does cross-compilation work?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. In paravirtualization, one computer runs multiple operating systems and a cross compiler could generate an executable for each of them from one main source.

How is Cross Compiler Different from Native Compiler?

1. Native Compiler :

Native compilers are compilers that generate code for the same platform on which it runs. It converts high language into the computer’s native language. For example Turbo C or GCC compiler

2. Cross compiler :

A Cross compiler is a compiler that generates executable code for a platform other than one on which the compiler is running. For example, a compiler that runs on a Linux/x86 box is building a program that will run on a separate Arduino/ARM.

Source: https://linuxbaya.blogspot.com/2020/09/cross-platform-development-toolchain.html

Why Cross Compile?

In theory, a PC user who wanted to build programs for some device could get the appropriate target hardware (or emulator), boot a Linux distro on that, and compile natively within that environment. While this is a valid approach (and possibly even a good idea when dealing with something like a Mac Mini), it has a few prominent downsides for things like a Linksys router or iPod:

  • Speed — Target platforms are usually much slower than hosts, by an order of magnitude or more. Most special-purpose embedded hardware is designed for low cost and low power consumption, not high performance. Modern emulators are actually faster than a lot of the real-world hardware they emulate, by virtue of running on high-powered desktop hardware.
  • Capability — Compiling is very resource-intensive. The target platform usually doesn’t have gigabytes of memory and hundreds of gigabytes of disk space the way a desktop does; it may not even have the resources to build “hello world”, let alone large and complicated packages.
  • Availability — Bringing Linux up on a hardware platform it’s never run on before requires a cross-compiler. Even on long-established platforms like Arm or Mips, finding an up-to-date full-featured prebuilt native environment for a given target can be hard. If the platform in question isn’t normally used as a development workstation, there may not be a recent prebuilt distro readily available for it, and if there is it’s probably out of date. If you have to build your own distro for the target before you can build on the target, you’re back to cross-compiling anyway.
  • Flexibility — A fully capable Linux distribution consists of hundreds of packages, but a cross-compile environment can depend on the host’s existing distro from most things. Cross-compiling focuses on building the target packages to be deployed, not spending time getting build-only prerequisites working on the target system.
  • Convenience — The user interface of headless boxes tends to be a bit cramped. Diagnosing build breaks is frustrating enough as it is. Installing from CD onto a machine that hasn’t got a CD-ROM drive is a pain. Rebooting back and forth between your test environment and your development environment gets old fast, and it’s nice to be able to recover from accidentally lobotomizing your test system.

Why is cross-compiling so hard?

Building a cross-compiler is significantly harder than building a compiler that targets the platform it runs on.” The problem exists due to the way libraries are built and accessed. In a normal situation, all the libraries are located in a specific spot and are used by all apps on that system.

Applications of Cross Compiler

  • Embedded Computers: Embedded computers has extremely limited resources. For example, a microwave oven will have an extremely small computer to read its keypad and door sensor, provide output to a digital display and speaker, and control the machinery for cooking food. This computer is generally not powerful enough to run a compiler, a file system, or a development environment.
  • Compiling for multiple machines. For example, a company may wish to support several different versions of an operating system or to support several different operating systems. By using a cross compiler, a single build environment can be set up to compile for each of these targets.
  • Bootstrapping: When developing software for a new platform, or the emulator of a future platform, one uses a cross compiler to compile necessary tools such as the operating system and a native compiler.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

PayUMoney Integration in Android

Why I Am Building CourseLit — An Open-source Alternative To Teachable, Thinkific, Podia etc.

Starting with Istio in GKE and Kiali [Basic]

How Pinterest is using Kubernetes for solving the Challenges?

Use Charles Proxy to Debug Network Requests in Flutter Apps

What is GraphQL? Will it Replace REST API?

Data Structures: Stack

Deploying Java application on AWS Lambda using Docker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Satyam Muluk

Satyam Muluk

More from Medium

AMA with Revellab

Dream Log 030622

Mosley’s Manifesto

2/14/2022