How to build an ARM cross-compiling environment in Linux

Source: Internet
Author: User
Tags ftp site gcov

The simplest example is to download buildtoolchain.tar.gz for installation. Sometimes you just want to experience the installation and customization process yourself. This is also the pleasure of Linux. I collected some information from the Internet for future reference.
How to build a cross-compiling environment for Embedded Development


 
<Language = java type = text/java script>
 
 

 
Liang yuanen, Software Engineer

September 2005

Before embedded development, you must first establish a cross-compilation environment, which is a development environment consisting of a set of compilers, connectors, and libc libraries. The article illustrates the production process of these embedded cross-compilation development tools through a specific example.

With the development and application of a large number of consumer electronic products and the continuous robustness and strength of Linux operating systems, more and more embedded systems are coming into people's lives, and the application scope is getting wider and wider.

Before cutting and customizing Linux and applying it to your embedded system, the storage size of embedded development systems is limited, generally, you need to create a cross-compiling environment for the target machine on your powerful PC. This is a comprehensive development environment consisting of compilers, connectors, and interpreters. The cross-compilation tool consists of binutils, gcc, and glibc. Sometimes, to reduce the libc library size, you can use other c libraries to replace glibc, such as uClibc, dietlibc, and newlib. Creating a cross-compilation toolchain is quite complicated. If you don't want to go through complicated compilation processes, you can download some of the available cross-compilation toolchains on the Internet.

Next we will take the establishment of a cross-compilation development environment for arm as an example to explain the entire process. Other architectures are similar to this, as long as some corresponding changes are made. My development environment is, host machine i386-redhat-7.2, target machine arm.

The process is as follows:

1. Download source files, patches, and create compiled directories.

2. Create a kernel header file

3. binutils)

4. Create an initial Compiler (bootstrap gcc)

5. Create a c library (glibc)

6. Create a full set of compilers (full gcc)

Download source files, patches, and create compiled Directories

1. Select the Software Version Number

When selecting the software version number, first check the INSTALL file in the glibc source code. This section lists the binutils and gcc versions required for glibc compilation. For example, gcc 2.95 or above is recommended in the glibc-2.2.3/INSTALL file, binutils 2.10.1 or later.

The versions of the software I selected are:

Linux-2.4.21 + rmk2
Binutils-2.10.1
Gcc-2.95.3
Glibc-2.2.3
Glibc-linuxthreads-2.2.3

. You can download the Linux kernel from www.kernel.org or its image.

Binutils, gcc, and glibc you can download from the fsf ftp site ftp://ftp.gun.org/gnu/ or its image. When compiling glibc, use the kernel header file of the include directory in the Linux kernel. If a variable is not defined and compilation fails, you can change the kernel version number. For example, when I started using linux-2.4.25 + vrs2, compiling the glibc-2.2.3 times BUS_ISA was not defined, and then came up with 2.4.23 to start with its name being changed to CTL_BUS_ISA. If you are not fully sure that the kernel you have changed is completely changed, do not change the kernel. Instead, reduce or increase the version number of your Linux kernel to adapt to glibc.

Gcc version, it is recommended to use gcc-2.95 or above. Too old versions may cause problems during compilation. Gcc-2.95.3 is a relatively stable version, but also a gcc version recommended by kernel developers.

If you find that the software cannot be compiled in the past, it may be because some of the software you selected has added some new features that are not supported by other selected software, and the version number of the software is reduced accordingly. For example, I began to use gcc-3.3.2, found that the compilation is not, reporting as, ld and other versions are too old, I will drop gcc to 2.95.3. Most of the new versions have not undergone a large number of tests. We recommend that you do not use them.


 

2. Create a working directory

First, we create several directories for working:

In your user directory, I use the user liang. Therefore, if the user directory is/home/liang, create a project directory embedded first.

$ Pwd
/Home/liang
$ Mkdir embedded
 

In the project directory embedded, create three directories: build-tools, kernel, and tools.

Build-tools-stores the source code of binutils, gcc, and glibc you downloaded, And the directories used to compile the source code.

Kernel-used to store your kernel source code and kernel patches.

Tools-used to store compiled cross-compilation tools and library files.

$ Cd embedded
$ Mkdir build-tools kernel tools
 

After the execution, the directory structure is as follows:

$ Ls embedded
Build-tools kernel tools
 

