/*
GCC编译需要mpfr和mpc(-->gmp、-->mpfr)库的支持
GCC编译工具链:
GCC编译工具链(toolchain),是指以GCC编译器为核心的一整套工具。它主要包含以下三部分内容:
gcc-core:即GCC编译器,用于完成预处理和编译过程,把C代码转换成汇编代码。
Binutils :除GCC编译器外的一系列小工具包括了链接器ld,汇编器as、目标文件格式查看器readelf等。
glibc:包含了主要的 C语言标准函数库,C语言中常常使用的打印函数printf、malloc函数就在glibc 库中。
在很多场合下会直接用GCC编译器来指代整套GCC编译工具链。
*/
//gmp:GNU多重精度运算库(英语:GNU Multiple Precision Arithmetic Library,简称GMP或gmpal)是一个开源的任意精度运算库,支持正负数的整数、有理数、浮点数。它没有任何精度限制,只受限于可用内存。GCC的依赖库mpc的依赖库。
tar xjf gmp-5.0.2.tar.bz2
cd gmp-5.0.2
./configure --prefix=/usr/local
make
make install # This step may require privilege (sudo make install)
cd ..
// ./configure --prefix=/usr/local指定了安装make install 的位置
//MPFR库是一个C库,用于具有正确舍入的多精度浮点计算。GCC的依赖库gmp的依赖库
tar xjf mpfr-3.1.2.tar.bz2
cd mpfr-3.1.2
./configure --prefix=/usr/local
make
make install # This step may require privilege (sudo make install)
cd ..
//gun mpc是对IEEE-754固定精度实浮点数计算标准的扩展,对每种计算操作提供了良好语义接口,同时保持较高的计算速度。GCC的依赖库
tar xzf mpc-0.9.tar.gz
cd mpc-0.9
./configure --prefix=/usr/local
make
make install # This step may require privilege (sudo make install)
cd ..
/*
Binutils(bin utility),是GNU二进制工具集,通常跟GCC编译器一起打包安装到系统
在进行程序开发的时候通常不会直接调用这些工具,而是在使用GCC编译指令的时候由GCC编译器间接调用。下面是其中一些常用的工具:
as:汇编器,把汇编语言代码转换为机器码(目标文件)。
ld:链接器,把编译生成的多个目标文件组织成最终的可执行程序文件。
readelf:可用于查看目标文件或可执行程序文件的信息。
nm : 可用于查看目标文件中出现的符号。
objcopy: 可用于目标文件格式转换,如.bin 转换成 .elf 、.elf 转换成 .bin等。
objdump:可用于查看目标文件的信息,最主要的作用是反汇编。
size:可用于查看目标文件不同部分的尺寸和总尺寸,例如代码段大小、数据段大小、使用的静态内存、总大小等。
关于./configure命令及其参数说明:在gcc编译中使用命令./configure --build=编译平台 --host=运行平台 --target=目标平台 [各种编译参数] 来配置gcc的源代码,以让其编译出需要的gcc编译器。
build:自动测试在用平台名称,若无法检测出来则需要指定。host:若无指定,自动使用build的结果。build和host相同时表示本地编译,若不相同则表示交叉编译。target:表示需要处理的目标平台名称,若无指定使用host相同名称
简单举例:如果需要在x86下编译一个运行于arm上的编译器gcc,这个gcc产生mips平台的目标代码,那么配置方式为:
--build=x86 --host=arm --target=mips
#这里的build没写,所以默认是实验平台linux,host也没写默认是build所以也是linux,target指明为i386(Internet 80386)-jos-elf
所以这是在Linux下编译的一个运行于Linux上的二进制处理工具包,这个二进制工具包产生i386平台的目标代码
--disable-werror:这会防止来自宿主编译器的警告事件导致停止编译。
*/
tar xjf binutils-2.21.1.tar.bz2
cd binutils-2.21.1
./configure --prefix=/usr/local --target=i386-jos-elf --disable-werror
make
make install # This step may require privilege (sudo make install)
cd ..
//测试安装的binutils
i386-jos-elf-objdump -i
# Should produce output like:
# BFD header file version (GNU Binutils) 2.21.1
# elf32-i386
# (header little endian, data little endian)
# i386...
//gcc-core是GCC的一部分,作用见首行注释
tar xjf gcc-core-4.6.4.tar.bz2
cd gcc-4.6.4
mkdir build # GCC will not compile correctly unless you build in a separate directory
cd build
../configure --prefix=/usr/local \
--target=i386-jos-elf --disable-werror \
--disable-libssp --disable-libmudflap --with-newlib \
--without-headers --enable-languages=c MAKEINFO=missing
make all-gcc
make install-gcc # This step may require privilege (sudo make install-gcc)
make all-target-libgcc
make install-target-libgcc # This step may require privilege (sudo make install-target-libgcc)
cd ../..
//测试刚刚安装的gcc-core
i386-jos-elf-gcc -v
# Should produce output like:
# Using built-in specs.
# COLLECT_GCC=i386-jos-elf-gcc
# COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i386-jos-elf/4.6.4/lto-wrapper
# Target: i386-jos-elf
/*GDB 全称“GNU symbolic debugger”,从名称上不难看出,它诞生于 GNU 计划(同时诞生的还有 GCC、Emacs 等),是 Linux 下常用的程序调试器。实际场景中,GDB 更常用来调试 C 和 C++ 程序。一般来说,GDB主要帮助我们完成以下四个方面的功能:
1.启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2.在某个指定的地方或条件下暂停程序。
3.当程序被停住时,可以检查此时你的程序中所发生的事。
4.在程序执行过程中修改程序中的变量或条件,将一个bug产生的影响修正从而测试其他bug。
*/
tar xjf gdb-7.3.1.tar.bz2
cd gdb-7.3.1
./configure --prefix=/usr/local --target=i386-jos-elf --program-prefix=i386-jos-elf- \
--disable-werror
make all
make install # This step may require privilege (sudo make install)
cd ..
Linux troubleshooting
Q. I can't run make install because I don't have root permission on this machine.
A. Our instructions assume you are installing into the /usr/local directory. However, this may not be allowed in your environment. If you can only install code into your home directory, that's OK. In the instructions above, replace --prefix=/usr/local with --prefix=$HOME (and click here to update the instructions further). You will also need to change your PATH and LD_LIBRARY_PATH environment variables, to inform your shell where to find the tools. For example:
export PATH=$HOME/bin:$PATH
export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH