What is a cross compiler?

Source: Internet
Author: User
Tags posix

because of the needs of the project, we need a cross compiler that can run on the x86 platform, generate MIPS applications, and have been doing this for the last few days, read a lot of articles and try A lot of methods, finally succeeded, but also good, some new and everyone to exchange a bit.
    • What is a cross compiler?
Why is it called a cross-compiler, because it compiles programs across platforms compiler! Do a cross-compiler to figure out 3 concepts: host, build, Target:
      • Build--the compiler that you compiled on what platform
      • Host-what platform will this compiler run on in the future
      • Target--the compiler will eventually generate executable code that executes on which platform
Here I can give an example build=i386 HOST=SPARC64 target=mips32 that we compiled a compiler on the x86 platform that runs on the SPARC64 platform, It compiles the source code to generate an executable program to run on the MIPS32 platform.
Describe a platform that typically uses triples: Arch-vendor-os, such as I386-redhat-linux, which represents the x86 platform, the producer is Redhat, and the OS used is Linux.
    • Basic process for making cross compilers
If you have not done cross-compiler before, then I tell you, it will be a painful process, do not think like a simple Linux under the program, you will encounterOne mistake after another, and in the moment you can not solve, and some are not your mistakes, so if there is ready, do not do it yourself.
The basic process is divided into 4 steps:
    1. Compile binutils: This is generally not a problem.
    2. Compile bootstrap GCC: This is also easier, this step is for compiling glibc prepared
    3. Compile glibc: This is the most error-prone because you need to prepare a kernel header file for it, and sometimes you need to patch
    4. Recompile gcc: Tell the configuration file the location of the glibc you just compiled, so that you can eventually compile a cross-compiler that automatically finds the library and header files.
    • My Practice
The purpose of writing this article station is to write this, so that in the future they need not to find the information, follow this redo again is, but then I think, I do those cross-compiler,I have backed up, and my approach is to follow the http://documents.jg555.com/cross-lfs/mips64/, they are often updated, and the explanation is more detailed, simply give Everybody This website, oneself follow above step step to be, if above URL cannot access to use search engine, search "Cross-lfs" or "Clfs", should be able to find.
Here is a script I wrote, first of all assume that you have to the http://documents.jg555.com/cross-lfs/site to the required packages and patches (binutils, GCC, glibc, etc.) are Download down, the next work can be handed to me to write this script to complete, of course, you can also change some of the values to meet your requirements.
#!/bin/bash

crosstool_path=/opt/crossgcc
kerel_path=/work/lirui/tarball/crossgcc/linux-2.6.16.8
tarball_path=/work/lirui/tarball/crossgcc
Tool_src_path=/work/lirui/tarball/crossgcc/src
Build_path=/work/lirui/builds

binutils_ver=binutils-2.16.92
gcc_ver=gcc-4.1.0
glibc_ver=glibc-2.4

Unset CFLAGS
Unset cxxflags
Export lfs_host= "' Echo ${machtype} | Sed-e ' s/unknown/cross/g '-e ' s/-pc-/-cross-/g ' "

# for a MIPS Little Endian machine:
Export lfs_target= "Mips64el-unknown-linux-gnu"

# for a MIPS Big Endian machine:
# export lfs_target= "Mips64-unknown-linux-gnu"

Export lfs_target32= "' Echo ${lfs_target}| Sed-e ' s/64//g ' "

Export build32= "-mabi=32"
Export buildn32= "-mabi=n32"
Export build64= "-mabi=64"

# Prepare Kernel-headers
CD ${kerel_path}/include
MKDIR-PV ${crosstool_path}/include
CP-RV asm-mips ${crosstool_path}/include/asm
CP-RV Asm-generic ${crosstool_path}/include
CP-RV Linux ${crosstool_path}/include

# step 1) Build Binutils
cd {Tool_src_path}
Tar jxf {tarball_path}/{binutils_ver}.tar.bz2
CD ${tool_src_path}/${binutils_ver}
Patch-np1-i ${tarball_path}/patches/${binutils_ver}-posix-1.patch
Patch-np1-i ${tarball_path}/patches/${binutils_ver}-genscripts_multilib-1.patch

