Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

Linux Compilers and Assemblers

SEP 19, 2003 By Christopher Paul, Rafeeq Rehman. Article is provided courtesy of Prentice Hall.

3.2 Installing GNU Compiler

In most cases, GCC comes with all Linux distributions. However you can download the latest version, and build and install it according to your requirements. You may also need to build your compiler if you are building a cross-compiling development system. The best way to build a new version of the compiler is to have some pre-installed and pre-configured Linux distribution on your system that will be used to build the new compiler. For the purpose of writing this book, we have used Red Hat 7.1 but the process is the same on any distribution of Linux.

The installation process is done in multiple steps. After downloading, you have to untar the source code and build it in a directory. This directory should be separate from the source code directory tree. The building process includes configuration and compiling stages. Once you have successfully created the new compiler, you can install it in a directory of your choice. It is advised to keep this installation directory separate from the location where the original compiler is installed.

3.2.1 Downloading

You can download the latest version of GCC from ftp://ftp.gnu.org/gnu/gcc/. I downloaded GCC 3.0.4 and it is about 17.5 MB. You can also find a mirror site near you to get GCC. A list of mirror sites is available on http://www.gnu.org/order/ftp.html.

3.2.2 Building and Installing GCC

The GCC installation process can be divided into four steps for simplicity and understanding.

  1. Download and extract

  2. Configure

  3. Build

  4. Install

3.2.2.1 Download and Extract

First create a directory where you will unpack the source code. Use the tar command to unpack the code. For our purpose, I have created a directory /gcc3 to compile and build the GCC compiler. The untar process looks like the following and it creates a directory gcc-3.0.4 under /gcc3 directory.

[root@laptop /gcc3]# tar zxvf gcc-3.0.4.tar.gz
   gcc-3.0.4/
   gcc-3.0.4/INSTALL/
   gcc-3.0.4/INSTALL/index.html
   gcc-3.0.4/INSTALL/README
   gcc-3.0.4/INSTALL/specific.html
   gcc-3.0.4/INSTALL/download.html
   gcc-3.0.4/INSTALL/configure.html
   gcc-3.0.4/INSTALL/build.html
   gcc-3.0.4/INSTALL/test.html
   gcc-3.0.4/INSTALL/finalinstall.html
   gcc-3.0.4/INSTALL/binaries.html
   gcc-3.0.4/INSTALL/gfdl.html
   gcc-3.0.4/.cvsignore
   gcc-3.0.4/COPYING
   gcc-3.0.4/COPYING.LIB
   gcc-3.0.4/ChangeLog
   gcc-3.0.4/MAINTAINERS
   gcc-3.0.4/Makefile.in
   gcc-3.0.4/README
   

This is a partial output of the command. Most of the output is truncated to save space.

3.2.2.2 Running configure Script

After uncompressing the source code, the first thing is to run the configure script. This script is found in /gcc3/gcc-3.0.4 directory. You can specify three major things when you run the configure script.

  1. Build machine. This is the machine where you compile and build the compiler.

  2. Host machine. This is the machine where compiler will be installed. This is usually the same as the build machine.

  3. Target machine. This is the machine for which the newly built compiler will generate executable code. If you are building a native compiler, this is the same machine as the host machine. If you are building a cross-compiler, this machine will be different than the host machine.

Each of these machines names are used in the following format:

CPUname-CompanyName-SystemName

For example, if you want to build a compiler that will run on a sparc processor made by Sun Microsystems and on SunOS 4.1 operating system, the command line for configure script will be as follows:

./configure –host=sparc-sun-sunos4.1

Please see a list of supported systems at http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_4.html.

For a native compiler (a compiler that runs on the same machine where it is built and generates code for the same machine), you don't need to specify any options on the command line when you run the configure script. However, if you are building a cross-compiler, you must specify the target machine. Similarly, if you are building a compiler that will be installed on some other machine, you have to specify the host machine.

There may be some other requirements when you are building a non-native compiler.

It is recommended to run the configure script in a directory other than the source code directory so that the source code tree is completely separate from the place where you build the compiler. For the sake of this example, I have created a directory /gcc3/objdir and I ran the configure script from that directory. As you already know, the source code tree is under the /gcc3/gcc-3.0.4 directory. You may need to add a prefix for GCC installation files. The prefix shows the directory where GCC files will be finally installed. The default prefix is /usr/local. This means that GCC binary files will be installed in /usr/local/bin directory. For installation purposes, I have selected /opt/gcc-3.0.4 directory as the prefix. In a typical development environment, you would do something similar to that. Just create a directory under /opt and install all of your tools and applications under /opt. The following command line will configure the GCC compilation process and will create many files under /gcc3/objdir. These files will be used later on to build the compiler. When you start the configure script, the following messages will start scrolling up.