3. Output and Environment Variables

The following environment variables are output to facilitate compilation.

$ Export PRJROOT =/home/liang/embedded
$ Export TARGET = arm-linux
$ Export PREFIX = $ PRJROOT/tools
$ Export TARGET_PREFIX = $ PREFIX/$ TARGET
$ Export PATH = $ PREFIX/bin: $ PATH
 

If you are not familiar with environment variables, you can directly use absolute or relative paths. If I do not use environment variables, absolute paths are generally used, and relative paths sometimes fail. The environment variables can also be defined in the. bashrc file, so that when you log out or change the console, you do not need to always export these variables.

The architecture corresponds to your TAEGET variable in the following table:

 

You can use the config. sub script in glibc to check whether your TARGET variable is supported. For example:

$./Config. sub arm-linux
Arm-unknown-linux-gnu
 

In my environment, config. sub is under the glibc-2.2.3/scripts directory.

For more information about HOWTO, see The GNU Toolchain for ARM Target HOWTO In The ARM architecture and Linux for PowerPC Embedded Systems HOWTO In The PowerPC architecture. The selection of TARGET may be helpful.

4. Create a compilation directory

In order to separate the source code from the files generated during compilation, the compilation is generally not in the source code directory. Another directory should be created for compilation. Use the following command to build a directory for compiling the source code of binutils, gcc, and glibc you downloaded.

$ Cd $ PRJROOT/build-tools
$ Mkdir build-binutils build-boot-gcc build-glibc gcc-patch
 

Build-binutils-directory for compiling binutils
Build-boot-gcc-compile the gcc startup directory
Build-glibc-directory for compiling glibc
Build-gcc-compile all gcc Directories
Gcc-patch-directory where gcc patches are installed

Patches for gcc-2.95.3 include gcc-2.95.3-2.patch, gcc-2.95.3-no-fixinc.patch, and gcc-2.95.3-returntype-fix.patch, which can be downloaded from http://www.linuxfromscratch.org.

Then put the source code of the binutils-2.10.1, gcc-2.95.3, glibc-2.2.3, and glibc-linuxthreads-2.2.3 you downloaded into the build-tools directory.

Take a look at your build-tools Directory, which has the following content:

$ Ls
Binutils-2.10.1.tar.bz2 build-gcc-patch
Build-binutls build-glibc glibc-2.2.3.tar.gz
Build-boot-gcc gcc-2.95.3.tar.gz glibc-linuxthreads-2.2.3.tar.gz
 


 

Create a kernel header file

Put the kernel source code you downloaded from www.kernel.org into the $ PRJROOT/kernel directory.

Go to your kernel directory:

$ Cd $ PRJROOT/kernel
 

Unlock kernel source code

$ Tar-xzvf linux-2.4.21.tar.gz
 

Or

$ Tar-xjvf linux-2.4.21.tar.bz2
 

If the kernel version is earlier than 2.4.19, a linux directory will be generated and renamed without the version number.

$ Mv linux linux-2.4.x
 

Patch your Linux Kernel

$ Linux-2.4.21 cd
$ Patch-p1 <../patch-2.4.21-rmk2
 

Compile the kernel to generate the header file

$ Make ARCH = arm CROSS_COMPILE = arm-linux-menuconfig

You can also use config and xconfig to replace menuconfig, but in this way, some configuration file options may not be set and the following header files required for compilation are not generated. We recommend that you use make menuconfig, which is the most common configuration method used by kernel developers. Exit and save the configuration. Check the include/linux/version in the kernel directory. h and include/linux/autoconf. h file is generated, which is used to compile glibc, version. h and autoconf. the existence of the h file also indicates that you have generated the correct header file.

You also need to create several correct links.

$ Cd include
$ Ln-s asm-arm asm
$ Cd asm
$ Ln-s arch-epxa arch
$ Ln-s proc-armv proc
 

Next, create a link to your kernel header file for your cross-compilation environment.

$ Mkdir-p $ TARGET_PREFIX/include
$ Ln-s $ PRJROOT/kernel/linux-2.4.21/include/linux $ TARGET_PREFIX/include/linux
$ In-s $ PRJROOT/kernel/linux-2.4.21/include/asm-arm $ TARGET_PREFIX/include/asm
 

You can also copy the Linux kernel header file.