MKDIR-PV ${build_path}/build-binutils

CD ${build_path}/build-binutils
${tool_src_path}/${binutils_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_host}--target=${lfs_target}--with-lib-path=${crosstool_path}/lib/
--disable-nls--enable-shared--ENABLE-64-BIT-BFD

Make Configure-host
Make
If [$? = = "]; Then
echo "Build Binutils successfully!"
CP ${tool_src_path}/${binutils_ver}/include/libiberty.h ${crosstool_path}/include
Make install
Else
echo "Build Binutils failed!"
Exit
Fi
Export Path=${crosstool_path}/bin:${path}
# step 2) Build bootstrap gcc
CD ${tool_src_path}
Tar jxvf ${tarball_path}/${gcc_ver}.tar.bz2
CD ${tool_src_path}/${gcc_ver}
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-specs-1.patch
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-posix-1.patch
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-cross_search_paths-1.patch

echo "
#undef Startfile_prefix_spec
#define STARTFILE_PREFIX_SPEC/"/opt/crossgcc/lib//" ">> gcc/config/linux.h

Cp-v Gcc/makefile.in{,.orig}
Sed-e "[Email protected]/(^cross_system_header_dir =/). *@/1/opt/crossgcc/[email protected]"/
Gcc/makefile.in.orig > Gcc/makefile.in

MKDIR-PV ${BUILD_PATH}/BUILD-GCC
CD ${BUILD_PATH}/BUILD-GCC
${tool_src_path}/${gcc_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_host}--target=${lfs_target}--with-local-prefix=${crosstool_path}/
--disable-nls--disable-shared--disable-threads/
--enable-languages=c

Make ALL-GCC

If [$? = = "]; Then
echo "Build bootstrap gcc successfully!"
Make INSTALL-GCC
Else
echo "Build bootstrap gcc failed!"
Exit
Fi

# Step 3) Build glibc abi=32
CD ${tool_src_path}
Tar ${tarball_path}/${glibc_ver}.tar.bz2
CD ${tool_src_path}/${glibc_ver}
Tar ${tarball_path}/glibc-ports-2.4.tar.bz2
Mv-v glibc-ports-2.4 Ports

Patch-np1-i ${tarball_path}/patches/${glibc_ver}-mips_fixes-1.patch
Patch-np1-i ${tarball_path}/patches/${glibc_ver}-libgcc_eh-1.patch
Patch-np1-i ${tarball_path}/patches/${glibc_ver}-localedef_segfault-1.patch

Cp-v Nscd/makefile{,.orig}
Sed-e "/nscd_stat.o:sysincludes = # nothing/d" Nscd/makefile.orig >/
Nscd/makefile

MKDIR-PV ${BUILD_PATH}/BUILD-GLIBC
CD ${BUILD_PATH}/BUILD-GLIBC
echo "Libc_cv_forced_unwind=yes" > Config.cache
echo "Libc_cv_c_cleanup=yes" >> Config.cache
build_cc= "gcc" cc= "${LFS_TARGET}-GCC ${build32}"/
Ar= "${lfs_target}-ar" ranlib= "${lfs_target}-ranlib"/
${tool_src_path}/${glibc_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_target32}--build=${lfs_host}/
--disable-profile--enable-add-ons/
--with-tls--enable-kernel=2.6.0--with-__thread/
--with-binutils=${crosstool_path}/bin--with-headers=${crosstool_path}/include/
--cache-file=config.cache

Make
If [$? = = "]; Then
echo "Build glibc abi=32 successfully!"
Make install
Else
echo "Build glibc abi=32 failed!"
Exit
Fi

# Step 4) Build glibc Abi=n32
CD ${BUILD_PATH}/BUILD-GLIBC && rm-rf./*
echo "Libc_cv_forced_unwind=yes" > Config.cache
echo "Libc_cv_c_cleanup=yes" >> Config.cache
echo "Slibdir=/opt/crossgcc/lib32" >> configparms
build_cc= "gcc" cc= "${LFS_TARGET}-GCC ${buildn32}"/
Ar= "${lfs_target}-ar" ranlib= "${lfs_target}-ranlib"/
${tool_src_path}/${glibc_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_target32}--build=${lfs_host}/
--disable-profile--enable-add-ons/
--with-tls--enable-kernel=2.6.0--with-__thread/
--with-binutils=${crosstool_path}/bin--with-headers=${crosstool_path}/include/
--cache-file=config.cache--libdir=${crosstool_path}/lib32

Make
If [$? = = "]; Then
echo "Build glibc abi=n32 successfully!"
Make install
Else
echo "Build glibc abi=n32 failed!"
Exit
Fi

# Step 5) Build glibc abi=64
CD ${BUILD_PATH}/BUILD-GLIBC && rm-rf./*
echo "Libc_cv_forced_unwind=yes" > Config.cache
echo "Libc_cv_c_cleanup=yes" >> Config.cache
echo "slibdir=/crossgcc/lib64" >> configparms
build_cc= "gcc" cc= "${LFS_TARGET}-GCC ${build64}"/
Ar= "${lfs_target}-ar" ranlib= "${lfs_target}-ranlib"/
${tool_src_path}/${glibc_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_target32}--build=${lfs_host}/
--disable-profile--enable-add-ons/
--with-tls--enable-kernel=2.6.0--with-__thread/
--with-binutils=${crosstool_path}/bin--with-headers=${crosstool_path}/include/
--cache-file=config.cache--libdir=${crosstool_path}/lib64

Make
If [$? = = "]; Then
echo "Build glibc abi=64 successfully!"
Make install
Else
echo "Build glibc abi=64 failed!"
Exit
Fi

# Step 6) Rebuild GCC finally
CD ${tool_src_path} && rm-rf ${gcc_ver}
Tar jxvf ${tarball_path}/${gcc_ver}.tar.bz2
CD ${tool_src_path}/${gcc_ver}
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-pr20425-1.patch
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-specs-1.patch
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-posix-1.patch
Patch-np1-i ${tarball_path}/patches/${gcc_ver}-cross_search_paths-1.patch

echo "
#undef Startfile_prefix_spec
#define STARTFILE_PREFIX_SPEC/"/opt/crossgcc/lib32//" ">> gcc/config/linux.h

Cp-v Gcc/makefile.in{,.orig}
Sed-e "[Email protected]/(^cross_system_header_dir =/). *@/1/opt/crossgcc/[email protected]"/
Gcc/makefile.in.orig > Gcc/makefile.in

MKDIR-PV ${BUILD_PATH}/BUILD-GCC
CD ${BUILD_PATH}/BUILD-GCC
${tool_src_path}/${gcc_ver}/configure--prefix=${crosstool_path}/
--host=${lfs_host}--target=${lfs_target}--with-local-prefix=${crosstool_path}/
--disable-nls--enable-shared--enable-threads=posix/
--enable-__cxa_atexit--enable-c99--enable-long-long/
--enable-languages= "C,c++,fortran"

Make as_for_target= "${lfs_target}-as" ld_for_target= "${lfs_target}-ld"

If [$? = = "]; Then
echo "Build gcc successfully!"
Make install
Else
echo "Build gcc failed!"
Exit
Fi

echo "++++++++++++++++++++all is done!+++++++++++++++++++++++++++"
    • network resources
If you use Google or Baidu to search the "cross-compiler" This keyword, you can find a lot of articles, but can tell you, most of you are useless, also include me this (unless your environment and I very similar). But still want to give everyone a list of network resources, how much still can some help:
    • http://www.linux-mips.org/This site maintains the support for the MIPS architecture of Linux, as well as the introduction of various MIPS CPUs.
    • http://trac.cross-lfs.org/This site will show you how to compile a Linux system from the source, of course, including how to make cross-compiler
    • http://documents.jg555.com/cross-lfs/mips64-64/index.html cross-compiled Linux from Scratch This site is better, my production process is to follow this
    • http://documents.jg555.com/cross-lfs/mips64/this tells you how to make a cross-compiler that supports libraries with multiple ABI annotations
    • http://kegel.com/crosstool/This website provides an automated tool Crosstool, he can help you automatically, but for me seems to be not good, if for you can use, it is better.
    • Http://vmlinux.org/crash/mirror/www.objsw.com/CrossGCC/crossGcc

What is a cross compiler?

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.