2 Installating Gnat on Linux


  <--Last Chapter Table of Contents Next Chapter-->  


Gnat is a part of the gcc project. The gcc command itself isn't a compiler: it's a program that determines the kind of source file you have and runs the appropriate compiler for you. For example the Ada compiler is called gnat1 and the C compiler is called cc1. When gcc detects an Ada source file it runs gnat1 to compile it.

Because gcc and Gnat must work as a team specific versions of Gnat are created for specific versions of gcc. Gnat 3.10p is compiled against the gcc for kernel 2.0.29 (for example the version of gcc used in the Slackware 3.2 distribution). Gnat 3.11p 3.12p and 3.13p are compiled against gcc 2.8.1.

NoteTo find out which version of gcc you have run gcc with the -v switch.

As of GCC 3 Gnat is now built-in as a part of the compiler and Ada support is included with most distributions of Linux. To test to see if you have Ada support create a file called "hello.adb" and try compiling it with "gcc hello.adb". If gcc recognizes the file as an Ada program then your copy of gcc has Ada support included.
Gnat 5 (the version of Gnat included with gcc) is considered to be experimental. That is it hasn't undergone rigorous testing and changes to the gcc system may introduce new bugs. Although my experience shows that Gnat 5 is as robust as any other version I've used if you are concerned about reliability you should consider using the standalone version of Gnat supplied by ACT.
The standalone Gnat distribution from ACT comes with its copy of the correct version of gcc and can install Gnat and its gcc in a separate directory. The binary versions from ACT's web site have C++ support removed so if you want gcc to support C++ and Ada simultaneously you'll have to recompile gcc and Gnat from their sources.
The ACT releases of GNAT include letter codes:
The version numbers have special meaning:
NoteIt is possible to install one version of gcc overtop of another and to select one version or the other using the gcc -V switch but gcc must again be recompiled from its (newest) sources to make it aware of the other version.

There are patches available via the ALT web site for compiling gnat from the sources for the egcs compiler instead of gcc. egcs (pronounced "eggs") is a variation of gcc designed specifically for Pentium computers. Egcs is based on gcc 2.8.0. Slackware 3.6 for example used egcs. The egcs optimizations are being merged with gcc for the upcoming release of gcc 3.0. ACT has announced plans to fold Gnat into the Gcc project for Gcc 3.1 and have moved some of their discussions to the gcc mailing list.

2.1 Installing the ALT RPMs

The Ada Linux Team version of Gnat is available from their web site. Versions exist for the Red Hat S.u.S.E. and Debian distributions. They may also work on the Mandrake and Caldera distributions.

Recently the chief ALT maintainer has moved onto other interests and the ALT web site is out-of-date. Due to Gnat's integration into Gcc 3.x there has been less interested in Gnat RPMS. However there are still RPMs around hosted by other people (and usually based on the ALT system). Check out other sites such as ftp.ada95.com/pub/gnat-3.14p/RH-7/ or http://prdownloads.sourceforge.net/gnuada/.

The ALT versions include support for ASIS GLADE and native Linux threads. The package includes gnatgcc a version of gcc with Gnat and C++ support and gnatgdb a version of gdb that supports Ada source code plus gnatprep and the other Gnat utilities.

NoteThe rpm files are built for Red Hat and S.u.S.E. distributions. If you try installing it on another distribution use --nodep to ignore any package dependency warnings.

RPMS for Version 3.15p for Red Hat 7.2 are available at http://www.alex.wubn.net/packages/3.15p/RPMS and http://www.alex.wubn.net/packages/3.15p/SRPMS.

  1. Download and read the readme file.
  2. Download the gnat-3.xxp-runtime* rpm file (where xx is the current version of Gnat and * is the rest of the filename). For older RPMs this is gnat-3.xxp*.
  3. Download the gnat-3.xxp rpm file. For older RPMs this is gnat-3.xxp-devel*.
  4. rpm -i gnat-3.xxp-runtime*
  5. rpm -i gnat-3.xxp*
  6. Download and install any of the additional Gnat packages you need
The rpm files on the ALT site are configured to work with the ALT version of gnat. To install them simply download them and run rpm with the -i switch.

The ALT GNAT build system is available for those wanting to know more about how the RPMs are constructed. Using CVS you can check our the source code.

   export CVSROOT=":pserver:anoncvs@hornet.rus.uni-stuttgart.de:/var/cvs"
   cd $HOME
   cvs login  # (use empty password)
   cvs -z9 co -d ALT gnuada/alt-build

2.2 Installing the ACT Binaries

The latest version of the Gnat compiler is available from the ACT FTP site. These binaries do not have the extra features available with the ALT RPMs but they include extra installation information including how to install Gnat's various add-ons. There are also versions for other operating systems besides Linux.

Note Gnat 3.12 and older have an additional install option to overwrite you're existing copy of gcc provided it is right version. Since it is rare that a distribution has the exact same version of gcc this option is no longer provided.

ACT will sometimes release several versions of Gnat for different C libraries. When downloading the binaries make sure that you download the version compiled against the appropriate C library. This is due to the constantly evolving nature of Linux.

