Embedded System Development: What is "Cross Compilation" and Environment Variables in Embedded Development

 What is "Cross Compilation" ?



In embedded system development, you often hear a term: cross-compilation. What exactly is "cross-compilation"? And why do we need to use "cross-compilation"?


1.Compilation 

In software development, code written in high-level languages is referred to as source code, such as files with the .c extension written in C language, or files with the .cpp extension written in C++. Source code cannot be executed by the machine directly; it must be converted into binary machine code (instructions + data) to be executed by the CPU. The process of converting source code into machine code is called compilation, and this task is performed by a compiler.

The compiler performs syntax checking on the source code, and only source code without syntax errors can be compiled successfully. After compilation, the source code does not generate the final executable file; instead, it generates an intermediate file called an object file. For example, Visual C++ object files have the .obj extension, while GCC object files have the .o extension.

Source code may consist of multiple source files, such as main.c/fun1.c/fun2.c, etc. The compiler processes each source file individually. Therefore, for each source file, a corresponding object file is generated.

Object files cannot be executed because they may have some issues, such as problems caused by reference relationships between source files.

For example: File A.c references the variable "EXT_someflag" in File B.c. A.o and B.o are compiled separately from A.c and B.c, respectively. A.o does not contain the definition of the variable "EXT_someflag," so it must rely on B.o to form a complete code.

The same situation applies to the reference of library functions in the source code.

The process of linking object files generated after compilation into a complete, executable file based on their internal reference relationships is called linking. The linking process is performed by a linker.

Therefore, to generate an executable file from source files, it requires both compilation and linking. For convenience, we also refer to this process as compilation. The topic of this article, "cross-compilation," also includes both compilation and linking steps, without further elaboration.

In contrast to cross-compilation is "native compilation."

Understanding native compilation helps to better understand cross-compilation. So, let's first understand what native compilation is.


2.Local compilation

Local compilation, also known as "native compilation," refers to compiling source code on the same platform where the compiled program will be executed. The term "platform" here refers to the CPU architecture and operating system. For example, an executable file compiled using Visual C++ on the Intel x86 architecture and Windows 10 platform will run on the same Intel x86 architecture and Windows 10 platform.

3.Cross compilation

Cross compilation refers to compiling source code on one platform and executing the compiled program on a different platform. For example, generating an executable file using a cross-compilation toolchain on an Intel x86 architecture/Linux (Ubuntu) platform and running it on an ARM architecture/Linux platform. Cross compilation is relatively complex and involves considerations such as:

  1. CPU architecture: e.g., ARM, x86, MIPS, etc.
  2. Endianness: big-endian or little-endian.
  3. Support for floating-point arithmetic.
  4. Application Binary Interface (ABI).

Why do we use cross compilation? There are mainly two reasons:

  1. The target system for cross compilation usually has limited memory, basic display devices, or no capability for local compilation.
  2. The CPU architecture or operating system of the platform capable of source code compilation differs from the target platform.

A cross-compilation toolchain is indispensable for cross compilation and is a skill that embedded developers must master.


Environment Variables in Embedded Development





I apologize for misunderstanding your request. Here's the translation without the code formatting:

Embedded Linux uses the Make command for compilation, and before compiling, you need to first write a Makefile. In the Makefile, you may see lines like: "CC = $(CROSS_COMPILE)gcc", where "CROSS_COMPILE" is an environment variable.

Linux is a multi-user operating system, and environment variables are used to represent the environment configurations of different users. Environment variables can be divided into permanent environment variables and temporary environment variables according to their lifecycle. They can also be categorized into system environment variables and user environment variables based on their scope.

Temporary environment variables are added directly in the Linux terminal using the export command. For example: export ARCH=arm This command assigns the value "arm" to the ARCH variable, and it is only effective for the current terminal session. It will be lost once the terminal is closed.

To make the configuration permanent, you need to modify the .profile file in the user's directory, which is ~/.profile. For example, you can use the Vim editor to open the file: $ vim ~/.profile At the end of the file, add the following command: export ARCH=arm #or other commands

After saving the file, execute the command $ source ~/.profile or restart the Linux system, and the variable will become permanently effective.


Using the command $ echo $ARCH allows you to view the value of the set variable. If set correctly, it will output "arm".

This modification only affects the current user. If you want it to apply to all users, you need to modify the /etc/profile file. For example: $ vim /etc/profile, add export ARCH=arm at the end of the file, and save it. Then execute the command $ source /etc/profile or restart the Linux system. This way, the environment variable will be effective for all users.

Here are several commands for viewing or setting environment variables:

  • export: The export command without parameters lists all current environment variables. With parameters, it sets the variable as an environment variable.
  • env: Displays the existing environment variables in the system.
  • echo: Outputs the value of an environment variable.

Common environment variables include:

  • PATH: System path.
  • HOME: User's home directory.
  • SHELL: Path of the shell.
  • CROSS_COMPILE: Cross-compiler.
  • PWD: Current working directory.