[root@laptop objdir]# ../gcc-3.0.4/configure --prefix=/opt/gcc-3.0.4
   Configuring for a i686-pc-linux-gnu host.
   Created "Makefile" in /gcc3/gcc-3.0.4 using "mt-frag"
   Configuring libiberty...
   creating cache ../config.cache
   checking host system type... i686-pc-linux-gnu
   checking build system type... i686-pc-linux-gnu
   checking for ar... ar
   checking for ranlib... ranlib
   checking for gcc... gcc
   checking whether we are using GNU C... yes
   checking whether gcc accepts -g... yes
   checking for POSIXized ISC... no
   checking for working const... yes
   checking for inline... inline
   checking for a BSD compatible install... /usr/bin/install -c
   checking how to run the C preprocessor... gcc -E
   checking for sys/file.h... yes
   checking for sys/param.h... yes
   checking for limits.h... yes
   checking for stdlib.h... yes
   checking for string.h... yes
   

Most of the output is truncated from the configure script to save space. When the configure script is completed, you will get back the command prompt. Now you may start building the compiler.

If you want to enable threads on Linux systems, you can use --enable-threads=posix as a command line option.

3.2.2.3 Building GCC

It is recommended that you use GNU make for building purpose. Running the make bootstrap command will build the compiler, libraries and related utilities. Following is part of the output when you start the building process.

[root@laptop objdir]# make bootstrap
   make[1]: Entering directory `/gcc3/objdir/libiberty'
   if [ x"" != x ] && [ ! -d pic ]; then    mkdir pic;    else true; fi
   touch stamp-picdir
   if [ x"" != x ]; then    gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic  ../../gcc-3.0.4/libiberty/argv.c -o pic/argv.o;    else true; fi
   gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic ../../gcc-3.0.4/libiberty/argv.c
   if [ x"" != x ]; then    gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic  ../../gcc-3.0.4/libiberty/choose-temp.c -o pic/choose-temp.o;    else true; fi
   gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic ../../gcc-3.0.4/libiberty/choose-temp.c
   if [ x"" != x ]; then    gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic  ../../gcc-3.0.4/libiberty/concat.c -o pic/concat.o;    else true; fi
   gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic ../../gcc-3.0.4/libiberty/concat.c
   if [ x"" != x ]; then    gcc -c -DHAVE_CONFIG_H -g -O2 -I. -I../../gcc-3.0.4/libiberty/../include  -W -Wall 
   -Wtraditional -pedantic  ../../gcc-3.0.4/libiberty/cplus-dem.c -o pic/cplus-dem.o;    else true; fi
   

Again most of the output is truncated. The building process may take a long time depending upon how powerful a computer you are using for building GCC. The process is completed in three stages. Stage 1 and stage 2 binaries are deleted as soon as they are no longer needed. Typically this building process will do the following:

  • Build some basic tools like bison, gperf and so on. These tools are necessary to continue the compilation process.

  • Build target tools like gas, ld, binutils and so on.

  • Perform a three-stage bootstrap of the compiler.

  • Perform comparison of stage 2 and stage 3.

  • Build run-time libraries.

  • Remove unnecessary files.

You should have enough disk space to build GCC. If you are short of disk space, you can use the bootstrap-lean option on the command line instead of bootstrap.

3.2.2.4 Final Install

The final installation process is necessary to move GCC binaries under the directory used as a prefix during the configure script execution. We have used /opt/gcc-3.0.4 as the prefix directory. Executing the following command in /gcc3/objdir will move GCC binaries in a tree structure under /opt/gcc-3.0.4.

make install

A typical directory tree after installation is shown below.

[root@conformix gcc-3.0.4]# tree -d |more
   .
   |-- bin
   |-- include
   |   |-- g++-v3
   |   |   |-- backward
   |   |   |-- bits
   |   |   |-- ext
   |   |   `-- i686-pc-linux-gnu
   |   |       `-- bits
   |   |-- gcj
   |   |-- gnu
   |   |   |-- awt
   |   |   |   `-- j2d
   |   |   |-- classpath
   |   |   |-- gcj
   |   |   |   |-- awt
   |   |   |   |-- convert
   |   |   |   |-- io
   |   |   |   |-- jni
   |   |   |   |-- math
   |   |   |   |-- protocol
   |   |   |   |   |-- file
   |   |   |   |   |-- http
   |   |   |   |   `-- jar
   |   |   |   |-- runtime
   |   |   |   |-- text
   |   |   |   `-- util
   |   |   `-- java
   |   |       |-- beans
   |   |       |   |-- editors
   |   |       |   `-- info
   |   |       |-- io
   |   |       |-- lang
   |   |       |   `-- reflect
   |   |       |-- locale
   |   |       `-- security
   |   |           `-- provider
   |   `-- java
   |       |-- applet
   |       |-- awt
   |       |   |-- color
   |       |   |-- datatransfer
   |       |   |-- event
   |       |   |-- geom
   |       |   |-- image
   |       |   `-- peer
   |       |-- beans
   |       |   `-- beancontext
   |       |-- io
   |       |-- lang
   |       |   |-- ref
   |       |   `-- reflect
   |       |-- math
   |       |-- net
   |       |-- security
   |       |   |-- cert
   |       |   |-- interfaces
   |       |   `-- spec
   |       |-- sql
   |       |-- text
   |       `-- util
   |           |-- jar
   |           `-- zip
   |-- info
   |-- lib
   |   `-- gcc-lib
   |       `-- i686-pc-linux-gnu
   |           `-- 3.0.4
   |               `-- include
   |                   |-- SDL
   |                   |-- X11 -> root/usr/X11R6/include/X11
   |                   |-- linux
   |                   |-- mozilla
   |                   |-- ncurses
   |                   |-- objc
   |                   |-- openssl
   |                   |-- pcap
   |                   |   `-- net
   |                   |-- pgsql
   |                   |   `-- utils
   |                   |-- root
   |                   |   `-- usr
   |                   |       `-- X11R6
   |                   |           `-- include
   |                   |               `-- X11
   |                   |-- schily
   |                   |   `-- scg
   |                   |-- slang
   |                   |-- ucd-snmp
   |                   |-- w3c-libwww
   |                   |-- wnn
   |                   `-- wnn6
   |-- man
   |   |-- man1
   |   `-- man7
   `-- share
   
   95 directories
   [root@conformix gcc-3.0.4]#
   