NoteTo find out which libc library your distribution uses examine the /lib/libc.so link to find out which file it points to. For example if /lib/libc.so points to a libc5 library then you'll need the libc5 version.

The latest public version from ACT is 3.13p which has been compiled for gcc 2.8.1.

By default Gnat is installed in a /usr/gnat subdirectory.

If you don't have gcc 2.8.1 you can specify a separate directory where gnat will install itself and its own personal copy of gcc 2.8.1. Using this method you need to perform an additional step. The installation program (doconfig) creates a shell script containing environment variables that you can copy to your shell startup script (under bash this is usually the .profile file in your home directory). Gcc uses these variables to locate the gnat files.

You will need to include the gnat directory in the front of your PATH variable to prevent gnat from using the gcc that came with your Linux distribution. For example use the shell command:

  export PATH="/usr/gnat/bin:$PATH"

Only use this command when you want to use Gnat since it effectively hides the copy of gcc that came with your distribution.

If you don't want gnat to be enabled by default you can write a short shell script that assigns the environment variables sets the path and starts a new shell.

2.3 Compiling Gnat from its Sources

Occasionally you may want to compile Gnat yourself from its sources. For example

As of GCC 3.1 GNAT 5.x is included with the GCC sources. Because this is the first version of GNAT designed for compatibility with the latest GCC releases GNAT 5.x is officially a "test version" with known bugs. (Previous versions of GNAT were "hard-coded" and tested for particular GCC releases.)

The latest GCC build instructions are at http://gcc.gnu.org/install/

2.4 Case Study: Building GCC 3.2 with Ada

We tried to build GCC 3.2 with Ada on Red Hat 7.1 and a Pentium II.

  1. You must have a working copy of GNAT 3.14 in order to compile the Ada language (GNAT 3.15 won't work). (For JGNAT 3.13 is your best choice.) GNAT is partially written in Ada 95. If you are compiling GCC/Ada on a system that has no Ada compiler available you will have to attempt to write a GCC cross-compiler. Instructions are at http://www.objsw.com/CrossGCC/ and the steps are different.
  2. Download gcc-3.2.tar.bz2. This contains all languages tests and runtime libraries. Alternatively download the individual packages for only the languages that you will run--you must download gcc-core (the C language) and gcc-ada (the Ada language).
       $ ls -l gcc-3.2.tar.bz2
       -rwxr-xr-x     1 root      root      20523950 Aug 21 19:39 gcc-3.2-tar.bz2
  3. Unpack the downloaded files using the tar command into the same directory.
       $ tar xfvj gcc-3.2.tar.bz2
  4. Create an "objdir" directory. Object files and configuration files will be stored here. This keeps the original directories unchanged. $ mkdir objdir $ cd objdir
  5. For Gcc 3.3 you may have to build libiberty first:
    cd objdir
    mkdir libiberty
    cd libiberty
  6. With your current directory "objdir" and the GCC 3.2 sources in the adjacent ../gcc-3.2 directory run configure to determine your operating system and its requirements. This will also create a customized Makefiles to build GCC on your system.
       $ nice ../gcc-3.2/configure
       Configuring for a i686-pc-linux-gnu host
  7. Check the machine dependent notes for i?86-*-linux. Need binutils- or newer for exception handling. If your bintutils package is out of date you will have to upgrade it.
       $ rpm -q binutils
  8. Build the compiler with make (if you already have a version of GCC installed) or make bootstrap (if you are building GCC for the first time using a different C compiler and libraries).
       $ nice make
    This step takes the most time. GCC will need to build all the languages three times. These are called the stage1 stage2 and stage3 compilers.
  9. The GCC run-time library and tools are currenty built separately.
       $ cd ../gcc-3.2/gcc/ada
       $ touch treprts.ads [es]info.h nmake.ad[bs]
       $ cd ../../../objdir/gcc
       $ make gnatlib_and_tools
       $ cd ..
    It may be a good idea to use "LD_ASSUME_KERNEL=2.2.5" or "LD_ASSUME_KERNEL=2.4.1"...this affects the thread support.
  10. The compile crashed with a gnat bug. We ran the make command again and it succeeded.
  11. If you have downloaded the GCC testsuites dejagnu tcl and expect you can run testsuites to verify the GCC compilers. These testsuites do not test the Ada compiler.
  12. Install the compiler. By default it is installed in the /usr/local/ subdirectories. Do not install GCC overtop of an existing version: remove the old version before running "make install". (There was a bug in some versions of Gcc 3.3 that changed the .ads files to read-only instead of the .ali files.)
        $ make install

2.5 Compiling Older Versions of Gnat from Sources

In order to recompile Gnat 3.x you'll need the following:

  1. A copy of the gcc sources in order to build a copy of gcc that's compatible with Gnat. The required version is listed in the Gnat documentation.
  2. A copy of the gnat sources. The sources are available for download from the gnat download site and its mirrors.
[I should compile gnat and make notes and flush out the details more--KB]

First you need to recompile the gcc compiler. Make sure you follow gcc's instructions for activating Ada support.

make CFLAGS="-g -fsigned-char" LANGUAGES="c c++ ada"
make stage1
make CC="stage1/xgcc -Bstage1/" CC="-g -O2 -fsigned-char" STAGE_PREFIX="stage1/" LANGUAGES="c c++ ada"
<build tools and lib with CC="./xgcc -B./">
There are two problems that can arise:
  1. The standard C library may have changed.
  2. The souce code for the gcc compiler itself may have changed.
Changes to the C library rare unless the library is several generations out of date. Even so by consulting the man pages you can usually find out the new parameters the various C functions expect.

Upgrading gcc to a new version of gcc however can be difficult. Gnat's gcc patches are designed for a specific version of gcc. It is usually a good idea to get a copy of the source code for the version of gcc Gnat was designed for and compile a second gcc compiler just for use with Gnat. For gnat 3.13 you'll need the gcc 2.8.1 source code. You should be able to compile an older version of gcc to work with newer C libraries provided the compiler is only a few months out of date.

Now follow the directions to compile Gnat. Make sure libgnat.a is accessible to the linker. If it isn't copy it to /usr/lib and run ldconfig to update Linux's shared library tables.

2.6 Case Study: Installing Gnat 3.11 on over an old Linux Distribution

We installed Gnat 3.11p on a Pentium running a Slackware distribution with egcs and lib6. We wanted to replace egcs with gcc 2.8.1 and install the Gnat binaries (compiled for 2.8.1) over top.

We first went to the Sunsite mirror which provides Linux compiled binaries of gcc ready to be unpacked and installed Unfortunately the readme file reported they had trouble compiling gcc and supplied egcs instead. egcs is based on gcc 2.8.0 which meant that we couldn't use it with gnat 3.11. Instead we downloaded the gcc 2.8.1 source code from a GNU FTP mirror site and prepared to build the compiler from scratch.

  1. We ran Gnat doconfig program and select option 1. The gcc path that it's expecting is displayed as i686-pc-linux-gnu. This was going to be our configuration host setting for gcc.
  2. We followed the instructions in the gcc INSTALL file. configure --with-gnu-as --with-gnu-ld --enable-threads=posix --host i686-pc-linux-gnu
  3. We checked the gcc makefile to make sure i686-pc-linux-gnu was reasonable. It required lib6 and lib6 was installed. The Makefile also showed that the i686 setting is compatible with our Pentium (i586).
  4. Before running make we changed the Makefile's OLDCC variable from cc to /usr/bin/gcc. There was a cpp syntax error while building libgcc1.a probably the error the Slackware people encountered. We tracked down the line causing the problem in the Makefile and discovered they were calling cc to do the compiling which doesn't handle the C preprocessor (cpp) properly. Typing in the line at the shell prompt showed that /usr/bin/gcc worked fine while /usr/bin/cc would not. The note in the Makefile said we shouldn't use gcc to avoid circular references in some of the functions (that is that it might inadvertantly call the 2.8.1 compiler instead of the old compiler) so we made sure we included the full path.
  5. make LANGUAGES="c c++"
  6. mkdir stage1; make stage1
    make CC="stage1/xgcc -Bstage1/" CFLAGS="-g -O2 -fsigned-char"
  7. mkdir stage2; make stage2
    make CC="stage2/xgcc -Bstage2/" CFLAGS="-g -O2 -fsigned-char"
  8. make compare reported no errors.
  9. make install CC="stage2/xgcc" -Bstage2/" CFLAGS="-g -O2" LANGUAGES="c c++"
  10. gcc -v  and  gcc -dumpversion reported the correct version. We deleted the old /usr/lib/gcc-lib/i486-linux directory to save some space.
  11. We installed gnat by running doinstall

If we were doing C++ programming we would need to install the standard C++ library libstdc++ as well. In this case we enabled C++ support to avoid recompiling gcc for C++ in the future.

2.7 Gnat and Windows

Although this is a Linux book here's some tips for Windows users:

Most Gcc uses use the Cygwin system which has a POSIX-compliant run-time system. This makes it easier to port and run programs from UNIX-like systems including Linux. However Gnat is based on Mingw and it uses the msvcrt runtime. (The reason for Mingw is apparently a problem with the Cygwin licensing agreement.) Unfortunately because they have different run-time systems Cygwin and Mingw are not compatible with one another.

If you want to build it on windows you'll need the Mingw compiler. Try http://www.mingw.org/download.shtml" and look for the "experimental/test packages" section. If you have Cygwin and want it to use Gnat at the same time you'll modify the registry entries for Gnat and replace GCC for GNAT_GCC in key names/values. If you're willing to use the Mingwin version of Gcc for your C programs put the Gnat/Mingwin path ahead of the Cygwin path in your environment PATH variable.

For those simply looking for a compiler to run at the DOS prompt DJGPP (the Gcc for MS-DOS project) supports recent versions of Gnat.

ACT sells commerical support for Gnat on VxWorks the Windows emulator for Linux.


  <--Last Chapter Table of Contents Next Chapter-->