Библиотека сайта rus-linux.net
Linux Compilers and AssemblersSEP 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.
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.
220.127.116.11 Download and Extract
First create a directory where you will unpack the source code. Use the
[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.
This variable shows location of temporary file location. GCC uses this location to store temporary files during the compiling and linking processes.
If this variable is set, GCC will look into the directory to find sub programs.
This is a colon-separated list of directories that GCC uses to find out sub programs if search fails using
This is a colon-separated list of directories that is used to find out libraries for linking process.
Colon separated list of directories to find out header files for C programs.
Colon separated list of directories to find out header files for Objective C programs.
Colon separated list of directories to find out header files for C++ programs.
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
[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 18.104.22.168.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.
22.214.171.124 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:
/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.
126.96.36.199 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.
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.
188.8.131.52 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.
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
184.108.40.206 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:
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.
© 2003 Pearson Education, Inc. InformIT Division. All rights reserved.
800 East 96th Street Indianapolis, Indiana 46240