Detailed instructions for compiling and installing GCC are available at http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_4.html as well as at http://gcc.gnu.org/install/.

3.2.3 Environment Variables

The GCC compiler relies on many environment variables for its operation. These variables are used for different purposes including location of library and header files, location of helping programs and so on. Some important variables and their respective use are introduced in this section.

TMPDIR

This variable shows location of temporary file location. GCC uses this location to store temporary files during the compiling and linking processes.

GCC_EXEC_PREFIX

If this variable is set, GCC will look into the directory to find sub programs.

COMPILER_PATH

This is a colon-separated list of directories that GCC uses to find out sub programs if search fails using GCC_EXEC_PREFIX variable.

LIBRARY_PATH

This is a colon-separated list of directories that is used to find out libraries for linking process.

C_INCLUDE_PATH

Colon separated list of directories to find out header files for C programs.

OBJC_INCLUDE_PATH

Colon separated list of directories to find out header files for Objective C programs.

CPLUS_INCLUDE_PATH

Colon separated list of directories to find out header files for C++ programs.

LD_LIBRARY_PATH

Path for shared libraries.

There are other environment variables and settings that GCC uses while building a target. You can display these using the –v command line switch with the gcc command when you compile a program. This will show you the path including files, programs used during the compilation process, and command line arguments and switches passed to each of these programs. The following is the output of the command when you compile the hello.c program.

[rr@conformix 4]$ gcc -v hello.c
   Reading specs from /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/specs
   Configured with: ../gcc-3.0.4/configure --prefix=/opt/gcc-3.0.4 --enable-threads=posix
   Thread model: posix
   gcc version 3.0.4
   /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/cc1 -lang-c -v -D__GNUC__=3 
   -D__GNUC_MINOR__=0 -D__GNUC_PATCHLEVEL__=4 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ 
   -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 
   -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ 
   -D__tune_pentiumpro__ hello.c -quiet -dumpbase hello.c -version -o /tmp/ccJsUmYa.s
   GNU CPP version 3.0.4 (cpplib) (i386 Linux/ELF)
   GNU C version 3.0.4 (i686-pc-linux-gnu)
   compiled by GNU C version 3.0.4.
   ignoring nonexistent directory "/opt/gcc-3.0.4/i686-pc-linux-gnu/include"
   #include "..." search starts here:
   #include <...> search starts here:
   /usr/local/include
   /opt/gcc-3.0.4/include
   /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/include
   /usr/include
   End of search list.
   as --traditional-format -V -Qy -o /tmp/ccn7wLgw.o /tmp/ccJsUmYa.s
   GNU assembler version 2.10.91 (i386-redhat-linux) using BFD version 2.10.91.0.2
   /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/collect2 -m elf_i386 -dynamic-linker /
   lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /opt/gcc-3.0.4/lib/gcc-lib/
   i686-pc-linux-gnu/3.0.4/crtbegin.o -L/opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4 
   -L/opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/../../.. /tmp/ccn7wLgw.o -lgcc -lc 
   -lgcc /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/crtend.o /usr/lib/crtn.o
   [rr@conformix 4]$
   

