Readers may have questions, why use a cross compiler? In layman's terms, cross-compilation is to compile a program on one platform that can run on another platform with a different architecture, such as compiling on a PC platform (X86 CPU) to run on a CPU platform with ARM as the core. The compiled program cannot run on the X86 CPU platform, and must be put on the ARM CPU platform to run, although both platforms use Linux systems. This method is very useful when transplanting to different platforms and embedded development. Compared with cross-compilation, the usual compilation is called local compilation, that is, compiled on the current platform, and the compiled program is also executed locally. The compiler used to compile such cross-platform programs is called a cross-compiler. Relatively speaking, the tools used for local compilation are called local compilers. Therefore, to generate a program to run on the target machine, a cross-compilation tool chain must be used to complete it. Before cutting down and customizing the Linux kernel for use in embedded systems, due to the limited storage size of general embedded development systems, it is usually necessary to establish a cross-compilation tool chain for the target machine on a PC with superior performance, and use the cross-compilation tool chain Compile the program to be run on the target machine on the PC. The cross-compilation tool chain is a comprehensive development environment composed of a compiler, linker and interpreter. The cross-compilation tool chain is mainly composed of three parts: binutils, gcc and glibc. Sometimes for the consideration of reducing the size of the libc library, other c libraries can be used instead of glibc, such as uClibc, dietlibc and newlib. Establishing a cross-compilation tool chain is a very complicated process. If you don’t want to go through the complicated and tedious compilation process yourself, there are some compiled cross-compilation tool chains available for download on the Internet, but for the purpose of learning, readers need to learn from themselves. Make a cross-compilation tool chain. This chapter describes the production process of the ARM-based cross-compilation tool chain for embedded Linux through specific examples.
2.2 Building the ARM Linux cross-compilation tool chain
The first step in building a cross compiler is to determine the target platform. In the GNU system, each target platform has a clear format, and this information is used to identify the correct version of the different tools to be used during the build process. Therefore, when running GCC on a specific target machine, GCC searches the directory path for the application path that contains the target specification. The target specification format of GNU is CPU-PLATFORM-OS. For example, the x86/i386 target machine is named i686-pc-linux-gnu. The purpose of this chapter is to describe the establishment of a cross tool chain based on the ARM platform, so the target platform is named arm-linux-gnu.
There are usually 3 ways to build a cross tool chain.
Method 1 Compile and install the libraries and source codes required by the cross-compilation tool chain step by step, and finally generate the cross-compilation tool chain. This method is relatively difficult and suitable for readers who want to learn to build a cross tool chain in depth. If you just want to use a cross tool chain, it is recommended to use Method 2 or Method 3 to build a cross tool chain.
Method 2 Use the Crosstool script tool to compile and generate a cross-compilation tool chain at one time. This method is much simpler than Method 1, and the chance of error is very small. It is recommended to use this method to build a cross-compilation tool chain in most cases.
Method 3 Download the cross-compilation tool chain that has been made directly from the Internet (ftp.arm.kernel.org.uk). Needless to say, the advantages of this method are of course simple and trouble-free, but at the same time, this method has certain disadvantages that it is too limited. After all, it is built by others, that is, it is fixed and has no flexibility, so the library used is built And the version of the compiler may not be suitable for the program you want to compile, and there may be many inexplicable errors when using it. Readers are advised to use this method with caution.
In order to allow readers to truly learn the construction of the cross-compilation tool chain, the following will focus on the first two methods of building the ARM Linux cross-compilation tool chain.
2.2.1 Build a cross-compilation chain step by step
Step-by-step construction, as the name suggests, is to build a cross-compilation chain step by step, which is different from the one-time compilation and generation method of the Crosstool script tool described in section 2.2.2. This method is suitable for those who want to learn more about building a cross-compilation tool chain. This method is relatively difficult, and it is usually difficult, just like Tang Seng Xitian learning the scriptures, but this article will introduce each step of the construction in detail as much as possible, and readers can practice independently according to the content of this section and construct their own cross Tool chain. This process takes a long time. I hope readers have strong patience and perseverance to learn and practice it. Through practice, readers can be more aware of the cross-compiler construction process and the role of various toolkits. The resources required for this method are shown in Table 2.1.
Table 2.1 Required resources
Installation package
download link
Installation package
download link
linux-2.6.10.tar.gz
ftp.kernel.org
glibc-2.3.2.tar.gz
ftp.gnu.org
binutils-2.15.tar.bz2
ftp.gnu.org
glibc-linuxthreads-2.3.2.tar.gz
ftp.gnu.org
gcc-3.3.6.tar.gz
ftp.gnu.org
After downloading the above resources through the relevant sites, you can start to build a cross-compilation tool chain.
1. Create a working directory
First, create a working directory. The working directory is the directory under which the cross tool chain is built. Generally, there are no special requirements for the construction of the directory, and it can be created according to personal preference. The directory created below is self-defined by the author. The current user is defined as mike, so the user directory is /home/mike. First create a working directory (armlinux) under the user directory. The command line operations for creating a working directory are as follows:
# cd /home/mike
# mkdir armlinux
Then create 3 directories build-tools, kernel and tools under this working directory armlinux. The specific operations are as follows:
# cd armlinux
# mkdir build-tools kernel tools
The functions of each directory are as follows.
● build-tools is used to store the downloaded binutils, gcc, glibc and other source codes and the directory used to compile these source codes;
● kernel is used to store the kernel source code;
● tools are used to store the compiled cross-compilation tools and library files.
2. Create environment variables
The purpose of this step is to facilitate repeated input of the path, because repeating every same thing will always make people feel very troublesome. If the reader is not used to using environment variables, you can skip this step and enter the absolute path directly. The purpose of declaring the following environment variables is to be used when compiling the tool library later, which is very convenient for input, especially to reduce the risk of entering the wrong path.
# export PRJROOT=/home/mike/armlinux
# export TARGET=arm-linux
# export PREFIX=$PRJROOT/tools
# export TARGET_PREFIX=$PREFIX/$TARGET
# export PATH=$PREFIX/bin:$PATH
Note that the variables declared with export are temporary variables, that is, when you log out or replace the console, these environment variables disappear. If you need to use these environment variables, you must repeat the export operation, so it can sometimes be troublesome. Fortunately, environment variables can also be defined in the bashrc file, so that when logging out or changing the console, these variables will always be valid, and there is no need to always export these variables.
3. Compile and install Binutils
Binutils is one of the GNU tools. It includes linkers, assemblers and other tools for object files and archives. It is a tool for processing and maintaining binary codes. The programs included in installing the Binutils tool include addr2line, ar, as, c++filt, gprof, ld, nm, objcopy, objdump, ranlib, readelf, size, strings, strip, libiberty, libbfd, and libopcodes. A brief explanation of these procedures is as follows.
● addr2line converts the program address into file name and line number. Give it an address and an executable file name on the command line, and it will use the debugging information of the executable file to point out which file and line number are at the given address.
● ar create, modify, and extract archive files. The archive file is a large file containing multiple file contents, and its structure ensures that the original file contents can be restored.
● as is mainly used to compile the assembly files output by the GNU C compiler gcc, and the generated object files are linked by the linker ld.
● The c++filt linker uses it to filter C++ and Java symbols to prevent conflicts with overloaded functions.
● gprof displays various data of the program call segment.
● ld is a linker, which combines some objects with archive files, relocates data, and connects symbolic references. Usually, the last step in creating a new compiler is to call ld.
● nm lists the symbols in the target file.
● objcopy copies the contents of one type of object file to another type of object file.
● objdump displays information about one or more object files. Use options to control the information it displays. The information it displays is usually only of interest to those who write compilation tools.
● ranlib generates an archive file index and saves it in this archive file. The redistributable target file defined by each member of the archive file is listed in the index.
● readelf displays information about executable files in elf format.
● size lists the size of each segment of the target file and the overall size. By default, only one line of output is generated for each object file or each module in an archive file.
● strings Print the printable strings of a file. These strings are at least 4 characters long. You can also use the option -n to set the minimum length of the string. By default, it only prints the printable characters in the initialization and loadable sections of the target file; for other types of files, it prints the printable characters of the entire file. This program is very helpful for understanding the contents of non-text files.
● strip discards all or specific symbols in the target file.
● Libiberty contains functions used by many GNU programs, such as getopt, obstack, strerror, strtol, and strtoul.
● libbfd binary file description library.
● libopcode is a library used to process opcodes, and it is also used when generating some applications.
Binutils tool installation depends on tools such as Bash, Coreutils, Diffutils, GCC, Gettext, Glibc, Grep, Make, Perl, Sed, Texinfo, etc.
After introducing the Binutils tool, the following will introduce the process of installing binutils-2.15 step by step.
First unzip the binutils-2.15.tar.bz2 package, the command is as follows:
# cd $PRJROOT/build-tools
# tar –xjvf binutils-2.15.tar.bz2
Then configure the Binutils tool. It is recommended to create a new directory to store the configuration and compilation files, so that the source files and the compilation files can be separated separately. The specific operations are as follows:
# cd $PRJROOT/build-tools
# mkdir build-binutils
# cd build-binutils
# ../ binutils-2.15/configure --target=$TARGET --prefix=$PREFIX
The option -target means that the tool that is generated is arm-linux, and -prefix is the location where the executable file is installed. A lot of check information will appear when the above operations are performed, and finally a Makefile file will be generated. Next execute make and installation operations, the commands are as follows:
# make
# make install
The compilation process is slow and takes tens of minutes. After the installation is complete, check the files in the /home/mike/armlinux/tools/bin directory. If the results are as follows, the Binutils tool has been installed at this time.
# ls $PREFIX/bin
arm-linux-addr2line arm-linux-ld arm-linux-ranlib arm-linux-strip
arm-linux-ar arm-linux-nm arm-linux-readelf
arm-linux-as arm-linux-objcopy arm-linux-size
arm-linux-c++filt arm-linux-objdump arm-linux-strings
4. Get the kernel header file
The compiler needs to obtain the information needed for system function calls supported by the target platform through the header file of the system kernel. For the Linux kernel, the best way is to download a suitable kernel, and then copy the header files. A basic configuration of the kernel is needed to generate the correct header files; however, there is no need to compile the kernel. For the target arm-linux in this example, the following steps are required.
(1) Unzip linux in the kernel directory -2.6.10.tar.gz kernel package, execute the command as follows:
# cd $PRJROOT/kernel
# tar –xvzf linux-2.6.10.tar.gz
(2) Next, configure the compiled kernel to generate the correct header file, and execute the command as follows:
# cd linux-2.6.10
# make ARCH=arm CROSS_COMPILE=arm-linux- menuconfig
Among them, ARCH=arm means arm is the architecture, CROSS_COMPILE=arm-linux- means a cross compiler with arm-linux- prefix. You can also use config and xconfig instead of menuconfig. Make menuconfig is recommended, which is the most common configuration method used by kernel developers. Note that the processor type must be selected when configuring, here select Samsung S3C2410 (System Type->ARM System Type->/Samsung S3C2410), as shown in Figure 2.1. After configuration, exit and save, check whether the include/linux/version.h and include/linux/autoconf.h files in the kernel directory are generated. This is used when compiling glibc, if version.h and autoconf. The h file exists, indicating that the correct header file is generated.
Figure 2.1 Linux 2.6.10 kernel configuration interface
To copy the header files to the directory of the cross-compilation toolchain, you first need to create the tool header file directory inlcude in the /home/mike/armlinux/tools/arm-linux directory, and then copy the kernel header files to this directory. The specific operations are as follows:
# mkdir –p $TARGET_PREFIX/include
# cp –r $PRJROOT/kernel/linux-2.6.10/include/linux $TARGET_PREFIX/include
# cp –r $PRJROOT/kernel/linux-2.6.10/include/asm-arm $TARGET_PREFIX/include/asm
5. Compile and install boot-trap gcc
The purpose of this step is to build the arm-linux-gcc tool. Note that this gcc is not supported by the glibc library, so it can only be used to compile the kernel, BootLoader and other programs that do not require C library support. This is also used when creating the C library later. Compiler, so the creation of it is mainly to prepare for the creation of the C library. If you only want to compile the kernel and BootLoader, then you can end here after installing this. The installation command is as follows:
# cd $PRJROOT/build-tools
# tar –xvzf gcc-3.3.6.tar.gz
# mkdir build-gcc
# cd gcc-3.3.6
# vi gcc/config/arm/t-linux
Since it is the first time to install the ARM cross-compilation tool, there is no header file that supports the libc library, so add the operating parameter option -Dinhibit_libc -D__gthr_posix_h to the variable TARGET_LIBGCC2_CFLAGS in the gcc/config/arm/t-linux file to block the use of the header file. Otherwise, the /usr/inlcude header file is generally used by default.
Change TARGET_LIBGCC2-CFLAGS = -fomit-frame-pointer –fPIC to TARGET_LIBGCC2- CFLAGS=-fomit-frame-pointer–fPIC -Dinhibit_libc -D__gthr_posix_h
After modifying the t-linux file, save it, and then execute the configuration operation, as follows:
# cd build-gcc
# ../ build-gcc /configure --target=$TARGET --prefix=$PREFIX --enable-languages=c
--disable-threads --disable-shared
The option --enable-languages=c means that only C language is supported, and --disable-threads means that the thread function is removed. This function needs the support of glibc. --disable-shared means that only static library compilation is performed, and shared library compilation is not supported.
Next, perform the compilation and installation operations, the command is as follows:
# make
# make install
After the installation is complete, check under /home/mike/armlinux/tools/bin. If tools such as arm-linux-gcc have been generated, it means that the boot-trap gcc tool has been installed successfully.
6. Build glibc library
glibc is the GUN C library, which is a very important part of compiling Linux system programs. It is recommended to install the following tools before installing glibc-2.3.2:
● GNU make 3.79 or newer;
● GCC 3.2 or newer;
● GNU binutils 2.13 or newer.
First unzip the glibc-2.2.3.tar.gz and glibc-linuxthreads-2.2.3.tar.gz source code, the operation is as follows:
# cd $PRJROOT/build-tools
# tar -xvzf glibc-2.2.3.tar.gz
# tar -xzvf glibc-linuxthreads-2.2.3.tar.gz --directory=glibc-2.2.3
Then compile and configure. Before configuring glibc-2.2.3, you must create a new compile directory, otherwise configuration operations are not allowed in the glibc-2.2.3 directory. Here, create the name build-glibc in the $PRJROOT/build-tools directory The configuration operation is as follows:
# cd $PRJROOT/build-tools
# mkdir build-glibc
# cd build-glibc
# CC=arm-linux-gcc ../glibc-2.2.3 /configure --host=$TARGET --prefix="/usr"
--enable-add-ons --with-headers=$TARGET_PREFIX/include
The option CC=arm-linux-gcc is to set the CC (Cross Compiler) variable to the newly compiled gcc, and use it to compile glibc. --prefix="/usr" defines a directory for installing some data files that have nothing to do with the target machine. By default it is the /usr/local directory. --enable-add-ons tells glibc to use the linuxthreads package, which has been placed in the glibc source code directory above. This option is equivalent to -enable-add-ons=linuxthreads. --with-headers tells glibc linux kernel header file directory location.
After configuration, you can compile and install glibc, the specific operations are as follows:
# make
# make install
7. Compile and install the complete gcc
Since the gcc installed for the first time does not support cross-glibc, glibc has been installed now, so you need to recompile to support cross-glibc. And the gcc above only supports C language, and now it can support both C language and C++ language. The specific operations are as follows:
# cd $PRJROOT/build-tools/gcc-2.3.6
# ./configure --target=arm-linux --enable-languages=c,c++ --prefix=$PREFIX
# make
# make install
After the installation is complete, you will find more files such as arm-linux-g++ and arm-linux-c++ in the $PREFIX/bin directory.
# ls $PREFIX/bin
arm-linux-addr2line arm-linux-g77 arm-linux-gnatbind arm-linux-ranlib
arm-linux-ar arm-linux-gcc arm-linux-jcf-dump arm-linux-readelf
arm-linux-as arm-linux-gcc-3.3.6 arm-linux-jv-scan arm-linux-size
arm-linux-c++ arm-linux-gccbug arm-linux-ld arm-linux-strings
arm-linux-c++filt arm-linux-gcj arm-linux-nm arm-linux-strip
arm-linux-cpp arm-linux-gcjh arm-linux-objcopy grepjar
arm-linux-g++ arm-linux-gcov arm-linux-objdump jar
8. Test cross compilation toolchain
So far, the method of building a cross-compilation tool chain has been introduced step by step. Let's test the cross-compilation tool chain just established through a simple program to see if it can work normally. Write a simple hello.c source file, the content is as follows:
#include <stdio.h>
int main()
{
printf("Hello,world!\n");
return 0;
}
Use the following command to compile and generate an executable file named hello after compilation. The file type can be viewed through the file command. When the following information is displayed, it indicates that the cross tool chain is normally installed, and the executable file of the ARM system is generated through compilation. Note that the executable file compiled by this cross-compilation chain can only be executed under the ARM system, and cannot be executed on an X86-based ordinary PC.
# arm-linux-gcc –o hello hello.c
# file hello
hello: ELF 32-bit LSB executable, ARM, version 1 (ARM), for GNU/Linux 2.4.3,
dynamically linked (uses shared libs), not stripped
2.2.2 Build a cross tool chain with Crosstool
Crosstool is a set of scripting tools that can build and test different versions of gcc and glibc for those architectures that support glibc. It is also an open source project, and the download address is http://kegel.com/crosstool. Using Crosstool to build a cross tool chain is much easier than the above step-by-step compilation, and it is also much more convenient. This method is recommended for readers who just need to build a cross compilation tool chain for work. Use the Crosstool tool to build the required resources as shown in Table 2.2.
Table 2.2 Required resources
Installation package
download link
crosstool-0.42.tar.gz
http://kegel.com/crosstool
linux-2.6.10.tar.gz
ftp.kernel.org
binutils-2.15.tar.bz2
ftp.gnu.org
gcc-3.3.6.tar.gz
ftp.gnu.org
glibc-2.3.2.tar.gz
ftp.gnu.org
glibc-linuxthreads-2.3.2.tar.gz
ftp.gnu.org
linux-libc-headers-2.6.12.0.tar.bz2
ftp.gnu.org
1. Prepare resource files
First download the required resource files linux-2.6.10.tar.gz, binutils-2.15.tar.bz2, gcc-3.3.6.tar.gz, glibc- 2.3.2.tar.gz, glibc-linuxthreads- 2.3.2.tar.gz and linux-libc-headers- 2.6.12.0.tar.bz2. Then put these toolkit files in the newly created /home/mike/downloads directory, and finally decompress crosstool-0.42.tar.gz in the /home/mike directory, the command is as follows:
# cd /home/mike
# tar –xvzf crosstool-0.42.tar.gz
2. Create script file
Then you need to create your own compilation script, named arm.sh, in order to simplify writing a rm.sh, look for the closest script file demo-arm.sh as a template, then copy the content of the script to arm.sh, modify the arm.sh script, the specific operations are as follows:
# cd crosstool-0.42
# cp demo-arm.sh arm.sh
# vi arm.sh
The content of the modified arm.sh script is as follows:
#!/bin/sh
set -ex
TARBALLS_DIR=/home/mike/downloads # Define where the toolchain source code is stored.
RESULT_TOP=/opt/crosstool # Define the installation directory of the tool chain
export TARBALLS_DIR RESULT_TOP
GCC_LANGUAGES="c,c++" # Define support for C, C++ language
export GCC_LANGUAGES
# Create /opt/crosstool directory
mkdir -p $RESULT_TOP
# Compile the tool chain, the process takes several hours to complete.
eval ‘cat arm.dat gcc-3.3.6-glibc-2.3.2.dat’ sh all.sh --notest
echo Done.
3. Create a configuration file
Note the arm.dat and gcc-3.3.6-glibc-2.3.2.dat files in the arm.sh script file. These two files are used as configuration files for Crosstool compilation. The contents of the arm.dat file are as follows, which are mainly used to define the configuration file, the name of the generated compilation tool chain, and the definition of compilation options.
KERNELCONFIG=‘pwd’/arm.config # Kernel configuration
TARGET=arm-linux- # The name of the compiled toolchain
TARGET_CFLAGS="-O" # Compile options
The content of the gcc-3.3.6-glibc-2.3.2.dat file is as follows. The file mainly defines the libraries needed during the compilation process and the version defined by it. If some libraries are found not to exist during the compilation process, Crosstool will automatically Downloaded from related websites, this tool is relatively smart at this point and very useful.
BINUTILS_DIR=binutils-2.15
GCC_DIR=gcc-3.3.6
GLIBC_DIR=glibc-2.3.2
GLIBCTHREADS_FILENAME=glibc-linuxthreads-2.3.2
LINUX_DIR=linux-2.6.10
LINUX_SANITIZED_HEADER_DIR=linux-libc-headers-2.6.12.0
4. Execute script
After preparing the Crosstool script file and configuration file, start to execute the arm.sh script to compile the cross-compilation tool. The specific execution commands are as follows:
# cd crosstool-0.42
# ./arm.sh
After several hours of lengthy compilation, a new cross-compilation tool will be generated in the /opt/crosstool directory, which includes the following:
arm-linux-addr2line arm-linux-g++ arm-linux-ld arm-linux-size
arm-linux-ar arm-linux-gcc arm-linux-nm arm-linux-strings
arm-linux-as arm-linux-gcc-3.3.6 arm-linux-objcopy arm-linux-strip
arm-linux-c++ arm-linux-gccbug arm-linux-objdump fix-embedded-paths
arm-linux-c++filt arm-linux-gcov arm-linux-ranlib
arm-linux-cpp arm-linux-gprof arm-linux-readelf
5. Add environment variables
Then add the generated compiler toolchain path to the environment variable PATH. The method to add is to add the following line at the end of the system /etc/bashrc file, as shown in Figure 2.2.
Figure 2.2 Use the Vi editor to add environment variables to the bashrc file
export PATH=/opt/crosstool/gcc-3.3.6-glibc-2.3.2/arm-linux/bin:$PATH
After setting the environment variables, it means that the cross-compilation tool chain has been built, and then you can use the method in section 2.2.1.8 to test the newly built tool chain, so I won’t go into details here.
Embedded cross toolchain compilation
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service