[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. The GNAT Compilation Model

2.1 Source Representation  
2.2 Foreign Language Representation  
2.3 File Naming Rules  
2.4 Using Other File Names  
2.5 Generating Object Files  
2.6 Source Dependencies  
2.7 The Ada Library Information Files  
2.8 Representation of Time Stamps  
2.9 Binding an Ada Program  
2.10 Mixed Language Programming  
2.11 Building mixed Ada & C++ programs  
2.12 Comparison between GNAT and C/C++ Compilation Models  
2.13 Comparison between GNAT and Conventional Ada Library Models  

This chapter describes the compilation model used by GNAT. Although similar to that used by other languages, such as C and C++, this model is substantially different from the traditional Ada compilation models, which are based on a library. The model is initially described without reference to the library-based model. If you have not previously used an Ada compiler, you need only read the first part of this chapter. The last section describes and discusses the differences between the GNAT model and the traditional Ada compiler models. If you have used other Ada compilers, this section will help you to understand those differences, and the advantages of the GNAT model.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Source Representation

Ada source programs are represented in standard text files, using Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar 7-bit ASCII set, plus additional characters used for representing foreign languages (see section 2.2 Foreign Language Representation for support of non-USA character sets). The format effector characters are represented using their standard ASCII encodings, as follows:

Vertical tab, 16#0B#

Horizontal tab, 16#09#

Carriage return, 16#0D#

Line feed, 16#0A#

Form feed, 16#0C#

Source files are in standard text file format. In addition, GNAT will recognize a wide variety of stream formats, in which the end of physical physical lines is marked by any of the following sequences: LF, CR, CR-LF, or LF-CR. This is useful in accommodating files that are imported from other operating systems.

The end of a source file is normally represented by the physical end of file. However, the control character 16#1A# (SUB) is also recognized as signalling the end of the source file. Again, this is provided for compatibility with other operating systems where this code is used to represent the end of file.

Each file contains a single Ada compilation unit, including any pragmas associated with the unit. For example, this means you must place a package declaration (a package spec) and the corresponding body in separate files. An Ada compilation (which is a sequence of compilation units) is represented using a sequence of files. Similarly, you will place each subunit or child unit in a separate file.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2 Foreign Language Representation

GNAT supports the standard character sets defined in Ada 95 as well as several other non-standard character sets for use in localized versions of the compiler (see section 3.2.9 Character Set Control).

2.2.1 Latin-1  
2.2.2 Other 8-Bit Codes  
2.2.3 Wide Character Encodings  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.1 Latin-1

The basic character set is Latin-1. This character set is defined by ISO standard 8859, part 1. The lower half (character codes 16#00# ... 16#7F#) is identical to standard ASCII coding, but the upper half is used to represent additional characters. These include extended letters used by European languages, such as French accents, the vowels with umlauts used in German, and the extra letter A-ring used in Swedish.

For a complete list of Latin-1 codes and their encodings, see the source file of library unit Ada.Characters.Latin_1 in file `a-chlat1.ads'. You may use any of these extended characters freely in character or string literals. In addition, the extended characters that represent letters can be used in identifiers.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.2 Other 8-Bit Codes

GNAT also supports several other 8-bit coding schemes:

Latin-2 letters allowed in identifiers, with uppercase and lowercase equivalence.

Latin-3 letters allowed in identifiers, with uppercase and lowercase equivalence.

Latin-4 letters allowed in identifiers, with uppercase and lowercase equivalence.

IBM PC (code page 437)
This code page is the normal default for PCs in the U.S. It corresponds to the original IBM PC character set. This set has some, but not all, of the extended Latin-1 letters, but these letters do not have the same encoding as Latin-1. In this mode, these letters are allowed in identifiers with uppercase and lowercase equivalence.

IBM PC (code page 850)
This code page is a modification of 437 extended to include all the Latin-1 letters, but still not with the usual Latin-1 encoding. In this mode, all these letters are allowed in identifiers with uppercase and lowercase equivalence.

Full Upper 8-bit
Any character in the range 80-FF allowed in identifiers, and all are considered distinct. In other words, there are no uppercase and lowercase equivalences in this range. This is useful in conjunction with certain encoding schemes used for some foreign character sets (e.g. the typical method of representing Chinese characters on the PC).

No Upper-Half
No upper-half characters in the range 80-FF are allowed in identifiers. This gives Ada 83 compatibility for identifier names.

For precise data on the encodings permitted, and the uppercase and lowercase equivalences that are recognized, see the file `csets.adb' in the GNAT compiler sources. You will need to obtain a full source release of GNAT to obtain this file.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.3 Wide Character Encodings

GNAT allows wide character codes to appear in character and string literals, and also optionally in identifiers, by means of the following possible encoding schemes:

Hex Coding
In this encoding, a wide character is represented by the following five character sequence:

   ESC a b c d

Where a, b, c, d are the four hexadecimal characters (using uppercase letters) of the wide character code. For example, ESC A345 is used to represent the wide character with code 16#A345#. This scheme is compatible with use of the full Wide_Character set.

Upper-Half Coding
The wide character with encoding 16#abcd# where the upper bit is on (in other words, "a" is in the range 8-F) is represented as two bytes, 16#ab# and 16#cd#. The second byte cannot be a format control character, but is not required to be in the upper half. This method can be also used for shift-JIS or EUC, where the internal coding matches the external coding.

Shift JIS Coding
A wide character is represented by a two-character sequence, 16#ab# and 16#cd#, with the restrictions described for upper-half encoding as described above. The internal character code is the corresponding JIS character according to the standard algorithm for Shift-JIS conversion. Only characters defined in the JIS code set table can be used with this encoding method.

EUC Coding
A wide character is represented by a two-character sequence 16#ab# and 16#cd#, with both characters being in the upper half. The internal character code is the corresponding JIS character according to the EUC encoding algorithm. Only characters defined in the JIS code set table can be used with this encoding method.

UTF-8 Coding
A wide character is represented using UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO 10646-1/Am.2. Depending on the character value, the representation is a one, two, or three byte sequence:

   16#0000#-16#007f#: 2#0xxxxxxx#
   16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
   16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#

where the xxx bits correspond to the left-padded bits of the 16-bit character value. Note that all lower half ASCII characters are represented as ASCII bytes and all upper half characters and other wide characters are represented as sequences of upper-half (The full UTF-8 scheme allows for encoding 31-bit characters as 6-byte sequences, but in this implementation, all UTF-8 sequences of four or more bytes length will be treated as illegal).

Brackets Coding
In this encoding, a wide character is represented by the following eight character sequence:

   [ " a b c d " ]

Where a, b, c, d are the four hexadecimal characters (using uppercase letters) of the wide character code. For example, ["A345"] is used to represent the wide character with code 16#A345#. It is also possible (though not required) to use the Brackets coding for upper half characters. For example, the code 16#A3# can be represented as ["A3"].

This scheme is compatible with use of the full Wide_Character set, and is also the method used for wide character encoding in the standard ACVC (Ada Compiler Validation Capability) test suite distributions.

Note: Some of these coding schemes do not permit the full use of the Ada 95 character set. For example, neither Shift JIS, nor EUC allow the use of the upper half of the Latin-1 set.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 File Naming Rules

The default file name is determined by the name of the unit that the file contains. The name is formed by taking the full expanded name of the unit and replacing the separating dots with hyphens and using lowercase for all letters.

An exception arises if the file name generated by the above rules starts with one of the characters a,g,i, or s, and the second character is a minus. In this case, the character tilde is used in place of the minus. The reason for this special rule is to avoid clashes with the standard names for child units of the packages System, Ada, Interfaces, and GNAT, which use the prefixes s- a- i- and g- respectively.

The file extension is `.ads' for a spec and `.adb' for a body. The following list shows some examples of these rules.

Main (spec)
Main (body)
Arith_Functions (package spec)
Arith_Functions (package body)
Func.Spec (child package spec)
Func.Spec (child package body)
Sub (subunit of Main)
A.Bad (child package body)

Following these rules can result in excessively long file names if corresponding unit names are long (for example, if child units or subunits are heavily nested). An option is available to shorten such long file names (called file name "krunching"). This may be particularly useful when programs being developed with GNAT are to be used on operating systems with limited file name lengths. See section 11.2 Using gnatkr.

Of course, no file shortening algorithm can guarantee uniqueness over all possible unit names; if file name krunching is used, it is your responsibility to ensure no name clashes occur. Alternatively you can specify the exact file names that you want used, as described in the next section. Finally, if your Ada programs are migrating from a compiler with a different naming convention, you can use the gnatchop utility to produce source files that follow the GNAT naming conventions. (For details see section 7. Renaming Files Using gnatchop.)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.4 Using Other File Names

In the previous section, we have described the default rules used by GNAT to determine the file name in which a given unit resides. It is often convenient to follow these default rules, and if you follow them, the compiler knows without being explicitly told where to find all the files it needs.

However, in some cases, particularly when a program is imported from another Ada compiler environment, it may be more convenient for the programmer to specify which file names contain which units. GNAT allows arbitrary file names to be used by means of the Source_File_Name pragma. The form of this pragma is as shown in the following examples:

   pragma Source_File_Name (My_Utilities.Stacks,
     Spec_File_Name => "myutilst_a.ada");
   pragma Source_File_name (My_Utilities.Stacks,
     Body_File_Name => "myutilst.ada");

As shown in this example, the first argument for the pragma is the unit name (in this example a child unit). The second argument has the form of a named association. The identifier indicates whether the file name is for a spec or a body; the file name itself is given by a string literal.

The source file name pragma is a configuration pragma, which means that normally it will be placed in the `gnat.adc' file used to hold configuration pragmas that apply to a complete compilation environment. For more details on how the `gnat.adc' file is created and used see section 8.1 Handling of Configuration Pragmas

GNAT allows completely arbitrary file names to be specified using the source file name pragma. However, if the file name specified has an extension other than `.ads' or `.adb' it is necessary to use a special syntax when compiling the file. The name in this case must be preceded by the special sequence -x followed by a space and the name of the language, here ada, as in:

   $ gnatgcc -c -x ada peculiar_file_name.sim

gnatmake handles non-standard file names in the usual manner (the non-standard file name for the main program is simply used as the argument to gnatmake). Note that if the extension is also non-standard, then it must be included in the gnatmake command, it may not be omitted.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.5 Generating Object Files

An Ada program consists of a set of source files, and the first step in compiling the program is to generate the corresponding object files. These are generated by compiling a subset of these source files. The files you need to compile are the following:

The preceding rules describe the set of files that must be compiled to generate the object files for a program. Each object file has the same name as the corresponding source file, except that the extension is `.o' as usual.

You may wish to compile other files for the purpose of checking their syntactic and semantic correctness. For example, in the case where a package has a separate spec and body, you would not normally compile the spec. However, it is convenient in practice to compile the spec to make sure it is error-free before compiling clients of this spec, because such compilations will fail if there is an error in the spec.

GNAT provides an option for compiling such files purely for the purposes of checking correctness; such compilations are not required as part of the process of building a program. To compile a file in this checking mode, use the -gnatc switch.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.6 Source Dependencies

A given object file clearly depends on the source file which is compiled to produce it. Here we are using depends in the sense of a typical make utility; in other words, an object file depends on a source file if changes to the source file require the object file to be recompiled. In addition to this basic dependency, a given object may depend on additional source files as follows:

These rules are applied transitively: if unit A with's unit B, whose elaboration calls an inlined procedure in package C, the object file for unit A will depend on the body of C, in file `c.adb'.

The set of dependent files described by these rules includes all the files on which the unit is semantically dependent, as described in the Ada 95 Language Reference Manual. However, it is a superset of what the ARM describes, because it includes generic, inline, and subunit dependencies.

An object file must be recreated by recompiling the corresponding source file if any of the source files on which it depends are modified. For example, if the make utility is used to control compilation, the rule for an Ada object file must mention all the source files on which the object file depends, according to the above definition. The determination of the necessary recompilations is done automatically when one uses gnatmake.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.7 The Ada Library Information Files

Each compilation actually generates two output files. The first of these is the normal object file that has a `.o' extension. The second is a text file containing full dependency information. It has the same name as the source file, but an `.ali' extension. This file is known as the Ada Library Information (ALI) file.

Normally you need not be concerned with the contents of this file. This section is included in case you want to understand how these files are being used by the binder and other GNAT utilities. Each ALI file consists of a series of lines of the form:

   Key_Character parameter parameter ...

The first two lines in the file identify the library output version and Standard version. These are required to be consistent across the entire set of compilation units in your program.

   V "xxxxxxxxxxxxxxxx"

This line indicates the library output version, as defined in `gnatvsn.ads'. It ensures that separate object modules of a program are consistent. The library output version must be changed if anything in the compiler changes that would affect successful binding of modules compiled separately. Examples of such changes are modifications in the format of the library information described in this package, modifications to calling sequences, or to the way data is represented.

   S "xxxxxxxxxxxxxxxx"

This line contains information regarding types declared in packages Standard as stored in Gnatvsn.Standard_Version. The purpose of this information is to ensure that all units in a program are compiled with a consistent set of options. This is critical on systems where, for example, the size of Integer can be set by command line switches.

   M type [priority]

This line is present only for a unit that can be a main program. type is either P for a parameterless procedure or F for a function returning a value of integral type. The latter is for writing a main program that returns an exit status. priority is present only if there was a valid pragma Priority in the corresponding unit to set the main task priority. It is an unsigned decimal integer.

   F x

This line is present if a pragma Float_Representation or Long_Float is used to specify other than the default floating-point format. This option applies only to implementations of GNAT for the Digital Alpha Systems. The character x is 'I' for IEEE_Float, 'G' for VAX_Float with Long_Float using G_Float, and 'D' for VAX_Float for Long_Float with D_Float.

   P L=x Q=x T=x

This line is present if the unit uses tasking directly or indirectly, and has one or more valid xxx_Policy pragmas that apply to the unit. The arguments are as follows

   L=x (locking policy)

This is present if a valid Locking_Policy pragma applies to the unit. The single character indicates the policy in effect (e.g. `C' for Ceiling_Locking).

   Q=x (queuing policy)
This is present if a valid Queuing_Policy pragma applies to the unit. The single character indicates the policy in effect (e.g. `P' for Priority_Queuing).

   T=x (task_dispatching policy)
This is present if a valid Task_Dispatching_Policy pragma applies to the unit. The single character indicates the policy in effect (e.g. `F' for FIFO_Within_Priorities).

Following these header lines is a set of information lines, one per compilation unit. Each line lists a unit in the object file corresponding to this ALI file. In particular, when a package body or subprogram body is compiled there will be two such lines, one for the spec and one for the body, with the entry for the body appearing first. This is the only case in which a single ALI file contains more than one unit. Note that subunits do not count as compilation units for this purpose, and generate no library information, because they are inlined. The lines for each compilation unit have the following form:

   U unit-name source-name version [attributes]

This line identifies the unit to which this section of the library information file applies. unit-name is the unit name in internal format, as described in package Uname, and source-name is the name of the source file containing the unit.

version is the version, given by eight hexadecimal characters with lowercase letters. This value is a hash code that includes contributions from the time stamps of this unit and all the units on which it semantically depends.

The optional attributes are a series of two-letter codes indicating information about the unit. They indicate the nature of the unit and they summarize information provided by categorization pragmas.

Unit has pragma Elaborate_Body.

Unit has no elaboration routine. All subprogram specs are in this category, as are subprogram bodies if access-before-elaboration checks are being generated. Package bodies and specs may or may not have NE set, depending on whether or not elaboration code is required.

Unit is a package.

Unit has pragma Pure.

Unit has pragma Preelaborate.

Unit has pragma Remote_Call_Interface.

Unit has pragma Remote_Types.

Unit has pragma Shared_Passive.

Unit is a subprogram.

The attributes may appear in any order, separated by spaces. The next set of lines in the ALI file have the following form:

   W unit-name [source-name lib-name [E] [EA] [ED]]

One of these lines is present for each unit mentioned in an explicit with clause in the current unit. unit-name is the unit name in internal format. source-name is the file name of the file that must be compiled to compile that unit (usually the file for the body, except for packages that have no body). lib-name is the file name of the library information file that contains the results of compiling the unit. The E and EA parameters are present if pragma Elaborate or pragma Elaborate_All, respectively, apply to this unit. ED is used to indicate that the compiler has determined that a pragma Elaborate_All for this unit would be desirable. For details on the use of the ED parameter see See section 9. Elaboration Order Handling in GNAT.

Following the unit information is an optional series of lines that indicate the usage of pragma Linker_Options. For each appearance of pragma Linker_Options in any of the units for which unit lines are present, a line of the form

   L string

appears. string is the string from the pragma enclosed in quotes. Within the quotes, the following can occur:

For further details, see Stringt.Write_String_Table_Entry in the file `stringt.ads'. Note that wide characters of the form {hhhh} cannot be produced, because pragma Linker_Option accepts only String, not Wide_String.

Finally, the rest of the ALI file contains a series of lines that indicate the source files on which the compiled units depend. This is used by the binder for consistency checking and looks like:
   D source-name time-stamp [comments]

where comments, if present, must be separated from the time stamp by at least one blank. Currently this field is unused.

Blank lines are ignored when the library information is read, and separate sections of the file are separated by blank lines to help readability. Extra blanks between fields are also ignored.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.8 Representation of Time Stamps

All compiled units are marked with a time stamp, which is derived from the source file. The binder uses these time stamps to ensure consistency of the set of units that constitutes a single program. Time stamps are fourteen-character strings of the form YYYYMMDDHHMMSS. The fields have the following meaning:

year (4 digits)
month (2 digits 01-12)
day (2 digits 01-31)
hour (2 digits 00-23)
minutes (2 digits 00-59)
seconds (2 digits 00-59)

Time stamps may be compared lexicographically (in other words, the order of Ada comparison operations on strings) to determine which is later or earlier. However, in normal mode, only equality comparisons have any effect on the semantics of the library. Later/earlier comparisons are used only for determining the most informative error messages to be issued by the binder.

The time stamp is the actual stamp stored with the file without any adjustment resulting from time zone comparisons. This avoids problems in using libraries across networks with clients spread across multiple time zones, but it means that the time stamp might differ from that displayed in a directory listing. For example, in UNIX systems, file time stamps are stored in Greenwich Mean Time (GMT), but the ls command displays local times.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.9 Binding an Ada Program

When using languages such as C and C++, once the source files have been compiled the only remaining step in building an executable program is linking the object modules together. This means that it is possible to link an inconsistent version of a program, in which two units have included different versions of the same header.

The rules of Ada do not permit such an inconsistent program to be built. For example, if two clients have different versions of the same package, it is illegal to build a program containing these two clients. These rules are enforced by the GNAT binder, which also determines an elaboration order consistent with the Ada rules.

The GNAT binder is run after all the object files for a program have been created. It is given the name of the main program unit, and from this it determines the set of units required by the program, by reading the corresponding ALI files. It generates error messages if the program is inconsistent or if no valid order of elaboration exists.

If no errors are detected, the binder produces a main program, in Ada by default, that contains calls to the elaboration procedures of those compilation unit that require them, followed by a call to the main program. This Ada program is compiled to generate the object file for the main program. The name of the Ada file is b~xxx.adb (with the corresponding spec b~xxx.ads) where xxx is the name of the main program unit.

Finally, the linker is used to build the resulting executable program, using the object from the main program from the bind step as well as the object files for the Ada units of the program.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.10 Mixed Language Programming

2.10.1 Interfacing to C  
2.10.2 Calling Conventions  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.10.1 Interfacing to C

There are two ways to build a program that contains some Ada files and some other language files depending on whether the main program is in Ada or not. If the main program is in Ada, you should proceed as follows:

  1. Compile the other language files to generate object files. For instance:
    gnatgcc -c file1.c
    gnatgcc -c file2.c

  2. Compile the Ada units to produce a set of object files and ALI files. For instance:
    gnatmake -c my_main.adb

  3. Run the Ada binder on the Ada main program. For instance:
    gnatbind my_main

  4. Link the Ada main program, the Ada objects and the other language objects. For instance:
    gnatlink my_main.ali file1.o file2.o

The three last steps can be grouped in a single command:
gnatmake my_main.adb -largs file1.o file2.o

If the main program is in some language other than Ada, Then you may have more than one entry point in the Ada subsystem. You must use a special option of the binder to generate callable routines to initialize and finalize the Ada units (see section 4.6 Binding with Non-Ada Main Programs). Calls to the initialization and finalization routines must be inserted in the main program, or some other appropriate point in the code. The call to initialize the Ada units must occur before the first Ada subprogram is called, and the call to finalize the Ada units must occur after the last Ada subprogram returns. You use the same procedure for building the program as described previously. In this case, however, the binder only places the initialization and finalization subprograms into file `b~xxx.adb' instead of the main program. So, if the main program is not in Ada, you should proceed as follows:

  1. Compile the other language files to generate object files. For instance:
    gnatgcc -c file1.c
    gnatgcc -c file2.c

  2. Compile the Ada units to produce a set of object files and ALI files. For instance:
    gnatmake -c entry_point1.adb
    gnatmake -c entry_point2.adb

  3. Run the Ada binder on the Ada main program. For instance:
    gnatbind -n entry_point1 entry_point2

  4. Link the Ada main program, the Ada objects and the other language objects. You only need to give the last entry point here. For instance:
    gnatlink entry_point2.ali file1.o file2.o

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.10.2 Calling Conventions

GNAT follows standard calling sequence conventions and will thus interface to any other language that also follows these conventions. The following Convention identifiers are recognized by GNAT:

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.11 Building mixed Ada & C++ programs

Building a mixed application containing both Ada and C++ code may be a challenge for the unaware programmer. As a matter of fact, this interfacing has not been standardized in the Ada 95 reference manual due to the immaturity and lack of standard of C++ at the time. This section gives a few hints that should make this task easier. In particular the first section addresses the differences with interfacing with C. The second section looks into the delicate problem of linking the complete application from its Ada and C++ parts. The last section give some hints on how the GNAT runtime can be adapted in order to allow inter-language dispatching with a new C++ compiler.

2.11.1 Interfacing to C++  
2.11.2 Linking a mixed C++ & Ada program  
2.11.3 Adapting the runtime to a new C++ compiler  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.11.1 Interfacing to C++

GNAT supports interfacing with C++ compilers generating code that is compatible with the standard Application Binary Interface of the given platform.

Interfacing can be done at 3 levels: simple data, subprograms and classes. In the first 2 cases, GNAT offer a specific Convention CPP that behaves exactly like Convention C. Usually C++ mangle names of subprograms and currently GNAT does not provide any help to solve the demangling problem. This problem can be addressed in 2 ways:

Interfacing at the class level can be achieved by using the GNAT specific pragmas such as CPP_Class and CPP_Virtual. See the GNAT Reference Manual for additional information.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.11.2 Linking a mixed C++ & Ada program

Usually the linker of the C++ development system must be used to link mixed applications because most C++ systems will resolve elaboration issues (such as calling constructors on global class instances) transparently during the link phase. GNAT has been adapted to ease the use of a foreign linker for the last phase. Three cases can be considered:

  1. Using GNAT and G++ (GNU C++ compiler) from the same GCC installation. The c++ linker can simply be called by using the c++ specific driver called c++. Note that this setup is not very common because it may request recompiling the whole GCC tree from sources and it does not allow to upgrade easily to a new version of one compiler for one of the two languages without taking the risk of destabilizing the other.

    $ c++ -c file1.C
    $ c++ -c file2.C
    $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++

  2. Using GNAT and G++ from 2 different GCC installations. If both compilers are on the PATH, the same method can be used. It is important to be aware that environment variables such as C_INCLUDE_PATH or GCC_EXEC_PREFIX will affect both compilers at the same time and thus may make one of the 2 compilers operate improperly if they are set for the other. In particular it is important that the link command has access to the proper gcc library 'libgnatgcc.a', that is to say the one that is part of the C++ compiler installation. The implicit link command as suggested in the gnatmake command from the former example can be replaced by an explicit link command with full verbosity in order to verify which library is used:
    $ gnatbind ada_unit
    $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
    If there is a problem due to interfering environment variables, it can be workaround by using an intermediate script:

    $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
    $ cat ./my_script
    unset C_INCLUDE_PATH
    c++ $*

  3. Using a non GNU C++ compiler. The same set of command as previously described can be used to insure that the c++ linker is used. Nonetheless, the Ada code may implicitly depend on the gnatgcc library. The latter can be located thanks to gnatls: it is to be found on the last directory of the object path. It must then be explicitly mentioned in the link command :
    $ gnatls -v
    $ Gdir=<the last directory on the object path>
    $ gnatlink ada_unit file1.o file2.o -L$Gdir -lgnatgcc --LINK=<cpp_linker>

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.11.3 Adapting the runtime to a new C++ compiler

GNAT offers the capability to derive Ada 95 tagged types directly from preexisting C++ classes and . See "Interfacing with C++" in the GNAT reference manual. The mechanism used by GNAT for achieving such a goal has been made user configurable through a GNAT library unit Interfaces.CPP. The default version of this file is adapted to the GNU c++ compiler. Internal knowledge of the virtual table layout used by the new C++ compiler is needed to configure properly this unit. The Interface of this unit is known by the compiler and cannot be changed except for the value of the constants defining the characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size, CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source of this unit for more details.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.12 Comparison between GNAT and C/C++ Compilation Models

The GNAT model of compilation is close to the C and C++ models. You can think of Ada specs as corresponding to header files in C. As in C, you don't need to compile specs; they are compiled when they are used. The Ada with is similar in effect to the #include of a C header.

One notable difference is that, in Ada, you may compile specs separately to check them for semantic and syntactic accuracy. This is not always possible with C headers because they are fragments of programs that have less specific syntactic or semantic rules.

The other major difference is the requirement for running the binder, which performs two important functions. First, it checks for consistency. In C or C++, the only defense against assembling inconsistent programs lies outside the compiler, in a makefile, for example. The binder satisfies the Ada requirement that it be impossible to construct an inconsistent program when the compiler is used in normal mode.

The other important function of the binder is to deal with elaboration issues. There are also elaboration issues in C++ that are handled automatically. This automatic handling has the advantage of being simpler to use, but the C++ programmer has no control over elaboration. Where gnatbind might complain there was no valid order of elaboration, a C++ compiler would simply construct a program that malfunctioned at run time.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.13 Comparison between GNAT and Conventional Ada Library Models

This section is intended to be useful to Ada programmers who have previously used an Ada compiler implementing the traditional Ada library model, as described in the Ada 95 Language Reference Manual. If you have not used such a system, please go on to the next section.

In GNAT, there is no library in the normal sense. Instead, the set of source files themselves acts as the library. Compiling Ada programs does not generate any centralized information, but rather an object file and a ALI file, which are of interest only to the binder and linker. In a traditional system, the compiler reads information not only from the source file being compiled, but also from the centralized library. This means that the effect of a compilation depends on what has been previously compiled. In particular:

In GNAT, compiling one unit never affects the compilation of any other units because the compiler reads only source files. Only changes to source files can affect the results of a compilation. In particular:

The most important result of these differences is that order of compilation is never significant in GNAT. There is no situation in which one is required to do one compilation before another. What shows up as order of compilation requirements in the traditional Ada library becomes, in GNAT, simple source dependencies; in other words, there is only a set of rules saying what source files must be present when a file is compiled.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Tom Bennet on August, 25 2000 using texi2html