$ Mkdir-p $ TARGET_PREFIX/include
$ Cp-r $ PRJROOT/kernel/linux-2.4.21/include/linux $ TARGET_PREFIX/include
$ Cp-r $ PRJROOT/kernel/linux-2.4.21/include/asm-arm $ TARGET_PREFIX/include
 


 

Binutils)

Binutils is a collection of binary tools, including the commonly used as and ld.

First, decompress the downloaded binutils source file.

$ Cd $ PRJROOT/build-tools
$ Tar-xvjf binutils-2.10.1.tar.bz2
 

Then go to the build-binutils directory to configure and compile binutils.

$ Cd build-binutils
$ ../Binutils-2.10.1/configure -- target = $ TARGET -- prefix = $ PREFIX
 

The -- target option indicates that we generated the arm-linux tool, and the -- prefix indicates the location where the executable file is installed.

There will be a lot of check and the Makefile file will be generated.

With Makefile, compile and install binutils. The command is simple.

$ Make
$ Make install
 

Let's take a look at the files generated under $ PREFIX/bin.

$ Ls $ PREFIX/bin
Arm-linux-addr2line arm-linux-gasp arm-linux-objdump arm-linux-strings
Arm-linux-ar arm-linux-ld arm-linux-ranlib arm-linux-strip
Arm-linux-as arm-linux-nm arm-linux-readelf
Arm-linux-c ++ filt arm-linux-objcopy arm-linux-size
 

Let's explain what the above generated executable files are.

Add2line-convert the address you are looking for into a file and a line number. It uses the debug information.

Ar-generate, modify, and unlock an archive file

As-gnu Assembler

C ++ filt-C ++ and java have a type of overload function. The used overload function will be compiled and converted to the Assembly label, c ++ filt is to implement this inverse conversion, and obtain the function name based on the number.

Gasp-gnu Compiler pre-compiler.

Ld-gnu Connector

Nm-list the symbols and corresponding addresses of the target file

Objcopy-convert a target file of a certain format into a target file of another format

Objdump-display the information of the target file

Ranlib-generates an index for an archive file and stores the index in the archive file.

Readelf-display the information of the target file in elf format

Size-displays the Size of each section of the target file and the Size of the target file.

Strings-print the printable Strings in the target file. The default length is 4.

Strip-Strip all the symbolic information of the target file


 

Create an initial Compiler (bootstrap gcc)

First, go to the build-tools directory and unzip the gcc source code.

$ Cd $ PRJROOT/build-tools
$ Tar-xvzf gcc-2.95.3.tar.gz
 

Then go to the gcc-2.95.3 directory and patch gcc

$ Gcc-2.95.3 cd
$ Patch-p1 <../gcc-patches/gcc-2.95.3.-2. patch
$ Patch-p1 <../gcc-patch/gcc-2.95.3.-no-fixinc.patch
$ Patch-p1 <../gcc-patch/gcc-2.95.3-returntype-fix.patch
Echo timestamp> gcc/cstamp-h.in
 

Before compiling and installing gcc, we need to change the file $ PRJROOT/gcc/config/arm/t-linux
TARGET_LIBGCC2-CFLAGS =-fomit-frame-pointer-fPIC
This line is changed
TARGET_LIBGCC2-CFLAGS =-fomit-frame-pointer-fPIC-Dinhibit_libc-D _ gthr_posix_h

If you do not define-Dinhibit, the following error will be reported during compilation:

Http://www.cnblogs.com/gcc-2.95.3/gcc/libgcc2.c:41: stdlib. h: No such file or directory
Http://www.cnblogs.com/gcc-2.95.3/gcc/libgcc2.c:42: unistd. h: No such file or directory
Make [3]: *** [libgcc2.a] Error 1
Make [2]: *** [stmp-multilib-sub] Error 2
Make [1]: *** [stmp-multilib] Error 1
Make: *** [all-gcc] Error 2
 

If-D _ gthr_posix_h is not defined, the following error is reported during compilation.

In file included from gthr-default.h: 1,
From http://www.cnblogs.com/gcc-2.95.3/gcc/gthr.h:98,
From http://www.cnblogs.com/gcc-2.95.3/gcc/libgcc2.c:3034:
Http://www.cnblogs.com/gcc-2.95.3/gcc/gthr-posix.h:37: pthread. h: No such file or directory
Make [3]: *** [libgcc2.a] Error 1
Make [2]: *** [stmp-multilib-sub] Error 2
Make [1]: *** [stmp-multilib] Error 1
Make: *** [all-gcc] Error 2
 

There is also a way to achieve the same effect as-Dinhibit, that is, add a parameter-with-newlib when you configure. This option will not force us to use newlib. After compiling bootstrap-gcc, you can select any c library.

The next step is to configure boostrap gcc. Later, we will use bootstrap gcc to compile the glibc library.

$ Cd...; cd build-boot-gcc
$ ../Gcc-2.95.3/configure -- target = $ TARGET -- prefix = $ PREFIX \
> -- Without-headers -- enable-packages ages = c -- disable-threads
 

-Target, -- prefix, and binutils configuration in this command have the same meaning. -- without-headers indicates that header files are not required because they are cross-compilation tools and do not need header files on the local machine. -Enable-languages ages = c indicates that our boot-gcc only supports the c language. -- Disable-threads removes the thread function, which must be supported by glibc.

Next, compile and Install boot-gcc.

$ Make all-gcc
$ Make install-gcc
 

Let's take a look at what is added to $ PREFIX/bin.

$ Ls $ PREFIX/bin
 

You will find multiple files, including arm-linux-gcc, arm-linux-unprotoize, cpp, and gcov.

Gcc-gnu C language compiler

Unprotoize-convert the source code of ansi c to K & r c and remove the parameter types in the function prototype.

Cpp-gnu C pre-Compiler

Gcov-gcc's auxiliary testing tool, which can be used for analysis and optimization.

When gcc3.2 and gcc3.2 are used, the -- without-headers option cannot be used to configure boot-gcc, but the header file of glibc must be used.


 

Create a c library (glibc)

First unzip the glibc-2.2.3.tar.gz and glibc-linuxthreads-2.2.3.tar.gz source code

$ 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 go to the build-glibc directory to configure 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
 

CC = arm-linux-gcc sets the CC variable to your compiled boostrap gcc and uses it to compile your glibc. -- Enable-add-ons tells glibc to use the linuxthreads package. We have put it in the glibc source code directory above. This option is equivalent to-enable-add-ons = linuxthreads. -- With-headers tells glibc the directory location of our linux kernel header file.

After configuration, You can compile and install glibc.

$ Make
$ Make install_root = $ TARGET_PREFIX prefix = "" install
 

Then you need to modify the libc. so file.

Set
GROUP (/lib/libc. so.6/lib/libc_nonshared.a)

Change
GROUP (libc. so.6 libc_nonshared.a)

In this way, the Connection Program ld will be in libc. the directory where so is located looks for the library it requires, because the/lib directory of your machine may have installed a library with the same name, A library for compiling programs that can run on your host machine, rather than for cross-compilation.


 

Create a full set of compilers (full gcc)

We only support C when creating boot-gcc. Here, we need to build a full set of compilers to support C and C ++.

$ Cd $ PRJROOT/build-tools/build-gcc
$ ../Gcc-2.95.3/configure -- target = $ TARGET -- prefix = $ PREFIX -- enable-languages ages = c, c ++
 

-- Enable-languages ages = c, c ++ tells full gcc to support c and c ++ languages.

Then compile and install your full gcc

$ Make all
$ Make install
 

Let's take a look at what is added to $ PREFIX/bin.

$ Ls $ PREFIX/bin
 

You will find multiple files, including arm-linux-g ++, arm-linux-protoize, and arm-linux-c ++.

G ++-gnu c ++ compiler.

Protoize-opposite to Unprotoize, convert the source code of K & r c to ansi c, and add the parameter type to the function prototype.

C ++-gnu c ++ compiler.

Here, even if your cross-compilation tool is complete, you can simply verify your cross-compilation tool.

Use it to compile a very simple program helloworld. c

# I nclude

Int main (void)
{
Printf ("hello world \ n ");
Return 0;
}

$ Arm-linux-gcc helloworld. c-o helloworld
$ File helloworld
Helloworld: ELF 32-bit LSB executable, ARM, version 1,
Dynamically linked (uses shared libs), not stripped
 

The above output indicates that you have compiled a helloworld that can run under the arm architecture, proving that your compilation tool has been successful.
 

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/wonsoft/archive/2008/11/06/3242030.aspx

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.