If you examine the output of this command, you can find out which helper programs gcc uses and what command line switches are passed to these programs.

3.2.4 Post-Installation Tasks

There are a few tasks that you need to carry out after the installation process of the compilers.

3.2.4.1 Setting PATH Variable

This is the first important task. Your PATH variable must include the directory where GCC binaries are installed. We have installed these in /opt/gcc-3.0.4/bin directory because we used /opt/gcc-3.0.4 as the prefix while running the configure script. This directory should come before the directories where the old compiler is installed. A typical command to do this in bash or other POSIX-compliant shells to include our installation location is as follows:

export PATH=/opt/gcc-3.0.4/bin:$PATH

where /opt/gcc-3.0.4/bin is the path to newly installed compilers.

It is also extremely important that you make sure the GCC in the path is the correct one. The 'which gcc' command will provide this.

3.2.4.2 Setting the Location of Libraries

There are two steps to set up the location of libraries. First edit /etc/ld/so.config and add the path of any newly created libraries. This directory is /opt/gcc-3.0.4/lib because we used –prefix=/opt/gcc-3.0.4 while building the compiler. Typical contents of this file after adding the new directory are as follows:

/opt/gcc-3.0.4/lib
/usr/lib
/usr/kerberos/lib
/usr/X11R6/lib
/usr/lib/sane
/usr/lib/qt-2.3.0/lib
/usr/lib/mysql
/usr/lib/qt-1.45/lib

After editing this file, execute the ldconfig program, which will configure dynamic linker binding. You can use the –v command line option to get more information when you run this command. Note that the order of commands is important.

The next step is to setup the LD_LIBRARY_PATH variable. You can do this by adding the following line at the end of /etc/profile file so that it is set for all users at login time.

export LD_LIBRARY_PATH=/opt/gcc-3.0.4/lib

Again note that this is the path where new library files are installed. Please note that if you make these changes, some older programs that are compiled for some other set of shared libraries may not function properly.

3.2.4.3 Setting Location of include Files

The default search path for include files can be found by executing the following command:

[rr@conformix 4]$ gcc -v -E -
   Reading specs from /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/specs
   Configured with: ../gcc-3.0.4/configure --prefix=/opt/gcc-3.0.4 --enable-threads=posix
   Thread model: posix
   gcc version 3.0.4
   /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/cpp0 -lang-c -v -D__GNUC__=3 
   -D__GNUC_MINOR__=0 -D__GNUC_PATCHLEVEL__=4 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ 
   -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 
   -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ 
   -D__tune_pentiumpro__ -
   GNU CPP version 3.0.4 (cpplib) (i386 Linux/ELF)
   ignoring nonexistent directory "/opt/gcc-3.0.4/i686-pc-linux-gnu/include"
   #include "..." search starts here:
   #include <...> search starts here:
   /usr/local/include
   /opt/gcc-3.0.4/include
   /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/include
   /usr/include
   End of search list.
   

The last part of the output shows that include files will be searched in the following directories by default.

/usr/local/include
/opt/gcc-3.0.4/include
/opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-gnu/3.0.4/include
/usr/include

You can place include files in other directories if you have set the C_INCLUDE_PATH environment variable. Setting this variable to /opt using the following command will include /opt directory also in the search list. Also note that order is once again extremely important.

export C_INCLUDE_PATH=/opt

If you again execute the gcc –v –E – command, /opt path will be included in the last part of the output.

To set additional include paths permanently for all users, it is a good idea to add a line with the export command in /etc/profile file.

3.2.4.4 Setting Manual Pages Path

To be able to use manual pages installed with GCC, you have to add a line in the /etc/man.config file. This will enable the man command to also look into the /opt/gcc-3.0.4/man directory when searching for manual pages. The line looks like the following:

MANPATH /opt/gcc-3.0.4/man

Location of this line with respect to other MANPATH entries is important. If you put this line after other entries, you will still get the same old man pages. That is why it is recommended to put this entry BEFORE any other line that starts with MANPATH keyword in this file.

3.2.5 What Not to Do when Installing Development Tools

When building GCC, don't build it into the source directory. I would also recommend not installing your development tools in the default location (under /usr/local). Instead use some place under the /opt directory. This way if something goes wrong, you can just delete this directory under /opt without making any other change in the system. If you install these tools in the default location, you may overwrite some existing files and may not be able to reverse the installation process.


Next Section 3. Compiling a Program
© 2003 Pearson Education, Inc. InformIT Division. All rights reserved.
800 East 96th Street Indianapolis, Indiana 46240