1 \input texinfo @c -*-texinfo-*-
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
6 @c GNAT DOCUMENTATION o
10 @c Copyright (C) 1992-2006, AdaCore o
12 @c GNAT is free software; you can redistribute it and/or modify it under o
13 @c terms of the GNU General Public License as published by the Free Soft- o
14 @c ware Foundation; either version 2, or (at your option) any later ver- o
15 @c sion. GNAT is distributed in the hope that it will be useful, but WITH- o
16 @c OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY o
17 @c or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License o
18 @c for more details. You should have received a copy of the GNU General o
19 @c Public License distributed with GNAT; see file COPYING. If not, write o
20 @c to the Free Software Foundation, 51 Franklin Street, Fifth Floor, o
21 @c Boston, MA 02110-1301, USA. o
23 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
25 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
27 @c GNAT_UGN Style Guide
29 @c 1. Always put a @noindent on the line before the first paragraph
30 @c after any of these commands:
42 @c 2. DO NOT use @example. Use @smallexample instead.
43 @c a) DO NOT use highlighting commands (@b{}, @i{}) inside an @smallexample
44 @c context. These can interfere with the readability of the texi
45 @c source file. Instead, use one of the following annotated
46 @c @smallexample commands, and preprocess the texi file with the
47 @c ada2texi tool (which generates appropriate highlighting):
48 @c @smallexample @c ada
49 @c @smallexample @c adanocomment
50 @c @smallexample @c projectfile
51 @c b) The "@c ada" markup will result in boldface for reserved words
52 @c and italics for comments
53 @c c) The "@c adanocomment" markup will result only in boldface for
54 @c reserved words (comments are left alone)
55 @c d) The "@c projectfile" markup is like "@c ada" except that the set
56 @c of reserved words include the new reserved words for project files
58 @c 3. Each @chapter, @section, @subsection, @subsubsection, etc.
59 @c command must be preceded by two empty lines
61 @c 4. The @item command should be on a line of its own if it is in an
62 @c @itemize or @enumerate command.
64 @c 5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
67 @c 6. DO NOT put trailing spaces at the end of a line. Such spaces will
68 @c cause the document build to fail.
70 @c 7. DO NOT use @cartouche for examples that are longer than around 10 lines.
71 @c This command inhibits page breaks, so long examples in a @cartouche can
72 @c lead to large, ugly patches of empty space on a page.
74 @c NOTE: This file should be submitted to xgnatugn with either the vms flag
75 @c or the unw flag set. The unw flag covers topics for both Unix and
78 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
81 @setfilename gnat_ugn_vms.info
85 @setfilename gnat_ugn_unw.info
93 @set FILE gnat_ugn_unw
98 @set FILE gnat_ugn_vms
101 @settitle @value{EDITION} User's Guide @value{PLATFORM}
102 @dircategory GNU Ada tools
104 * @value{EDITION} User's Guide (@value{FILE}) @value{PLATFORM}
107 @include gcc-common.texi
109 @setchapternewpage odd
114 Copyright @copyright{} 1995-2005, Free Software Foundation
116 Permission is granted to copy, distribute and/or modify this document
117 under the terms of the GNU Free Documentation License, Version 1.2
118 or any later version published by the Free Software Foundation;
119 with the Invariant Sections being ``GNU Free Documentation License'', with the
120 Front-Cover Texts being
121 ``@value{EDITION} User's Guide'',
122 and with no Back-Cover Texts.
123 A copy of the license is included in the section entitled
124 ``GNU Free Documentation License''.
129 @title @value{EDITION} User's Guide
134 @titlefont{@i{@value{PLATFORM}}}
140 @subtitle GNAT, The GNU Ada 95 Compiler
141 @subtitle GCC version @value{version-GCC}
146 @vskip 0pt plus 1filll
153 @node Top, About This Guide, (dir), (dir)
154 @top @value{EDITION} User's Guide
157 @value{EDITION} User's Guide @value{PLATFORM}
160 GNAT, The GNU Ada 95 Compiler@*
161 GCC version @value{version-GCC}@*
168 * Getting Started with GNAT::
169 * The GNAT Compilation Model::
170 * Compiling Using gcc::
171 * Binding Using gnatbind::
172 * Linking Using gnatlink::
173 * The GNAT Make Program gnatmake::
174 * Improving Performance::
175 * Renaming Files Using gnatchop::
176 * Configuration Pragmas::
177 * Handling Arbitrary File Naming Conventions Using gnatname::
178 * GNAT Project Manager::
179 * The Cross-Referencing Tools gnatxref and gnatfind::
180 * The GNAT Pretty-Printer gnatpp::
181 * The GNAT Metric Tool gnatmetric::
182 * File Name Krunching Using gnatkr::
183 * Preprocessing Using gnatprep::
185 * The GNAT Run-Time Library Builder gnatlbr::
187 * The GNAT Library Browser gnatls::
188 * Cleaning Up Using gnatclean::
190 * GNAT and Libraries::
191 * Using the GNU make Utility::
193 * Memory Management Issues::
194 * Stack Related Facilities::
195 * Verifying properties using gnatcheck::
196 * Creating Sample Bodies Using gnatstub::
197 * Other Utility Programs::
198 * Running and Debugging Ada Programs::
200 * Compatibility with HP Ada::
202 * Platform-Specific Information for the Run-Time Libraries::
203 * Example of Binder Output File::
204 * Elaboration Order Handling in GNAT::
206 * Compatibility and Porting Guide::
208 * Microsoft Windows Topics::
210 * GNU Free Documentation License::
213 --- The Detailed Node Listing ---
217 * What This Guide Contains::
218 * What You Should Know before Reading This Guide::
219 * Related Information::
222 Getting Started with GNAT
225 * Running a Simple Ada Program::
226 * Running a Program with Multiple Units::
227 * Using the gnatmake Utility::
229 * Editing with Emacs::
232 * Introduction to GPS::
233 * Introduction to Glide and GVD::
236 The GNAT Compilation Model
238 * Source Representation::
239 * Foreign Language Representation::
240 * File Naming Rules::
241 * Using Other File Names::
242 * Alternative File Naming Schemes::
243 * Generating Object Files::
244 * Source Dependencies::
245 * The Ada Library Information Files::
246 * Binding an Ada Program::
247 * Mixed Language Programming::
249 * Building Mixed Ada & C++ Programs::
250 * Comparison between GNAT and C/C++ Compilation Models::
252 * Comparison between GNAT and Conventional Ada Library Models::
254 * Placement of temporary files::
257 Foreign Language Representation
260 * Other 8-Bit Codes::
261 * Wide Character Encodings::
263 Compiling Ada Programs With gcc
265 * Compiling Programs::
267 * Search Paths and the Run-Time Library (RTL)::
268 * Order of Compilation Issues::
273 * Output and Error Message Control::
274 * Warning Message Control::
275 * Debugging and Assertion Control::
276 * Validity Checking::
279 * Using gcc for Syntax Checking::
280 * Using gcc for Semantic Checking::
281 * Compiling Different Versions of Ada::
282 * Character Set Control::
283 * File Naming Control::
284 * Subprogram Inlining Control::
285 * Auxiliary Output Control::
286 * Debugging Control::
287 * Exception Handling Control::
288 * Units to Sources Mapping Files::
289 * Integrated Preprocessing::
294 Binding Ada Programs With gnatbind
297 * Switches for gnatbind::
298 * Command-Line Access::
299 * Search Paths for gnatbind::
300 * Examples of gnatbind Usage::
302 Switches for gnatbind
304 * Consistency-Checking Modes::
305 * Binder Error Message Control::
306 * Elaboration Control::
308 * Binding with Non-Ada Main Programs::
309 * Binding Programs with No Main Subprogram::
311 Linking Using gnatlink
314 * Switches for gnatlink::
316 The GNAT Make Program gnatmake
319 * Switches for gnatmake::
320 * Mode Switches for gnatmake::
321 * Notes on the Command Line::
322 * How gnatmake Works::
323 * Examples of gnatmake Usage::
325 Improving Performance
326 * Performance Considerations::
327 * Reducing the Size of Ada Executables with gnatelim::
328 * Reducing the Size of Executables with unused subprogram/data elimination::
330 Performance Considerations
331 * Controlling Run-Time Checks::
332 * Use of Restrictions::
333 * Optimization Levels::
334 * Debugging Optimized Code::
335 * Inlining of Subprograms::
336 * Other Optimization Switches::
337 * Optimization and Strict Aliasing::
339 * Coverage Analysis::
342 Reducing the Size of Ada Executables with gnatelim
345 * Correcting the List of Eliminate Pragmas::
346 * Making Your Executables Smaller::
347 * Summary of the gnatelim Usage Cycle::
349 Reducing the Size of Executables with unused subprogram/data elimination
350 * About unused subprogram/data elimination::
351 * Compilation options::
353 Renaming Files Using gnatchop
355 * Handling Files with Multiple Units::
356 * Operating gnatchop in Compilation Mode::
357 * Command Line for gnatchop::
358 * Switches for gnatchop::
359 * Examples of gnatchop Usage::
361 Configuration Pragmas
363 * Handling of Configuration Pragmas::
364 * The Configuration Pragmas Files::
366 Handling Arbitrary File Naming Conventions Using gnatname
368 * Arbitrary File Naming Conventions::
370 * Switches for gnatname::
371 * Examples of gnatname Usage::
376 * Examples of Project Files::
377 * Project File Syntax::
378 * Objects and Sources in Project Files::
379 * Importing Projects::
380 * Project Extension::
381 * Project Hierarchy Extension::
382 * External References in Project Files::
383 * Packages in Project Files::
384 * Variables from Imported Projects::
387 * Stand-alone Library Projects::
388 * Switches Related to Project Files::
389 * Tools Supporting Project Files::
390 * An Extended Example::
391 * Project File Complete Syntax::
393 The Cross-Referencing Tools gnatxref and gnatfind
395 * gnatxref Switches::
396 * gnatfind Switches::
397 * Project Files for gnatxref and gnatfind::
398 * Regular Expressions in gnatfind and gnatxref::
399 * Examples of gnatxref Usage::
400 * Examples of gnatfind Usage::
402 The GNAT Pretty-Printer gnatpp
404 * Switches for gnatpp::
407 The GNAT Metrics Tool gnatmetric
409 * Switches for gnatmetric::
411 File Name Krunching Using gnatkr
416 * Examples of gnatkr Usage::
418 Preprocessing Using gnatprep
421 * Switches for gnatprep::
422 * Form of Definitions File::
423 * Form of Input Text for gnatprep::
426 The GNAT Run-Time Library Builder gnatlbr
429 * Switches for gnatlbr::
430 * Examples of gnatlbr Usage::
433 The GNAT Library Browser gnatls
436 * Switches for gnatls::
437 * Examples of gnatls Usage::
439 Cleaning Up Using gnatclean
441 * Running gnatclean::
442 * Switches for gnatclean::
443 @c * Examples of gnatclean Usage::
449 * Introduction to Libraries in GNAT::
450 * General Ada Libraries::
451 * Stand-alone Ada Libraries::
452 * Rebuilding the GNAT Run-Time Library::
454 Using the GNU make Utility
456 * Using gnatmake in a Makefile::
457 * Automatically Creating a List of Directories::
458 * Generating the Command Line Switches::
459 * Overcoming Command Line Length Limits::
462 Memory Management Issues
464 * Some Useful Memory Pools::
465 * The GNAT Debug Pool Facility::
470 Stack Related Facilities
472 * Stack Overflow Checking::
473 * Static Stack Usage Analysis::
474 * Dynamic Stack Usage Analysis::
476 Some Useful Memory Pools
478 The GNAT Debug Pool Facility
484 * Switches for gnatmem::
485 * Example of gnatmem Usage::
488 Verifying properties using gnatcheck
490 * Format of the Report File::
491 * General gnatcheck Switches::
492 * gnatcheck Rule Options::
493 * Add the Results of Compiler Checks to gnatcheck Output::
495 Sample Bodies Using gnatstub
498 * Switches for gnatstub::
500 Other Utility Programs
502 * Using Other Utility Programs with GNAT::
503 * The External Symbol Naming Scheme of GNAT::
505 * Ada Mode for Glide::
507 * Converting Ada Files to html with gnathtml::
509 Running and Debugging Ada Programs
511 * The GNAT Debugger GDB::
513 * Introduction to GDB Commands::
514 * Using Ada Expressions::
515 * Calling User-Defined Subprograms::
516 * Using the Next Command in a Function::
519 * Debugging Generic Units::
520 * GNAT Abnormal Termination or Failure to Terminate::
521 * Naming Conventions for GNAT Source Files::
522 * Getting Internal Debugging Information::
530 Compatibility with HP Ada
532 * Ada 95 Compatibility::
533 * Differences in the Definition of Package System::
534 * Language-Related Features::
535 * The Package STANDARD::
536 * The Package SYSTEM::
537 * Tasking and Task-Related Features::
538 * Pragmas and Pragma-Related Features::
539 * Library of Predefined Units::
541 * Main Program Definition::
542 * Implementation-Defined Attributes::
543 * Compiler and Run-Time Interfacing::
544 * Program Compilation and Library Management::
546 * Implementation Limits::
547 * Tools and Utilities::
549 Language-Related Features
551 * Integer Types and Representations::
552 * Floating-Point Types and Representations::
553 * Pragmas Float_Representation and Long_Float::
554 * Fixed-Point Types and Representations::
555 * Record and Array Component Alignment::
557 * Other Representation Clauses::
559 Tasking and Task-Related Features
561 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
562 * Assigning Task IDs::
563 * Task IDs and Delays::
564 * Task-Related Pragmas::
565 * Scheduling and Task Priority::
567 * External Interrupts::
569 Pragmas and Pragma-Related Features
571 * Restrictions on the Pragma INLINE::
572 * Restrictions on the Pragma INTERFACE::
573 * Restrictions on the Pragma SYSTEM_NAME::
575 Library of Predefined Units
577 * Changes to DECLIB::
581 * Shared Libraries and Options Files::
585 Platform-Specific Information for the Run-Time Libraries
587 * Summary of Run-Time Configurations::
588 * Specifying a Run-Time Library::
589 * Choosing the Scheduling Policy::
590 * Solaris-Specific Considerations::
591 * Linux-Specific Considerations::
592 * AIX-Specific Considerations::
594 Example of Binder Output File
596 Elaboration Order Handling in GNAT
598 * Elaboration Code in Ada 95::
599 * Checking the Elaboration Order in Ada 95::
600 * Controlling the Elaboration Order in Ada 95::
601 * Controlling Elaboration in GNAT - Internal Calls::
602 * Controlling Elaboration in GNAT - External Calls::
603 * Default Behavior in GNAT - Ensuring Safety::
604 * Treatment of Pragma Elaborate::
605 * Elaboration Issues for Library Tasks::
606 * Mixing Elaboration Models::
607 * What to Do If the Default Elaboration Behavior Fails::
608 * Elaboration for Access-to-Subprogram Values::
609 * Summary of Procedures for Elaboration Control::
610 * Other Elaboration Order Considerations::
614 * Basic Assembler Syntax::
615 * A Simple Example of Inline Assembler::
616 * Output Variables in Inline Assembler::
617 * Input Variables in Inline Assembler::
618 * Inlining Inline Assembler Code::
619 * Other Asm Functionality::
621 Compatibility and Porting Guide
623 * Compatibility with Ada 83::
624 * Implementation-dependent characteristics::
626 @c This brief section is only in the non-VMS version
627 @c The complete chapter on HP Ada issues is in the VMS version
628 * Compatibility with HP Ada 83::
630 * Compatibility with Other Ada 95 Systems::
631 * Representation Clauses::
633 * Transitioning from Alpha to I64 OpenVMS::
637 Microsoft Windows Topics
639 * Using GNAT on Windows::
640 * CONSOLE and WINDOWS subsystems::
642 * Mixed-Language Programming on Windows::
643 * Windows Calling Conventions::
644 * Introduction to Dynamic Link Libraries (DLLs)::
645 * Using DLLs with GNAT::
646 * Building DLLs with GNAT::
647 * GNAT and Windows Resources::
649 * Setting Stack Size from gnatlink::
650 * Setting Heap Size from gnatlink::
657 @node About This Guide
658 @unnumbered About This Guide
662 This guide describes the use of @value{EDITION},
663 a full language compiler for the Ada
664 95 programming language, implemented on OpenVMS for HP's Alpha and
665 Integrity server (I64) platforms.
668 This guide describes the use of @value{EDITION},
669 a compiler and software development
670 toolset for the full Ada 95 programming language.
672 It describes the features of the compiler and tools, and details
673 how to use them to build Ada 95 applications.
676 For ease of exposition, ``GNAT Pro'' will be referred to simply as
677 ``GNAT'' in the remainder of this document.
681 * What This Guide Contains::
682 * What You Should Know before Reading This Guide::
683 * Related Information::
687 @node What This Guide Contains
688 @unnumberedsec What This Guide Contains
691 This guide contains the following chapters:
695 @ref{Getting Started with GNAT}, describes how to get started compiling
696 and running Ada programs with the GNAT Ada programming environment.
698 @ref{The GNAT Compilation Model}, describes the compilation model used
702 @ref{Compiling Using gcc}, describes how to compile
703 Ada programs with @command{gcc}, the Ada compiler.
706 @ref{Binding Using gnatbind}, describes how to
707 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
711 @ref{Linking Using gnatlink},
712 describes @command{gnatlink}, a
713 program that provides for linking using the GNAT run-time library to
714 construct a program. @command{gnatlink} can also incorporate foreign language
715 object units into the executable.
718 @ref{The GNAT Make Program gnatmake}, describes @command{gnatmake}, a
719 utility that automatically determines the set of sources
720 needed by an Ada compilation unit, and executes the necessary compilations
724 @ref{Improving Performance}, shows various techniques for making your
725 Ada program run faster or take less space.
726 It discusses the effect of the compiler's optimization switch and
727 also describes the @command{gnatelim} tool and unused subprogram/data
731 @ref{Renaming Files Using gnatchop}, describes
732 @code{gnatchop}, a utility that allows you to preprocess a file that
733 contains Ada source code, and split it into one or more new files, one
734 for each compilation unit.
737 @ref{Configuration Pragmas}, describes the configuration pragmas
741 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
742 shows how to override the default GNAT file naming conventions,
743 either for an individual unit or globally.
746 @ref{GNAT Project Manager}, describes how to use project files
747 to organize large projects.
750 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
751 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
752 way to navigate through sources.
755 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
756 version of an Ada source file with control over casing, indentation,
757 comment placement, and other elements of program presentation style.
760 @ref{The GNAT Metric Tool gnatmetric}, shows how to compute various
761 metrics for an Ada source file, such as the number of types and subprograms,
762 and assorted complexity measures.
765 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
766 file name krunching utility, used to handle shortened
767 file names on operating systems with a limit on the length of names.
770 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
771 preprocessor utility that allows a single source file to be used to
772 generate multiple or parameterized source files, by means of macro
777 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
778 a tool for rebuilding the GNAT run time with user-supplied
779 configuration pragmas.
783 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
784 utility that displays information about compiled units, including dependences
785 on the corresponding sources files, and consistency of compilations.
788 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
789 to delete files that are produced by the compiler, binder and linker.
793 @ref{GNAT and Libraries}, describes the process of creating and using
794 Libraries with GNAT. It also describes how to recompile the GNAT run-time
798 @ref{Using the GNU make Utility}, describes some techniques for using
799 the GNAT toolset in Makefiles.
803 @ref{Memory Management Issues}, describes some useful predefined storage pools
804 and in particular the GNAT Debug Pool facility, which helps detect incorrect
807 It also describes @command{gnatmem}, a utility that monitors dynamic
808 allocation and deallocation and helps detect ``memory leaks''.
812 @ref{Stack Related Facilities}, describes some useful tools associated with
813 stack checking and analysis.
816 @ref{Verifying properties using gnatcheck}, discusses @code{gnatcheck},
817 a utility that checks Ada code against a set of rules.
820 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
821 a utility that generates empty but compilable bodies for library units.
824 @ref{Other Utility Programs}, discusses several other GNAT utilities,
825 including @code{gnathtml}.
828 @ref{Running and Debugging Ada Programs}, describes how to run and debug
833 @ref{Compatibility with HP Ada}, details the compatibility of GNAT with
834 HP Ada 83 @footnote{``HP Ada'' refers to the legacy product originally
835 developed by Digital Equipment Corporation and currently supported by HP.}
836 for OpenVMS Alpha. This product was formerly known as DEC Ada,
839 historical compatibility reasons, the relevant libraries still use the
844 @ref{Platform-Specific Information for the Run-Time Libraries},
845 describes the various run-time
846 libraries supported by GNAT on various platforms and explains how to
847 choose a particular library.
850 @ref{Example of Binder Output File}, shows the source code for the binder
851 output file for a sample program.
854 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
855 you deal with elaboration order issues.
858 @ref{Inline Assembler}, shows how to use the inline assembly facility
862 @ref{Compatibility and Porting Guide}, includes sections on compatibility
863 of GNAT with other Ada 83 and Ada 95 compilation systems, to assist
864 in porting code from other environments.
868 @ref{Microsoft Windows Topics}, presents information relevant to the
869 Microsoft Windows platform.
873 @c *************************************************
874 @node What You Should Know before Reading This Guide
875 @c *************************************************
876 @unnumberedsec What You Should Know before Reading This Guide
878 @cindex Ada 95 Language Reference Manual
880 This user's guide assumes that you are familiar with Ada 95 language, as
881 described in the International Standard ANSI/ISO/IEC-8652:1995, January
884 @node Related Information
885 @unnumberedsec Related Information
888 For further information about related tools, refer to the following
893 @cite{GNAT Reference Manual}, which contains all reference
894 material for the GNAT implementation of Ada 95.
898 @cite{Using the GNAT Programming System}, which describes the GPS
899 integrated development environment.
902 @cite{GNAT Programming System Tutorial}, which introduces the
903 main GPS features through examples.
907 @cite{Ada 95 Language Reference Manual}, which contains all reference
908 material for the Ada 95 programming language.
911 @cite{Debugging with GDB}
913 , located in the GNU:[DOCS] directory,
915 contains all details on the use of the GNU source-level debugger.
918 @cite{GNU Emacs Manual}
920 , located in the GNU:[DOCS] directory if the EMACS kit is installed,
922 contains full information on the extensible editor and programming
929 @unnumberedsec Conventions
931 @cindex Typographical conventions
934 Following are examples of the typographical and graphic conventions used
939 @code{Functions}, @code{utility program names}, @code{standard names},
946 @file{File Names}, @file{button names}, and @file{field names}.
955 [optional information or parameters]
958 Examples are described by text
960 and then shown this way.
965 Commands that are entered by the user are preceded in this manual by the
966 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
967 uses this sequence as a prompt, then the commands will appear exactly as
968 you see them in the manual. If your system uses some other prompt, then
969 the command will appear with the @code{$} replaced by whatever prompt
970 character you are using.
973 Full file names are shown with the ``@code{/}'' character
974 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
975 If you are using GNAT on a Windows platform, please note that
976 the ``@code{\}'' character should be used instead.
979 @c ****************************
980 @node Getting Started with GNAT
981 @chapter Getting Started with GNAT
984 This chapter describes some simple ways of using GNAT to build
985 executable Ada programs.
987 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
988 show how to use the command line environment.
989 @ref{Introduction to Glide and GVD}, provides a brief
990 introduction to the visually-oriented IDE for GNAT.
991 Supplementing Glide on some platforms is GPS, the
992 GNAT Programming System, which offers a richer graphical
993 ``look and feel'', enhanced configurability, support for
994 development in other programming language, comprehensive
995 browsing features, and many other capabilities.
996 For information on GPS please refer to
997 @cite{Using the GNAT Programming System}.
1002 * Running a Simple Ada Program::
1003 * Running a Program with Multiple Units::
1004 * Using the gnatmake Utility::
1006 * Editing with Emacs::
1009 * Introduction to GPS::
1010 * Introduction to Glide and GVD::
1015 @section Running GNAT
1018 Three steps are needed to create an executable file from an Ada source
1023 The source file(s) must be compiled.
1025 The file(s) must be bound using the GNAT binder.
1027 All appropriate object files must be linked to produce an executable.
1031 All three steps are most commonly handled by using the @command{gnatmake}
1032 utility program that, given the name of the main program, automatically
1033 performs the necessary compilation, binding and linking steps.
1035 @node Running a Simple Ada Program
1036 @section Running a Simple Ada Program
1039 Any text editor may be used to prepare an Ada program.
1042 used, the optional Ada mode may be helpful in laying out the program.
1045 program text is a normal text file. We will suppose in our initial
1046 example that you have used your editor to prepare the following
1047 standard format text file:
1049 @smallexample @c ada
1051 with Ada.Text_IO; use Ada.Text_IO;
1054 Put_Line ("Hello WORLD!");
1060 This file should be named @file{hello.adb}.
1061 With the normal default file naming conventions, GNAT requires
1063 contain a single compilation unit whose file name is the
1065 with periods replaced by hyphens; the
1066 extension is @file{ads} for a
1067 spec and @file{adb} for a body.
1068 You can override this default file naming convention by use of the
1069 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1070 Alternatively, if you want to rename your files according to this default
1071 convention, which is probably more convenient if you will be using GNAT
1072 for all your compilations, then the @code{gnatchop} utility
1073 can be used to generate correctly-named source files
1074 (@pxref{Renaming Files Using gnatchop}).
1076 You can compile the program using the following command (@code{$} is used
1077 as the command prompt in the examples in this document):
1084 @command{gcc} is the command used to run the compiler. This compiler is
1085 capable of compiling programs in several languages, including Ada 95 and
1086 C. It assumes that you have given it an Ada program if the file extension is
1087 either @file{.ads} or @file{.adb}, and it will then call
1088 the GNAT compiler to compile the specified file.
1091 The @option{-c} switch is required. It tells @command{gcc} to only do a
1092 compilation. (For C programs, @command{gcc} can also do linking, but this
1093 capability is not used directly for Ada programs, so the @option{-c}
1094 switch must always be present.)
1097 This compile command generates a file
1098 @file{hello.o}, which is the object
1099 file corresponding to your Ada program. It also generates
1100 an ``Ada Library Information'' file @file{hello.ali},
1101 which contains additional information used to check
1102 that an Ada program is consistent.
1103 To build an executable file,
1104 use @code{gnatbind} to bind the program
1105 and @command{gnatlink} to link it. The
1106 argument to both @code{gnatbind} and @command{gnatlink} is the name of the
1107 @file{ALI} file, but the default extension of @file{.ali} can
1108 be omitted. This means that in the most common case, the argument
1109 is simply the name of the main program:
1117 A simpler method of carrying out these steps is to use
1119 a master program that invokes all the required
1120 compilation, binding and linking tools in the correct order. In particular,
1121 @command{gnatmake} automatically recompiles any sources that have been
1122 modified since they were last compiled, or sources that depend
1123 on such modified sources, so that ``version skew'' is avoided.
1124 @cindex Version skew (avoided by @command{gnatmake})
1127 $ gnatmake hello.adb
1131 The result is an executable program called @file{hello}, which can be
1139 assuming that the current directory is on the search path
1140 for executable programs.
1143 and, if all has gone well, you will see
1150 appear in response to this command.
1152 @c ****************************************
1153 @node Running a Program with Multiple Units
1154 @section Running a Program with Multiple Units
1157 Consider a slightly more complicated example that has three files: a
1158 main program, and the spec and body of a package:
1160 @smallexample @c ada
1163 package Greetings is
1168 with Ada.Text_IO; use Ada.Text_IO;
1169 package body Greetings is
1172 Put_Line ("Hello WORLD!");
1175 procedure Goodbye is
1177 Put_Line ("Goodbye WORLD!");
1194 Following the one-unit-per-file rule, place this program in the
1195 following three separate files:
1199 spec of package @code{Greetings}
1202 body of package @code{Greetings}
1205 body of main program
1209 To build an executable version of
1210 this program, we could use four separate steps to compile, bind, and link
1211 the program, as follows:
1215 $ gcc -c greetings.adb
1221 Note that there is no required order of compilation when using GNAT.
1222 In particular it is perfectly fine to compile the main program first.
1223 Also, it is not necessary to compile package specs in the case where
1224 there is an accompanying body; you only need to compile the body. If you want
1225 to submit these files to the compiler for semantic checking and not code
1226 generation, then use the
1227 @option{-gnatc} switch:
1230 $ gcc -c greetings.ads -gnatc
1234 Although the compilation can be done in separate steps as in the
1235 above example, in practice it is almost always more convenient
1236 to use the @command{gnatmake} tool. All you need to know in this case
1237 is the name of the main program's source file. The effect of the above four
1238 commands can be achieved with a single one:
1241 $ gnatmake gmain.adb
1245 In the next section we discuss the advantages of using @command{gnatmake} in
1248 @c *****************************
1249 @node Using the gnatmake Utility
1250 @section Using the @command{gnatmake} Utility
1253 If you work on a program by compiling single components at a time using
1254 @command{gcc}, you typically keep track of the units you modify. In order to
1255 build a consistent system, you compile not only these units, but also any
1256 units that depend on the units you have modified.
1257 For example, in the preceding case,
1258 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1259 you edit @file{greetings.ads}, you must recompile both
1260 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1261 units that depend on @file{greetings.ads}.
1263 @code{gnatbind} will warn you if you forget one of these compilation
1264 steps, so that it is impossible to generate an inconsistent program as a
1265 result of forgetting to do a compilation. Nevertheless it is tedious and
1266 error-prone to keep track of dependencies among units.
1267 One approach to handle the dependency-bookkeeping is to use a
1268 makefile. However, makefiles present maintenance problems of their own:
1269 if the dependencies change as you change the program, you must make
1270 sure that the makefile is kept up-to-date manually, which is also an
1271 error-prone process.
1273 The @command{gnatmake} utility takes care of these details automatically.
1274 Invoke it using either one of the following forms:
1277 $ gnatmake gmain.adb
1278 $ gnatmake ^gmain^GMAIN^
1282 The argument is the name of the file containing the main program;
1283 you may omit the extension. @command{gnatmake}
1284 examines the environment, automatically recompiles any files that need
1285 recompiling, and binds and links the resulting set of object files,
1286 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1287 In a large program, it
1288 can be extremely helpful to use @command{gnatmake}, because working out by hand
1289 what needs to be recompiled can be difficult.
1291 Note that @command{gnatmake}
1292 takes into account all the Ada 95 rules that
1293 establish dependencies among units. These include dependencies that result
1294 from inlining subprogram bodies, and from
1295 generic instantiation. Unlike some other
1296 Ada make tools, @command{gnatmake} does not rely on the dependencies that were
1297 found by the compiler on a previous compilation, which may possibly
1298 be wrong when sources change. @command{gnatmake} determines the exact set of
1299 dependencies from scratch each time it is run.
1302 @node Editing with Emacs
1303 @section Editing with Emacs
1307 Emacs is an extensible self-documenting text editor that is available in a
1308 separate VMSINSTAL kit.
1310 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1311 click on the Emacs Help menu and run the Emacs Tutorial.
1312 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1313 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1315 Documentation on Emacs and other tools is available in Emacs under the
1316 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1317 use the middle mouse button to select a topic (e.g. Emacs).
1319 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1320 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1321 get to the Emacs manual.
1322 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1325 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1326 which is sufficiently extensible to provide for a complete programming
1327 environment and shell for the sophisticated user.
1331 @node Introduction to GPS
1332 @section Introduction to GPS
1333 @cindex GPS (GNAT Programming System)
1334 @cindex GNAT Programming System (GPS)
1336 Although the command line interface (@command{gnatmake}, etc.) alone
1337 is sufficient, a graphical Interactive Development
1338 Environment can make it easier for you to compose, navigate, and debug
1339 programs. This section describes the main features of GPS
1340 (``GNAT Programming System''), the GNAT graphical IDE.
1341 You will see how to use GPS to build and debug an executable, and
1342 you will also learn some of the basics of the GNAT ``project'' facility.
1344 GPS enables you to do much more than is presented here;
1345 e.g., you can produce a call graph, interface to a third-party
1346 Version Control System, and inspect the generated assembly language
1348 Indeed, GPS also supports languages other than Ada.
1349 Such additional information, and an explanation of all of the GPS menu
1350 items. may be found in the on-line help, which includes
1351 a user's guide and a tutorial (these are also accessible from the GNAT
1355 * Building a New Program with GPS::
1356 * Simple Debugging with GPS::
1359 @node Building a New Program with GPS
1360 @subsection Building a New Program with GPS
1362 GPS invokes the GNAT compilation tools using information
1363 contained in a @emph{project} (also known as a @emph{project file}):
1364 a collection of properties such
1365 as source directories, identities of main subprograms, tool switches, etc.,
1366 and their associated values.
1367 See @ref{GNAT Project Manager} for details.
1368 In order to run GPS, you will need to either create a new project
1369 or else open an existing one.
1371 This section will explain how you can use GPS to create a project,
1372 to associate Ada source files with a project, and to build and run
1376 @item @emph{Creating a project}
1378 Invoke GPS, either from the command line or the platform's IDE.
1379 After it starts, GPS will display a ``Welcome'' screen with three
1384 @code{Start with default project in directory}
1387 @code{Create new project with wizard}
1390 @code{Open existing project}
1394 Select @code{Create new project with wizard} and press @code{OK}.
1395 A new window will appear. In the text box labeled with
1396 @code{Enter the name of the project to create}, type @file{sample}
1397 as the project name.
1398 In the next box, browse to choose the directory in which you
1399 would like to create the project file.
1400 After selecting an appropriate directory, press @code{Forward}.
1402 A window will appear with the title
1403 @code{Version Control System Configuration}.
1404 Simply press @code{Forward}.
1406 A window will appear with the title
1407 @code{Please select the source directories for this project}.
1408 The directory that you specified for the project file will be selected
1409 by default as the one to use for sources; simply press @code{Forward}.
1411 A window will appear with the title
1412 @code{Please select the build directory for this project}.
1413 The directory that you specified for the project file will be selected
1414 by default for object files and executables;
1415 simply press @code{Forward}.
1417 A window will appear with the title
1418 @code{Please select the main units for this project}.
1419 You will supply this information later, after creating the source file.
1420 Simply press @code{Forward} for now.
1422 A window will appear with the title
1423 @code{Please select the switches to build the project}.
1424 Press @code{Apply}. This will create a project file named
1425 @file{sample.prj} in the directory that you had specified.
1427 @item @emph{Creating and saving the source file}
1429 After you create the new project, a GPS window will appear, which is
1430 partitioned into two main sections:
1434 A @emph{Workspace area}, initially greyed out, which you will use for
1435 creating and editing source files
1438 Directly below, a @emph{Messages area}, which initially displays a
1439 ``Welcome'' message.
1440 (If the Messages area is not visible, drag its border upward to expand it.)
1444 Select @code{File} on the menu bar, and then the @code{New} command.
1445 The Workspace area will become white, and you can now
1446 enter the source program explicitly.
1447 Type the following text
1449 @smallexample @c ada
1451 with Ada.Text_IO; use Ada.Text_IO;
1454 Put_Line("Hello from GPS!");
1460 Select @code{File}, then @code{Save As}, and enter the source file name
1462 The file will be saved in the same directory you specified as the
1463 location of the default project file.
1465 @item @emph{Updating the project file}
1467 You need to add the new source file to the project.
1469 the @code{Project} menu and then @code{Edit project properties}.
1470 Click the @code{Main files} tab on the left, and then the
1472 Choose @file{hello.adb} from the list, and press @code{Open}.
1473 The project settings window will reflect this action.
1476 @item @emph{Building and running the program}
1478 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1479 and select @file{hello.adb}.
1480 The Messages window will display the resulting invocations of @command{gcc},
1481 @command{gnatbind}, and @command{gnatlink}
1482 (reflecting the default switch settings from the
1483 project file that you created) and then a ``successful compilation/build''
1486 To run the program, choose the @code{Build} menu, then @code{Run}, and
1487 select @command{hello}.
1488 An @emph{Arguments Selection} window will appear.
1489 There are no command line arguments, so just click @code{OK}.
1491 The Messages window will now display the program's output (the string
1492 @code{Hello from GPS}), and at the bottom of the GPS window a status
1493 update is displayed (@code{Run: hello}).
1494 Close the GPS window (or select @code{File}, then @code{Exit}) to
1495 terminate this GPS session.
1498 @node Simple Debugging with GPS
1499 @subsection Simple Debugging with GPS
1501 This section illustrates basic debugging techniques (setting breakpoints,
1502 examining/modifying variables, single stepping).
1505 @item @emph{Opening a project}
1507 Start GPS and select @code{Open existing project}; browse to
1508 specify the project file @file{sample.prj} that you had created in the
1511 @item @emph{Creating a source file}
1513 Select @code{File}, then @code{New}, and type in the following program:
1515 @smallexample @c ada
1517 with Ada.Text_IO; use Ada.Text_IO;
1518 procedure Example is
1519 Line : String (1..80);
1522 Put_Line("Type a line of text at each prompt; an empty line to exit");
1526 Put_Line (Line (1..N) );
1534 Select @code{File}, then @code{Save as}, and enter the file name
1537 @item @emph{Updating the project file}
1539 Add @code{Example} as a new main unit for the project:
1542 Select @code{Project}, then @code{Edit Project Properties}.
1545 Select the @code{Main files} tab, click @code{Add}, then
1546 select the file @file{example.adb} from the list, and
1548 You will see the file name appear in the list of main units
1554 @item @emph{Building/running the executable}
1556 To build the executable
1557 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1559 Run the program to see its effect (in the Messages area).
1560 Each line that you enter is displayed; an empty line will
1561 cause the loop to exit and the program to terminate.
1563 @item @emph{Debugging the program}
1565 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1566 which are required for debugging, are on by default when you create
1568 Thus unless you intentionally remove these settings, you will be able
1569 to debug any program that you develop using GPS.
1572 @item @emph{Initializing}
1574 Select @code{Debug}, then @code{Initialize}, then @file{example}
1576 @item @emph{Setting a breakpoint}
1578 After performing the initialization step, you will observe a small
1579 icon to the right of each line number.
1580 This serves as a toggle for breakpoints; clicking the icon will
1581 set a breakpoint at the corresponding line (the icon will change to
1582 a red circle with an ``x''), and clicking it again
1583 will remove the breakpoint / reset the icon.
1585 For purposes of this example, set a breakpoint at line 10 (the
1586 statement @code{Put_Line@ (Line@ (1..N));}
1588 @item @emph{Starting program execution}
1590 Select @code{Debug}, then @code{Run}. When the
1591 @code{Program Arguments} window appears, click @code{OK}.
1592 A console window will appear; enter some line of text,
1593 e.g. @code{abcde}, at the prompt.
1594 The program will pause execution when it gets to the
1595 breakpoint, and the corresponding line is highlighted.
1597 @item @emph{Examining a variable}
1599 Move the mouse over one of the occurrences of the variable @code{N}.
1600 You will see the value (5) displayed, in ``tool tip'' fashion.
1601 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1602 You will see information about @code{N} appear in the @code{Debugger Data}
1603 pane, showing the value as 5.
1605 @item @emph{Assigning a new value to a variable}
1607 Right click on the @code{N} in the @code{Debugger Data} pane, and
1608 select @code{Set value of N}.
1609 When the input window appears, enter the value @code{4} and click
1611 This value does not automatically appear in the @code{Debugger Data}
1612 pane; to see it, right click again on the @code{N} in the
1613 @code{Debugger Data} pane and select @code{Update value}.
1614 The new value, 4, will appear in red.
1616 @item @emph{Single stepping}
1618 Select @code{Debug}, then @code{Next}.
1619 This will cause the next statement to be executed, in this case the
1620 call of @code{Put_Line} with the string slice.
1621 Notice in the console window that the displayed string is simply
1622 @code{abcd} and not @code{abcde} which you had entered.
1623 This is because the upper bound of the slice is now 4 rather than 5.
1625 @item @emph{Removing a breakpoint}
1627 Toggle the breakpoint icon at line 10.
1629 @item @emph{Resuming execution from a breakpoint}
1631 Select @code{Debug}, then @code{Continue}.
1632 The program will reach the next iteration of the loop, and
1633 wait for input after displaying the prompt.
1634 This time, just hit the @kbd{Enter} key.
1635 The value of @code{N} will be 0, and the program will terminate.
1636 The console window will disappear.
1640 @node Introduction to Glide and GVD
1641 @section Introduction to Glide and GVD
1645 This section describes the main features of Glide,
1646 a GNAT graphical IDE, and also shows how to use the basic commands in GVD,
1647 the GNU Visual Debugger.
1648 These tools may be present in addition to, or in place of, GPS on some
1650 Additional information on Glide and GVD may be found
1651 in the on-line help for these tools.
1654 * Building a New Program with Glide::
1655 * Simple Debugging with GVD::
1656 * Other Glide Features::
1659 @node Building a New Program with Glide
1660 @subsection Building a New Program with Glide
1662 The simplest way to invoke Glide is to enter @command{glide}
1663 at the command prompt. It will generally be useful to issue this
1664 as a background command, thus allowing you to continue using
1665 your command window for other purposes while Glide is running:
1672 Glide will start up with an initial screen displaying the top-level menu items
1673 as well as some other information. The menu selections are as follows
1675 @item @code{Buffers}
1686 For this introductory example, you will need to create a new Ada source file.
1687 First, select the @code{Files} menu. This will pop open a menu with around
1688 a dozen or so items. To create a file, select the @code{Open file...} choice.
1689 Depending on the platform, you may see a pop-up window where you can browse
1690 to an appropriate directory and then enter the file name, or else simply
1691 see a line at the bottom of the Glide window where you can likewise enter
1692 the file name. Note that in Glide, when you attempt to open a non-existent
1693 file, the effect is to create a file with that name. For this example enter
1694 @file{hello.adb} as the name of the file.
1696 A new buffer will now appear, occupying the entire Glide window,
1697 with the file name at the top. The menu selections are slightly different
1698 from the ones you saw on the opening screen; there is an @code{Entities} item,
1699 and in place of @code{Glide} there is now an @code{Ada} item. Glide uses
1700 the file extension to identify the source language, so @file{adb} indicates
1703 You will enter some of the source program lines explicitly,
1704 and use the syntax-oriented template mechanism to enter other lines.
1705 First, type the following text:
1707 with Ada.Text_IO; use Ada.Text_IO;
1713 Observe that Glide uses different colors to distinguish reserved words from
1714 identifiers. Also, after the @code{procedure Hello is} line, the cursor is
1715 automatically indented in anticipation of declarations. When you enter
1716 @code{begin}, Glide recognizes that there are no declarations and thus places
1717 @code{begin} flush left. But after the @code{begin} line the cursor is again
1718 indented, where the statement(s) will be placed.
1720 The main part of the program will be a @code{for} loop. Instead of entering
1721 the text explicitly, however, use a statement template. Select the @code{Ada}
1722 item on the top menu bar, move the mouse to the @code{Statements} item,
1723 and you will see a large selection of alternatives. Choose @code{for loop}.
1724 You will be prompted (at the bottom of the buffer) for a loop name;
1725 simply press the @key{Enter} key since a loop name is not needed.
1726 You should see the beginning of a @code{for} loop appear in the source
1727 program window. You will now be prompted for the name of the loop variable;
1728 enter a line with the identifier @code{ind} (lower case). Note that,
1729 by default, Glide capitalizes the name (you can override such behavior
1730 if you wish, although this is outside the scope of this introduction).
1731 Next, Glide prompts you for the loop range; enter a line containing
1732 @code{1..5} and you will see this also appear in the source program,
1733 together with the remaining elements of the @code{for} loop syntax.
1735 Next enter the statement (with an intentional error, a missing semicolon)
1736 that will form the body of the loop:
1738 Put_Line("Hello, World" & Integer'Image(I))
1742 Finally, type @code{end Hello;} as the last line in the program.
1743 Now save the file: choose the @code{File} menu item, and then the
1744 @code{Save buffer} selection. You will see a message at the bottom
1745 of the buffer confirming that the file has been saved.
1747 You are now ready to attempt to build the program. Select the @code{Ada}
1748 item from the top menu bar. Although we could choose simply to compile
1749 the file, we will instead attempt to do a build (which invokes
1750 @command{gnatmake}) since, if the compile is successful, we want to build
1751 an executable. Thus select @code{Ada build}. This will fail because of the
1752 compilation error, and you will notice that the Glide window has been split:
1753 the top window contains the source file, and the bottom window contains the
1754 output from the GNAT tools. Glide allows you to navigate from a compilation
1755 error to the source file position corresponding to the error: click the
1756 middle mouse button (or simultaneously press the left and right buttons,
1757 on a two-button mouse) on the diagnostic line in the tool window. The
1758 focus will shift to the source window, and the cursor will be positioned
1759 on the character at which the error was detected.
1761 Correct the error: type in a semicolon to terminate the statement.
1762 Although you can again save the file explicitly, you can also simply invoke
1763 @code{Ada} @result{} @code{Build} and you will be prompted to save the file.
1764 This time the build will succeed; the tool output window shows you the
1765 options that are supplied by default. The GNAT tools' output (e.g.
1766 object and ALI files, executable) will go in the directory from which
1769 To execute the program, choose @code{Ada} and then @code{Run}.
1770 You should see the program's output displayed in the bottom window:
1780 @node Simple Debugging with GVD
1781 @subsection Simple Debugging with GVD
1784 This section describes how to set breakpoints, examine/modify variables,
1785 and step through execution.
1787 In order to enable debugging, you need to pass the @option{-g} switch
1788 to both the compiler and to @command{gnatlink}. If you are using
1789 the command line, passing @option{-g} to @command{gnatmake} will have
1790 this effect. You can then launch GVD, e.g. on the @code{hello} program,
1791 by issuing the command:
1798 If you are using Glide, then @option{-g} is passed to the relevant tools
1799 by default when you do a build. Start the debugger by selecting the
1800 @code{Ada} menu item, and then @code{Debug}.
1802 GVD comes up in a multi-part window. One pane shows the names of files
1803 comprising your executable; another pane shows the source code of the current
1804 unit (initially your main subprogram), another pane shows the debugger output
1805 and user interactions, and the fourth pane (the data canvas at the top
1806 of the window) displays data objects that you have selected.
1808 To the left of the source file pane, you will notice green dots adjacent
1809 to some lines. These are lines for which object code exists and where
1810 breakpoints can thus be set. You set/reset a breakpoint by clicking
1811 the green dot. When a breakpoint is set, the dot is replaced by an @code{X}
1812 in a red circle. Clicking the circle toggles the breakpoint off,
1813 and the red circle is replaced by the green dot.
1815 For this example, set a breakpoint at the statement where @code{Put_Line}
1818 Start program execution by selecting the @code{Run} button on the top menu bar.
1819 (The @code{Start} button will also start your program, but it will
1820 cause program execution to break at the entry to your main subprogram.)
1821 Evidence of reaching the breakpoint will appear: the source file line will be
1822 highlighted, and the debugger interactions pane will display
1825 You can examine the values of variables in several ways. Move the mouse
1826 over an occurrence of @code{Ind} in the @code{for} loop, and you will see
1827 the value (now @code{1}) displayed. Alternatively, right-click on @code{Ind}
1828 and select @code{Display Ind}; a box showing the variable's name and value
1829 will appear in the data canvas.
1831 Although a loop index is a constant with respect to Ada semantics,
1832 you can change its value in the debugger. Right-click in the box
1833 for @code{Ind}, and select the @code{Set Value of Ind} item.
1834 Enter @code{2} as the new value, and press @command{OK}.
1835 The box for @code{Ind} shows the update.
1837 Press the @code{Step} button on the top menu bar; this will step through
1838 one line of program text (the invocation of @code{Put_Line}), and you can
1839 observe the effect of having modified @code{Ind} since the value displayed
1842 Remove the breakpoint, and resume execution by selecting the @code{Cont}
1843 button. You will see the remaining output lines displayed in the debugger
1844 interaction window, along with a message confirming normal program
1847 @node Other Glide Features
1848 @subsection Other Glide Features
1851 You may have observed that some of the menu selections contain abbreviations;
1852 e.g., @code{(C-x C-f)} for @code{Open file...} in the @code{Files} menu.
1853 These are @emph{shortcut keys} that you can use instead of selecting
1854 menu items. The @key{C} stands for @key{Ctrl}; thus @code{(C-x C-f)} means
1855 @key{Ctrl-x} followed by @key{Ctrl-f}, and this sequence can be used instead
1856 of selecting @code{Files} and then @code{Open file...}.
1858 To abort a Glide command, type @key{Ctrl-g}.
1860 If you want Glide to start with an existing source file, you can either
1861 launch Glide as above and then open the file via @code{Files} @result{}
1862 @code{Open file...}, or else simply pass the name of the source file
1863 on the command line:
1870 While you are using Glide, a number of @emph{buffers} exist.
1871 You create some explicitly; e.g., when you open/create a file.
1872 Others arise as an effect of the commands that you issue; e.g., the buffer
1873 containing the output of the tools invoked during a build. If a buffer
1874 is hidden, you can bring it into a visible window by first opening
1875 the @code{Buffers} menu and then selecting the desired entry.
1877 If a buffer occupies only part of the Glide screen and you want to expand it
1878 to fill the entire screen, then click in the buffer and then select
1879 @code{Files} @result{} @code{One Window}.
1881 If a window is occupied by one buffer and you want to split the window
1882 to bring up a second buffer, perform the following steps:
1884 @item Select @code{Files} @result{} @code{Split Window};
1885 this will produce two windows each of which holds the original buffer
1886 (these are not copies, but rather different views of the same buffer contents)
1888 @item With the focus in one of the windows,
1889 select the desired buffer from the @code{Buffers} menu
1893 To exit from Glide, choose @code{Files} @result{} @code{Exit}.
1896 @node The GNAT Compilation Model
1897 @chapter The GNAT Compilation Model
1898 @cindex GNAT compilation model
1899 @cindex Compilation model
1902 * Source Representation::
1903 * Foreign Language Representation::
1904 * File Naming Rules::
1905 * Using Other File Names::
1906 * Alternative File Naming Schemes::
1907 * Generating Object Files::
1908 * Source Dependencies::
1909 * The Ada Library Information Files::
1910 * Binding an Ada Program::
1911 * Mixed Language Programming::
1913 * Building Mixed Ada & C++ Programs::
1914 * Comparison between GNAT and C/C++ Compilation Models::
1916 * Comparison between GNAT and Conventional Ada Library Models::
1918 * Placement of temporary files::
1923 This chapter describes the compilation model used by GNAT. Although
1924 similar to that used by other languages, such as C and C++, this model
1925 is substantially different from the traditional Ada compilation models,
1926 which are based on a library. The model is initially described without
1927 reference to the library-based model. If you have not previously used an
1928 Ada compiler, you need only read the first part of this chapter. The
1929 last section describes and discusses the differences between the GNAT
1930 model and the traditional Ada compiler models. If you have used other
1931 Ada compilers, this section will help you to understand those
1932 differences, and the advantages of the GNAT model.
1934 @node Source Representation
1935 @section Source Representation
1939 Ada source programs are represented in standard text files, using
1940 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1941 7-bit ASCII set, plus additional characters used for
1942 representing foreign languages (@pxref{Foreign Language Representation}
1943 for support of non-USA character sets). The format effector characters
1944 are represented using their standard ASCII encodings, as follows:
1949 Vertical tab, @code{16#0B#}
1953 Horizontal tab, @code{16#09#}
1957 Carriage return, @code{16#0D#}
1961 Line feed, @code{16#0A#}
1965 Form feed, @code{16#0C#}
1969 Source files are in standard text file format. In addition, GNAT will
1970 recognize a wide variety of stream formats, in which the end of
1971 physical lines is marked by any of the following sequences:
1972 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1973 in accommodating files that are imported from other operating systems.
1975 @cindex End of source file
1976 @cindex Source file, end
1978 The end of a source file is normally represented by the physical end of
1979 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1980 recognized as signalling the end of the source file. Again, this is
1981 provided for compatibility with other operating systems where this
1982 code is used to represent the end of file.
1984 Each file contains a single Ada compilation unit, including any pragmas
1985 associated with the unit. For example, this means you must place a
1986 package declaration (a package @dfn{spec}) and the corresponding body in
1987 separate files. An Ada @dfn{compilation} (which is a sequence of
1988 compilation units) is represented using a sequence of files. Similarly,
1989 you will place each subunit or child unit in a separate file.
1991 @node Foreign Language Representation
1992 @section Foreign Language Representation
1995 GNAT supports the standard character sets defined in Ada 95 as well as
1996 several other non-standard character sets for use in localized versions
1997 of the compiler (@pxref{Character Set Control}).
2000 * Other 8-Bit Codes::
2001 * Wide Character Encodings::
2009 The basic character set is Latin-1. This character set is defined by ISO
2010 standard 8859, part 1. The lower half (character codes @code{16#00#}
2011 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper half
2012 is used to represent additional characters. These include extended letters
2013 used by European languages, such as French accents, the vowels with umlauts
2014 used in German, and the extra letter A-ring used in Swedish.
2016 @findex Ada.Characters.Latin_1
2017 For a complete list of Latin-1 codes and their encodings, see the source
2018 file of library unit @code{Ada.Characters.Latin_1} in file
2019 @file{a-chlat1.ads}.
2020 You may use any of these extended characters freely in character or
2021 string literals. In addition, the extended characters that represent
2022 letters can be used in identifiers.
2024 @node Other 8-Bit Codes
2025 @subsection Other 8-Bit Codes
2028 GNAT also supports several other 8-bit coding schemes:
2031 @item ISO 8859-2 (Latin-2)
2034 Latin-2 letters allowed in identifiers, with uppercase and lowercase
2037 @item ISO 8859-3 (Latin-3)
2040 Latin-3 letters allowed in identifiers, with uppercase and lowercase
2043 @item ISO 8859-4 (Latin-4)
2046 Latin-4 letters allowed in identifiers, with uppercase and lowercase
2049 @item ISO 8859-5 (Cyrillic)
2052 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
2053 lowercase equivalence.
2055 @item ISO 8859-15 (Latin-9)
2058 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
2059 lowercase equivalence
2061 @item IBM PC (code page 437)
2062 @cindex code page 437
2063 This code page is the normal default for PCs in the U.S. It corresponds
2064 to the original IBM PC character set. This set has some, but not all, of
2065 the extended Latin-1 letters, but these letters do not have the same
2066 encoding as Latin-1. In this mode, these letters are allowed in
2067 identifiers with uppercase and lowercase equivalence.
2069 @item IBM PC (code page 850)
2070 @cindex code page 850
2071 This code page is a modification of 437 extended to include all the
2072 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
2073 mode, all these letters are allowed in identifiers with uppercase and
2074 lowercase equivalence.
2076 @item Full Upper 8-bit
2077 Any character in the range 80-FF allowed in identifiers, and all are
2078 considered distinct. In other words, there are no uppercase and lowercase
2079 equivalences in this range. This is useful in conjunction with
2080 certain encoding schemes used for some foreign character sets (e.g.
2081 the typical method of representing Chinese characters on the PC).
2084 No upper-half characters in the range 80-FF are allowed in identifiers.
2085 This gives Ada 83 compatibility for identifier names.
2089 For precise data on the encodings permitted, and the uppercase and lowercase
2090 equivalences that are recognized, see the file @file{csets.adb} in
2091 the GNAT compiler sources. You will need to obtain a full source release
2092 of GNAT to obtain this file.
2094 @node Wide Character Encodings
2095 @subsection Wide Character Encodings
2098 GNAT allows wide character codes to appear in character and string
2099 literals, and also optionally in identifiers, by means of the following
2100 possible encoding schemes:
2105 In this encoding, a wide character is represented by the following five
2113 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2114 characters (using uppercase letters) of the wide character code. For
2115 example, ESC A345 is used to represent the wide character with code
2117 This scheme is compatible with use of the full Wide_Character set.
2119 @item Upper-Half Coding
2120 @cindex Upper-Half Coding
2121 The wide character with encoding @code{16#abcd#} where the upper bit is on
2122 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
2123 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
2124 character, but is not required to be in the upper half. This method can
2125 be also used for shift-JIS or EUC, where the internal coding matches the
2128 @item Shift JIS Coding
2129 @cindex Shift JIS Coding
2130 A wide character is represented by a two-character sequence,
2132 @code{16#cd#}, with the restrictions described for upper-half encoding as
2133 described above. The internal character code is the corresponding JIS
2134 character according to the standard algorithm for Shift-JIS
2135 conversion. Only characters defined in the JIS code set table can be
2136 used with this encoding method.
2140 A wide character is represented by a two-character sequence
2142 @code{16#cd#}, with both characters being in the upper half. The internal
2143 character code is the corresponding JIS character according to the EUC
2144 encoding algorithm. Only characters defined in the JIS code set table
2145 can be used with this encoding method.
2148 A wide character is represented using
2149 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
2150 10646-1/Am.2. Depending on the character value, the representation
2151 is a one, two, or three byte sequence:
2156 16#0000#-16#007f#: 2#0xxxxxxx#
2157 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
2158 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
2163 where the xxx bits correspond to the left-padded bits of the
2164 16-bit character value. Note that all lower half ASCII characters
2165 are represented as ASCII bytes and all upper half characters and
2166 other wide characters are represented as sequences of upper-half
2167 (The full UTF-8 scheme allows for encoding 31-bit characters as
2168 6-byte sequences, but in this implementation, all UTF-8 sequences
2169 of four or more bytes length will be treated as illegal).
2170 @item Brackets Coding
2171 In this encoding, a wide character is represented by the following eight
2179 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2180 characters (using uppercase letters) of the wide character code. For
2181 example, [``A345''] is used to represent the wide character with code
2182 @code{16#A345#}. It is also possible (though not required) to use the
2183 Brackets coding for upper half characters. For example, the code
2184 @code{16#A3#} can be represented as @code{[``A3'']}.
2186 This scheme is compatible with use of the full Wide_Character set,
2187 and is also the method used for wide character encoding in the standard
2188 ACVC (Ada Compiler Validation Capability) test suite distributions.
2193 Note: Some of these coding schemes do not permit the full use of the
2194 Ada 95 character set. For example, neither Shift JIS, nor EUC allow the
2195 use of the upper half of the Latin-1 set.
2197 @node File Naming Rules
2198 @section File Naming Rules
2201 The default file name is determined by the name of the unit that the
2202 file contains. The name is formed by taking the full expanded name of
2203 the unit and replacing the separating dots with hyphens and using
2204 ^lowercase^uppercase^ for all letters.
2206 An exception arises if the file name generated by the above rules starts
2207 with one of the characters
2214 and the second character is a
2215 minus. In this case, the character ^tilde^dollar sign^ is used in place
2216 of the minus. The reason for this special rule is to avoid clashes with
2217 the standard names for child units of the packages System, Ada,
2218 Interfaces, and GNAT, which use the prefixes
2227 The file extension is @file{.ads} for a spec and
2228 @file{.adb} for a body. The following list shows some
2229 examples of these rules.
2236 @item arith_functions.ads
2237 Arith_Functions (package spec)
2238 @item arith_functions.adb
2239 Arith_Functions (package body)
2241 Func.Spec (child package spec)
2243 Func.Spec (child package body)
2245 Sub (subunit of Main)
2246 @item ^a~bad.adb^A$BAD.ADB^
2247 A.Bad (child package body)
2251 Following these rules can result in excessively long
2252 file names if corresponding
2253 unit names are long (for example, if child units or subunits are
2254 heavily nested). An option is available to shorten such long file names
2255 (called file name ``krunching''). This may be particularly useful when
2256 programs being developed with GNAT are to be used on operating systems
2257 with limited file name lengths. @xref{Using gnatkr}.
2259 Of course, no file shortening algorithm can guarantee uniqueness over
2260 all possible unit names; if file name krunching is used, it is your
2261 responsibility to ensure no name clashes occur. Alternatively you
2262 can specify the exact file names that you want used, as described
2263 in the next section. Finally, if your Ada programs are migrating from a
2264 compiler with a different naming convention, you can use the gnatchop
2265 utility to produce source files that follow the GNAT naming conventions.
2266 (For details @pxref{Renaming Files Using gnatchop}.)
2268 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2269 systems, case is not significant. So for example on @code{Windows XP}
2270 if the canonical name is @code{main-sub.adb}, you can use the file name
2271 @code{Main-Sub.adb} instead. However, case is significant for other
2272 operating systems, so for example, if you want to use other than
2273 canonically cased file names on a Unix system, you need to follow
2274 the procedures described in the next section.
2276 @node Using Other File Names
2277 @section Using Other File Names
2281 In the previous section, we have described the default rules used by
2282 GNAT to determine the file name in which a given unit resides. It is
2283 often convenient to follow these default rules, and if you follow them,
2284 the compiler knows without being explicitly told where to find all
2287 However, in some cases, particularly when a program is imported from
2288 another Ada compiler environment, it may be more convenient for the
2289 programmer to specify which file names contain which units. GNAT allows
2290 arbitrary file names to be used by means of the Source_File_Name pragma.
2291 The form of this pragma is as shown in the following examples:
2292 @cindex Source_File_Name pragma
2294 @smallexample @c ada
2296 pragma Source_File_Name (My_Utilities.Stacks,
2297 Spec_File_Name => "myutilst_a.ada");
2298 pragma Source_File_name (My_Utilities.Stacks,
2299 Body_File_Name => "myutilst.ada");
2304 As shown in this example, the first argument for the pragma is the unit
2305 name (in this example a child unit). The second argument has the form
2306 of a named association. The identifier
2307 indicates whether the file name is for a spec or a body;
2308 the file name itself is given by a string literal.
2310 The source file name pragma is a configuration pragma, which means that
2311 normally it will be placed in the @file{gnat.adc}
2312 file used to hold configuration
2313 pragmas that apply to a complete compilation environment.
2314 For more details on how the @file{gnat.adc} file is created and used
2315 see @ref{Handling of Configuration Pragmas}.
2316 @cindex @file{gnat.adc}
2319 GNAT allows completely arbitrary file names to be specified using the
2320 source file name pragma. However, if the file name specified has an
2321 extension other than @file{.ads} or @file{.adb} it is necessary to use
2322 a special syntax when compiling the file. The name in this case must be
2323 preceded by the special sequence @code{-x} followed by a space and the name
2324 of the language, here @code{ada}, as in:
2327 $ gcc -c -x ada peculiar_file_name.sim
2332 @command{gnatmake} handles non-standard file names in the usual manner (the
2333 non-standard file name for the main program is simply used as the
2334 argument to gnatmake). Note that if the extension is also non-standard,
2335 then it must be included in the gnatmake command, it may not be omitted.
2337 @node Alternative File Naming Schemes
2338 @section Alternative File Naming Schemes
2339 @cindex File naming schemes, alternative
2342 In the previous section, we described the use of the @code{Source_File_Name}
2343 pragma to allow arbitrary names to be assigned to individual source files.
2344 However, this approach requires one pragma for each file, and especially in
2345 large systems can result in very long @file{gnat.adc} files, and also create
2346 a maintenance problem.
2348 GNAT also provides a facility for specifying systematic file naming schemes
2349 other than the standard default naming scheme previously described. An
2350 alternative scheme for naming is specified by the use of
2351 @code{Source_File_Name} pragmas having the following format:
2352 @cindex Source_File_Name pragma
2354 @smallexample @c ada
2355 pragma Source_File_Name (
2356 Spec_File_Name => FILE_NAME_PATTERN
2357 [,Casing => CASING_SPEC]
2358 [,Dot_Replacement => STRING_LITERAL]);
2360 pragma Source_File_Name (
2361 Body_File_Name => FILE_NAME_PATTERN
2362 [,Casing => CASING_SPEC]
2363 [,Dot_Replacement => STRING_LITERAL]);
2365 pragma Source_File_Name (
2366 Subunit_File_Name => FILE_NAME_PATTERN
2367 [,Casing => CASING_SPEC]
2368 [,Dot_Replacement => STRING_LITERAL]);
2370 FILE_NAME_PATTERN ::= STRING_LITERAL
2371 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2375 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2376 It contains a single asterisk character, and the unit name is substituted
2377 systematically for this asterisk. The optional parameter
2378 @code{Casing} indicates
2379 whether the unit name is to be all upper-case letters, all lower-case letters,
2380 or mixed-case. If no
2381 @code{Casing} parameter is used, then the default is all
2382 ^lower-case^upper-case^.
2384 The optional @code{Dot_Replacement} string is used to replace any periods
2385 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2386 argument is used then separating dots appear unchanged in the resulting
2388 Although the above syntax indicates that the
2389 @code{Casing} argument must appear
2390 before the @code{Dot_Replacement} argument, but it
2391 is also permissible to write these arguments in the opposite order.
2393 As indicated, it is possible to specify different naming schemes for
2394 bodies, specs, and subunits. Quite often the rule for subunits is the
2395 same as the rule for bodies, in which case, there is no need to give
2396 a separate @code{Subunit_File_Name} rule, and in this case the
2397 @code{Body_File_name} rule is used for subunits as well.
2399 The separate rule for subunits can also be used to implement the rather
2400 unusual case of a compilation environment (e.g. a single directory) which
2401 contains a subunit and a child unit with the same unit name. Although
2402 both units cannot appear in the same partition, the Ada Reference Manual
2403 allows (but does not require) the possibility of the two units coexisting
2404 in the same environment.
2406 The file name translation works in the following steps:
2411 If there is a specific @code{Source_File_Name} pragma for the given unit,
2412 then this is always used, and any general pattern rules are ignored.
2415 If there is a pattern type @code{Source_File_Name} pragma that applies to
2416 the unit, then the resulting file name will be used if the file exists. If
2417 more than one pattern matches, the latest one will be tried first, and the
2418 first attempt resulting in a reference to a file that exists will be used.
2421 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2422 for which the corresponding file exists, then the standard GNAT default
2423 naming rules are used.
2428 As an example of the use of this mechanism, consider a commonly used scheme
2429 in which file names are all lower case, with separating periods copied
2430 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2431 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2434 @smallexample @c ada
2435 pragma Source_File_Name
2436 (Spec_File_Name => "*.1.ada");
2437 pragma Source_File_Name
2438 (Body_File_Name => "*.2.ada");
2442 The default GNAT scheme is actually implemented by providing the following
2443 default pragmas internally:
2445 @smallexample @c ada
2446 pragma Source_File_Name
2447 (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2448 pragma Source_File_Name
2449 (Body_File_Name => "*.adb", Dot_Replacement => "-");
2453 Our final example implements a scheme typically used with one of the
2454 Ada 83 compilers, where the separator character for subunits was ``__''
2455 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2456 by adding @file{.ADA}, and subunits by
2457 adding @file{.SEP}. All file names were
2458 upper case. Child units were not present of course since this was an
2459 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2460 the same double underscore separator for child units.
2462 @smallexample @c ada
2463 pragma Source_File_Name
2464 (Spec_File_Name => "*_.ADA",
2465 Dot_Replacement => "__",
2466 Casing = Uppercase);
2467 pragma Source_File_Name
2468 (Body_File_Name => "*.ADA",
2469 Dot_Replacement => "__",
2470 Casing = Uppercase);
2471 pragma Source_File_Name
2472 (Subunit_File_Name => "*.SEP",
2473 Dot_Replacement => "__",
2474 Casing = Uppercase);
2477 @node Generating Object Files
2478 @section Generating Object Files
2481 An Ada program consists of a set of source files, and the first step in
2482 compiling the program is to generate the corresponding object files.
2483 These are generated by compiling a subset of these source files.
2484 The files you need to compile are the following:
2488 If a package spec has no body, compile the package spec to produce the
2489 object file for the package.
2492 If a package has both a spec and a body, compile the body to produce the
2493 object file for the package. The source file for the package spec need
2494 not be compiled in this case because there is only one object file, which
2495 contains the code for both the spec and body of the package.
2498 For a subprogram, compile the subprogram body to produce the object file
2499 for the subprogram. The spec, if one is present, is as usual in a
2500 separate file, and need not be compiled.
2504 In the case of subunits, only compile the parent unit. A single object
2505 file is generated for the entire subunit tree, which includes all the
2509 Compile child units independently of their parent units
2510 (though, of course, the spec of all the ancestor unit must be present in order
2511 to compile a child unit).
2515 Compile generic units in the same manner as any other units. The object
2516 files in this case are small dummy files that contain at most the
2517 flag used for elaboration checking. This is because GNAT always handles generic
2518 instantiation by means of macro expansion. However, it is still necessary to
2519 compile generic units, for dependency checking and elaboration purposes.
2523 The preceding rules describe the set of files that must be compiled to
2524 generate the object files for a program. Each object file has the same
2525 name as the corresponding source file, except that the extension is
2528 You may wish to compile other files for the purpose of checking their
2529 syntactic and semantic correctness. For example, in the case where a
2530 package has a separate spec and body, you would not normally compile the
2531 spec. However, it is convenient in practice to compile the spec to make
2532 sure it is error-free before compiling clients of this spec, because such
2533 compilations will fail if there is an error in the spec.
2535 GNAT provides an option for compiling such files purely for the
2536 purposes of checking correctness; such compilations are not required as
2537 part of the process of building a program. To compile a file in this
2538 checking mode, use the @option{-gnatc} switch.
2540 @node Source Dependencies
2541 @section Source Dependencies
2544 A given object file clearly depends on the source file which is compiled
2545 to produce it. Here we are using @dfn{depends} in the sense of a typical
2546 @code{make} utility; in other words, an object file depends on a source
2547 file if changes to the source file require the object file to be
2549 In addition to this basic dependency, a given object may depend on
2550 additional source files as follows:
2554 If a file being compiled @code{with}'s a unit @var{X}, the object file
2555 depends on the file containing the spec of unit @var{X}. This includes
2556 files that are @code{with}'ed implicitly either because they are parents
2557 of @code{with}'ed child units or they are run-time units required by the
2558 language constructs used in a particular unit.
2561 If a file being compiled instantiates a library level generic unit, the
2562 object file depends on both the spec and body files for this generic
2566 If a file being compiled instantiates a generic unit defined within a
2567 package, the object file depends on the body file for the package as
2568 well as the spec file.
2572 @cindex @option{-gnatn} switch
2573 If a file being compiled contains a call to a subprogram for which
2574 pragma @code{Inline} applies and inlining is activated with the
2575 @option{-gnatn} switch, the object file depends on the file containing the
2576 body of this subprogram as well as on the file containing the spec. Note
2577 that for inlining to actually occur as a result of the use of this switch,
2578 it is necessary to compile in optimizing mode.
2580 @cindex @option{-gnatN} switch
2581 The use of @option{-gnatN} activates a more extensive inlining optimization
2582 that is performed by the front end of the compiler. This inlining does
2583 not require that the code generation be optimized. Like @option{-gnatn},
2584 the use of this switch generates additional dependencies.
2586 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
2587 to specify both options.
2590 If an object file O depends on the proper body of a subunit through inlining
2591 or instantiation, it depends on the parent unit of the subunit. This means that
2592 any modification of the parent unit or one of its subunits affects the
2596 The object file for a parent unit depends on all its subunit body files.
2599 The previous two rules meant that for purposes of computing dependencies and
2600 recompilation, a body and all its subunits are treated as an indivisible whole.
2603 These rules are applied transitively: if unit @code{A} @code{with}'s
2604 unit @code{B}, whose elaboration calls an inlined procedure in package
2605 @code{C}, the object file for unit @code{A} will depend on the body of
2606 @code{C}, in file @file{c.adb}.
2608 The set of dependent files described by these rules includes all the
2609 files on which the unit is semantically dependent, as described in the
2610 Ada 95 Language Reference Manual. However, it is a superset of what the
2611 ARM describes, because it includes generic, inline, and subunit dependencies.
2613 An object file must be recreated by recompiling the corresponding source
2614 file if any of the source files on which it depends are modified. For
2615 example, if the @code{make} utility is used to control compilation,
2616 the rule for an Ada object file must mention all the source files on
2617 which the object file depends, according to the above definition.
2618 The determination of the necessary
2619 recompilations is done automatically when one uses @command{gnatmake}.
2622 @node The Ada Library Information Files
2623 @section The Ada Library Information Files
2624 @cindex Ada Library Information files
2625 @cindex @file{ALI} files
2628 Each compilation actually generates two output files. The first of these
2629 is the normal object file that has a @file{.o} extension. The second is a
2630 text file containing full dependency information. It has the same
2631 name as the source file, but an @file{.ali} extension.
2632 This file is known as the Ada Library Information (@file{ALI}) file.
2633 The following information is contained in the @file{ALI} file.
2637 Version information (indicates which version of GNAT was used to compile
2638 the unit(s) in question)
2641 Main program information (including priority and time slice settings,
2642 as well as the wide character encoding used during compilation).
2645 List of arguments used in the @command{gcc} command for the compilation
2648 Attributes of the unit, including configuration pragmas used, an indication
2649 of whether the compilation was successful, exception model used etc.
2652 A list of relevant restrictions applying to the unit (used for consistency)
2656 Categorization information (e.g. use of pragma @code{Pure}).
2659 Information on all @code{with}'ed units, including presence of
2660 @code{Elaborate} or @code{Elaborate_All} pragmas.
2663 Information from any @code{Linker_Options} pragmas used in the unit
2666 Information on the use of @code{Body_Version} or @code{Version}
2667 attributes in the unit.
2670 Dependency information. This is a list of files, together with
2671 time stamp and checksum information. These are files on which
2672 the unit depends in the sense that recompilation is required
2673 if any of these units are modified.
2676 Cross-reference data. Contains information on all entities referenced
2677 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2678 provide cross-reference information.
2683 For a full detailed description of the format of the @file{ALI} file,
2684 see the source of the body of unit @code{Lib.Writ}, contained in file
2685 @file{lib-writ.adb} in the GNAT compiler sources.
2687 @node Binding an Ada Program
2688 @section Binding an Ada Program
2691 When using languages such as C and C++, once the source files have been
2692 compiled the only remaining step in building an executable program
2693 is linking the object modules together. This means that it is possible to
2694 link an inconsistent version of a program, in which two units have
2695 included different versions of the same header.
2697 The rules of Ada do not permit such an inconsistent program to be built.
2698 For example, if two clients have different versions of the same package,
2699 it is illegal to build a program containing these two clients.
2700 These rules are enforced by the GNAT binder, which also determines an
2701 elaboration order consistent with the Ada rules.
2703 The GNAT binder is run after all the object files for a program have
2704 been created. It is given the name of the main program unit, and from
2705 this it determines the set of units required by the program, by reading the
2706 corresponding ALI files. It generates error messages if the program is
2707 inconsistent or if no valid order of elaboration exists.
2709 If no errors are detected, the binder produces a main program, in Ada by
2710 default, that contains calls to the elaboration procedures of those
2711 compilation unit that require them, followed by
2712 a call to the main program. This Ada program is compiled to generate the
2713 object file for the main program. The name of
2714 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2715 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2718 Finally, the linker is used to build the resulting executable program,
2719 using the object from the main program from the bind step as well as the
2720 object files for the Ada units of the program.
2722 @node Mixed Language Programming
2723 @section Mixed Language Programming
2724 @cindex Mixed Language Programming
2727 This section describes how to develop a mixed-language program,
2728 specifically one that comprises units in both Ada and C.
2731 * Interfacing to C::
2732 * Calling Conventions::
2735 @node Interfacing to C
2736 @subsection Interfacing to C
2738 Interfacing Ada with a foreign language such as C involves using
2739 compiler directives to import and/or export entity definitions in each
2740 language---using @code{extern} statements in C, for instance, and the
2741 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada. For
2742 a full treatment of these topics, read Appendix B, section 1 of the Ada
2743 95 Language Reference Manual.
2745 There are two ways to build a program using GNAT that contains some Ada
2746 sources and some foreign language sources, depending on whether or not
2747 the main subprogram is written in Ada. Here is a source example with
2748 the main subprogram in Ada:
2754 void print_num (int num)
2756 printf ("num is %d.\n", num);
2762 /* num_from_Ada is declared in my_main.adb */
2763 extern int num_from_Ada;
2767 return num_from_Ada;
2771 @smallexample @c ada
2773 procedure My_Main is
2775 -- Declare then export an Integer entity called num_from_Ada
2776 My_Num : Integer := 10;
2777 pragma Export (C, My_Num, "num_from_Ada");
2779 -- Declare an Ada function spec for Get_Num, then use
2780 -- C function get_num for the implementation.
2781 function Get_Num return Integer;
2782 pragma Import (C, Get_Num, "get_num");
2784 -- Declare an Ada procedure spec for Print_Num, then use
2785 -- C function print_num for the implementation.
2786 procedure Print_Num (Num : Integer);
2787 pragma Import (C, Print_Num, "print_num");
2790 Print_Num (Get_Num);
2796 To build this example, first compile the foreign language files to
2797 generate object files:
2799 ^gcc -c file1.c^gcc -c FILE1.C^
2800 ^gcc -c file2.c^gcc -c FILE2.C^
2804 Then, compile the Ada units to produce a set of object files and ALI
2807 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2811 Run the Ada binder on the Ada main program:
2813 gnatbind my_main.ali
2817 Link the Ada main program, the Ada objects and the other language
2820 gnatlink my_main.ali file1.o file2.o
2824 The last three steps can be grouped in a single command:
2826 gnatmake my_main.adb -largs file1.o file2.o
2829 @cindex Binder output file
2831 If the main program is in a language other than Ada, then you may have
2832 more than one entry point into the Ada subsystem. You must use a special
2833 binder option to generate callable routines that initialize and
2834 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2835 Calls to the initialization and finalization routines must be inserted
2836 in the main program, or some other appropriate point in the code. The
2837 call to initialize the Ada units must occur before the first Ada
2838 subprogram is called, and the call to finalize the Ada units must occur
2839 after the last Ada subprogram returns. The binder will place the
2840 initialization and finalization subprograms into the
2841 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2842 sources. To illustrate, we have the following example:
2846 extern void adainit (void);
2847 extern void adafinal (void);
2848 extern int add (int, int);
2849 extern int sub (int, int);
2851 int main (int argc, char *argv[])
2857 /* Should print "21 + 7 = 28" */
2858 printf ("%d + %d = %d\n", a, b, add (a, b));
2859 /* Should print "21 - 7 = 14" */
2860 printf ("%d - %d = %d\n", a, b, sub (a, b));
2866 @smallexample @c ada
2869 function Add (A, B : Integer) return Integer;
2870 pragma Export (C, Add, "add");
2874 package body Unit1 is
2875 function Add (A, B : Integer) return Integer is
2883 function Sub (A, B : Integer) return Integer;
2884 pragma Export (C, Sub, "sub");
2888 package body Unit2 is
2889 function Sub (A, B : Integer) return Integer is
2898 The build procedure for this application is similar to the last
2899 example's. First, compile the foreign language files to generate object
2902 ^gcc -c main.c^gcc -c main.c^
2906 Next, compile the Ada units to produce a set of object files and ALI
2909 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2910 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2914 Run the Ada binder on every generated ALI file. Make sure to use the
2915 @option{-n} option to specify a foreign main program:
2917 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2921 Link the Ada main program, the Ada objects and the foreign language
2922 objects. You need only list the last ALI file here:
2924 gnatlink unit2.ali main.o -o exec_file
2927 This procedure yields a binary executable called @file{exec_file}.
2930 @node Calling Conventions
2931 @subsection Calling Conventions
2932 @cindex Foreign Languages
2933 @cindex Calling Conventions
2934 GNAT follows standard calling sequence conventions and will thus interface
2935 to any other language that also follows these conventions. The following
2936 Convention identifiers are recognized by GNAT:
2939 @cindex Interfacing to Ada
2940 @cindex Other Ada compilers
2941 @cindex Convention Ada
2943 This indicates that the standard Ada calling sequence will be
2944 used and all Ada data items may be passed without any limitations in the
2945 case where GNAT is used to generate both the caller and callee. It is also
2946 possible to mix GNAT generated code and code generated by another Ada
2947 compiler. In this case, the data types should be restricted to simple
2948 cases, including primitive types. Whether complex data types can be passed
2949 depends on the situation. Probably it is safe to pass simple arrays, such
2950 as arrays of integers or floats. Records may or may not work, depending
2951 on whether both compilers lay them out identically. Complex structures
2952 involving variant records, access parameters, tasks, or protected types,
2953 are unlikely to be able to be passed.
2955 Note that in the case of GNAT running
2956 on a platform that supports HP Ada 83, a higher degree of compatibility
2957 can be guaranteed, and in particular records are layed out in an identical
2958 manner in the two compilers. Note also that if output from two different
2959 compilers is mixed, the program is responsible for dealing with elaboration
2960 issues. Probably the safest approach is to write the main program in the
2961 version of Ada other than GNAT, so that it takes care of its own elaboration
2962 requirements, and then call the GNAT-generated adainit procedure to ensure
2963 elaboration of the GNAT components. Consult the documentation of the other
2964 Ada compiler for further details on elaboration.
2966 However, it is not possible to mix the tasking run time of GNAT and
2967 HP Ada 83, All the tasking operations must either be entirely within
2968 GNAT compiled sections of the program, or entirely within HP Ada 83
2969 compiled sections of the program.
2971 @cindex Interfacing to Assembly
2972 @cindex Convention Assembler
2974 Specifies assembler as the convention. In practice this has the
2975 same effect as convention Ada (but is not equivalent in the sense of being
2976 considered the same convention).
2978 @cindex Convention Asm
2981 Equivalent to Assembler.
2983 @cindex Interfacing to COBOL
2984 @cindex Convention COBOL
2987 Data will be passed according to the conventions described
2988 in section B.4 of the Ada 95 Reference Manual.
2991 @cindex Interfacing to C
2992 @cindex Convention C
2994 Data will be passed according to the conventions described
2995 in section B.3 of the Ada 95 Reference Manual.
2997 A note on interfacing to a C ``varargs'' function:
2998 @findex C varargs function
2999 @cindex Interfacing to C varargs function
3000 @cindex varargs function interfaces
3004 In C, @code{varargs} allows a function to take a variable number of
3005 arguments. There is no direct equivalent in this to Ada. One
3006 approach that can be used is to create a C wrapper for each
3007 different profile and then interface to this C wrapper. For
3008 example, to print an @code{int} value using @code{printf},
3009 create a C function @code{printfi} that takes two arguments, a
3010 pointer to a string and an int, and calls @code{printf}.
3011 Then in the Ada program, use pragma @code{Import} to
3012 interface to @code{printfi}.
3015 It may work on some platforms to directly interface to
3016 a @code{varargs} function by providing a specific Ada profile
3017 for a particular call. However, this does not work on
3018 all platforms, since there is no guarantee that the
3019 calling sequence for a two argument normal C function
3020 is the same as for calling a @code{varargs} C function with
3021 the same two arguments.
3024 @cindex Convention Default
3029 @cindex Convention External
3036 @cindex Interfacing to C++
3037 @cindex Convention C++
3039 This stands for C++. For most purposes this is identical to C.
3040 See the separate description of the specialized GNAT pragmas relating to
3041 C++ interfacing for further details.
3045 @cindex Interfacing to Fortran
3046 @cindex Convention Fortran
3048 Data will be passed according to the conventions described
3049 in section B.5 of the Ada 95 Reference Manual.
3052 This applies to an intrinsic operation, as defined in the Ada 95
3053 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
3054 this means that the body of the subprogram is provided by the compiler itself,
3055 usually by means of an efficient code sequence, and that the user does not
3056 supply an explicit body for it. In an application program, the pragma can
3057 only be applied to the following two sets of names, which the GNAT compiler
3062 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right,
3063 Shift_Right_Arithmetic. The corresponding subprogram declaration must have
3064 two formal parameters. The
3065 first one must be a signed integer type or a modular type with a binary
3066 modulus, and the second parameter must be of type Natural.
3067 The return type must be the same as the type of the first argument. The size
3068 of this type can only be 8, 16, 32, or 64.
3069 @item binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
3070 The corresponding operator declaration must have parameters and result type
3071 that have the same root numeric type (for example, all three are long_float
3072 types). This simplifies the definition of operations that use type checking
3073 to perform dimensional checks:
3075 @smallexample @c ada
3076 type Distance is new Long_Float;
3077 type Time is new Long_Float;
3078 type Velocity is new Long_Float;
3079 function "/" (D : Distance; T : Time)
3081 pragma Import (Intrinsic, "/");
3085 This common idiom is often programmed with a generic definition and an
3086 explicit body. The pragma makes it simpler to introduce such declarations.
3087 It incurs no overhead in compilation time or code size, because it is
3088 implemented as a single machine instruction.
3094 @cindex Convention Stdcall
3096 This is relevant only to Windows XP/2000/NT/95 implementations of GNAT,
3097 and specifies that the @code{Stdcall} calling sequence will be used,
3098 as defined by the NT API. Nevertheless, to ease building
3099 cross-platform bindings this convention will be handled as a @code{C} calling
3100 convention on non Windows platforms.
3103 @cindex Convention DLL
3105 This is equivalent to @code{Stdcall}.
3108 @cindex Convention Win32
3110 This is equivalent to @code{Stdcall}.
3114 @cindex Convention Stubbed
3116 This is a special convention that indicates that the compiler
3117 should provide a stub body that raises @code{Program_Error}.
3121 GNAT additionally provides a useful pragma @code{Convention_Identifier}
3122 that can be used to parametrize conventions and allow additional synonyms
3123 to be specified. For example if you have legacy code in which the convention
3124 identifier Fortran77 was used for Fortran, you can use the configuration
3127 @smallexample @c ada
3128 pragma Convention_Identifier (Fortran77, Fortran);
3132 And from now on the identifier Fortran77 may be used as a convention
3133 identifier (for example in an @code{Import} pragma) with the same
3137 @node Building Mixed Ada & C++ Programs
3138 @section Building Mixed Ada and C++ Programs
3141 A programmer inexperienced with mixed-language development may find that
3142 building an application containing both Ada and C++ code can be a
3143 challenge. As a matter of fact, interfacing with C++ has not been
3144 standardized in the Ada 95 Reference Manual due to the immaturity of --
3145 and lack of standards for -- C++ at the time. This section gives a few
3146 hints that should make this task easier. The first section addresses
3147 the differences regarding interfacing with C. The second section
3148 looks into the delicate problem of linking the complete application from
3149 its Ada and C++ parts. The last section gives some hints on how the GNAT
3150 run time can be adapted in order to allow inter-language dispatching
3151 with a new C++ compiler.
3154 * Interfacing to C++::
3155 * Linking a Mixed C++ & Ada Program::
3156 * A Simple Example::
3157 * Adapting the Run Time to a New C++ Compiler::
3160 @node Interfacing to C++
3161 @subsection Interfacing to C++
3164 GNAT supports interfacing with C++ compilers generating code that is
3165 compatible with the standard Application Binary Interface of the given
3169 Interfacing can be done at 3 levels: simple data, subprograms, and
3170 classes. In the first two cases, GNAT offers a specific @var{Convention
3171 CPP} that behaves exactly like @var{Convention C}. Usually, C++ mangles
3172 the names of subprograms, and currently, GNAT does not provide any help
3173 to solve the demangling problem. This problem can be addressed in two
3177 by modifying the C++ code in order to force a C convention using
3178 the @code{extern "C"} syntax.
3181 by figuring out the mangled name and use it as the Link_Name argument of
3186 Interfacing at the class level can be achieved by using the GNAT specific
3187 pragmas such as @code{CPP_Class} and @code{CPP_Virtual}. See the GNAT
3188 Reference Manual for additional information.
3190 @node Linking a Mixed C++ & Ada Program
3191 @subsection Linking a Mixed C++ & Ada Program
3194 Usually the linker of the C++ development system must be used to link
3195 mixed applications because most C++ systems will resolve elaboration
3196 issues (such as calling constructors on global class instances)
3197 transparently during the link phase. GNAT has been adapted to ease the
3198 use of a foreign linker for the last phase. Three cases can be
3203 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3204 The C++ linker can simply be called by using the C++ specific driver
3205 called @code{c++}. Note that this setup is not very common because it
3206 may involve recompiling the whole GCC tree from sources, which makes it
3207 harder to upgrade the compilation system for one language without
3208 destabilizing the other.
3213 $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++
3217 Using GNAT and G++ from two different GCC installations: If both
3218 compilers are on the PATH, the previous method may be used. It is
3219 important to note that environment variables such as C_INCLUDE_PATH,
3220 GCC_EXEC_PREFIX, BINUTILS_ROOT, and GCC_ROOT will affect both compilers
3221 at the same time and may make one of the two compilers operate
3222 improperly if set during invocation of the wrong compiler. It is also
3223 very important that the linker uses the proper @file{libgcc.a} GCC
3224 library -- that is, the one from the C++ compiler installation. The
3225 implicit link command as suggested in the gnatmake command from the
3226 former example can be replaced by an explicit link command with the
3227 full-verbosity option in order to verify which library is used:
3230 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3232 If there is a problem due to interfering environment variables, it can
3233 be worked around by using an intermediate script. The following example
3234 shows the proper script to use when GNAT has not been installed at its
3235 default location and g++ has been installed at its default location:
3243 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3247 Using a non-GNU C++ compiler: The commands previously described can be
3248 used to insure that the C++ linker is used. Nonetheless, you need to add
3249 a few more parameters to the link command line, depending on the exception
3252 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3253 to the libgcc libraries are required:
3258 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3259 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3262 Where CC is the name of the non-GNU C++ compiler.
3264 If the @code{zero cost} exception mechanism is used, and the platform
3265 supports automatic registration of exception tables (e.g. Solaris or IRIX),
3266 paths to more objects are required:
3271 CC `gcc -print-file-name=crtbegin.o` $* \
3272 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3273 `gcc -print-file-name=crtend.o`
3274 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3277 If the @code{zero cost} exception mechanism is used, and the platform
3278 doesn't support automatic registration of exception tables (e.g. HP-UX,
3279 Tru64 or AIX), the simple approach described above will not work and
3280 a pre-linking phase using GNAT will be necessary.
3284 @node A Simple Example
3285 @subsection A Simple Example
3287 The following example, provided as part of the GNAT examples, shows how
3288 to achieve procedural interfacing between Ada and C++ in both
3289 directions. The C++ class A has two methods. The first method is exported
3290 to Ada by the means of an extern C wrapper function. The second method
3291 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3292 a limited record with a layout comparable to the C++ class. The Ada
3293 subprogram, in turn, calls the C++ method. So, starting from the C++
3294 main program, the process passes back and forth between the two
3298 Here are the compilation commands:
3300 $ gnatmake -c simple_cpp_interface
3303 $ gnatbind -n simple_cpp_interface
3304 $ gnatlink simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
3305 -lstdc++ ex7.o cpp_main.o
3309 Here are the corresponding sources:
3317 void adainit (void);
3318 void adafinal (void);
3319 void method1 (A *t);
3341 class A : public Origin @{
3343 void method1 (void);
3344 void method2 (int v);
3354 extern "C" @{ void ada_method2 (A *t, int v);@}
3356 void A::method1 (void)
3359 printf ("in A::method1, a_value = %d \n",a_value);
3363 void A::method2 (int v)
3365 ada_method2 (this, v);
3366 printf ("in A::method2, a_value = %d \n",a_value);
3373 printf ("in A::A, a_value = %d \n",a_value);
3377 @b{package} @b{body} Simple_Cpp_Interface @b{is}
3379 @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer) @b{is}
3383 @b{end} Ada_Method2;
3385 @b{end} Simple_Cpp_Interface;
3387 @b{package} Simple_Cpp_Interface @b{is}
3388 @b{type} A @b{is} @b{limited}
3393 @b{pragma} Convention (C, A);
3395 @b{procedure} Method1 (This : @b{in} @b{out} A);
3396 @b{pragma} Import (C, Method1);
3398 @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer);
3399 @b{pragma} Export (C, Ada_Method2);
3401 @b{end} Simple_Cpp_Interface;
3404 @node Adapting the Run Time to a New C++ Compiler
3405 @subsection Adapting the Run Time to a New C++ Compiler
3407 GNAT offers the capability to derive Ada 95 tagged types directly from
3408 preexisting C++ classes and . See ``Interfacing with C++'' in the
3409 @cite{GNAT Reference Manual}. The mechanism used by GNAT for achieving
3411 has been made user configurable through a GNAT library unit
3412 @code{Interfaces.CPP}. The default version of this file is adapted to
3413 the GNU C++ compiler. Internal knowledge of the virtual
3414 table layout used by the new C++ compiler is needed to configure
3415 properly this unit. The Interface of this unit is known by the compiler
3416 and cannot be changed except for the value of the constants defining the
3417 characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size,
3418 CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source
3419 of this unit for more details.
3421 @node Comparison between GNAT and C/C++ Compilation Models
3422 @section Comparison between GNAT and C/C++ Compilation Models
3425 The GNAT model of compilation is close to the C and C++ models. You can
3426 think of Ada specs as corresponding to header files in C. As in C, you
3427 don't need to compile specs; they are compiled when they are used. The
3428 Ada @code{with} is similar in effect to the @code{#include} of a C
3431 One notable difference is that, in Ada, you may compile specs separately
3432 to check them for semantic and syntactic accuracy. This is not always
3433 possible with C headers because they are fragments of programs that have
3434 less specific syntactic or semantic rules.
3436 The other major difference is the requirement for running the binder,
3437 which performs two important functions. First, it checks for
3438 consistency. In C or C++, the only defense against assembling
3439 inconsistent programs lies outside the compiler, in a makefile, for
3440 example. The binder satisfies the Ada requirement that it be impossible
3441 to construct an inconsistent program when the compiler is used in normal
3444 @cindex Elaboration order control
3445 The other important function of the binder is to deal with elaboration
3446 issues. There are also elaboration issues in C++ that are handled
3447 automatically. This automatic handling has the advantage of being
3448 simpler to use, but the C++ programmer has no control over elaboration.
3449 Where @code{gnatbind} might complain there was no valid order of
3450 elaboration, a C++ compiler would simply construct a program that
3451 malfunctioned at run time.
3454 @node Comparison between GNAT and Conventional Ada Library Models
3455 @section Comparison between GNAT and Conventional Ada Library Models
3458 This section is intended for Ada programmers who have
3459 used an Ada compiler implementing the traditional Ada library
3460 model, as described in the Ada 95 Language Reference Manual.
3462 @cindex GNAT library
3463 In GNAT, there is no ``library'' in the normal sense. Instead, the set of
3464 source files themselves acts as the library. Compiling Ada programs does
3465 not generate any centralized information, but rather an object file and
3466 a ALI file, which are of interest only to the binder and linker.
3467 In a traditional system, the compiler reads information not only from
3468 the source file being compiled, but also from the centralized library.
3469 This means that the effect of a compilation depends on what has been
3470 previously compiled. In particular:
3474 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3475 to the version of the unit most recently compiled into the library.
3478 Inlining is effective only if the necessary body has already been
3479 compiled into the library.
3482 Compiling a unit may obsolete other units in the library.
3486 In GNAT, compiling one unit never affects the compilation of any other
3487 units because the compiler reads only source files. Only changes to source
3488 files can affect the results of a compilation. In particular:
3492 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3493 to the source version of the unit that is currently accessible to the
3498 Inlining requires the appropriate source files for the package or
3499 subprogram bodies to be available to the compiler. Inlining is always
3500 effective, independent of the order in which units are complied.
3503 Compiling a unit never affects any other compilations. The editing of
3504 sources may cause previous compilations to be out of date if they
3505 depended on the source file being modified.
3509 The most important result of these differences is that order of compilation
3510 is never significant in GNAT. There is no situation in which one is
3511 required to do one compilation before another. What shows up as order of
3512 compilation requirements in the traditional Ada library becomes, in
3513 GNAT, simple source dependencies; in other words, there is only a set
3514 of rules saying what source files must be present when a file is
3518 @node Placement of temporary files
3519 @section Placement of temporary files
3520 @cindex Temporary files (user control over placement)
3523 GNAT creates temporary files in the directory designated by the environment
3524 variable @env{TMPDIR}.
3525 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3526 for detailed information on how environment variables are resolved.
3527 For most users the easiest way to make use of this feature is to simply
3528 define @env{TMPDIR} as a job level logical name).
3529 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3530 for compiler temporary files, then you can include something like the
3531 following command in your @file{LOGIN.COM} file:
3534 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3538 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3539 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3540 designated by @env{TEMP}.
3541 If none of these environment variables are defined then GNAT uses the
3542 directory designated by the logical name @code{SYS$SCRATCH:}
3543 (by default the user's home directory). If all else fails
3544 GNAT uses the current directory for temporary files.
3547 @c *************************
3548 @node Compiling Using gcc
3549 @chapter Compiling Using @command{gcc}
3552 This chapter discusses how to compile Ada programs using the @command{gcc}
3553 command. It also describes the set of switches
3554 that can be used to control the behavior of the compiler.
3556 * Compiling Programs::
3557 * Switches for gcc::
3558 * Search Paths and the Run-Time Library (RTL)::
3559 * Order of Compilation Issues::
3563 @node Compiling Programs
3564 @section Compiling Programs
3567 The first step in creating an executable program is to compile the units
3568 of the program using the @command{gcc} command. You must compile the
3573 the body file (@file{.adb}) for a library level subprogram or generic
3577 the spec file (@file{.ads}) for a library level package or generic
3578 package that has no body
3581 the body file (@file{.adb}) for a library level package
3582 or generic package that has a body
3587 You need @emph{not} compile the following files
3592 the spec of a library unit which has a body
3599 because they are compiled as part of compiling related units. GNAT
3601 when the corresponding body is compiled, and subunits when the parent is
3604 @cindex cannot generate code
3605 If you attempt to compile any of these files, you will get one of the
3606 following error messages (where fff is the name of the file you compiled):
3609 cannot generate code for file @var{fff} (package spec)
3610 to check package spec, use -gnatc
3612 cannot generate code for file @var{fff} (missing subunits)
3613 to check parent unit, use -gnatc
3615 cannot generate code for file @var{fff} (subprogram spec)
3616 to check subprogram spec, use -gnatc
3618 cannot generate code for file @var{fff} (subunit)
3619 to check subunit, use -gnatc
3623 As indicated by the above error messages, if you want to submit
3624 one of these files to the compiler to check for correct semantics
3625 without generating code, then use the @option{-gnatc} switch.
3627 The basic command for compiling a file containing an Ada unit is
3630 $ gcc -c [@var{switches}] @file{file name}
3634 where @var{file name} is the name of the Ada file (usually
3636 @file{.ads} for a spec or @file{.adb} for a body).
3639 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3641 The result of a successful compilation is an object file, which has the
3642 same name as the source file but an extension of @file{.o} and an Ada
3643 Library Information (ALI) file, which also has the same name as the
3644 source file, but with @file{.ali} as the extension. GNAT creates these
3645 two output files in the current directory, but you may specify a source
3646 file in any directory using an absolute or relative path specification
3647 containing the directory information.
3650 @command{gcc} is actually a driver program that looks at the extensions of
3651 the file arguments and loads the appropriate compiler. For example, the
3652 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3653 These programs are in directories known to the driver program (in some
3654 configurations via environment variables you set), but need not be in
3655 your path. The @command{gcc} driver also calls the assembler and any other
3656 utilities needed to complete the generation of the required object
3659 It is possible to supply several file names on the same @command{gcc}
3660 command. This causes @command{gcc} to call the appropriate compiler for
3661 each file. For example, the following command lists three separate
3662 files to be compiled:
3665 $ gcc -c x.adb y.adb z.c
3669 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3670 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3671 The compiler generates three object files @file{x.o}, @file{y.o} and
3672 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3673 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3676 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3679 @node Switches for gcc
3680 @section Switches for @command{gcc}
3683 The @command{gcc} command accepts switches that control the
3684 compilation process. These switches are fully described in this section.
3685 First we briefly list all the switches, in alphabetical order, then we
3686 describe the switches in more detail in functionally grouped sections.
3688 More switches exist for GCC than those documented here, especially
3689 for specific targets. However, their use is not recommended as
3690 they may change code generation in ways that are incompatible with
3691 the Ada run-time library, or can cause inconsistencies between
3695 * Output and Error Message Control::
3696 * Warning Message Control::
3697 * Debugging and Assertion Control::
3698 * Validity Checking::
3701 * Using gcc for Syntax Checking::
3702 * Using gcc for Semantic Checking::
3703 * Compiling Different Versions of Ada::
3704 * Character Set Control::
3705 * File Naming Control::
3706 * Subprogram Inlining Control::
3707 * Auxiliary Output Control::
3708 * Debugging Control::
3709 * Exception Handling Control::
3710 * Units to Sources Mapping Files::
3711 * Integrated Preprocessing::
3712 * Code Generation Control::
3721 @cindex @option{-b} (@command{gcc})
3722 @item -b @var{target}
3723 Compile your program to run on @var{target}, which is the name of a
3724 system configuration. You must have a GNAT cross-compiler built if
3725 @var{target} is not the same as your host system.
3728 @cindex @option{-B} (@command{gcc})
3729 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3730 from @var{dir} instead of the default location. Only use this switch
3731 when multiple versions of the GNAT compiler are available. See the
3732 @command{gcc} manual page for further details. You would normally use the
3733 @option{-b} or @option{-V} switch instead.
3736 @cindex @option{-c} (@command{gcc})
3737 Compile. Always use this switch when compiling Ada programs.
3739 Note: for some other languages when using @command{gcc}, notably in
3740 the case of C and C++, it is possible to use
3741 use @command{gcc} without a @option{-c} switch to
3742 compile and link in one step. In the case of GNAT, you
3743 cannot use this approach, because the binder must be run
3744 and @command{gcc} cannot be used to run the GNAT binder.
3748 @cindex @option{-fno-inline} (@command{gcc})
3749 Suppresses all back-end inlining, even if other optimization or inlining
3751 This includes suppression of inlining that results
3752 from the use of the pragma @code{Inline_Always}.
3753 See also @option{-gnatn} and @option{-gnatN}.
3755 @item -fno-strict-aliasing
3756 @cindex @option{-fno-strict-aliasing} (@command{gcc})
3757 Causes the compiler to avoid assumptions regarding non-aliasing
3758 of objects of different types. See
3759 @ref{Optimization and Strict Aliasing} for details.
3762 @cindex @option{-fstack-check} (@command{gcc})
3763 Activates stack checking.
3764 See @ref{Stack Overflow Checking} for details.
3767 @cindex @option{-fstack-usage} (@command{gcc})
3768 Makes the compiler output stack usage information for the program, on a
3769 per-function basis. See @ref{Static Stack Usage Analysis} for details.
3771 @item -fcallgraph-info[=su]
3772 @cindex @option{-fcallgraph-info} (@command{gcc})
3773 Makes the compiler output callgraph information for the program, on a
3774 per-file basis. The information is generated in the VCG format. It can
3775 be decorated with stack-usage per-node information.
3778 @cindex @option{^-g^/DEBUG^} (@command{gcc})
3779 Generate debugging information. This information is stored in the object
3780 file and copied from there to the final executable file by the linker,
3781 where it can be read by the debugger. You must use the
3782 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
3785 @cindex @option{-gnat83} (@command{gcc})
3786 Enforce Ada 83 restrictions.
3789 @cindex @option{-gnat95} (@command{gcc})
3790 Enforce Ada 95 restrictions.
3793 @cindex @option{-gnat05} (@command{gcc})
3794 Allow full Ada 2005 features.
3797 @cindex @option{-gnata} (@command{gcc})
3798 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
3799 activated. Note that these pragmas can also be controlled using the
3800 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
3803 @cindex @option{-gnatA} (@command{gcc})
3804 Avoid processing @file{gnat.adc}. If a gnat.adc file is present,
3808 @cindex @option{-gnatb} (@command{gcc})
3809 Generate brief messages to @file{stderr} even if verbose mode set.
3812 @cindex @option{-gnatc} (@command{gcc})
3813 Check syntax and semantics only (no code generation attempted).
3816 @cindex @option{-gnatd} (@command{gcc})
3817 Specify debug options for the compiler. The string of characters after
3818 the @option{-gnatd} specify the specific debug options. The possible
3819 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
3820 compiler source file @file{debug.adb} for details of the implemented
3821 debug options. Certain debug options are relevant to applications
3822 programmers, and these are documented at appropriate points in this
3826 @cindex @option{-gnatD} (@command{gcc})
3827 Create expanded source files for source level debugging. This switch
3828 also suppress generation of cross-reference information
3829 (see @option{-gnatx}).
3831 @item -gnatec=@var{path}
3832 @cindex @option{-gnatec} (@command{gcc})
3833 Specify a configuration pragma file
3835 (the equal sign is optional)
3837 (@pxref{The Configuration Pragmas Files}).
3839 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
3840 @cindex @option{-gnateD} (@command{gcc})
3841 Defines a symbol, associated with value, for preprocessing.
3842 (@pxref{Integrated Preprocessing}).
3845 @cindex @option{-gnatef} (@command{gcc})
3846 Display full source path name in brief error messages.
3848 @item -gnatem=@var{path}
3849 @cindex @option{-gnatem} (@command{gcc})
3850 Specify a mapping file
3852 (the equal sign is optional)
3854 (@pxref{Units to Sources Mapping Files}).
3856 @item -gnatep=@var{file}
3857 @cindex @option{-gnatep} (@command{gcc})
3858 Specify a preprocessing data file
3860 (the equal sign is optional)
3862 (@pxref{Integrated Preprocessing}).
3865 @cindex @option{-gnatE} (@command{gcc})
3866 Full dynamic elaboration checks.
3869 @cindex @option{-gnatf} (@command{gcc})
3870 Full errors. Multiple errors per line, all undefined references, do not
3871 attempt to suppress cascaded errors.
3874 @cindex @option{-gnatF} (@command{gcc})
3875 Externals names are folded to all uppercase.
3878 @cindex @option{-gnatg} (@command{gcc})
3879 Internal GNAT implementation mode. This should not be used for
3880 applications programs, it is intended only for use by the compiler
3881 and its run-time library. For documentation, see the GNAT sources.
3882 Note that @option{-gnatg} implies @option{-gnatwu} so that warnings
3883 are generated on unreferenced entities, and all warnings are treated
3887 @cindex @option{-gnatG} (@command{gcc})
3888 List generated expanded code in source form.
3890 @item ^-gnath^/HELP^
3891 @cindex @option{^-gnath^/HELP^} (@command{gcc})
3892 Output usage information. The output is written to @file{stdout}.
3894 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3895 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
3896 Identifier character set
3898 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3901 For details of the possible selections for @var{c},
3902 see @ref{Character Set Control}.
3905 @item -gnatk=@var{n}
3906 @cindex @option{-gnatk} (@command{gcc})
3907 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
3910 @cindex @option{-gnatl} (@command{gcc})
3911 Output full source listing with embedded error messages.
3913 @item -gnatm=@var{n}
3914 @cindex @option{-gnatm} (@command{gcc})
3915 Limit number of detected error or warning messages to @var{n}
3916 where @var{n} is in the range 1..999_999. The default setting if
3917 no switch is given is 9999. Compilation is terminated if this
3918 limit is exceeded. The equal sign here is optional.
3921 @cindex @option{-gnatn} (@command{gcc})
3922 Activate inlining for subprograms for which
3923 pragma @code{inline} is specified. This inlining is performed
3924 by the GCC back-end.
3927 @cindex @option{-gnatN} (@command{gcc})
3928 Activate front end inlining for subprograms for which
3929 pragma @code{Inline} is specified. This inlining is performed
3930 by the front end and will be visible in the
3931 @option{-gnatG} output.
3932 In some cases, this has proved more effective than the back end
3933 inlining resulting from the use of
3936 @option{-gnatN} automatically implies
3937 @option{-gnatn} so it is not necessary
3938 to specify both options. There are a few cases that the back-end inlining
3939 catches that cannot be dealt with in the front-end.
3942 @cindex @option{-gnato} (@command{gcc})
3943 Enable numeric overflow checking (which is not normally enabled by
3944 default). Not that division by zero is a separate check that is not
3945 controlled by this switch (division by zero checking is on by default).
3948 @cindex @option{-gnatp} (@command{gcc})
3949 Suppress all checks.
3952 @cindex @option{-gnatP} (@command{gcc})
3953 Enable polling. This is required on some systems (notably Windows NT) to
3954 obtain asynchronous abort and asynchronous transfer of control capability.
3955 See the description of pragma Polling in the GNAT Reference Manual for
3959 @cindex @option{-gnatq} (@command{gcc})
3960 Don't quit; try semantics, even if parse errors.
3963 @cindex @option{-gnatQ} (@command{gcc})
3964 Don't quit; generate @file{ALI} and tree files even if illegalities.
3966 @item ^-gnatR[0/1/2/3[s]]^/REPRESENTATION_INFO^
3967 @cindex @option{-gnatR} (@command{gcc})
3968 Output representation information for declared types and objects.
3971 @cindex @option{-gnats} (@command{gcc})
3975 @cindex @option{-gnatS} (@command{gcc})
3976 Print package Standard.
3979 @cindex @option{-gnatt} (@command{gcc})
3980 Generate tree output file.
3982 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
3983 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
3984 All compiler tables start at @var{nnn} times usual starting size.
3987 @cindex @option{-gnatu} (@command{gcc})
3988 List units for this compilation.
3991 @cindex @option{-gnatU} (@command{gcc})
3992 Tag all error messages with the unique string ``error:''
3995 @cindex @option{-gnatv} (@command{gcc})
3996 Verbose mode. Full error output with source lines to @file{stdout}.
3999 @cindex @option{-gnatV} (@command{gcc})
4000 Control level of validity checking. See separate section describing
4003 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}[,...])^
4004 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4006 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4007 the exact warnings that
4008 are enabled or disabled (@pxref{Warning Message Control}).
4010 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4011 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4012 Wide character encoding method
4014 (@var{e}=n/h/u/s/e/8).
4017 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4021 @cindex @option{-gnatx} (@command{gcc})
4022 Suppress generation of cross-reference information.
4024 @item ^-gnaty^/STYLE_CHECKS=(option,option..)^
4025 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4026 Enable built-in style checks (@pxref{Style Checking}).
4028 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4029 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4030 Distribution stub generation and compilation
4032 (@var{m}=r/c for receiver/caller stubs).
4035 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4036 to be generated and compiled).
4039 @item ^-I^/SEARCH=^@var{dir}
4040 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4042 Direct GNAT to search the @var{dir} directory for source files needed by
4043 the current compilation
4044 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4046 @item ^-I-^/NOCURRENT_DIRECTORY^
4047 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4049 Except for the source file named in the command line, do not look for source
4050 files in the directory containing the source file named in the command line
4051 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4055 @cindex @option{-mbig-switch} (@command{gcc})
4056 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4057 This standard gcc switch causes the compiler to use larger offsets in its
4058 jump table representation for @code{case} statements.
4059 This may result in less efficient code, but is sometimes necessary
4060 (for example on HP-UX targets)
4061 @cindex HP-UX and @option{-mbig-switch} option
4062 in order to compile large and/or nested @code{case} statements.
4065 @cindex @option{-o} (@command{gcc})
4066 This switch is used in @command{gcc} to redirect the generated object file
4067 and its associated ALI file. Beware of this switch with GNAT, because it may
4068 cause the object file and ALI file to have different names which in turn
4069 may confuse the binder and the linker.
4073 @cindex @option{-nostdinc} (@command{gcc})
4074 Inhibit the search of the default location for the GNAT Run Time
4075 Library (RTL) source files.
4078 @cindex @option{-nostdlib} (@command{gcc})
4079 Inhibit the search of the default location for the GNAT Run Time
4080 Library (RTL) ALI files.
4084 @cindex @option{-O} (@command{gcc})
4085 @var{n} controls the optimization level.
4089 No optimization, the default setting if no @option{-O} appears
4092 Normal optimization, the default if you specify @option{-O} without
4093 an operand. A good compromise between code quality and compilation
4097 Extensive optimization, may improve execution time, possibly at the cost of
4098 substantially increased compilation time.
4105 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4106 Equivalent to @option{/OPTIMIZE=NONE}.
4107 This is the default behavior in the absence of an @option{/OPTMIZE}
4110 @item /OPTIMIZE[=(keyword[,...])]
4111 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4112 Selects the level of optimization for your program. The supported
4113 keywords are as follows:
4116 Perform most optimizations, including those that
4118 This is the default if the @option{/OPTMIZE} qualifier is supplied
4119 without keyword options.
4122 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4125 Perform some optimizations, but omit ones that are costly.
4128 Same as @code{SOME}.
4131 Try to unroll loops. This keyword may be specified together with
4132 any keyword above other than @code{NONE}. Loop unrolling
4133 usually, but not always, improves the performance of programs.
4138 @item -pass-exit-codes
4139 @cindex @option{-pass-exit-codes} (@command{gcc})
4140 Catch exit codes from the compiler and use the most meaningful as
4144 @item --RTS=@var{rts-path}
4145 @cindex @option{--RTS} (@command{gcc})
4146 Specifies the default location of the runtime library. Same meaning as the
4147 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4150 @cindex @option{^-S^/ASM^} (@command{gcc})
4151 ^Used in place of @option{-c} to^Used to^
4152 cause the assembler source file to be
4153 generated, using @file{^.s^.S^} as the extension,
4154 instead of the object file.
4155 This may be useful if you need to examine the generated assembly code.
4157 @item ^-fverbose-asm^/VERBOSE_ASM^
4158 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4159 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4160 to cause the generated assembly code file to be annotated with variable
4161 names, making it significantly easier to follow.
4164 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4165 Show commands generated by the @command{gcc} driver. Normally used only for
4166 debugging purposes or if you need to be sure what version of the
4167 compiler you are executing.
4171 @cindex @option{-V} (@command{gcc})
4172 Execute @var{ver} version of the compiler. This is the @command{gcc}
4173 version, not the GNAT version.
4176 @item ^-w^NO_BACK_END_WARNINGS^
4177 @cindex @option{-w} (@command{gcc})
4178 Turn off warnings generated by the back end of the compiler. Use of
4179 this switch also causes the default for front end warnings to be set
4180 to suppress (as though @option{-gnatws} had appeared at the start of
4186 @c Combining qualifiers does not work on VMS
4187 You may combine a sequence of GNAT switches into a single switch. For
4188 example, the combined switch
4190 @cindex Combining GNAT switches
4196 is equivalent to specifying the following sequence of switches:
4199 -gnato -gnatf -gnati3
4204 The following restrictions apply to the combination of switches
4209 The switch @option{-gnatc} if combined with other switches must come
4210 first in the string.
4213 The switch @option{-gnats} if combined with other switches must come
4214 first in the string.
4218 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4219 may not be combined with any other switches.
4223 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4224 switch), then all further characters in the switch are interpreted
4225 as style modifiers (see description of @option{-gnaty}).
4228 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4229 switch), then all further characters in the switch are interpreted
4230 as debug flags (see description of @option{-gnatd}).
4233 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4234 switch), then all further characters in the switch are interpreted
4235 as warning mode modifiers (see description of @option{-gnatw}).
4238 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4239 switch), then all further characters in the switch are interpreted
4240 as validity checking options (see description of @option{-gnatV}).
4244 @node Output and Error Message Control
4245 @subsection Output and Error Message Control
4249 The standard default format for error messages is called ``brief format''.
4250 Brief format messages are written to @file{stderr} (the standard error
4251 file) and have the following form:
4254 e.adb:3:04: Incorrect spelling of keyword "function"
4255 e.adb:4:20: ";" should be "is"
4259 The first integer after the file name is the line number in the file,
4260 and the second integer is the column number within the line.
4261 @code{glide} can parse the error messages
4262 and point to the referenced character.
4263 The following switches provide control over the error message
4269 @cindex @option{-gnatv} (@command{gcc})
4272 The v stands for verbose.
4274 The effect of this setting is to write long-format error
4275 messages to @file{stdout} (the standard output file.
4276 The same program compiled with the
4277 @option{-gnatv} switch would generate:
4281 3. funcion X (Q : Integer)
4283 >>> Incorrect spelling of keyword "function"
4286 >>> ";" should be "is"
4291 The vertical bar indicates the location of the error, and the @samp{>>>}
4292 prefix can be used to search for error messages. When this switch is
4293 used the only source lines output are those with errors.
4296 @cindex @option{-gnatl} (@command{gcc})
4298 The @code{l} stands for list.
4300 This switch causes a full listing of
4301 the file to be generated. The output might look as follows:
4307 3. funcion X (Q : Integer)
4309 >>> Incorrect spelling of keyword "function"
4312 >>> ";" should be "is"
4324 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4325 standard output is redirected, a brief summary is written to
4326 @file{stderr} (standard error) giving the number of error messages and
4327 warning messages generated.
4330 @cindex @option{-gnatU} (@command{gcc})
4331 This switch forces all error messages to be preceded by the unique
4332 string ``error:''. This means that error messages take a few more
4333 characters in space, but allows easy searching for and identification
4337 @cindex @option{-gnatb} (@command{gcc})
4339 The @code{b} stands for brief.
4341 This switch causes GNAT to generate the
4342 brief format error messages to @file{stderr} (the standard error
4343 file) as well as the verbose
4344 format message or full listing (which as usual is written to
4345 @file{stdout} (the standard output file).
4347 @item -gnatm=@var{n}
4348 @cindex @option{-gnatm} (@command{gcc})
4350 The @code{m} stands for maximum.
4352 @var{n} is a decimal integer in the
4353 range of 1 to 999 and limits the number of error messages to be
4354 generated. For example, using @option{-gnatm2} might yield
4357 e.adb:3:04: Incorrect spelling of keyword "function"
4358 e.adb:5:35: missing ".."
4359 fatal error: maximum errors reached
4360 compilation abandoned
4364 Note that the equal sign is optional, so the switches
4365 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4368 @cindex @option{-gnatf} (@command{gcc})
4369 @cindex Error messages, suppressing
4371 The @code{f} stands for full.
4373 Normally, the compiler suppresses error messages that are likely to be
4374 redundant. This switch causes all error
4375 messages to be generated. In particular, in the case of
4376 references to undefined variables. If a given variable is referenced
4377 several times, the normal format of messages is
4379 e.adb:7:07: "V" is undefined (more references follow)
4383 where the parenthetical comment warns that there are additional
4384 references to the variable @code{V}. Compiling the same program with the
4385 @option{-gnatf} switch yields
4388 e.adb:7:07: "V" is undefined
4389 e.adb:8:07: "V" is undefined
4390 e.adb:8:12: "V" is undefined
4391 e.adb:8:16: "V" is undefined
4392 e.adb:9:07: "V" is undefined
4393 e.adb:9:12: "V" is undefined
4397 The @option{-gnatf} switch also generates additional information for
4398 some error messages. Some examples are:
4402 Full details on entities not available in high integrity mode
4404 Details on possibly non-portable unchecked conversion
4406 List possible interpretations for ambiguous calls
4408 Additional details on incorrect parameters
4412 @cindex @option{-gnatq} (@command{gcc})
4414 The @code{q} stands for quit (really ``don't quit'').
4416 In normal operation mode, the compiler first parses the program and
4417 determines if there are any syntax errors. If there are, appropriate
4418 error messages are generated and compilation is immediately terminated.
4420 GNAT to continue with semantic analysis even if syntax errors have been
4421 found. This may enable the detection of more errors in a single run. On
4422 the other hand, the semantic analyzer is more likely to encounter some
4423 internal fatal error when given a syntactically invalid tree.
4426 @cindex @option{-gnatQ} (@command{gcc})
4427 In normal operation mode, the @file{ALI} file is not generated if any
4428 illegalities are detected in the program. The use of @option{-gnatQ} forces
4429 generation of the @file{ALI} file. This file is marked as being in
4430 error, so it cannot be used for binding purposes, but it does contain
4431 reasonably complete cross-reference information, and thus may be useful
4432 for use by tools (e.g. semantic browsing tools or integrated development
4433 environments) that are driven from the @file{ALI} file. This switch
4434 implies @option{-gnatq}, since the semantic phase must be run to get a
4435 meaningful ALI file.
4437 In addition, if @option{-gnatt} is also specified, then the tree file is
4438 generated even if there are illegalities. It may be useful in this case
4439 to also specify @option{-gnatq} to ensure that full semantic processing
4440 occurs. The resulting tree file can be processed by ASIS, for the purpose
4441 of providing partial information about illegal units, but if the error
4442 causes the tree to be badly malformed, then ASIS may crash during the
4445 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4446 being in error, @command{gnatmake} will attempt to recompile the source when it
4447 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4449 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4450 since ALI files are never generated if @option{-gnats} is set.
4454 @node Warning Message Control
4455 @subsection Warning Message Control
4456 @cindex Warning messages
4458 In addition to error messages, which correspond to illegalities as defined
4459 in the Ada 95 Reference Manual, the compiler detects two kinds of warning
4462 First, the compiler considers some constructs suspicious and generates a
4463 warning message to alert you to a possible error. Second, if the
4464 compiler detects a situation that is sure to raise an exception at
4465 run time, it generates a warning message. The following shows an example
4466 of warning messages:
4468 e.adb:4:24: warning: creation of object may raise Storage_Error
4469 e.adb:10:17: warning: static value out of range
4470 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4474 GNAT considers a large number of situations as appropriate
4475 for the generation of warning messages. As always, warnings are not
4476 definite indications of errors. For example, if you do an out-of-range
4477 assignment with the deliberate intention of raising a
4478 @code{Constraint_Error} exception, then the warning that may be
4479 issued does not indicate an error. Some of the situations for which GNAT
4480 issues warnings (at least some of the time) are given in the following
4481 list. This list is not complete, and new warnings are often added to
4482 subsequent versions of GNAT. The list is intended to give a general idea
4483 of the kinds of warnings that are generated.
4487 Possible infinitely recursive calls
4490 Out-of-range values being assigned
4493 Possible order of elaboration problems
4499 Fixed-point type declarations with a null range
4502 Direct_IO or Sequential_IO instantiated with a type that has access values
4505 Variables that are never assigned a value
4508 Variables that are referenced before being initialized
4511 Task entries with no corresponding @code{accept} statement
4514 Duplicate accepts for the same task entry in a @code{select}
4517 Objects that take too much storage
4520 Unchecked conversion between types of differing sizes
4523 Missing @code{return} statement along some execution path in a function
4526 Incorrect (unrecognized) pragmas
4529 Incorrect external names
4532 Allocation from empty storage pool
4535 Potentially blocking operation in protected type
4538 Suspicious parenthesization of expressions
4541 Mismatching bounds in an aggregate
4544 Attempt to return local value by reference
4547 Premature instantiation of a generic body
4550 Attempt to pack aliased components
4553 Out of bounds array subscripts
4556 Wrong length on string assignment
4559 Violations of style rules if style checking is enabled
4562 Unused @code{with} clauses
4565 @code{Bit_Order} usage that does not have any effect
4568 @code{Standard.Duration} used to resolve universal fixed expression
4571 Dereference of possibly null value
4574 Declaration that is likely to cause storage error
4577 Internal GNAT unit @code{with}'ed by application unit
4580 Values known to be out of range at compile time
4583 Unreferenced labels and variables
4586 Address overlays that could clobber memory
4589 Unexpected initialization when address clause present
4592 Bad alignment for address clause
4595 Useless type conversions
4598 Redundant assignment statements and other redundant constructs
4601 Useless exception handlers
4604 Accidental hiding of name by child unit
4607 Access before elaboration detected at compile time
4610 A range in a @code{for} loop that is known to be null or might be null
4615 The following switches are available to control the handling of
4621 @emph{Activate all optional errors.}
4622 @cindex @option{-gnatwa} (@command{gcc})
4623 This switch activates most optional warning messages, see remaining list
4624 in this section for details on optional warning messages that can be
4625 individually controlled. The warnings that are not turned on by this
4627 @option{-gnatwd} (implicit dereferencing),
4628 @option{-gnatwh} (hiding),
4629 and @option{-gnatwl} (elaboration warnings).
4630 All other optional warnings are turned on.
4633 @emph{Suppress all optional errors.}
4634 @cindex @option{-gnatwA} (@command{gcc})
4635 This switch suppresses all optional warning messages, see remaining list
4636 in this section for details on optional warning messages that can be
4637 individually controlled.
4640 @emph{Activate warnings on bad fixed values.}
4641 @cindex @option{-gnatwb} (@command{gcc})
4642 @cindex Bad fixed values
4643 @cindex Fixed-point Small value
4645 This switch activates warnings for static fixed-point expressions whose
4646 value is not an exact multiple of Small. Such values are implementation
4647 dependent, since an implementation is free to choose either of the multiples
4648 that surround the value. GNAT always chooses the closer one, but this is not
4649 required behavior, and it is better to specify a value that is an exact
4650 multiple, ensuring predictable execution. The default is that such warnings
4654 @emph{Suppress warnings on bad fixed values.}
4655 @cindex @option{-gnatwB} (@command{gcc})
4656 This switch suppresses warnings for static fixed-point expressions whose
4657 value is not an exact multiple of Small.
4660 @emph{Activate warnings on conditionals.}
4661 @cindex @option{-gnatwc} (@command{gcc})
4662 @cindex Conditionals, constant
4663 This switch activates warnings for conditional expressions used in
4664 tests that are known to be True or False at compile time. The default
4665 is that such warnings are not generated.
4666 Note that this warning does
4667 not get issued for the use of boolean variables or constants whose
4668 values are known at compile time, since this is a standard technique
4669 for conditional compilation in Ada, and this would generate too many
4670 ``false positive'' warnings.
4672 This warning option also activates a special test for comparisons using
4673 the operators ``>='' and`` <=''.
4674 If the compiler can tell that only the equality condition is possible,
4675 then it will warn that the ``>'' or ``<'' part of the test
4676 is useless and that the operator could be replaced by ``=''.
4677 An example would be comparing a @code{Natural} variable <= 0.
4679 This warning can also be turned on using @option{-gnatwa}.
4682 @emph{Suppress warnings on conditionals.}
4683 @cindex @option{-gnatwC} (@command{gcc})
4684 This switch suppresses warnings for conditional expressions used in
4685 tests that are known to be True or False at compile time.
4688 @emph{Activate warnings on implicit dereferencing.}
4689 @cindex @option{-gnatwd} (@command{gcc})
4690 If this switch is set, then the use of a prefix of an access type
4691 in an indexed component, slice, or selected component without an
4692 explicit @code{.all} will generate a warning. With this warning
4693 enabled, access checks occur only at points where an explicit
4694 @code{.all} appears in the source code (assuming no warnings are
4695 generated as a result of this switch). The default is that such
4696 warnings are not generated.
4697 Note that @option{-gnatwa} does not affect the setting of
4698 this warning option.
4701 @emph{Suppress warnings on implicit dereferencing.}
4702 @cindex @option{-gnatwD} (@command{gcc})
4703 @cindex Implicit dereferencing
4704 @cindex Dereferencing, implicit
4705 This switch suppresses warnings for implicit dereferences in
4706 indexed components, slices, and selected components.
4709 @emph{Treat warnings as errors.}
4710 @cindex @option{-gnatwe} (@command{gcc})
4711 @cindex Warnings, treat as error
4712 This switch causes warning messages to be treated as errors.
4713 The warning string still appears, but the warning messages are counted
4714 as errors, and prevent the generation of an object file.
4717 @emph{Activate warnings on unreferenced formals.}
4718 @cindex @option{-gnatwf} (@command{gcc})
4719 @cindex Formals, unreferenced
4720 This switch causes a warning to be generated if a formal parameter
4721 is not referenced in the body of the subprogram. This warning can
4722 also be turned on using @option{-gnatwa} or @option{-gnatwu}.
4725 @emph{Suppress warnings on unreferenced formals.}
4726 @cindex @option{-gnatwF} (@command{gcc})
4727 This switch suppresses warnings for unreferenced formal
4728 parameters. Note that the
4729 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4730 effect of warning on unreferenced entities other than subprogram
4734 @emph{Activate warnings on unrecognized pragmas.}
4735 @cindex @option{-gnatwg} (@command{gcc})
4736 @cindex Pragmas, unrecognized
4737 This switch causes a warning to be generated if an unrecognized
4738 pragma is encountered. Apart from issuing this warning, the
4739 pragma is ignored and has no effect. This warning can
4740 also be turned on using @option{-gnatwa}. The default
4741 is that such warnings are issued (satisfying the Ada Reference
4742 Manual requirement that such warnings appear).
4745 @emph{Suppress warnings on unrecognized pragmas.}
4746 @cindex @option{-gnatwG} (@command{gcc})
4747 This switch suppresses warnings for unrecognized pragmas.
4750 @emph{Activate warnings on hiding.}
4751 @cindex @option{-gnatwh} (@command{gcc})
4752 @cindex Hiding of Declarations
4753 This switch activates warnings on hiding declarations.
4754 A declaration is considered hiding
4755 if it is for a non-overloadable entity, and it declares an entity with the
4756 same name as some other entity that is directly or use-visible. The default
4757 is that such warnings are not generated.
4758 Note that @option{-gnatwa} does not affect the setting of this warning option.
4761 @emph{Suppress warnings on hiding.}
4762 @cindex @option{-gnatwH} (@command{gcc})
4763 This switch suppresses warnings on hiding declarations.
4766 @emph{Activate warnings on implementation units.}
4767 @cindex @option{-gnatwi} (@command{gcc})
4768 This switch activates warnings for a @code{with} of an internal GNAT
4769 implementation unit, defined as any unit from the @code{Ada},
4770 @code{Interfaces}, @code{GNAT},
4771 ^^@code{DEC},^ or @code{System}
4772 hierarchies that is not
4773 documented in either the Ada Reference Manual or the GNAT
4774 Programmer's Reference Manual. Such units are intended only
4775 for internal implementation purposes and should not be @code{with}'ed
4776 by user programs. The default is that such warnings are generated
4777 This warning can also be turned on using @option{-gnatwa}.
4780 @emph{Disable warnings on implementation units.}
4781 @cindex @option{-gnatwI} (@command{gcc})
4782 This switch disables warnings for a @code{with} of an internal GNAT
4783 implementation unit.
4786 @emph{Activate warnings on obsolescent features (Annex J).}
4787 @cindex @option{-gnatwj} (@command{gcc})
4788 @cindex Features, obsolescent
4789 @cindex Obsolescent features
4790 If this warning option is activated, then warnings are generated for
4791 calls to subprograms marked with @code{pragma Obsolescent} and
4792 for use of features in Annex J of the Ada Reference Manual. In the
4793 case of Annex J, not all features are flagged. In particular use
4794 of the renamed packages (like @code{Text_IO}) and use of package
4795 @code{ASCII} are not flagged, since these are very common and
4796 would generate many annoying positive warnings. The default is that
4797 such warnings are not generated.
4799 In addition to the above cases, warnings are also generated for
4800 GNAT features that have been provided in past versions but which
4801 have been superseded (typically by features in the new Ada standard).
4802 For example, @code{pragma Ravenscar} will be flagged since its
4803 function is replaced by @code{pragma Profile(Ravenscar)}.
4805 Note that this warning option functions differently from the
4806 restriction @code{No_Obsolescent_Features} in two respects.
4807 First, the restriction applies only to annex J features.
4808 Second, the restriction does flag uses of package @code{ASCII}.
4811 @emph{Suppress warnings on obsolescent features (Annex J).}
4812 @cindex @option{-gnatwJ} (@command{gcc})
4813 This switch disables warnings on use of obsolescent features.
4816 @emph{Activate warnings on variables that could be constants.}
4817 @cindex @option{-gnatwk} (@command{gcc})
4818 This switch activates warnings for variables that are initialized but
4819 never modified, and then could be declared constants.
4822 @emph{Suppress warnings on variables that could be constants.}
4823 @cindex @option{-gnatwK} (@command{gcc})
4824 This switch disables warnings on variables that could be declared constants.
4827 @emph{Activate warnings for missing elaboration pragmas.}
4828 @cindex @option{-gnatwl} (@command{gcc})
4829 @cindex Elaboration, warnings
4830 This switch activates warnings on missing
4831 @code{Elaborate_All} and @code{Elaborate} pragmas.
4832 See the section in this guide on elaboration checking for details on
4833 when such pragmas should be used. Warnings are also generated if you
4834 are using the static mode of elaboration, and a @code{pragma Elaborate}
4835 is encountered. The default is that such warnings
4837 This warning is not automatically turned on by the use of @option{-gnatwa}.
4840 @emph{Suppress warnings for missing elaboration pragmas.}
4841 @cindex @option{-gnatwL} (@command{gcc})
4842 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
4843 See the section in this guide on elaboration checking for details on
4844 when such pragmas should be used.
4847 @emph{Activate warnings on modified but unreferenced variables.}
4848 @cindex @option{-gnatwm} (@command{gcc})
4849 This switch activates warnings for variables that are assigned (using
4850 an initialization value or with one or more assignment statements) but
4851 whose value is never read. The warning is suppressed for volatile
4852 variables and also for variables that are renamings of other variables
4853 or for which an address clause is given.
4854 This warning can also be turned on using @option{-gnatwa}.
4857 @emph{Disable warnings on modified but unreferenced variables.}
4858 @cindex @option{-gnatwM} (@command{gcc})
4859 This switch disables warnings for variables that are assigned or
4860 initialized, but never read.
4863 @emph{Set normal warnings mode.}
4864 @cindex @option{-gnatwn} (@command{gcc})
4865 This switch sets normal warning mode, in which enabled warnings are
4866 issued and treated as warnings rather than errors. This is the default
4867 mode. the switch @option{-gnatwn} can be used to cancel the effect of
4868 an explicit @option{-gnatws} or
4869 @option{-gnatwe}. It also cancels the effect of the
4870 implicit @option{-gnatwe} that is activated by the
4871 use of @option{-gnatg}.
4874 @emph{Activate warnings on address clause overlays.}
4875 @cindex @option{-gnatwo} (@command{gcc})
4876 @cindex Address Clauses, warnings
4877 This switch activates warnings for possibly unintended initialization
4878 effects of defining address clauses that cause one variable to overlap
4879 another. The default is that such warnings are generated.
4880 This warning can also be turned on using @option{-gnatwa}.
4883 @emph{Suppress warnings on address clause overlays.}
4884 @cindex @option{-gnatwO} (@command{gcc})
4885 This switch suppresses warnings on possibly unintended initialization
4886 effects of defining address clauses that cause one variable to overlap
4890 @emph{Activate warnings on ineffective pragma Inlines.}
4891 @cindex @option{-gnatwp} (@command{gcc})
4892 @cindex Inlining, warnings
4893 This switch activates warnings for failure of front end inlining
4894 (activated by @option{-gnatN}) to inline a particular call. There are
4895 many reasons for not being able to inline a call, including most
4896 commonly that the call is too complex to inline.
4897 This warning can also be turned on using @option{-gnatwa}.
4900 @emph{Suppress warnings on ineffective pragma Inlines.}
4901 @cindex @option{-gnatwP} (@command{gcc})
4902 This switch suppresses warnings on ineffective pragma Inlines. If the
4903 inlining mechanism cannot inline a call, it will simply ignore the
4907 @emph{Activate warnings on redundant constructs.}
4908 @cindex @option{-gnatwr} (@command{gcc})
4909 This switch activates warnings for redundant constructs. The following
4910 is the current list of constructs regarded as redundant:
4911 This warning can also be turned on using @option{-gnatwa}.
4915 Assignment of an item to itself.
4917 Type conversion that converts an expression to its own type.
4919 Use of the attribute @code{Base} where @code{typ'Base} is the same
4922 Use of pragma @code{Pack} when all components are placed by a record
4923 representation clause.
4925 Exception handler containing only a reraise statement (raise with no
4926 operand) which has no effect.
4928 Use of the operator abs on an operand that is known at compile time
4931 Comparison of boolean expressions to an explicit True value.
4935 @emph{Suppress warnings on redundant constructs.}
4936 @cindex @option{-gnatwR} (@command{gcc})
4937 This switch suppresses warnings for redundant constructs.
4940 @emph{Suppress all warnings.}
4941 @cindex @option{-gnatws} (@command{gcc})
4942 This switch completely suppresses the
4943 output of all warning messages from the GNAT front end.
4944 Note that it does not suppress warnings from the @command{gcc} back end.
4945 To suppress these back end warnings as well, use the switch @option{-w}
4946 in addition to @option{-gnatws}.
4949 @emph{Activate warnings on unused entities.}
4950 @cindex @option{-gnatwu} (@command{gcc})
4951 This switch activates warnings to be generated for entities that
4952 are declared but not referenced, and for units that are @code{with}'ed
4954 referenced. In the case of packages, a warning is also generated if
4955 no entities in the package are referenced. This means that if the package
4956 is referenced but the only references are in @code{use}
4957 clauses or @code{renames}
4958 declarations, a warning is still generated. A warning is also generated
4959 for a generic package that is @code{with}'ed but never instantiated.
4960 In the case where a package or subprogram body is compiled, and there
4961 is a @code{with} on the corresponding spec
4962 that is only referenced in the body,
4963 a warning is also generated, noting that the
4964 @code{with} can be moved to the body. The default is that
4965 such warnings are not generated.
4966 This switch also activates warnings on unreferenced formals
4967 (it includes the effect of @option{-gnatwf}).
4968 This warning can also be turned on using @option{-gnatwa}.
4971 @emph{Suppress warnings on unused entities.}
4972 @cindex @option{-gnatwU} (@command{gcc})
4973 This switch suppresses warnings for unused entities and packages.
4974 It also turns off warnings on unreferenced formals (and thus includes
4975 the effect of @option{-gnatwF}).
4978 @emph{Activate warnings on unassigned variables.}
4979 @cindex @option{-gnatwv} (@command{gcc})
4980 @cindex Unassigned variable warnings
4981 This switch activates warnings for access to variables which
4982 may not be properly initialized. The default is that
4983 such warnings are generated.
4986 @emph{Suppress warnings on unassigned variables.}
4987 @cindex @option{-gnatwV} (@command{gcc})
4988 This switch suppresses warnings for access to variables which
4989 may not be properly initialized.
4992 @emph{Activate warnings for Ada 2005 compatibility issues.}
4993 @cindex @option{-gnatwy} (@command{gcc})
4994 @cindex Ada 2005 compatibility issues warnings
4995 For the most part Ada 2005 is upwards compatible with Ada 95,
4996 but there are some exceptions (for example the fact that
4997 @code{interface} is now a reserved word in Ada 2005). This
4998 switch activates several warnings to help in identifying
4999 and correcting such incompatibilities. The default is that
5000 these warnings are generated. Note that at one point Ada 2005
5001 was called Ada 0Y, hence the choice of character.
5004 @emph{Disable warnings for Ada 2005 compatibility issues.}
5005 @cindex @option{-gnatwY} (@command{gcc})
5006 @cindex Ada 2005 compatibility issues warnings
5007 This switch suppresses several warnings intended to help in identifying
5008 incompatibilities between Ada 95 and Ada 2005.
5011 @emph{Activate warnings on Export/Import pragmas.}
5012 @cindex @option{-gnatwx} (@command{gcc})
5013 @cindex Export/Import pragma warnings
5014 This switch activates warnings on Export/Import pragmas when
5015 the compiler detects a possible conflict between the Ada and
5016 foreign language calling sequences. For example, the use of
5017 default parameters in a convention C procedure is dubious
5018 because the C compiler cannot supply the proper default, so
5019 a warning is issued. The default is that such warnings are
5023 @emph{Suppress warnings on Export/Import pragmas.}
5024 @cindex @option{-gnatwX} (@command{gcc})
5025 This switch suppresses warnings on Export/Import pragmas.
5026 The sense of this is that you are telling the compiler that
5027 you know what you are doing in writing the pragma, and it
5028 should not complain at you.
5031 @emph{Activate warnings on unchecked conversions.}
5032 @cindex @option{-gnatwz} (@command{gcc})
5033 @cindex Unchecked_Conversion warnings
5034 This switch activates warnings for unchecked conversions
5035 where the types are known at compile time to have different
5037 is that such warnings are generated.
5040 @emph{Suppress warnings on unchecked conversions.}
5041 @cindex @option{-gnatwZ} (@command{gcc})
5042 This switch suppresses warnings for unchecked conversions
5043 where the types are known at compile time to have different
5046 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5047 @cindex @option{-Wuninitialized}
5048 The warnings controlled by the @option{-gnatw} switch are generated by the
5049 front end of the compiler. In some cases, the @option{^gcc^GCC^} back end
5050 can provide additional warnings. One such useful warning is provided by
5051 @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^}. This must be used in
5052 conjunction with turning on optimization mode. This causes the flow
5053 analysis circuits of the back end optimizer to output additional
5054 warnings about uninitialized variables.
5056 @item ^-w^/NO_BACK_END_WARNINGS^
5058 This switch suppresses warnings from the @option{^gcc^GCC^} back end. The
5059 code generator detects a number of warning situations that are missed
5060 by the @option{GNAT} front end, and this switch can be used to suppress them.
5061 The use of this switch also sets the default front end warning mode to
5062 @option{-gnatws}, that is, front end warnings suppressed as well.
5068 A string of warning parameters can be used in the same parameter. For example:
5075 will turn on all optional warnings except for elaboration pragma warnings,
5076 and also specify that warnings should be treated as errors.
5078 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5103 @node Debugging and Assertion Control
5104 @subsection Debugging and Assertion Control
5108 @cindex @option{-gnata} (@command{gcc})
5114 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5115 are ignored. This switch, where @samp{a} stands for assert, causes
5116 @code{Assert} and @code{Debug} pragmas to be activated.
5118 The pragmas have the form:
5122 @b{pragma} Assert (@var{Boolean-expression} [,
5123 @var{static-string-expression}])
5124 @b{pragma} Debug (@var{procedure call})
5129 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5130 If the result is @code{True}, the pragma has no effect (other than
5131 possible side effects from evaluating the expression). If the result is
5132 @code{False}, the exception @code{Assert_Failure} declared in the package
5133 @code{System.Assertions} is
5134 raised (passing @var{static-string-expression}, if present, as the
5135 message associated with the exception). If no string expression is
5136 given the default is a string giving the file name and line number
5139 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5140 @code{pragma Debug} may appear within a declaration sequence, allowing
5141 debugging procedures to be called between declarations.
5144 @item /DEBUG[=debug-level]
5146 Specifies how much debugging information is to be included in
5147 the resulting object file where 'debug-level' is one of the following:
5150 Include both debugger symbol records and traceback
5152 This is the default setting.
5154 Include both debugger symbol records and traceback in
5157 Excludes both debugger symbol records and traceback
5158 the object file. Same as /NODEBUG.
5160 Includes only debugger symbol records in the object
5161 file. Note that this doesn't include traceback information.
5166 @node Validity Checking
5167 @subsection Validity Checking
5168 @findex Validity Checking
5171 The Ada 95 Reference Manual has specific requirements for checking
5172 for invalid values. In particular, RM 13.9.1 requires that the
5173 evaluation of invalid values (for example from unchecked conversions),
5174 not result in erroneous execution. In GNAT, the result of such an
5175 evaluation in normal default mode is to either use the value
5176 unmodified, or to raise Constraint_Error in those cases where use
5177 of the unmodified value would cause erroneous execution. The cases
5178 where unmodified values might lead to erroneous execution are case
5179 statements (where a wild jump might result from an invalid value),
5180 and subscripts on the left hand side (where memory corruption could
5181 occur as a result of an invalid value).
5183 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5186 The @code{x} argument is a string of letters that
5187 indicate validity checks that are performed or not performed in addition
5188 to the default checks described above.
5191 The options allowed for this qualifier
5192 indicate validity checks that are performed or not performed in addition
5193 to the default checks described above.
5199 @emph{All validity checks.}
5200 @cindex @option{-gnatVa} (@command{gcc})
5201 All validity checks are turned on.
5203 That is, @option{-gnatVa} is
5204 equivalent to @option{gnatVcdfimorst}.
5208 @emph{Validity checks for copies.}
5209 @cindex @option{-gnatVc} (@command{gcc})
5210 The right hand side of assignments, and the initializing values of
5211 object declarations are validity checked.
5214 @emph{Default (RM) validity checks.}
5215 @cindex @option{-gnatVd} (@command{gcc})
5216 Some validity checks are done by default following normal Ada semantics
5218 A check is done in case statements that the expression is within the range
5219 of the subtype. If it is not, Constraint_Error is raised.
5220 For assignments to array components, a check is done that the expression used
5221 as index is within the range. If it is not, Constraint_Error is raised.
5222 Both these validity checks may be turned off using switch @option{-gnatVD}.
5223 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5224 switch @option{-gnatVd} will leave the checks turned on.
5225 Switch @option{-gnatVD} should be used only if you are sure that all such
5226 expressions have valid values. If you use this switch and invalid values
5227 are present, then the program is erroneous, and wild jumps or memory
5228 overwriting may occur.
5231 @emph{Validity checks for floating-point values.}
5232 @cindex @option{-gnatVf} (@command{gcc})
5233 In the absence of this switch, validity checking occurs only for discrete
5234 values. If @option{-gnatVf} is specified, then validity checking also applies
5235 for floating-point values, and NaN's and infinities are considered invalid,
5236 as well as out of range values for constrained types. Note that this means
5237 that standard @code{IEEE} infinity mode is not allowed. The exact contexts
5238 in which floating-point values are checked depends on the setting of other
5239 options. For example,
5240 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5241 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5242 (the order does not matter) specifies that floating-point parameters of mode
5243 @code{in} should be validity checked.
5246 @emph{Validity checks for @code{in} mode parameters}
5247 @cindex @option{-gnatVi} (@command{gcc})
5248 Arguments for parameters of mode @code{in} are validity checked in function
5249 and procedure calls at the point of call.
5252 @emph{Validity checks for @code{in out} mode parameters.}
5253 @cindex @option{-gnatVm} (@command{gcc})
5254 Arguments for parameters of mode @code{in out} are validity checked in
5255 procedure calls at the point of call. The @code{'m'} here stands for
5256 modify, since this concerns parameters that can be modified by the call.
5257 Note that there is no specific option to test @code{out} parameters,
5258 but any reference within the subprogram will be tested in the usual
5259 manner, and if an invalid value is copied back, any reference to it
5260 will be subject to validity checking.
5263 @emph{No validity checks.}
5264 @cindex @option{-gnatVn} (@command{gcc})
5265 This switch turns off all validity checking, including the default checking
5266 for case statements and left hand side subscripts. Note that the use of
5267 the switch @option{-gnatp} suppresses all run-time checks, including
5268 validity checks, and thus implies @option{-gnatVn}. When this switch
5269 is used, it cancels any other @option{-gnatV} previously issued.
5272 @emph{Validity checks for operator and attribute operands.}
5273 @cindex @option{-gnatVo} (@command{gcc})
5274 Arguments for predefined operators and attributes are validity checked.
5275 This includes all operators in package @code{Standard},
5276 the shift operators defined as intrinsic in package @code{Interfaces}
5277 and operands for attributes such as @code{Pos}. Checks are also made
5278 on individual component values for composite comparisons, and on the
5279 expressions in type conversions and qualified expressions.
5282 @emph{Validity checks for parameters.}
5283 @cindex @option{-gnatVp} (@command{gcc})
5284 This controls the treatment of parameters within a subprogram (as opposed
5285 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5286 of parameters on a call. If either of these call options is used, then
5287 normally an assumption is made within a subprogram that the input arguments
5288 have been validity checking at the point of call, and do not need checking
5289 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5290 is not made, and parameters are not assumed to be valid, so their validity
5291 will be checked (or rechecked) within the subprogram.
5294 @emph{Validity checks for function returns.}
5295 @cindex @option{-gnatVr} (@command{gcc})
5296 The expression in @code{return} statements in functions is validity
5300 @emph{Validity checks for subscripts.}
5301 @cindex @option{-gnatVs} (@command{gcc})
5302 All subscripts expressions are checked for validity, whether they appear
5303 on the right side or left side (in default mode only left side subscripts
5304 are validity checked).
5307 @emph{Validity checks for tests.}
5308 @cindex @option{-gnatVt} (@command{gcc})
5309 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5310 statements are checked, as well as guard expressions in entry calls.
5315 The @option{-gnatV} switch may be followed by
5316 ^a string of letters^a list of options^
5317 to turn on a series of validity checking options.
5319 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5320 specifies that in addition to the default validity checking, copies and
5321 function return expressions are to be validity checked.
5322 In order to make it easier
5323 to specify the desired combination of effects,
5325 the upper case letters @code{CDFIMORST} may
5326 be used to turn off the corresponding lower case option.
5329 the prefix @code{NO} on an option turns off the corresponding validity
5332 @item @code{NOCOPIES}
5333 @item @code{NODEFAULT}
5334 @item @code{NOFLOATS}
5335 @item @code{NOIN_PARAMS}
5336 @item @code{NOMOD_PARAMS}
5337 @item @code{NOOPERANDS}
5338 @item @code{NORETURNS}
5339 @item @code{NOSUBSCRIPTS}
5340 @item @code{NOTESTS}
5344 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5345 turns on all validity checking options except for
5346 checking of @code{@b{in out}} procedure arguments.
5348 The specification of additional validity checking generates extra code (and
5349 in the case of @option{-gnatVa} the code expansion can be substantial.
5350 However, these additional checks can be very useful in detecting
5351 uninitialized variables, incorrect use of unchecked conversion, and other
5352 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5353 is useful in conjunction with the extra validity checking, since this
5354 ensures that wherever possible uninitialized variables have invalid values.
5356 See also the pragma @code{Validity_Checks} which allows modification of
5357 the validity checking mode at the program source level, and also allows for
5358 temporary disabling of validity checks.
5360 @node Style Checking
5361 @subsection Style Checking
5362 @findex Style checking
5365 The @option{-gnaty^x^(option,option,...)^} switch
5366 @cindex @option{-gnaty} (@command{gcc})
5367 causes the compiler to
5368 enforce specified style rules. A limited set of style rules has been used
5369 in writing the GNAT sources themselves. This switch allows user programs
5370 to activate all or some of these checks. If the source program fails a
5371 specified style check, an appropriate warning message is given, preceded by
5372 the character sequence ``(style)''.
5374 @code{(option,option,...)} is a sequence of keywords
5377 The string @var{x} is a sequence of letters or digits
5379 indicating the particular style
5380 checks to be performed. The following checks are defined:
5385 @emph{Specify indentation level.}
5386 If a digit from 1-9 appears
5387 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5388 then proper indentation is checked, with the digit indicating the
5389 indentation level required.
5390 The general style of required indentation is as specified by
5391 the examples in the Ada Reference Manual. Full line comments must be
5392 aligned with the @code{--} starting on a column that is a multiple of
5393 the alignment level.
5396 @emph{Check attribute casing.}
5397 If the ^letter a^word ATTRIBUTE^ appears in the string after @option{-gnaty}
5398 then attribute names, including the case of keywords such as @code{digits}
5399 used as attributes names, must be written in mixed case, that is, the
5400 initial letter and any letter following an underscore must be uppercase.
5401 All other letters must be lowercase.
5404 @emph{Blanks not allowed at statement end.}
5405 If the ^letter b^word BLANKS^ appears in the string after @option{-gnaty} then
5406 trailing blanks are not allowed at the end of statements. The purpose of this
5407 rule, together with h (no horizontal tabs), is to enforce a canonical format
5408 for the use of blanks to separate source tokens.
5411 @emph{Check comments.}
5412 If the ^letter c^word COMMENTS^ appears in the string after @option{-gnaty}
5413 then comments must meet the following set of rules:
5418 The ``@code{--}'' that starts the column must either start in column one,
5419 or else at least one blank must precede this sequence.
5422 Comments that follow other tokens on a line must have at least one blank
5423 following the ``@code{--}'' at the start of the comment.
5426 Full line comments must have two blanks following the ``@code{--}'' that
5427 starts the comment, with the following exceptions.
5430 A line consisting only of the ``@code{--}'' characters, possibly preceded
5431 by blanks is permitted.
5434 A comment starting with ``@code{--x}'' where @code{x} is a special character
5436 This allows proper processing of the output generated by specialized tools
5437 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5439 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5440 special character is defined as being in one of the ASCII ranges
5441 @code{16#21#..16#2F#} or @code{16#3A#..16#3F#}.
5442 Note that this usage is not permitted
5443 in GNAT implementation units (i.e. when @option{-gnatg} is used).
5446 A line consisting entirely of minus signs, possibly preceded by blanks, is
5447 permitted. This allows the construction of box comments where lines of minus
5448 signs are used to form the top and bottom of the box.
5451 A comment that starts and ends with ``@code{--}'' is permitted as long as at
5452 least one blank follows the initial ``@code{--}''. Together with the preceding
5453 rule, this allows the construction of box comments, as shown in the following
5456 ---------------------------
5457 -- This is a box comment --
5458 -- with two text lines. --
5459 ---------------------------
5463 @item ^d^DOS_LINE_ENDINGS^
5464 @emph{Check no DOS line terminators present.}
5465 If the ^letter d^word DOS_LINE_ENDINGS^ appears in the string after
5466 @option{-gnaty} then all lines must be terminated by a single ASCII.LF
5467 character (in particular the DOS line terminator sequence CR/LF is not
5471 @emph{Check end/exit labels.}
5472 If the ^letter e^word END^ appears in the string after @option{-gnaty} then
5473 optional labels on @code{end} statements ending subprograms and on
5474 @code{exit} statements exiting named loops, are required to be present.
5477 @emph{No form feeds or vertical tabs.}
5478 If the ^letter f^word VTABS^ appears in the string after @option{-gnaty} then
5479 neither form feeds nor vertical tab characters are permitted
5483 @emph{No horizontal tabs.}
5484 If the ^letter h^word HTABS^ appears in the string after @option{-gnaty} then
5485 horizontal tab characters are not permitted in the source text.
5486 Together with the b (no blanks at end of line) check, this
5487 enforces a canonical form for the use of blanks to separate
5491 @emph{Check if-then layout.}
5492 If the ^letter i^word IF_THEN^ appears in the string after @option{-gnaty},
5493 then the keyword @code{then} must appear either on the same
5494 line as corresponding @code{if}, or on a line on its own, lined
5495 up under the @code{if} with at least one non-blank line in between
5496 containing all or part of the condition to be tested.
5499 @emph{check mode IN keywords}
5500 If the ^letter I (upper case)^word IN_MODE^ appears in the string
5501 after @option{-gnaty} then mode @code{in} (the default mode) is not
5502 allowed to be given explicitly. @code{in out} is fine,
5503 but not @code{in} on its own.
5506 @emph{Check keyword casing.}
5507 If the ^letter k^word KEYWORD^ appears in the string after @option{-gnaty} then
5508 all keywords must be in lower case (with the exception of keywords
5509 such as @code{digits} used as attribute names to which this check
5513 @emph{Check layout.}
5514 If the ^letter l^word LAYOUT^ appears in the string after @option{-gnaty} then
5515 layout of statement and declaration constructs must follow the
5516 recommendations in the Ada Reference Manual, as indicated by the
5517 form of the syntax rules. For example an @code{else} keyword must
5518 be lined up with the corresponding @code{if} keyword.
5520 There are two respects in which the style rule enforced by this check
5521 option are more liberal than those in the Ada Reference Manual. First
5522 in the case of record declarations, it is permissible to put the
5523 @code{record} keyword on the same line as the @code{type} keyword, and
5524 then the @code{end} in @code{end record} must line up under @code{type}.
5525 For example, either of the following two layouts is acceptable:
5527 @smallexample @c ada
5543 Second, in the case of a block statement, a permitted alternative
5544 is to put the block label on the same line as the @code{declare} or
5545 @code{begin} keyword, and then line the @code{end} keyword up under
5546 the block label. For example both the following are permitted:
5548 @smallexample @c ada
5566 The same alternative format is allowed for loops. For example, both of
5567 the following are permitted:
5569 @smallexample @c ada
5571 Clear : while J < 10 loop
5582 @item ^Lnnn^MAX_NESTING=nnn^
5583 @emph{Set maximum nesting level}
5584 If the sequence ^Lnnn^MAX_NESTING=nnn^, where nnn is a decimal number in
5585 the range 0-999, appears in the string after @option{-gnaty} then the
5586 maximum level of nesting of constructs (including subprograms, loops,
5587 blocks, packages, and conditionals) may not exceed the given value. A
5588 value of zero disconnects this style check.
5590 @item ^m^LINE_LENGTH^
5591 @emph{Check maximum line length.}
5592 If the ^letter m^word LINE_LENGTH^ appears in the string after @option{-gnaty}
5593 then the length of source lines must not exceed 79 characters, including
5594 any trailing blanks. The value of 79 allows convenient display on an
5595 80 character wide device or window, allowing for possible special
5596 treatment of 80 character lines. Note that this count is of
5597 characters in the source text. This means that a tab character counts
5598 as one character in this count but a wide character sequence counts as
5599 a single character (however many bytes are needed in the encoding).
5601 @item ^Mnnn^MAX_LENGTH=nnn^
5602 @emph{Set maximum line length.}
5603 If the sequence ^M^MAX_LENGTH=^nnn, where nnn is a decimal number, appears in
5604 the string after @option{-gnaty} then the length of lines must not exceed the
5605 given value. The maximum value that can be specified is 32767.
5607 @item ^n^STANDARD_CASING^
5608 @emph{Check casing of entities in Standard.}
5609 If the ^letter n^word STANDARD_CASING^ appears in the string
5610 after @option{-gnaty} then any identifier from Standard must be cased
5611 to match the presentation in the Ada Reference Manual (for example,
5612 @code{Integer} and @code{ASCII.NUL}).
5614 @item ^o^ORDERED_SUBPROGRAMS^
5615 @emph{Check order of subprogram bodies.}
5616 If the ^letter o^word ORDERED_SUBPROGRAMS^ appears in the string
5617 after @option{-gnaty} then all subprogram bodies in a given scope
5618 (e.g. a package body) must be in alphabetical order. The ordering
5619 rule uses normal Ada rules for comparing strings, ignoring casing
5620 of letters, except that if there is a trailing numeric suffix, then
5621 the value of this suffix is used in the ordering (e.g. Junk2 comes
5625 @emph{Check pragma casing.}
5626 If the ^letter p^word PRAGMA^ appears in the string after @option{-gnaty} then
5627 pragma names must be written in mixed case, that is, the
5628 initial letter and any letter following an underscore must be uppercase.
5629 All other letters must be lowercase.
5631 @item ^r^REFERENCES^
5632 @emph{Check references.}
5633 If the ^letter r^word REFERENCES^ appears in the string after @option{-gnaty}
5634 then all identifier references must be cased in the same way as the
5635 corresponding declaration. No specific casing style is imposed on
5636 identifiers. The only requirement is for consistency of references
5640 @emph{Check separate specs.}
5641 If the ^letter s^word SPECS^ appears in the string after @option{-gnaty} then
5642 separate declarations (``specs'') are required for subprograms (a
5643 body is not allowed to serve as its own declaration). The only
5644 exception is that parameterless library level procedures are
5645 not required to have a separate declaration. This exception covers
5646 the most frequent form of main program procedures.
5649 @emph{Check token spacing.}
5650 If the ^letter t^word TOKEN^ appears in the string after @option{-gnaty} then
5651 the following token spacing rules are enforced:
5656 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
5659 The token @code{=>} must be surrounded by spaces.
5662 The token @code{<>} must be preceded by a space or a left parenthesis.
5665 Binary operators other than @code{**} must be surrounded by spaces.
5666 There is no restriction on the layout of the @code{**} binary operator.
5669 Colon must be surrounded by spaces.
5672 Colon-equal (assignment, initialization) must be surrounded by spaces.
5675 Comma must be the first non-blank character on the line, or be
5676 immediately preceded by a non-blank character, and must be followed
5680 If the token preceding a left parenthesis ends with a letter or digit, then
5681 a space must separate the two tokens.
5684 A right parenthesis must either be the first non-blank character on
5685 a line, or it must be preceded by a non-blank character.
5688 A semicolon must not be preceded by a space, and must not be followed by
5689 a non-blank character.
5692 A unary plus or minus may not be followed by a space.
5695 A vertical bar must be surrounded by spaces.
5698 @item ^u^UNNECESSARY_BLANK_LINES^
5699 @emph{Check unnecessary blank lines.}
5700 Check for unnecessary blank lines. A blank line is considered
5701 unnecessary if it appears at the end of the file, or if more than
5702 one blank line occurs in sequence.
5704 @item ^x^XTRA_PARENS^
5705 @emph{Check extra parentheses.}
5706 Check for the use of an unnecessary extra level of parentheses (C-style)
5707 around conditions in @code{if} statements, @code{while} statements and
5708 @code{exit} statements.
5713 In the above rules, appearing in column one is always permitted, that is,
5714 counts as meeting either a requirement for a required preceding space,
5715 or as meeting a requirement for no preceding space.
5717 Appearing at the end of a line is also always permitted, that is, counts
5718 as meeting either a requirement for a following space, or as meeting
5719 a requirement for no following space.
5722 If any of these style rules is violated, a message is generated giving
5723 details on the violation. The initial characters of such messages are
5724 always ``@code{(style)}''. Note that these messages are treated as warning
5725 messages, so they normally do not prevent the generation of an object
5726 file. The @option{-gnatwe} switch can be used to treat warning messages,
5727 including style messages, as fatal errors.
5731 @option{-gnaty} on its own (that is not
5732 followed by any letters or digits),
5733 is equivalent to @code{gnaty3abcefhiklmnprst}, that is all checking
5734 options enabled with the exception of @option{-gnatyo},
5735 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
5738 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
5739 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
5740 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
5742 an indentation level of 3 is set. This is similar to the standard
5743 checking option that is used for the GNAT sources.
5752 clears any previously set style checks.
5754 @node Run-Time Checks
5755 @subsection Run-Time Checks
5756 @cindex Division by zero
5757 @cindex Access before elaboration
5758 @cindex Checks, division by zero
5759 @cindex Checks, access before elaboration
5760 @cindex Checks, stack overflow checking
5763 If you compile with the default options, GNAT will insert many run-time
5764 checks into the compiled code, including code that performs range
5765 checking against constraints, but not arithmetic overflow checking for
5766 integer operations (including division by zero), checks for access
5767 before elaboration on subprogram calls, or stack overflow checking. All
5768 other run-time checks, as required by the Ada 95 Reference Manual, are
5769 generated by default. The following @command{gcc} switches refine this
5775 @cindex @option{-gnatp} (@command{gcc})
5776 @cindex Suppressing checks
5777 @cindex Checks, suppressing
5779 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
5780 had been present in the source. Validity checks are also suppressed (in
5781 other words @option{-gnatp} also implies @option{-gnatVn}.
5782 Use this switch to improve the performance
5783 of the code at the expense of safety in the presence of invalid data or
5787 @cindex @option{-gnato} (@command{gcc})
5788 @cindex Overflow checks
5789 @cindex Check, overflow
5790 Enables overflow checking for integer operations.
5791 This causes GNAT to generate slower and larger executable
5792 programs by adding code to check for overflow (resulting in raising
5793 @code{Constraint_Error} as required by standard Ada
5794 semantics). These overflow checks correspond to situations in which
5795 the true value of the result of an operation may be outside the base
5796 range of the result type. The following example shows the distinction:
5798 @smallexample @c ada
5799 X1 : Integer := Integer'Last;
5800 X2 : Integer range 1 .. 5 := 5;
5801 X3 : Integer := Integer'Last;
5802 X4 : Integer range 1 .. 5 := 5;
5803 F : Float := 2.0E+20;
5812 Here the first addition results in a value that is outside the base range
5813 of Integer, and hence requires an overflow check for detection of the
5814 constraint error. Thus the first assignment to @code{X1} raises a
5815 @code{Constraint_Error} exception only if @option{-gnato} is set.
5817 The second increment operation results in a violation
5818 of the explicit range constraint, and such range checks are always
5819 performed (unless specifically suppressed with a pragma @code{suppress}
5820 or the use of @option{-gnatp}).
5822 The two conversions of @code{F} both result in values that are outside
5823 the base range of type @code{Integer} and thus will raise
5824 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
5825 The fact that the result of the second conversion is assigned to
5826 variable @code{X4} with a restricted range is irrelevant, since the problem
5827 is in the conversion, not the assignment.
5829 Basically the rule is that in the default mode (@option{-gnato} not
5830 used), the generated code assures that all integer variables stay
5831 within their declared ranges, or within the base range if there is
5832 no declared range. This prevents any serious problems like indexes
5833 out of range for array operations.
5835 What is not checked in default mode is an overflow that results in
5836 an in-range, but incorrect value. In the above example, the assignments
5837 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
5838 range of the target variable, but the result is wrong in the sense that
5839 it is too large to be represented correctly. Typically the assignment
5840 to @code{X1} will result in wrap around to the largest negative number.
5841 The conversions of @code{F} will result in some @code{Integer} value
5842 and if that integer value is out of the @code{X4} range then the
5843 subsequent assignment would generate an exception.
5845 @findex Machine_Overflows
5846 Note that the @option{-gnato} switch does not affect the code generated
5847 for any floating-point operations; it applies only to integer
5849 For floating-point, GNAT has the @code{Machine_Overflows}
5850 attribute set to @code{False} and the normal mode of operation is to
5851 generate IEEE NaN and infinite values on overflow or invalid operations
5852 (such as dividing 0.0 by 0.0).
5854 The reason that we distinguish overflow checking from other kinds of
5855 range constraint checking is that a failure of an overflow check can
5856 generate an incorrect value, but cannot cause erroneous behavior. This
5857 is unlike the situation with a constraint check on an array subscript,
5858 where failure to perform the check can result in random memory description,
5859 or the range check on a case statement, where failure to perform the check
5860 can cause a wild jump.
5862 Note again that @option{-gnato} is off by default, so overflow checking is
5863 not performed in default mode. This means that out of the box, with the
5864 default settings, GNAT does not do all the checks expected from the
5865 language description in the Ada Reference Manual. If you want all constraint
5866 checks to be performed, as described in this Manual, then you must
5867 explicitly use the -gnato switch either on the @command{gnatmake} or
5868 @command{gcc} command.
5871 @cindex @option{-gnatE} (@command{gcc})
5872 @cindex Elaboration checks
5873 @cindex Check, elaboration
5874 Enables dynamic checks for access-before-elaboration
5875 on subprogram calls and generic instantiations.
5876 For full details of the effect and use of this switch,
5877 @xref{Compiling Using gcc}.
5880 @cindex @option{-fstack-check} (@command{gcc})
5881 @cindex Stack Overflow Checking
5882 @cindex Checks, stack overflow checking
5883 Activates stack overflow checking. For full details of the effect and use of
5884 this switch see @ref{Stack Overflow Checking}.
5889 The setting of these switches only controls the default setting of the
5890 checks. You may modify them using either @code{Suppress} (to remove
5891 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
5894 @node Using gcc for Syntax Checking
5895 @subsection Using @command{gcc} for Syntax Checking
5898 @cindex @option{-gnats} (@command{gcc})
5902 The @code{s} stands for ``syntax''.
5905 Run GNAT in syntax checking only mode. For
5906 example, the command
5909 $ gcc -c -gnats x.adb
5913 compiles file @file{x.adb} in syntax-check-only mode. You can check a
5914 series of files in a single command
5916 , and can use wild cards to specify such a group of files.
5917 Note that you must specify the @option{-c} (compile
5918 only) flag in addition to the @option{-gnats} flag.
5921 You may use other switches in conjunction with @option{-gnats}. In
5922 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
5923 format of any generated error messages.
5925 When the source file is empty or contains only empty lines and/or comments,
5926 the output is a warning:
5929 $ gcc -c -gnats -x ada toto.txt
5930 toto.txt:1:01: warning: empty file, contains no compilation units
5934 Otherwise, the output is simply the error messages, if any. No object file or
5935 ALI file is generated by a syntax-only compilation. Also, no units other
5936 than the one specified are accessed. For example, if a unit @code{X}
5937 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
5938 check only mode does not access the source file containing unit
5941 @cindex Multiple units, syntax checking
5942 Normally, GNAT allows only a single unit in a source file. However, this
5943 restriction does not apply in syntax-check-only mode, and it is possible
5944 to check a file containing multiple compilation units concatenated
5945 together. This is primarily used by the @code{gnatchop} utility
5946 (@pxref{Renaming Files Using gnatchop}).
5949 @node Using gcc for Semantic Checking
5950 @subsection Using @command{gcc} for Semantic Checking
5953 @cindex @option{-gnatc} (@command{gcc})
5957 The @code{c} stands for ``check''.
5959 Causes the compiler to operate in semantic check mode,
5960 with full checking for all illegalities specified in the
5961 Ada 95 Reference Manual, but without generation of any object code
5962 (no object file is generated).
5964 Because dependent files must be accessed, you must follow the GNAT
5965 semantic restrictions on file structuring to operate in this mode:
5969 The needed source files must be accessible
5970 (@pxref{Search Paths and the Run-Time Library (RTL)}).
5973 Each file must contain only one compilation unit.
5976 The file name and unit name must match (@pxref{File Naming Rules}).
5979 The output consists of error messages as appropriate. No object file is
5980 generated. An @file{ALI} file is generated for use in the context of
5981 cross-reference tools, but this file is marked as not being suitable
5982 for binding (since no object file is generated).
5983 The checking corresponds exactly to the notion of
5984 legality in the Ada 95 Reference Manual.
5986 Any unit can be compiled in semantics-checking-only mode, including
5987 units that would not normally be compiled (subunits,
5988 and specifications where a separate body is present).
5991 @node Compiling Different Versions of Ada
5992 @subsection Compiling Different Versions of Ada
5994 @cindex Compatibility with Ada 83
5997 @cindex Ada 2005 mode
5999 GNAT is primarily an Ada 95 compiler, but the switches described in
6000 this section allow operation in Ada 83 compatibility mode, and also
6001 allow the use of a preliminary implementation of many of the expected
6002 new features in Ada 2005, the forthcoming new version of the standard.
6004 @item -gnat83 (Ada 83 Compatibility Mode)
6005 @cindex @option{-gnat83} (@command{gcc})
6006 @cindex ACVC, Ada 83 tests
6009 Although GNAT is primarily an Ada 95 compiler, it accepts this switch to
6010 specify that an Ada 83 program is to be compiled in Ada 83 mode. If you specify
6011 this switch, GNAT rejects most Ada 95 extensions and applies Ada 83 semantics
6012 where this can be done easily.
6013 It is not possible to guarantee this switch does a perfect
6014 job; for example, some subtle tests, such as are
6015 found in earlier ACVC tests (and that have been removed from the ACATS suite
6016 for Ada 95), might not compile correctly.
6017 Nevertheless, this switch may be useful in some circumstances, for example
6018 where, due to contractual reasons, legacy code needs to be maintained
6019 using only Ada 83 features.
6021 With few exceptions (most notably the need to use @code{<>} on
6022 @cindex Generic formal parameters
6023 unconstrained generic formal parameters, the use of the new Ada 95
6024 reserved words, and the use of packages
6025 with optional bodies), it is not necessary to use the
6026 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6027 exceptions, Ada 95 is upwardly compatible with Ada 83. This
6028 means that a correct Ada 83 program is usually also a correct Ada 95
6030 For further information, please refer to @ref{Compatibility and Porting Guide}.
6032 @item -gnat95 (Ada 95 mode)
6033 @cindex @option{-gnat95} (@command{gcc})
6036 GNAT is primarily an Ada 95 compiler, and all current releases of GNAT Pro
6037 compile in Ada 95 mode by default. Typically, Ada 95 is sufficiently upwards
6038 compatible with Ada 83, that legacy Ada 83 programs may be compiled using
6039 this default Ada95 mode without problems (see section above describing the
6040 use of @option{-gnat83} to run in Ada 83 mode).
6042 In Ada 95 mode, the use of Ada 2005 features will in general cause error
6043 messages or warnings. Some specialized releases of GNAT (notably the GAP
6044 academic version) operate in Ada 2005 mode by default (see section below
6045 describing the use of @option{-gnat05} to run in Ada 2005 mode). For such
6046 versions the @option{-gnat95} switch may be used to enforce Ada 95 mode.
6047 This option also can be used to cancel the effect of a previous
6048 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6051 @item -gnat05 (Ada 2005 mode)
6052 @cindex @option{-gnat05} (@command{gcc})
6055 Although GNAT is primarily an Ada 95 compiler, it can be set to operate
6056 in Ada 2005 mode using this option. Although the new standard has not
6057 yet been issued (as of early 2005), many features have been discussed and
6058 approved in ``Ada Issues'' (AI's). For the text of these AI's, see
6059 @url{www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}. Included with GNAT
6060 releases is a file @file{features-ada0y} that describes the current set
6061 of implemented Ada 2005 features.
6063 If these features are used in Ada 95 mode (which is the normal default),
6064 then error messages or warnings may be
6065 generated, reflecting the fact that these new features are otherwise
6066 unauthorized extensions to Ada 95. The use of the @option{-gnat05}
6067 switch (or an equivalent pragma) causes these messages to be suppressed.
6069 Note that some specialized releases of GNAT (notably the GAP academic
6070 version) have Ada 2005 mode on by default, and in such environments,
6071 the Ada 2005 features can be used freely without the use of switches.
6075 @node Character Set Control
6076 @subsection Character Set Control
6078 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6079 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6082 Normally GNAT recognizes the Latin-1 character set in source program
6083 identifiers, as described in the Ada 95 Reference Manual.
6085 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6086 single character ^^or word^ indicating the character set, as follows:
6090 ISO 8859-1 (Latin-1) identifiers
6093 ISO 8859-2 (Latin-2) letters allowed in identifiers
6096 ISO 8859-3 (Latin-3) letters allowed in identifiers
6099 ISO 8859-4 (Latin-4) letters allowed in identifiers
6102 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6105 ISO 8859-15 (Latin-9) letters allowed in identifiers
6108 IBM PC letters (code page 437) allowed in identifiers
6111 IBM PC letters (code page 850) allowed in identifiers
6113 @item ^f^FULL_UPPER^
6114 Full upper-half codes allowed in identifiers
6117 No upper-half codes allowed in identifiers
6120 Wide-character codes (that is, codes greater than 255)
6121 allowed in identifiers
6124 @xref{Foreign Language Representation}, for full details on the
6125 implementation of these character sets.
6127 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6128 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6129 Specify the method of encoding for wide characters.
6130 @var{e} is one of the following:
6135 Hex encoding (brackets coding also recognized)
6138 Upper half encoding (brackets encoding also recognized)
6141 Shift/JIS encoding (brackets encoding also recognized)
6144 EUC encoding (brackets encoding also recognized)
6147 UTF-8 encoding (brackets encoding also recognized)
6150 Brackets encoding only (default value)
6152 For full details on these encoding
6153 methods see @ref{Wide Character Encodings}.
6154 Note that brackets coding is always accepted, even if one of the other
6155 options is specified, so for example @option{-gnatW8} specifies that both
6156 brackets and @code{UTF-8} encodings will be recognized. The units that are
6157 with'ed directly or indirectly will be scanned using the specified
6158 representation scheme, and so if one of the non-brackets scheme is
6159 used, it must be used consistently throughout the program. However,
6160 since brackets encoding is always recognized, it may be conveniently
6161 used in standard libraries, allowing these libraries to be used with
6162 any of the available coding schemes.
6163 scheme. If no @option{-gnatW?} parameter is present, then the default
6164 representation is Brackets encoding only.
6166 Note that the wide character representation that is specified (explicitly
6167 or by default) for the main program also acts as the default encoding used
6168 for Wide_Text_IO files if not specifically overridden by a WCEM form
6172 @node File Naming Control
6173 @subsection File Naming Control
6176 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6177 @cindex @option{-gnatk} (@command{gcc})
6178 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6179 1-999, indicates the maximum allowable length of a file name (not
6180 including the @file{.ads} or @file{.adb} extension). The default is not
6181 to enable file name krunching.
6183 For the source file naming rules, @xref{File Naming Rules}.
6186 @node Subprogram Inlining Control
6187 @subsection Subprogram Inlining Control
6192 @cindex @option{-gnatn} (@command{gcc})
6194 The @code{n} here is intended to suggest the first syllable of the
6197 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6198 inlining to actually occur, optimization must be enabled. To enable
6199 inlining of subprograms specified by pragma @code{Inline},
6200 you must also specify this switch.
6201 In the absence of this switch, GNAT does not attempt
6202 inlining and does not need to access the bodies of
6203 subprograms for which @code{pragma Inline} is specified if they are not
6204 in the current unit.
6206 If you specify this switch the compiler will access these bodies,
6207 creating an extra source dependency for the resulting object file, and
6208 where possible, the call will be inlined.
6209 For further details on when inlining is possible
6210 see @ref{Inlining of Subprograms}.
6213 @cindex @option{-gnatN} (@command{gcc})
6214 The front end inlining activated by this switch is generally more extensive,
6215 and quite often more effective than the standard @option{-gnatn} inlining mode.
6216 It will also generate additional dependencies.
6218 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
6219 to specify both options.
6222 @node Auxiliary Output Control
6223 @subsection Auxiliary Output Control
6227 @cindex @option{-gnatt} (@command{gcc})
6228 @cindex Writing internal trees
6229 @cindex Internal trees, writing to file
6230 Causes GNAT to write the internal tree for a unit to a file (with the
6231 extension @file{.adt}.
6232 This not normally required, but is used by separate analysis tools.
6234 these tools do the necessary compilations automatically, so you should
6235 not have to specify this switch in normal operation.
6238 @cindex @option{-gnatu} (@command{gcc})
6239 Print a list of units required by this compilation on @file{stdout}.
6240 The listing includes all units on which the unit being compiled depends
6241 either directly or indirectly.
6244 @item -pass-exit-codes
6245 @cindex @option{-pass-exit-codes} (@command{gcc})
6246 If this switch is not used, the exit code returned by @command{gcc} when
6247 compiling multiple files indicates whether all source files have
6248 been successfully used to generate object files or not.
6250 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
6251 exit status and allows an integrated development environment to better
6252 react to a compilation failure. Those exit status are:
6256 There was an error in at least one source file.
6258 At least one source file did not generate an object file.
6260 The compiler died unexpectedly (internal error for example).
6262 An object file has been generated for every source file.
6267 @node Debugging Control
6268 @subsection Debugging Control
6272 @cindex Debugging options
6275 @cindex @option{-gnatd} (@command{gcc})
6276 Activate internal debugging switches. @var{x} is a letter or digit, or
6277 string of letters or digits, which specifies the type of debugging
6278 outputs desired. Normally these are used only for internal development
6279 or system debugging purposes. You can find full documentation for these
6280 switches in the body of the @code{Debug} unit in the compiler source
6281 file @file{debug.adb}.
6285 @cindex @option{-gnatG} (@command{gcc})
6286 This switch causes the compiler to generate auxiliary output containing
6287 a pseudo-source listing of the generated expanded code. Like most Ada
6288 compilers, GNAT works by first transforming the high level Ada code into
6289 lower level constructs. For example, tasking operations are transformed
6290 into calls to the tasking run-time routines. A unique capability of GNAT
6291 is to list this expanded code in a form very close to normal Ada source.
6292 This is very useful in understanding the implications of various Ada
6293 usage on the efficiency of the generated code. There are many cases in
6294 Ada (e.g. the use of controlled types), where simple Ada statements can
6295 generate a lot of run-time code. By using @option{-gnatG} you can identify
6296 these cases, and consider whether it may be desirable to modify the coding
6297 approach to improve efficiency.
6299 The format of the output is very similar to standard Ada source, and is
6300 easily understood by an Ada programmer. The following special syntactic
6301 additions correspond to low level features used in the generated code that
6302 do not have any exact analogies in pure Ada source form. The following
6303 is a partial list of these special constructions. See the specification
6304 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6307 @item new @var{xxx} [storage_pool = @var{yyy}]
6308 Shows the storage pool being used for an allocator.
6310 @item at end @var{procedure-name};
6311 Shows the finalization (cleanup) procedure for a scope.
6313 @item (if @var{expr} then @var{expr} else @var{expr})
6314 Conditional expression equivalent to the @code{x?y:z} construction in C.
6316 @item @var{target}^^^(@var{source})
6317 A conversion with floating-point truncation instead of rounding.
6319 @item @var{target}?(@var{source})
6320 A conversion that bypasses normal Ada semantic checking. In particular
6321 enumeration types and fixed-point types are treated simply as integers.
6323 @item @var{target}?^^^(@var{source})
6324 Combines the above two cases.
6326 @item @var{x} #/ @var{y}
6327 @itemx @var{x} #mod @var{y}
6328 @itemx @var{x} #* @var{y}
6329 @itemx @var{x} #rem @var{y}
6330 A division or multiplication of fixed-point values which are treated as
6331 integers without any kind of scaling.
6333 @item free @var{expr} [storage_pool = @var{xxx}]
6334 Shows the storage pool associated with a @code{free} statement.
6336 @item [subtype or type declaration]
6337 Used to list an equivalent declaration for an internally generated
6338 type that is referenced elsewhere in the listing.
6340 @item freeze @var{type-name} [@var{actions}]
6341 Shows the point at which @var{type-name} is frozen, with possible
6342 associated actions to be performed at the freeze point.
6344 @item reference @var{itype}
6345 Reference (and hence definition) to internal type @var{itype}.
6347 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6348 Intrinsic function call.
6350 @item @var{label-name} : label
6351 Declaration of label @var{labelname}.
6353 @item #$ @var{subprogram-name}
6354 An implicit call to a run-time support routine
6355 (to meet the requirement of H.3.1(9) in a
6358 @item @var{expr} && @var{expr} && @var{expr} ... && @var{expr}
6359 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6360 @var{expr}, but handled more efficiently).
6362 @item [constraint_error]
6363 Raise the @code{Constraint_Error} exception.
6365 @item @var{expression}'reference
6366 A pointer to the result of evaluating @var{expression}.
6368 @item @var{target-type}!(@var{source-expression})
6369 An unchecked conversion of @var{source-expression} to @var{target-type}.
6371 @item [@var{numerator}/@var{denominator}]
6372 Used to represent internal real literals (that) have no exact
6373 representation in base 2-16 (for example, the result of compile time
6374 evaluation of the expression 1.0/27.0).
6378 @cindex @option{-gnatD} (@command{gcc})
6379 When used in conjunction with @option{-gnatG}, this switch causes
6380 the expanded source, as described above for
6381 @option{-gnatG} to be written to files with names
6382 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6383 instead of to the standard output file. For
6384 example, if the source file name is @file{hello.adb}, then a file
6385 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
6386 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
6387 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6388 you to do source level debugging using the generated code which is
6389 sometimes useful for complex code, for example to find out exactly
6390 which part of a complex construction raised an exception. This switch
6391 also suppress generation of cross-reference information (see
6392 @option{-gnatx}) since otherwise the cross-reference information
6393 would refer to the @file{^.dg^.DG^} file, which would cause
6394 confusion since this is not the original source file.
6396 Note that @option{-gnatD} actually implies @option{-gnatG}
6397 automatically, so it is not necessary to give both options.
6398 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6401 @item -gnatR[0|1|2|3[s]]
6402 @cindex @option{-gnatR} (@command{gcc})
6403 This switch controls output from the compiler of a listing showing
6404 representation information for declared types and objects. For
6405 @option{-gnatR0}, no information is output (equivalent to omitting
6406 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6407 so @option{-gnatR} with no parameter has the same effect), size and alignment
6408 information is listed for declared array and record types. For
6409 @option{-gnatR2}, size and alignment information is listed for all
6410 expression information for values that are computed at run time for
6411 variant records. These symbolic expressions have a mostly obvious
6412 format with #n being used to represent the value of the n'th
6413 discriminant. See source files @file{repinfo.ads/adb} in the
6414 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6415 output. If the switch is followed by an s (e.g. @option{-gnatR2s}), then
6416 the output is to a file with the name @file{^file.rep^file_REP^} where
6417 file is the name of the corresponding source file.
6420 @item /REPRESENTATION_INFO
6421 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
6422 This qualifier controls output from the compiler of a listing showing
6423 representation information for declared types and objects. For
6424 @option{/REPRESENTATION_INFO=NONE}, no information is output
6425 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
6426 @option{/REPRESENTATION_INFO} without option is equivalent to
6427 @option{/REPRESENTATION_INFO=ARRAYS}.
6428 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
6429 information is listed for declared array and record types. For
6430 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
6431 is listed for all expression information for values that are computed
6432 at run time for variant records. These symbolic expressions have a mostly
6433 obvious format with #n being used to represent the value of the n'th
6434 discriminant. See source files @file{REPINFO.ADS/ADB} in the
6435 @code{GNAT} sources for full details on the format of
6436 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
6437 If _FILE is added at the end of an option
6438 (e.g. @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
6439 then the output is to a file with the name @file{file_REP} where
6440 file is the name of the corresponding source file.
6442 Note that it is possible for record components to have zero size. In
6443 this case, the component clause uses an obvious extension of permitted
6444 Ada syntax, for example @code{at 0 range 0 .. -1}.
6447 @cindex @option{-gnatS} (@command{gcc})
6448 The use of the switch @option{-gnatS} for an
6449 Ada compilation will cause the compiler to output a
6450 representation of package Standard in a form very
6451 close to standard Ada. It is not quite possible to
6452 do this entirely in standard Ada (since new
6453 numeric base types cannot be created in standard
6454 Ada), but the output is easily
6455 readable to any Ada programmer, and is useful to
6456 determine the characteristics of target dependent
6457 types in package Standard.
6460 @cindex @option{-gnatx} (@command{gcc})
6461 Normally the compiler generates full cross-referencing information in
6462 the @file{ALI} file. This information is used by a number of tools,
6463 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
6464 suppresses this information. This saves some space and may slightly
6465 speed up compilation, but means that these tools cannot be used.
6468 @node Exception Handling Control
6469 @subsection Exception Handling Control
6472 GNAT uses two methods for handling exceptions at run-time. The
6473 @code{setjmp/longjmp} method saves the context when entering
6474 a frame with an exception handler. Then when an exception is
6475 raised, the context can be restored immediately, without the
6476 need for tracing stack frames. This method provides very fast
6477 exception propagation, but introduces significant overhead for
6478 the use of exception handlers, even if no exception is raised.
6480 The other approach is called ``zero cost'' exception handling.
6481 With this method, the compiler builds static tables to describe
6482 the exception ranges. No dynamic code is required when entering
6483 a frame containing an exception handler. When an exception is
6484 raised, the tables are used to control a back trace of the
6485 subprogram invocation stack to locate the required exception
6486 handler. This method has considerably poorer performance for
6487 the propagation of exceptions, but there is no overhead for
6488 exception handlers if no exception is raised. Note that in this
6489 mode and in the context of mixed Ada and C/C++ programming,
6490 to propagate an exception through a C/C++ code, the C/C++ code
6491 must be compiled with the @option{-funwind-tables} GCC's
6494 The following switches can be used to control which of the
6495 two exception handling methods is used.
6501 @cindex @option{--RTS=sjlj} (@command{gnatmake})
6502 This switch causes the setjmp/longjmp run-time to be used
6503 for exception handling. If this is the default mechanism for the
6504 target (see below), then this has no effect. If the default
6505 mechanism for the target is zero cost exceptions, then
6506 this switch can be used to modify this default, and must be
6507 used for all units in the partition.
6508 This option is rarely used. One case in which it may be
6509 advantageous is if you have an application where exception
6510 raising is common and the overall performance of the
6511 application is improved by favoring exception propagation.
6514 @cindex @option{--RTS=zcx} (@command{gnatmake})
6515 @cindex Zero Cost Exceptions
6516 This switch causes the zero cost approach to be used
6517 for exception handling. If this is the default mechanism for the
6518 target (see below), then this has no effect. If the default
6519 mechanism for the target is setjmp/longjmp exceptions, then
6520 this switch can be used to modify this default, and must be
6521 used for all units in the partition.
6522 This option can only be used if the zero cost approach
6523 is available for the target in use (see below).
6527 The @code{setjmp/longjmp} approach is available on all targets, while
6528 the @code{zero cost} approach is available on selected targets.
6529 To determine whether zero cost exceptions can be used for a
6530 particular target, look at the private part of the file system.ads.
6531 Either @code{GCC_ZCX_Support} or @code{Front_End_ZCX_Support} must
6532 be True to use the zero cost approach. If both of these switches
6533 are set to False, this means that zero cost exception handling
6534 is not yet available for that target. The switch
6535 @code{ZCX_By_Default} indicates the default approach. If this
6536 switch is set to True, then the @code{zero cost} approach is
6539 @node Units to Sources Mapping Files
6540 @subsection Units to Sources Mapping Files
6544 @item -gnatem^^=^@var{path}
6545 @cindex @option{-gnatem} (@command{gcc})
6546 A mapping file is a way to communicate to the compiler two mappings:
6547 from unit names to file names (without any directory information) and from
6548 file names to path names (with full directory information). These mappings
6549 are used by the compiler to short-circuit the path search.
6551 The use of mapping files is not required for correct operation of the
6552 compiler, but mapping files can improve efficiency, particularly when
6553 sources are read over a slow network connection. In normal operation,
6554 you need not be concerned with the format or use of mapping files,
6555 and the @option{-gnatem} switch is not a switch that you would use
6556 explicitly. it is intended only for use by automatic tools such as
6557 @command{gnatmake} running under the project file facility. The
6558 description here of the format of mapping files is provided
6559 for completeness and for possible use by other tools.
6561 A mapping file is a sequence of sets of three lines. In each set,
6562 the first line is the unit name, in lower case, with ``@code{%s}''
6564 specifications and ``@code{%b}'' appended for bodies; the second line is the
6565 file name; and the third line is the path name.
6571 /gnat/project1/sources/main.2.ada
6574 When the switch @option{-gnatem} is specified, the compiler will create
6575 in memory the two mappings from the specified file. If there is any problem
6576 (non existent file, truncated file or duplicate entries), no mapping
6579 Several @option{-gnatem} switches may be specified; however, only the last
6580 one on the command line will be taken into account.
6582 When using a project file, @command{gnatmake} create a temporary mapping file
6583 and communicates it to the compiler using this switch.
6587 @node Integrated Preprocessing
6588 @subsection Integrated Preprocessing
6591 GNAT sources may be preprocessed immediately before compilation; the actual
6592 text of the source is not the text of the source file, but is derived from it
6593 through a process called preprocessing. Integrated preprocessing is specified
6594 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
6595 indicates, through a text file, the preprocessing data to be used.
6596 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
6599 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
6600 used when Integrated Preprocessing is used. The reason is that preprocessing
6601 with another Preprocessing Data file without changing the sources will
6602 not trigger recompilation without this switch.
6605 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
6606 always trigger recompilation for sources that are preprocessed,
6607 because @command{gnatmake} cannot compute the checksum of the source after
6611 The actual preprocessing function is described in details in section
6612 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
6613 preprocessing is triggered and parameterized.
6617 @item -gnatep=@var{file}
6618 @cindex @option{-gnatep} (@command{gcc})
6619 This switch indicates to the compiler the file name (without directory
6620 information) of the preprocessor data file to use. The preprocessor data file
6621 should be found in the source directories.
6624 A preprocessing data file is a text file with significant lines indicating
6625 how should be preprocessed either a specific source or all sources not
6626 mentioned in other lines. A significant line is a non empty, non comment line.
6627 Comments are similar to Ada comments.
6630 Each significant line starts with either a literal string or the character '*'.
6631 A literal string is the file name (without directory information) of the source
6632 to preprocess. A character '*' indicates the preprocessing for all the sources
6633 that are not specified explicitly on other lines (order of the lines is not
6634 significant). It is an error to have two lines with the same file name or two
6635 lines starting with the character '*'.
6638 After the file name or the character '*', another optional literal string
6639 indicating the file name of the definition file to be used for preprocessing
6640 (@pxref{Form of Definitions File}). The definition files are found by the
6641 compiler in one of the source directories. In some cases, when compiling
6642 a source in a directory other than the current directory, if the definition
6643 file is in the current directory, it may be necessary to add the current
6644 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
6645 the compiler would not find the definition file.
6648 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
6649 be found. Those ^switches^switches^ are:
6654 Causes both preprocessor lines and the lines deleted by
6655 preprocessing to be replaced by blank lines, preserving the line number.
6656 This ^switch^switch^ is always implied; however, if specified after @option{-c}
6657 it cancels the effect of @option{-c}.
6660 Causes both preprocessor lines and the lines deleted
6661 by preprocessing to be retained as comments marked
6662 with the special string ``@code{--! }''.
6664 @item -Dsymbol=value
6665 Define or redefine a symbol, associated with value. A symbol is an Ada
6666 identifier, or an Ada reserved word, with the exception of @code{if},
6667 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6668 @code{value} is either a literal string, an Ada identifier or any Ada reserved
6669 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
6670 same name defined in a definition file.
6673 Causes a sorted list of symbol names and values to be
6674 listed on the standard output file.
6677 Causes undefined symbols to be treated as having the value @code{FALSE}
6679 of a preprocessor test. In the absence of this option, an undefined symbol in
6680 a @code{#if} or @code{#elsif} test will be treated as an error.
6685 Examples of valid lines in a preprocessor data file:
6688 "toto.adb" "prep.def" -u
6689 -- preprocess "toto.adb", using definition file "prep.def",
6690 -- undefined symbol are False.
6693 -- preprocess all other sources without a definition file;
6694 -- suppressed lined are commented; symbol VERSION has the value V101.
6696 "titi.adb" "prep2.def" -s
6697 -- preprocess "titi.adb", using definition file "prep2.def";
6698 -- list all symbols with their values.
6701 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
6702 @cindex @option{-gnateD} (@command{gcc})
6703 Define or redefine a preprocessing symbol, associated with value. If no value
6704 is given on the command line, then the value of the symbol is @code{True}.
6705 A symbol is an identifier, following normal Ada (case-insensitive)
6706 rules for its syntax, and value is any sequence (including an empty sequence)
6707 of characters from the set (letters, digits, period, underline).
6708 Ada reserved words may be used as symbols, with the exceptions of @code{if},
6709 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6712 A symbol declared with this ^switch^switch^ on the command line replaces a
6713 symbol with the same name either in a definition file or specified with a
6714 ^switch^switch^ -D in the preprocessor data file.
6717 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
6721 @node Code Generation Control
6722 @subsection Code Generation Control
6726 The GCC technology provides a wide range of target dependent
6727 @option{-m} switches for controlling
6728 details of code generation with respect to different versions of
6729 architectures. This includes variations in instruction sets (e.g.
6730 different members of the power pc family), and different requirements
6731 for optimal arrangement of instructions (e.g. different members of
6732 the x86 family). The list of available @option{-m} switches may be
6733 found in the GCC documentation.
6735 Use of these @option{-m} switches may in some cases result in improved
6738 The GNAT Pro technology is tested and qualified without any
6739 @option{-m} switches,
6740 so generally the most reliable approach is to avoid the use of these
6741 switches. However, we generally expect most of these switches to work
6742 successfully with GNAT Pro, and many customers have reported successful
6743 use of these options.
6745 Our general advice is to avoid the use of @option{-m} switches unless
6746 special needs lead to requirements in this area. In particular,
6747 there is no point in using @option{-m} switches to improve performance
6748 unless you actually see a performance improvement.
6752 @subsection Return Codes
6753 @cindex Return Codes
6754 @cindex @option{/RETURN_CODES=VMS}
6757 On VMS, GNAT compiled programs return POSIX-style codes by default,
6758 e.g. @option{/RETURN_CODES=POSIX}.
6760 To enable VMS style return codes, use GNAT BIND and LINK with the option
6761 @option{/RETURN_CODES=VMS}. For example:
6764 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
6765 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
6769 Programs built with /RETURN_CODES=VMS are suitable to be called in
6770 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
6771 are suitable for spawning with appropriate GNAT RTL routines.
6775 @node Search Paths and the Run-Time Library (RTL)
6776 @section Search Paths and the Run-Time Library (RTL)
6779 With the GNAT source-based library system, the compiler must be able to
6780 find source files for units that are needed by the unit being compiled.
6781 Search paths are used to guide this process.
6783 The compiler compiles one source file whose name must be given
6784 explicitly on the command line. In other words, no searching is done
6785 for this file. To find all other source files that are needed (the most
6786 common being the specs of units), the compiler examines the following
6787 directories, in the following order:
6791 The directory containing the source file of the main unit being compiled
6792 (the file name on the command line).
6795 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
6796 @command{gcc} command line, in the order given.
6799 @findex ADA_PRJ_INCLUDE_FILE
6800 Each of the directories listed in the text file whose name is given
6801 by the @code{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
6804 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
6805 driver when project files are used. It should not normally be set
6809 @findex ADA_INCLUDE_PATH
6810 Each of the directories listed in the value of the
6811 @code{ADA_INCLUDE_PATH} ^environment variable^logical name^.
6813 Construct this value
6814 exactly as the @code{PATH} environment variable: a list of directory
6815 names separated by colons (semicolons when working with the NT version).
6818 Normally, define this value as a logical name containing a comma separated
6819 list of directory names.
6821 This variable can also be defined by means of an environment string
6822 (an argument to the HP C exec* set of functions).
6826 DEFINE ANOTHER_PATH FOO:[BAG]
6827 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
6830 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
6831 first, followed by the standard Ada 95
6832 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
6833 If this is not redefined, the user will obtain the HP Ada 83 IO packages
6834 (Text_IO, Sequential_IO, etc)
6835 instead of the Ada95 packages. Thus, in order to get the Ada 95
6836 packages by default, ADA_INCLUDE_PATH must be redefined.
6840 The content of the @file{ada_source_path} file which is part of the GNAT
6841 installation tree and is used to store standard libraries such as the
6842 GNAT Run Time Library (RTL) source files.
6844 @ref{Installing a library}
6849 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
6850 inhibits the use of the directory
6851 containing the source file named in the command line. You can still
6852 have this directory on your search path, but in this case it must be
6853 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
6855 Specifying the switch @option{-nostdinc}
6856 inhibits the search of the default location for the GNAT Run Time
6857 Library (RTL) source files.
6859 The compiler outputs its object files and ALI files in the current
6862 Caution: The object file can be redirected with the @option{-o} switch;
6863 however, @command{gcc} and @code{gnat1} have not been coordinated on this
6864 so the @file{ALI} file will not go to the right place. Therefore, you should
6865 avoid using the @option{-o} switch.
6869 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
6870 children make up the GNAT RTL, together with the simple @code{System.IO}
6871 package used in the @code{"Hello World"} example. The sources for these units
6872 are needed by the compiler and are kept together in one directory. Not
6873 all of the bodies are needed, but all of the sources are kept together
6874 anyway. In a normal installation, you need not specify these directory
6875 names when compiling or binding. Either the environment variables or
6876 the built-in defaults cause these files to be found.
6878 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
6879 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
6880 consisting of child units of @code{GNAT}. This is a collection of generally
6881 useful types, subprograms, etc. See the @cite{GNAT Reference Manual} for
6884 Besides simplifying access to the RTL, a major use of search paths is
6885 in compiling sources from multiple directories. This can make
6886 development environments much more flexible.
6888 @node Order of Compilation Issues
6889 @section Order of Compilation Issues
6892 If, in our earlier example, there was a spec for the @code{hello}
6893 procedure, it would be contained in the file @file{hello.ads}; yet this
6894 file would not have to be explicitly compiled. This is the result of the
6895 model we chose to implement library management. Some of the consequences
6896 of this model are as follows:
6900 There is no point in compiling specs (except for package
6901 specs with no bodies) because these are compiled as needed by clients. If
6902 you attempt a useless compilation, you will receive an error message.
6903 It is also useless to compile subunits because they are compiled as needed
6907 There are no order of compilation requirements: performing a
6908 compilation never obsoletes anything. The only way you can obsolete
6909 something and require recompilations is to modify one of the
6910 source files on which it depends.
6913 There is no library as such, apart from the ALI files
6914 (@pxref{The Ada Library Information Files}, for information on the format
6915 of these files). For now we find it convenient to create separate ALI files,
6916 but eventually the information therein may be incorporated into the object
6920 When you compile a unit, the source files for the specs of all units
6921 that it @code{with}'s, all its subunits, and the bodies of any generics it
6922 instantiates must be available (reachable by the search-paths mechanism
6923 described above), or you will receive a fatal error message.
6930 The following are some typical Ada compilation command line examples:
6933 @item $ gcc -c xyz.adb
6934 Compile body in file @file{xyz.adb} with all default options.
6937 @item $ gcc -c -O2 -gnata xyz-def.adb
6940 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
6943 Compile the child unit package in file @file{xyz-def.adb} with extensive
6944 optimizations, and pragma @code{Assert}/@code{Debug} statements
6947 @item $ gcc -c -gnatc abc-def.adb
6948 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
6952 @node Binding Using gnatbind
6953 @chapter Binding Using @code{gnatbind}
6957 * Running gnatbind::
6958 * Switches for gnatbind::
6959 * Command-Line Access::
6960 * Search Paths for gnatbind::
6961 * Examples of gnatbind Usage::
6965 This chapter describes the GNAT binder, @code{gnatbind}, which is used
6966 to bind compiled GNAT objects. The @code{gnatbind} program performs
6967 four separate functions:
6971 Checks that a program is consistent, in accordance with the rules in
6972 Chapter 10 of the Ada 95 Reference Manual. In particular, error
6973 messages are generated if a program uses inconsistent versions of a
6977 Checks that an acceptable order of elaboration exists for the program
6978 and issues an error message if it cannot find an order of elaboration
6979 that satisfies the rules in Chapter 10 of the Ada 95 Language Manual.
6982 Generates a main program incorporating the given elaboration order.
6983 This program is a small Ada package (body and spec) that
6984 must be subsequently compiled
6985 using the GNAT compiler. The necessary compilation step is usually
6986 performed automatically by @command{gnatlink}. The two most important
6987 functions of this program
6988 are to call the elaboration routines of units in an appropriate order
6989 and to call the main program.
6992 Determines the set of object files required by the given main program.
6993 This information is output in the forms of comments in the generated program,
6994 to be read by the @command{gnatlink} utility used to link the Ada application.
6997 @node Running gnatbind
6998 @section Running @code{gnatbind}
7001 The form of the @code{gnatbind} command is
7004 $ gnatbind [@i{switches}] @i{mainprog}[.ali] [@i{switches}]
7008 where @file{@i{mainprog}.adb} is the Ada file containing the main program
7009 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7010 package in two files whose names are
7011 @file{b~@i{mainprog}.ads}, and @file{b~@i{mainprog}.adb}.
7012 For example, if given the
7013 parameter @file{hello.ali}, for a main program contained in file
7014 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7015 and @file{b~hello.adb}.
7017 When doing consistency checking, the binder takes into consideration
7018 any source files it can locate. For example, if the binder determines
7019 that the given main program requires the package @code{Pack}, whose
7021 file is @file{pack.ali} and whose corresponding source spec file is
7022 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7023 (using the same search path conventions as previously described for the
7024 @command{gcc} command). If it can locate this source file, it checks that
7026 or source checksums of the source and its references to in @file{ALI} files
7027 match. In other words, any @file{ALI} files that mentions this spec must have
7028 resulted from compiling this version of the source file (or in the case
7029 where the source checksums match, a version close enough that the
7030 difference does not matter).
7032 @cindex Source files, use by binder
7033 The effect of this consistency checking, which includes source files, is
7034 that the binder ensures that the program is consistent with the latest
7035 version of the source files that can be located at bind time. Editing a
7036 source file without compiling files that depend on the source file cause
7037 error messages to be generated by the binder.
7039 For example, suppose you have a main program @file{hello.adb} and a
7040 package @code{P}, from file @file{p.ads} and you perform the following
7045 Enter @code{gcc -c hello.adb} to compile the main program.
7048 Enter @code{gcc -c p.ads} to compile package @code{P}.
7051 Edit file @file{p.ads}.
7054 Enter @code{gnatbind hello}.
7058 At this point, the file @file{p.ali} contains an out-of-date time stamp
7059 because the file @file{p.ads} has been edited. The attempt at binding
7060 fails, and the binder generates the following error messages:
7063 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7064 error: "p.ads" has been modified and must be recompiled
7068 Now both files must be recompiled as indicated, and then the bind can
7069 succeed, generating a main program. You need not normally be concerned
7070 with the contents of this file, but for reference purposes a sample
7071 binder output file is given in @ref{Example of Binder Output File}.
7073 In most normal usage, the default mode of @command{gnatbind} which is to
7074 generate the main package in Ada, as described in the previous section.
7075 In particular, this means that any Ada programmer can read and understand
7076 the generated main program. It can also be debugged just like any other
7077 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7078 @command{gnatbind} and @command{gnatlink}.
7080 However for some purposes it may be convenient to generate the main
7081 program in C rather than Ada. This may for example be helpful when you
7082 are generating a mixed language program with the main program in C. The
7083 GNAT compiler itself is an example.
7084 The use of the @option{^-C^/BIND_FILE=C^} switch
7085 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7086 be generated in C (and compiled using the gnu C compiler).
7088 @node Switches for gnatbind
7089 @section Switches for @command{gnatbind}
7092 The following switches are available with @code{gnatbind}; details will
7093 be presented in subsequent sections.
7096 * Consistency-Checking Modes::
7097 * Binder Error Message Control::
7098 * Elaboration Control::
7100 * Binding with Non-Ada Main Programs::
7101 * Binding Programs with No Main Subprogram::
7106 @item ^-aO^/OBJECT_SEARCH^
7107 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
7108 Specify directory to be searched for ALI files.
7110 @item ^-aI^/SOURCE_SEARCH^
7111 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7112 Specify directory to be searched for source file.
7114 @item ^-A^/BIND_FILE=ADA^
7115 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
7116 Generate binder program in Ada (default)
7118 @item ^-b^/REPORT_ERRORS=BRIEF^
7119 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
7120 Generate brief messages to @file{stderr} even if verbose mode set.
7122 @item ^-c^/NOOUTPUT^
7123 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
7124 Check only, no generation of binder output file.
7126 @item ^-C^/BIND_FILE=C^
7127 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
7128 Generate binder program in C
7130 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}[k|m]
7131 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}[k|m]} (@command{gnatbind})
7132 This switch can be used to change the default task stack size value
7133 to a specified size @var{nn}, which is expressed in bytes by default, or
7134 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7136 In the absence of a [k|m] suffix, this switch is equivalent, in effect,
7137 to completing all task specs with
7138 @smallexample @c ada
7139 pragma Storage_Size (nn);
7141 When they do not already have such a pragma.
7143 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}[k|m]
7144 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
7145 This switch can be used to change the default secondary stack size value
7146 to a specified size @var{nn}, which is expressed in bytes by default, or
7147 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
7150 The secondary stack is used to deal with functions that return a variable
7151 sized result, for example a function returning an unconstrained
7152 String. There are two ways in which this secondary stack is allocated.
7154 For most targets, the secondary stack is growing on demand and is allocated
7155 as a chain of blocks in the heap. The -D option is not very
7156 relevant. It only give some control over the size of the allocated
7157 blocks (whose size is the minimum of the default secondary stack size value,
7158 and the actual size needed for the current allocation request).
7160 For certain targets, notably VxWorks 653,
7161 the secondary stack is allocated by carving off a fixed ratio chunk of the
7162 primary task stack. The -D option is used to defined the
7163 size of the environment task's secondary stack.
7165 @item ^-e^/ELABORATION_DEPENDENCIES^
7166 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
7167 Output complete list of elaboration-order dependencies.
7169 @item ^-E^/STORE_TRACEBACKS^
7170 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
7171 Store tracebacks in exception occurrences when the target supports it.
7172 This is the default with the zero cost exception mechanism.
7174 @c The following may get moved to an appendix
7175 This option is currently supported on the following targets:
7176 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
7178 See also the packages @code{GNAT.Traceback} and
7179 @code{GNAT.Traceback.Symbolic} for more information.
7181 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
7182 @command{gcc} option.
7185 @item ^-F^/FORCE_ELABS_FLAGS^
7186 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
7187 Force the checks of elaboration flags. @command{gnatbind} does not normally
7188 generate checks of elaboration flags for the main executable, except when
7189 a Stand-Alone Library is used. However, there are cases when this cannot be
7190 detected by gnatbind. An example is importing an interface of a Stand-Alone
7191 Library through a pragma Import and only specifying through a linker switch
7192 this Stand-Alone Library. This switch is used to guarantee that elaboration
7193 flag checks are generated.
7196 @cindex @option{^-h^/HELP^} (@command{gnatbind})
7197 Output usage (help) information
7200 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7201 Specify directory to be searched for source and ALI files.
7203 @item ^-I-^/NOCURRENT_DIRECTORY^
7204 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
7205 Do not look for sources in the current directory where @code{gnatbind} was
7206 invoked, and do not look for ALI files in the directory containing the
7207 ALI file named in the @code{gnatbind} command line.
7209 @item ^-l^/ORDER_OF_ELABORATION^
7210 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
7211 Output chosen elaboration order.
7213 @item ^-Lxxx^/BUILD_LIBRARY=xxx^
7214 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
7215 Bind the units for library building. In this case the adainit and
7216 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
7217 are renamed to ^xxxinit^XXXINIT^ and
7218 ^xxxfinal^XXXFINAL^.
7219 Implies ^-n^/NOCOMPILE^.
7221 (@xref{GNAT and Libraries}, for more details.)
7224 On OpenVMS, these init and final procedures are exported in uppercase
7225 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
7226 the init procedure will be "TOTOINIT" and the exported name of the final
7227 procedure will be "TOTOFINAL".
7230 @item ^-Mxyz^/RENAME_MAIN=xyz^
7231 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
7232 Rename generated main program from main to xyz. This option is
7233 supported on cross environments only.
7235 @item ^-m^/ERROR_LIMIT=^@var{n}
7236 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
7237 Limit number of detected errors to @var{n}, where @var{n} is
7238 in the range 1..999_999. The default value if no switch is
7239 given is 9999. Binding is terminated if the limit is exceeded.
7241 Furthermore, under Windows, the sources pointed to by the libraries path
7242 set in the registry are not searched for.
7246 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7250 @cindex @option{-nostdinc} (@command{gnatbind})
7251 Do not look for sources in the system default directory.
7254 @cindex @option{-nostdlib} (@command{gnatbind})
7255 Do not look for library files in the system default directory.
7257 @item --RTS=@var{rts-path}
7258 @cindex @option{--RTS} (@code{gnatbind})
7259 Specifies the default location of the runtime library. Same meaning as the
7260 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
7262 @item ^-o ^/OUTPUT=^@var{file}
7263 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7264 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7265 Note that if this option is used, then linking must be done manually,
7266 gnatlink cannot be used.
7268 @item ^-O^/OBJECT_LIST^
7269 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7272 @item ^-p^/PESSIMISTIC_ELABORATION^
7273 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7274 Pessimistic (worst-case) elaboration order
7276 @item ^-s^/READ_SOURCES=ALL^
7277 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7278 Require all source files to be present.
7280 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7281 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7282 Specifies the value to be used when detecting uninitialized scalar
7283 objects with pragma Initialize_Scalars.
7284 The @var{xxx} ^string specified with the switch^option^ may be either
7286 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7287 @item ``@option{^lo^LOW^}'' for the lowest possible value
7288 @item ``@option{^hi^HIGH^}'' for the highest possible value
7289 @item ``@option{xx}'' for a value consisting of repeated bytes with the
7290 value 16#xx# (i.e. xx is a string of two hexadecimal digits).
7293 In addition, you can specify @option{-Sev} to indicate that the value is
7294 to be set at run time. In this case, the program will look for an environment
7295 @cindex GNAT_INIT_SCALARS
7296 variable of the form @code{GNAT_INIT_SCALARS=xx}, where xx is one
7297 of @option{in/lo/hi/xx} with the same meanings as above.
7298 If no environment variable is found, or if it does not have a valid value,
7299 then the default is @option{in} (invalid values).
7303 @cindex @option{-static} (@code{gnatbind})
7304 Link against a static GNAT run time.
7307 @cindex @option{-shared} (@code{gnatbind})
7308 Link against a shared GNAT run time when available.
7311 @item ^-t^/NOTIME_STAMP_CHECK^
7312 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7313 Tolerate time stamp and other consistency errors
7315 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7316 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7317 Set the time slice value to @var{n} milliseconds. If the system supports
7318 the specification of a specific time slice value, then the indicated value
7319 is used. If the system does not support specific time slice values, but
7320 does support some general notion of round-robin scheduling, then any
7321 nonzero value will activate round-robin scheduling.
7323 A value of zero is treated specially. It turns off time
7324 slicing, and in addition, indicates to the tasking run time that the
7325 semantics should match as closely as possible the Annex D
7326 requirements of the Ada RM, and in particular sets the default
7327 scheduling policy to @code{FIFO_Within_Priorities}.
7330 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
7331 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
7332 Enable dynamic stack usage, with n result stored and displayed at program
7333 termination. Results that can't be stored are displayed on the fly, at task
7334 termination. This option is currently not supported on OpenVMS I64 platforms.
7336 @item ^-v^/REPORT_ERRORS=VERBOSE^
7337 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7338 Verbose mode. Write error messages, header, summary output to
7343 @cindex @option{-w} (@code{gnatbind})
7344 Warning mode (@var{x}=s/e for suppress/treat as error)
7348 @item /WARNINGS=NORMAL
7349 @cindex @option{/WARNINGS} (@code{gnatbind})
7350 Normal warnings mode. Warnings are issued but ignored
7352 @item /WARNINGS=SUPPRESS
7353 @cindex @option{/WARNINGS} (@code{gnatbind})
7354 All warning messages are suppressed
7356 @item /WARNINGS=ERROR
7357 @cindex @option{/WARNINGS} (@code{gnatbind})
7358 Warning messages are treated as fatal errors
7361 @item ^-x^/READ_SOURCES=NONE^
7362 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7363 Exclude source files (check object consistency only).
7366 @item /READ_SOURCES=AVAILABLE
7367 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7368 Default mode, in which sources are checked for consistency only if
7372 @item ^-z^/ZERO_MAIN^
7373 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7379 You may obtain this listing of switches by running @code{gnatbind} with
7383 @node Consistency-Checking Modes
7384 @subsection Consistency-Checking Modes
7387 As described earlier, by default @code{gnatbind} checks
7388 that object files are consistent with one another and are consistent
7389 with any source files it can locate. The following switches control binder
7394 @item ^-s^/READ_SOURCES=ALL^
7395 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
7396 Require source files to be present. In this mode, the binder must be
7397 able to locate all source files that are referenced, in order to check
7398 their consistency. In normal mode, if a source file cannot be located it
7399 is simply ignored. If you specify this switch, a missing source
7402 @item ^-x^/READ_SOURCES=NONE^
7403 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
7404 Exclude source files. In this mode, the binder only checks that ALI
7405 files are consistent with one another. Source files are not accessed.
7406 The binder runs faster in this mode, and there is still a guarantee that
7407 the resulting program is self-consistent.
7408 If a source file has been edited since it was last compiled, and you
7409 specify this switch, the binder will not detect that the object
7410 file is out of date with respect to the source file. Note that this is the
7411 mode that is automatically used by @command{gnatmake} because in this
7412 case the checking against sources has already been performed by
7413 @command{gnatmake} in the course of compilation (i.e. before binding).
7416 @item /READ_SOURCES=AVAILABLE
7417 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
7418 This is the default mode in which source files are checked if they are
7419 available, and ignored if they are not available.
7423 @node Binder Error Message Control
7424 @subsection Binder Error Message Control
7427 The following switches provide control over the generation of error
7428 messages from the binder:
7432 @item ^-v^/REPORT_ERRORS=VERBOSE^
7433 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7434 Verbose mode. In the normal mode, brief error messages are generated to
7435 @file{stderr}. If this switch is present, a header is written
7436 to @file{stdout} and any error messages are directed to @file{stdout}.
7437 All that is written to @file{stderr} is a brief summary message.
7439 @item ^-b^/REPORT_ERRORS=BRIEF^
7440 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
7441 Generate brief error messages to @file{stderr} even if verbose mode is
7442 specified. This is relevant only when used with the
7443 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
7447 @cindex @option{-m} (@code{gnatbind})
7448 Limits the number of error messages to @var{n}, a decimal integer in the
7449 range 1-999. The binder terminates immediately if this limit is reached.
7452 @cindex @option{-M} (@code{gnatbind})
7453 Renames the generated main program from @code{main} to @code{xxx}.
7454 This is useful in the case of some cross-building environments, where
7455 the actual main program is separate from the one generated
7459 @item ^-ws^/WARNINGS=SUPPRESS^
7460 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
7462 Suppress all warning messages.
7464 @item ^-we^/WARNINGS=ERROR^
7465 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
7466 Treat any warning messages as fatal errors.
7469 @item /WARNINGS=NORMAL
7470 Standard mode with warnings generated, but warnings do not get treated
7474 @item ^-t^/NOTIME_STAMP_CHECK^
7475 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7476 @cindex Time stamp checks, in binder
7477 @cindex Binder consistency checks
7478 @cindex Consistency checks, in binder
7479 The binder performs a number of consistency checks including:
7483 Check that time stamps of a given source unit are consistent
7485 Check that checksums of a given source unit are consistent
7487 Check that consistent versions of @code{GNAT} were used for compilation
7489 Check consistency of configuration pragmas as required
7493 Normally failure of such checks, in accordance with the consistency
7494 requirements of the Ada Reference Manual, causes error messages to be
7495 generated which abort the binder and prevent the output of a binder
7496 file and subsequent link to obtain an executable.
7498 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
7499 into warnings, so that
7500 binding and linking can continue to completion even in the presence of such
7501 errors. The result may be a failed link (due to missing symbols), or a
7502 non-functional executable which has undefined semantics.
7503 @emph{This means that
7504 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
7508 @node Elaboration Control
7509 @subsection Elaboration Control
7512 The following switches provide additional control over the elaboration
7513 order. For full details see @ref{Elaboration Order Handling in GNAT}.
7516 @item ^-p^/PESSIMISTIC_ELABORATION^
7517 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
7518 Normally the binder attempts to choose an elaboration order that is
7519 likely to minimize the likelihood of an elaboration order error resulting
7520 in raising a @code{Program_Error} exception. This switch reverses the
7521 action of the binder, and requests that it deliberately choose an order
7522 that is likely to maximize the likelihood of an elaboration error.
7523 This is useful in ensuring portability and avoiding dependence on
7524 accidental fortuitous elaboration ordering.
7526 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
7528 elaboration checking is used (@option{-gnatE} switch used for compilation).
7529 This is because in the default static elaboration mode, all necessary
7530 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
7531 These implicit pragmas are still respected by the binder in
7532 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
7533 safe elaboration order is assured.
7536 @node Output Control
7537 @subsection Output Control
7540 The following switches allow additional control over the output
7541 generated by the binder.
7546 @item ^-A^/BIND_FILE=ADA^
7547 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
7548 Generate binder program in Ada (default). The binder program is named
7549 @file{b~@var{mainprog}.adb} by default. This can be changed with
7550 @option{^-o^/OUTPUT^} @code{gnatbind} option.
7552 @item ^-c^/NOOUTPUT^
7553 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
7554 Check only. Do not generate the binder output file. In this mode the
7555 binder performs all error checks but does not generate an output file.
7557 @item ^-C^/BIND_FILE=C^
7558 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
7559 Generate binder program in C. The binder program is named
7560 @file{b_@var{mainprog}.c}.
7561 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
7564 @item ^-e^/ELABORATION_DEPENDENCIES^
7565 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
7566 Output complete list of elaboration-order dependencies, showing the
7567 reason for each dependency. This output can be rather extensive but may
7568 be useful in diagnosing problems with elaboration order. The output is
7569 written to @file{stdout}.
7572 @cindex @option{^-h^/HELP^} (@code{gnatbind})
7573 Output usage information. The output is written to @file{stdout}.
7575 @item ^-K^/LINKER_OPTION_LIST^
7576 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
7577 Output linker options to @file{stdout}. Includes library search paths,
7578 contents of pragmas Ident and Linker_Options, and libraries added
7581 @item ^-l^/ORDER_OF_ELABORATION^
7582 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
7583 Output chosen elaboration order. The output is written to @file{stdout}.
7585 @item ^-O^/OBJECT_LIST^
7586 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
7587 Output full names of all the object files that must be linked to provide
7588 the Ada component of the program. The output is written to @file{stdout}.
7589 This list includes the files explicitly supplied and referenced by the user
7590 as well as implicitly referenced run-time unit files. The latter are
7591 omitted if the corresponding units reside in shared libraries. The
7592 directory names for the run-time units depend on the system configuration.
7594 @item ^-o ^/OUTPUT=^@var{file}
7595 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
7596 Set name of output file to @var{file} instead of the normal
7597 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
7598 binder generated body filename. In C mode you would normally give
7599 @var{file} an extension of @file{.c} because it will be a C source program.
7600 Note that if this option is used, then linking must be done manually.
7601 It is not possible to use gnatlink in this case, since it cannot locate
7604 @item ^-r^/RESTRICTION_LIST^
7605 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
7606 Generate list of @code{pragma Restrictions} that could be applied to
7607 the current unit. This is useful for code audit purposes, and also may
7608 be used to improve code generation in some cases.
7612 @node Binding with Non-Ada Main Programs
7613 @subsection Binding with Non-Ada Main Programs
7616 In our description so far we have assumed that the main
7617 program is in Ada, and that the task of the binder is to generate a
7618 corresponding function @code{main} that invokes this Ada main
7619 program. GNAT also supports the building of executable programs where
7620 the main program is not in Ada, but some of the called routines are
7621 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
7622 The following switch is used in this situation:
7626 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
7627 No main program. The main program is not in Ada.
7631 In this case, most of the functions of the binder are still required,
7632 but instead of generating a main program, the binder generates a file
7633 containing the following callable routines:
7638 You must call this routine to initialize the Ada part of the program by
7639 calling the necessary elaboration routines. A call to @code{adainit} is
7640 required before the first call to an Ada subprogram.
7642 Note that it is assumed that the basic execution environment must be setup
7643 to be appropriate for Ada execution at the point where the first Ada
7644 subprogram is called. In particular, if the Ada code will do any
7645 floating-point operations, then the FPU must be setup in an appropriate
7646 manner. For the case of the x86, for example, full precision mode is
7647 required. The procedure GNAT.Float_Control.Reset may be used to ensure
7648 that the FPU is in the right state.
7652 You must call this routine to perform any library-level finalization
7653 required by the Ada subprograms. A call to @code{adafinal} is required
7654 after the last call to an Ada subprogram, and before the program
7659 If the @option{^-n^/NOMAIN^} switch
7660 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7661 @cindex Binder, multiple input files
7662 is given, more than one ALI file may appear on
7663 the command line for @code{gnatbind}. The normal @dfn{closure}
7664 calculation is performed for each of the specified units. Calculating
7665 the closure means finding out the set of units involved by tracing
7666 @code{with} references. The reason it is necessary to be able to
7667 specify more than one ALI file is that a given program may invoke two or
7668 more quite separate groups of Ada units.
7670 The binder takes the name of its output file from the last specified ALI
7671 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
7672 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
7673 The output is an Ada unit in source form that can
7674 be compiled with GNAT unless the -C switch is used in which case the
7675 output is a C source file, which must be compiled using the C compiler.
7676 This compilation occurs automatically as part of the @command{gnatlink}
7679 Currently the GNAT run time requires a FPU using 80 bits mode
7680 precision. Under targets where this is not the default it is required to
7681 call GNAT.Float_Control.Reset before using floating point numbers (this
7682 include float computation, float input and output) in the Ada code. A
7683 side effect is that this could be the wrong mode for the foreign code
7684 where floating point computation could be broken after this call.
7686 @node Binding Programs with No Main Subprogram
7687 @subsection Binding Programs with No Main Subprogram
7690 It is possible to have an Ada program which does not have a main
7691 subprogram. This program will call the elaboration routines of all the
7692 packages, then the finalization routines.
7694 The following switch is used to bind programs organized in this manner:
7697 @item ^-z^/ZERO_MAIN^
7698 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7699 Normally the binder checks that the unit name given on the command line
7700 corresponds to a suitable main subprogram. When this switch is used,
7701 a list of ALI files can be given, and the execution of the program
7702 consists of elaboration of these units in an appropriate order.
7705 @node Command-Line Access
7706 @section Command-Line Access
7709 The package @code{Ada.Command_Line} provides access to the command-line
7710 arguments and program name. In order for this interface to operate
7711 correctly, the two variables
7723 are declared in one of the GNAT library routines. These variables must
7724 be set from the actual @code{argc} and @code{argv} values passed to the
7725 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
7726 generates the C main program to automatically set these variables.
7727 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
7728 set these variables. If they are not set, the procedures in
7729 @code{Ada.Command_Line} will not be available, and any attempt to use
7730 them will raise @code{Constraint_Error}. If command line access is
7731 required, your main program must set @code{gnat_argc} and
7732 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
7735 @node Search Paths for gnatbind
7736 @section Search Paths for @code{gnatbind}
7739 The binder takes the name of an ALI file as its argument and needs to
7740 locate source files as well as other ALI files to verify object consistency.
7742 For source files, it follows exactly the same search rules as @command{gcc}
7743 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
7744 directories searched are:
7748 The directory containing the ALI file named in the command line, unless
7749 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
7752 All directories specified by @option{^-I^/SEARCH^}
7753 switches on the @code{gnatbind}
7754 command line, in the order given.
7757 @findex ADA_PRJ_OBJECTS_FILE
7758 Each of the directories listed in the text file whose name is given
7759 by the @code{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
7762 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7763 driver when project files are used. It should not normally be set
7767 @findex ADA_OBJECTS_PATH
7768 Each of the directories listed in the value of the
7769 @code{ADA_OBJECTS_PATH} ^environment variable^logical name^.
7771 Construct this value
7772 exactly as the @code{PATH} environment variable: a list of directory
7773 names separated by colons (semicolons when working with the NT version
7777 Normally, define this value as a logical name containing a comma separated
7778 list of directory names.
7780 This variable can also be defined by means of an environment string
7781 (an argument to the HP C exec* set of functions).
7785 DEFINE ANOTHER_PATH FOO:[BAG]
7786 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7789 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7790 first, followed by the standard Ada 95
7791 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
7792 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7793 (Text_IO, Sequential_IO, etc)
7794 instead of the Ada95 packages. Thus, in order to get the Ada 95
7795 packages by default, ADA_OBJECTS_PATH must be redefined.
7799 The content of the @file{ada_object_path} file which is part of the GNAT
7800 installation tree and is used to store standard libraries such as the
7801 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
7804 @ref{Installing a library}
7809 In the binder the switch @option{^-I^/SEARCH^}
7810 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7811 is used to specify both source and
7812 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
7813 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7814 instead if you want to specify
7815 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
7816 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
7817 if you want to specify library paths
7818 only. This means that for the binder
7819 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
7820 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
7821 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
7822 The binder generates the bind file (a C language source file) in the
7823 current working directory.
7829 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7830 children make up the GNAT Run-Time Library, together with the package
7831 GNAT and its children, which contain a set of useful additional
7832 library functions provided by GNAT. The sources for these units are
7833 needed by the compiler and are kept together in one directory. The ALI
7834 files and object files generated by compiling the RTL are needed by the
7835 binder and the linker and are kept together in one directory, typically
7836 different from the directory containing the sources. In a normal
7837 installation, you need not specify these directory names when compiling
7838 or binding. Either the environment variables or the built-in defaults
7839 cause these files to be found.
7841 Besides simplifying access to the RTL, a major use of search paths is
7842 in compiling sources from multiple directories. This can make
7843 development environments much more flexible.
7845 @node Examples of gnatbind Usage
7846 @section Examples of @code{gnatbind} Usage
7849 This section contains a number of examples of using the GNAT binding
7850 utility @code{gnatbind}.
7853 @item gnatbind hello
7854 The main program @code{Hello} (source program in @file{hello.adb}) is
7855 bound using the standard switch settings. The generated main program is
7856 @file{b~hello.adb}. This is the normal, default use of the binder.
7859 @item gnatbind hello -o mainprog.adb
7862 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
7864 The main program @code{Hello} (source program in @file{hello.adb}) is
7865 bound using the standard switch settings. The generated main program is
7866 @file{mainprog.adb} with the associated spec in
7867 @file{mainprog.ads}. Note that you must specify the body here not the
7868 spec, in the case where the output is in Ada. Note that if this option
7869 is used, then linking must be done manually, since gnatlink will not
7870 be able to find the generated file.
7873 @item gnatbind main -C -o mainprog.c -x
7876 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
7878 The main program @code{Main} (source program in
7879 @file{main.adb}) is bound, excluding source files from the
7880 consistency checking, generating
7881 the file @file{mainprog.c}.
7884 @item gnatbind -x main_program -C -o mainprog.c
7885 This command is exactly the same as the previous example. Switches may
7886 appear anywhere in the command line, and single letter switches may be
7887 combined into a single switch.
7891 @item gnatbind -n math dbase -C -o ada-control.c
7894 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
7896 The main program is in a language other than Ada, but calls to
7897 subprograms in packages @code{Math} and @code{Dbase} appear. This call
7898 to @code{gnatbind} generates the file @file{ada-control.c} containing
7899 the @code{adainit} and @code{adafinal} routines to be called before and
7900 after accessing the Ada units.
7903 @c ------------------------------------
7904 @node Linking Using gnatlink
7905 @chapter Linking Using @command{gnatlink}
7906 @c ------------------------------------
7910 This chapter discusses @command{gnatlink}, a tool that links
7911 an Ada program and builds an executable file. This utility
7912 invokes the system linker ^(via the @command{gcc} command)^^
7913 with a correct list of object files and library references.
7914 @command{gnatlink} automatically determines the list of files and
7915 references for the Ada part of a program. It uses the binder file
7916 generated by the @command{gnatbind} to determine this list.
7919 * Running gnatlink::
7920 * Switches for gnatlink::
7923 @node Running gnatlink
7924 @section Running @command{gnatlink}
7927 The form of the @command{gnatlink} command is
7930 $ gnatlink [@var{switches}] @var{mainprog}[.ali]
7931 [@var{non-Ada objects}] [@var{linker options}]
7935 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
7937 or linker options) may be in any order, provided that no non-Ada object may
7938 be mistaken for a main @file{ALI} file.
7939 Any file name @file{F} without the @file{.ali}
7940 extension will be taken as the main @file{ALI} file if a file exists
7941 whose name is the concatenation of @file{F} and @file{.ali}.
7944 @file{@var{mainprog}.ali} references the ALI file of the main program.
7945 The @file{.ali} extension of this file can be omitted. From this
7946 reference, @command{gnatlink} locates the corresponding binder file
7947 @file{b~@var{mainprog}.adb} and, using the information in this file along
7948 with the list of non-Ada objects and linker options, constructs a
7949 linker command file to create the executable.
7951 The arguments other than the @command{gnatlink} switches and the main
7952 @file{ALI} file are passed to the linker uninterpreted.
7953 They typically include the names of
7954 object files for units written in other languages than Ada and any library
7955 references required to resolve references in any of these foreign language
7956 units, or in @code{Import} pragmas in any Ada units.
7958 @var{linker options} is an optional list of linker specific
7960 The default linker called by gnatlink is @var{gcc} which in
7961 turn calls the appropriate system linker.
7962 Standard options for the linker such as @option{-lmy_lib} or
7963 @option{-Ldir} can be added as is.
7964 For options that are not recognized by
7965 @var{gcc} as linker options, use the @var{gcc} switches @option{-Xlinker} or
7967 Refer to the GCC documentation for
7968 details. Here is an example showing how to generate a linker map:
7971 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
7974 Using @var{linker options} it is possible to set the program stack and
7977 See @ref{Setting Stack Size from gnatlink} and
7978 @ref{Setting Heap Size from gnatlink}.
7981 @command{gnatlink} determines the list of objects required by the Ada
7982 program and prepends them to the list of objects passed to the linker.
7983 @command{gnatlink} also gathers any arguments set by the use of
7984 @code{pragma Linker_Options} and adds them to the list of arguments
7985 presented to the linker.
7988 @command{gnatlink} accepts the following types of extra files on the command
7989 line: objects (.OBJ), libraries (.OLB), sharable images (.EXE), and
7990 options files (.OPT). These are recognized and handled according to their
7994 @node Switches for gnatlink
7995 @section Switches for @command{gnatlink}
7998 The following switches are available with the @command{gnatlink} utility:
8003 @item ^-A^/BIND_FILE=ADA^
8004 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8005 The binder has generated code in Ada. This is the default.
8007 @item ^-C^/BIND_FILE=C^
8008 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8009 If instead of generating a file in Ada, the binder has generated one in
8010 C, then the linker needs to know about it. Use this switch to signal
8011 to @command{gnatlink} that the binder has generated C code rather than
8014 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8015 @cindex Command line length
8016 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8017 On some targets, the command line length is limited, and @command{gnatlink}
8018 will generate a separate file for the linker if the list of object files
8020 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8021 to be generated even if
8022 the limit is not exceeded. This is useful in some cases to deal with
8023 special situations where the command line length is exceeded.
8026 @cindex Debugging information, including
8027 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8028 The option to include debugging information causes the Ada bind file (in
8029 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8030 @option{^-g^/DEBUG^}.
8031 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
8032 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
8033 Without @option{^-g^/DEBUG^}, the binder removes these files by
8034 default. The same procedure apply if a C bind file was generated using
8035 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
8036 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
8038 @item ^-n^/NOCOMPILE^
8039 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
8040 Do not compile the file generated by the binder. This may be used when
8041 a link is rerun with different options, but there is no need to recompile
8045 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
8046 Causes additional information to be output, including a full list of the
8047 included object files. This switch option is most useful when you want
8048 to see what set of object files are being used in the link step.
8050 @item ^-v -v^/VERBOSE/VERBOSE^
8051 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
8052 Very verbose mode. Requests that the compiler operate in verbose mode when
8053 it compiles the binder file, and that the system linker run in verbose mode.
8055 @item ^-o ^/EXECUTABLE=^@var{exec-name}
8056 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
8057 @var{exec-name} specifies an alternate name for the generated
8058 executable program. If this switch is omitted, the executable has the same
8059 name as the main unit. For example, @code{gnatlink try.ali} creates
8060 an executable called @file{^try^TRY.EXE^}.
8063 @item -b @var{target}
8064 @cindex @option{-b} (@command{gnatlink})
8065 Compile your program to run on @var{target}, which is the name of a
8066 system configuration. You must have a GNAT cross-compiler built if
8067 @var{target} is not the same as your host system.
8070 @cindex @option{-B} (@command{gnatlink})
8071 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8072 from @var{dir} instead of the default location. Only use this switch
8073 when multiple versions of the GNAT compiler are available. See the
8074 @command{gcc} manual page for further details. You would normally use the
8075 @option{-b} or @option{-V} switch instead.
8077 @item --GCC=@var{compiler_name}
8078 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
8079 Program used for compiling the binder file. The default is
8080 @command{gcc}. You need to use quotes around @var{compiler_name} if
8081 @code{compiler_name} contains spaces or other separator characters.
8082 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
8083 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
8084 inserted after your command name. Thus in the above example the compiler
8085 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
8086 A limitation of this syntax is that the name and path name of the executable
8087 itself must not include any embedded spaces. If several
8088 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
8089 is taken into account. However, all the additional switches are also taken
8091 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8092 @option{--GCC="bar -x -y -z -t"}.
8094 @item --LINK=@var{name}
8095 @cindex @option{--LINK=} (@command{gnatlink})
8096 @var{name} is the name of the linker to be invoked. This is especially
8097 useful in mixed language programs since languages such as C++ require
8098 their own linker to be used. When this switch is omitted, the default
8099 name for the linker is @command{gcc}. When this switch is used, the
8100 specified linker is called instead of @command{gcc} with exactly the same
8101 parameters that would have been passed to @command{gcc} so if the desired
8102 linker requires different parameters it is necessary to use a wrapper
8103 script that massages the parameters before invoking the real linker. It
8104 may be useful to control the exact invocation by using the verbose
8110 @item /DEBUG=TRACEBACK
8111 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
8112 This qualifier causes sufficient information to be included in the
8113 executable file to allow a traceback, but does not include the full
8114 symbol information needed by the debugger.
8116 @item /IDENTIFICATION="<string>"
8117 @code{"<string>"} specifies the string to be stored in the image file
8118 identification field in the image header.
8119 It overrides any pragma @code{Ident} specified string.
8121 @item /NOINHIBIT-EXEC
8122 Generate the executable file even if there are linker warnings.
8124 @item /NOSTART_FILES
8125 Don't link in the object file containing the ``main'' transfer address.
8126 Used when linking with a foreign language main program compiled with an
8130 Prefer linking with object libraries over sharable images, even without
8137 @node The GNAT Make Program gnatmake
8138 @chapter The GNAT Make Program @command{gnatmake}
8142 * Running gnatmake::
8143 * Switches for gnatmake::
8144 * Mode Switches for gnatmake::
8145 * Notes on the Command Line::
8146 * How gnatmake Works::
8147 * Examples of gnatmake Usage::
8150 A typical development cycle when working on an Ada program consists of
8151 the following steps:
8155 Edit some sources to fix bugs.
8161 Compile all sources affected.
8171 The third step can be tricky, because not only do the modified files
8172 @cindex Dependency rules
8173 have to be compiled, but any files depending on these files must also be
8174 recompiled. The dependency rules in Ada can be quite complex, especially
8175 in the presence of overloading, @code{use} clauses, generics and inlined
8178 @command{gnatmake} automatically takes care of the third and fourth steps
8179 of this process. It determines which sources need to be compiled,
8180 compiles them, and binds and links the resulting object files.
8182 Unlike some other Ada make programs, the dependencies are always
8183 accurately recomputed from the new sources. The source based approach of
8184 the GNAT compilation model makes this possible. This means that if
8185 changes to the source program cause corresponding changes in
8186 dependencies, they will always be tracked exactly correctly by
8189 @node Running gnatmake
8190 @section Running @command{gnatmake}
8193 The usual form of the @command{gnatmake} command is
8196 $ gnatmake [@var{switches}] @var{file_name}
8197 [@var{file_names}] [@var{mode_switches}]
8201 The only required argument is one @var{file_name}, which specifies
8202 a compilation unit that is a main program. Several @var{file_names} can be
8203 specified: this will result in several executables being built.
8204 If @code{switches} are present, they can be placed before the first
8205 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8206 If @var{mode_switches} are present, they must always be placed after
8207 the last @var{file_name} and all @code{switches}.
8209 If you are using standard file extensions (.adb and .ads), then the
8210 extension may be omitted from the @var{file_name} arguments. However, if
8211 you are using non-standard extensions, then it is required that the
8212 extension be given. A relative or absolute directory path can be
8213 specified in a @var{file_name}, in which case, the input source file will
8214 be searched for in the specified directory only. Otherwise, the input
8215 source file will first be searched in the directory where
8216 @command{gnatmake} was invoked and if it is not found, it will be search on
8217 the source path of the compiler as described in
8218 @ref{Search Paths and the Run-Time Library (RTL)}.
8220 All @command{gnatmake} output (except when you specify
8221 @option{^-M^/DEPENDENCIES_LIST^}) is to
8222 @file{stderr}. The output produced by the
8223 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8226 @node Switches for gnatmake
8227 @section Switches for @command{gnatmake}
8230 You may specify any of the following switches to @command{gnatmake}:
8235 @item --GCC=@var{compiler_name}
8236 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
8237 Program used for compiling. The default is `@command{gcc}'. You need to use
8238 quotes around @var{compiler_name} if @code{compiler_name} contains
8239 spaces or other separator characters. As an example @option{--GCC="foo -x
8240 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
8241 compiler. A limitation of this syntax is that the name and path name of
8242 the executable itself must not include any embedded spaces. Note that
8243 switch @option{-c} is always inserted after your command name. Thus in the
8244 above example the compiler command that will be used by @command{gnatmake}
8245 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
8246 used, only the last @var{compiler_name} is taken into account. However,
8247 all the additional switches are also taken into account. Thus,
8248 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8249 @option{--GCC="bar -x -y -z -t"}.
8251 @item --GNATBIND=@var{binder_name}
8252 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
8253 Program used for binding. The default is `@code{gnatbind}'. You need to
8254 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8255 or other separator characters. As an example @option{--GNATBIND="bar -x
8256 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
8257 binder. Binder switches that are normally appended by @command{gnatmake}
8258 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8259 A limitation of this syntax is that the name and path name of the executable
8260 itself must not include any embedded spaces.
8262 @item --GNATLINK=@var{linker_name}
8263 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
8264 Program used for linking. The default is `@command{gnatlink}'. You need to
8265 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8266 or other separator characters. As an example @option{--GNATLINK="lan -x
8267 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
8268 linker. Linker switches that are normally appended by @command{gnatmake} to
8269 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
8270 A limitation of this syntax is that the name and path name of the executable
8271 itself must not include any embedded spaces.
8275 @item ^-a^/ALL_FILES^
8276 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
8277 Consider all files in the make process, even the GNAT internal system
8278 files (for example, the predefined Ada library files), as well as any
8279 locked files. Locked files are files whose ALI file is write-protected.
8281 @command{gnatmake} does not check these files,
8282 because the assumption is that the GNAT internal files are properly up
8283 to date, and also that any write protected ALI files have been properly
8284 installed. Note that if there is an installation problem, such that one
8285 of these files is not up to date, it will be properly caught by the
8287 You may have to specify this switch if you are working on GNAT
8288 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8289 in conjunction with @option{^-f^/FORCE_COMPILE^}
8290 if you need to recompile an entire application,
8291 including run-time files, using special configuration pragmas,
8292 such as a @code{Normalize_Scalars} pragma.
8295 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8298 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8301 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8304 @item ^-b^/ACTIONS=BIND^
8305 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
8306 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8307 compilation and binding, but no link.
8308 Can be combined with @option{^-l^/ACTIONS=LINK^}
8309 to do binding and linking. When not combined with
8310 @option{^-c^/ACTIONS=COMPILE^}
8311 all the units in the closure of the main program must have been previously
8312 compiled and must be up to date. The root unit specified by @var{file_name}
8313 may be given without extension, with the source extension or, if no GNAT
8314 Project File is specified, with the ALI file extension.
8316 @item ^-c^/ACTIONS=COMPILE^
8317 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
8318 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8319 is also specified. Do not perform linking, except if both
8320 @option{^-b^/ACTIONS=BIND^} and
8321 @option{^-l^/ACTIONS=LINK^} are also specified.
8322 If the root unit specified by @var{file_name} is not a main unit, this is the
8323 default. Otherwise @command{gnatmake} will attempt binding and linking
8324 unless all objects are up to date and the executable is more recent than
8328 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
8329 Use a temporary mapping file. A mapping file is a way to communicate to the
8330 compiler two mappings: from unit names to file names (without any directory
8331 information) and from file names to path names (with full directory
8332 information). These mappings are used by the compiler to short-circuit the path
8333 search. When @command{gnatmake} is invoked with this switch, it will create
8334 a temporary mapping file, initially populated by the project manager,
8335 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
8336 Each invocation of the compiler will add the newly accessed sources to the
8337 mapping file. This will improve the source search during the next invocation
8340 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
8341 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
8342 Use a specific mapping file. The file, specified as a path name (absolute or
8343 relative) by this switch, should already exist, otherwise the switch is
8344 ineffective. The specified mapping file will be communicated to the compiler.
8345 This switch is not compatible with a project file
8346 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
8347 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
8349 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
8350 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
8351 Put all object files and ALI file in directory @var{dir}.
8352 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
8353 and ALI files go in the current working directory.
8355 This switch cannot be used when using a project file.
8359 @cindex @option{-eL} (@command{gnatmake})
8360 Follow all symbolic links when processing project files.
8363 @item ^-f^/FORCE_COMPILE^
8364 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
8365 Force recompilations. Recompile all sources, even though some object
8366 files may be up to date, but don't recompile predefined or GNAT internal
8367 files or locked files (files with a write-protected ALI file),
8368 unless the @option{^-a^/ALL_FILES^} switch is also specified.
8370 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
8371 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
8372 When using project files, if some errors or warnings are detected during
8373 parsing and verbose mode is not in effect (no use of switch
8374 ^-v^/VERBOSE^), then error lines start with the full path name of the project
8375 file, rather than its simple file name.
8377 @item ^-i^/IN_PLACE^
8378 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
8379 In normal mode, @command{gnatmake} compiles all object files and ALI files
8380 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
8381 then instead object files and ALI files that already exist are overwritten
8382 in place. This means that once a large project is organized into separate
8383 directories in the desired manner, then @command{gnatmake} will automatically
8384 maintain and update this organization. If no ALI files are found on the
8385 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
8386 the new object and ALI files are created in the
8387 directory containing the source being compiled. If another organization
8388 is desired, where objects and sources are kept in different directories,
8389 a useful technique is to create dummy ALI files in the desired directories.
8390 When detecting such a dummy file, @command{gnatmake} will be forced to
8391 recompile the corresponding source file, and it will be put the resulting
8392 object and ALI files in the directory where it found the dummy file.
8394 @item ^-j^/PROCESSES=^@var{n}
8395 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
8396 @cindex Parallel make
8397 Use @var{n} processes to carry out the (re)compilations. On a
8398 multiprocessor machine compilations will occur in parallel. In the
8399 event of compilation errors, messages from various compilations might
8400 get interspersed (but @command{gnatmake} will give you the full ordered
8401 list of failing compiles at the end). If this is problematic, rerun
8402 the make process with n set to 1 to get a clean list of messages.
8404 @item ^-k^/CONTINUE_ON_ERROR^
8405 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
8406 Keep going. Continue as much as possible after a compilation error. To
8407 ease the programmer's task in case of compilation errors, the list of
8408 sources for which the compile fails is given when @command{gnatmake}
8411 If @command{gnatmake} is invoked with several @file{file_names} and with this
8412 switch, if there are compilation errors when building an executable,
8413 @command{gnatmake} will not attempt to build the following executables.
8415 @item ^-l^/ACTIONS=LINK^
8416 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
8417 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
8418 and linking. Linking will not be performed if combined with
8419 @option{^-c^/ACTIONS=COMPILE^}
8420 but not with @option{^-b^/ACTIONS=BIND^}.
8421 When not combined with @option{^-b^/ACTIONS=BIND^}
8422 all the units in the closure of the main program must have been previously
8423 compiled and must be up to date, and the main program needs to have been bound.
8424 The root unit specified by @var{file_name}
8425 may be given without extension, with the source extension or, if no GNAT
8426 Project File is specified, with the ALI file extension.
8428 @item ^-m^/MINIMAL_RECOMPILATION^
8429 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
8430 Specify that the minimum necessary amount of recompilations
8431 be performed. In this mode @command{gnatmake} ignores time
8432 stamp differences when the only
8433 modifications to a source file consist in adding/removing comments,
8434 empty lines, spaces or tabs. This means that if you have changed the
8435 comments in a source file or have simply reformatted it, using this
8436 switch will tell gnatmake not to recompile files that depend on it
8437 (provided other sources on which these files depend have undergone no
8438 semantic modifications). Note that the debugging information may be
8439 out of date with respect to the sources if the @option{-m} switch causes
8440 a compilation to be switched, so the use of this switch represents a
8441 trade-off between compilation time and accurate debugging information.
8443 @item ^-M^/DEPENDENCIES_LIST^
8444 @cindex Dependencies, producing list
8445 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
8446 Check if all objects are up to date. If they are, output the object
8447 dependences to @file{stdout} in a form that can be directly exploited in
8448 a @file{Makefile}. By default, each source file is prefixed with its
8449 (relative or absolute) directory name. This name is whatever you
8450 specified in the various @option{^-aI^/SOURCE_SEARCH^}
8451 and @option{^-I^/SEARCH^} switches. If you use
8452 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
8453 @option{^-q^/QUIET^}
8454 (see below), only the source file names,
8455 without relative paths, are output. If you just specify the
8456 @option{^-M^/DEPENDENCIES_LIST^}
8457 switch, dependencies of the GNAT internal system files are omitted. This
8458 is typically what you want. If you also specify
8459 the @option{^-a^/ALL_FILES^} switch,
8460 dependencies of the GNAT internal files are also listed. Note that
8461 dependencies of the objects in external Ada libraries (see switch
8462 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
8465 @item ^-n^/DO_OBJECT_CHECK^
8466 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
8467 Don't compile, bind, or link. Checks if all objects are up to date.
8468 If they are not, the full name of the first file that needs to be
8469 recompiled is printed.
8470 Repeated use of this option, followed by compiling the indicated source
8471 file, will eventually result in recompiling all required units.
8473 @item ^-o ^/EXECUTABLE=^@var{exec_name}
8474 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
8475 Output executable name. The name of the final executable program will be
8476 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
8477 name for the executable will be the name of the input file in appropriate form
8478 for an executable file on the host system.
8480 This switch cannot be used when invoking @command{gnatmake} with several
8483 @item ^-P^/PROJECT_FILE=^@var{project}
8484 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
8485 Use project file @var{project}. Only one such switch can be used.
8486 @xref{gnatmake and Project Files}.
8489 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
8490 Quiet. When this flag is not set, the commands carried out by
8491 @command{gnatmake} are displayed.
8493 @item ^-s^/SWITCH_CHECK/^
8494 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
8495 Recompile if compiler switches have changed since last compilation.
8496 All compiler switches but -I and -o are taken into account in the
8498 orders between different ``first letter'' switches are ignored, but
8499 orders between same switches are taken into account. For example,
8500 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
8501 is equivalent to @option{-O -g}.
8503 This switch is recommended when Integrated Preprocessing is used.
8506 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
8507 Unique. Recompile at most the main files. It implies -c. Combined with
8508 -f, it is equivalent to calling the compiler directly. Note that using
8509 ^-u^/UNIQUE^ with a project file and no main has a special meaning
8510 (@pxref{Project Files and Main Subprograms}).
8512 @item ^-U^/ALL_PROJECTS^
8513 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
8514 When used without a project file or with one or several mains on the command
8515 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
8516 on the command line, all sources of all project files are checked and compiled
8517 if not up to date, and libraries are rebuilt, if necessary.
8520 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
8521 Verbose. Display the reason for all recompilations @command{gnatmake}
8522 decides are necessary, with the highest verbosity level.
8524 @item ^-vl^/LOW_VERBOSITY^
8525 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
8526 Verbosity level Low. Display fewer lines than in verbosity Medium.
8528 @item ^-vm^/MEDIUM_VERBOSITY^
8529 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
8530 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
8532 @item ^-vh^/HIGH_VERBOSITY^
8533 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
8534 Verbosity level High. Equivalent to ^-v^/REASONS^.
8536 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
8537 Indicate the verbosity of the parsing of GNAT project files.
8538 @xref{Switches Related to Project Files}.
8540 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
8541 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
8542 Indicate that sources that are not part of any Project File may be compiled.
8543 Normally, when using Project Files, only sources that are part of a Project
8544 File may be compile. When this switch is used, a source outside of all Project
8545 Files may be compiled. The ALI file and the object file will be put in the
8546 object directory of the main Project. The compilation switches used will only
8547 be those specified on the command line.
8549 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
8550 Indicate that external variable @var{name} has the value @var{value}.
8551 The Project Manager will use this value for occurrences of
8552 @code{external(name)} when parsing the project file.
8553 @xref{Switches Related to Project Files}.
8556 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
8557 No main subprogram. Bind and link the program even if the unit name
8558 given on the command line is a package name. The resulting executable
8559 will execute the elaboration routines of the package and its closure,
8560 then the finalization routines.
8563 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
8564 Enable debugging. This switch is simply passed to the compiler and to the
8570 @item @command{gcc} @asis{switches}
8572 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
8573 is passed to @command{gcc} (e.g. @option{-O}, @option{-gnato,} etc.)
8576 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
8577 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
8578 automatically treated as a compiler switch, and passed on to all
8579 compilations that are carried out.
8584 Source and library search path switches:
8588 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
8589 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
8590 When looking for source files also look in directory @var{dir}.
8591 The order in which source files search is undertaken is
8592 described in @ref{Search Paths and the Run-Time Library (RTL)}.
8594 @item ^-aL^/SKIP_MISSING=^@var{dir}
8595 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
8596 Consider @var{dir} as being an externally provided Ada library.
8597 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
8598 files have been located in directory @var{dir}. This allows you to have
8599 missing bodies for the units in @var{dir} and to ignore out of date bodies
8600 for the same units. You still need to specify
8601 the location of the specs for these units by using the switches
8602 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
8603 or @option{^-I^/SEARCH=^@var{dir}}.
8604 Note: this switch is provided for compatibility with previous versions
8605 of @command{gnatmake}. The easier method of causing standard libraries
8606 to be excluded from consideration is to write-protect the corresponding
8609 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
8610 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
8611 When searching for library and object files, look in directory
8612 @var{dir}. The order in which library files are searched is described in
8613 @ref{Search Paths for gnatbind}.
8615 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
8616 @cindex Search paths, for @command{gnatmake}
8617 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
8618 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
8619 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8621 @item ^-I^/SEARCH=^@var{dir}
8622 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
8623 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
8624 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8626 @item ^-I-^/NOCURRENT_DIRECTORY^
8627 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
8628 @cindex Source files, suppressing search
8629 Do not look for source files in the directory containing the source
8630 file named in the command line.
8631 Do not look for ALI or object files in the directory
8632 where @command{gnatmake} was invoked.
8634 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
8635 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
8636 @cindex Linker libraries
8637 Add directory @var{dir} to the list of directories in which the linker
8638 will search for libraries. This is equivalent to
8639 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
8641 Furthermore, under Windows, the sources pointed to by the libraries path
8642 set in the registry are not searched for.
8646 @cindex @option{-nostdinc} (@command{gnatmake})
8647 Do not look for source files in the system default directory.
8650 @cindex @option{-nostdlib} (@command{gnatmake})
8651 Do not look for library files in the system default directory.
8653 @item --RTS=@var{rts-path}
8654 @cindex @option{--RTS} (@command{gnatmake})
8655 Specifies the default location of the runtime library. GNAT looks for the
8657 in the following directories, and stops as soon as a valid runtime is found
8658 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
8659 @file{ada_object_path} present):
8662 @item <current directory>/$rts_path
8664 @item <default-search-dir>/$rts_path
8666 @item <default-search-dir>/rts-$rts_path
8670 The selected path is handled like a normal RTS path.
8674 @node Mode Switches for gnatmake
8675 @section Mode Switches for @command{gnatmake}
8678 The mode switches (referred to as @code{mode_switches}) allow the
8679 inclusion of switches that are to be passed to the compiler itself, the
8680 binder or the linker. The effect of a mode switch is to cause all
8681 subsequent switches up to the end of the switch list, or up to the next
8682 mode switch, to be interpreted as switches to be passed on to the
8683 designated component of GNAT.
8687 @item -cargs @var{switches}
8688 @cindex @option{-cargs} (@command{gnatmake})
8689 Compiler switches. Here @var{switches} is a list of switches
8690 that are valid switches for @command{gcc}. They will be passed on to
8691 all compile steps performed by @command{gnatmake}.
8693 @item -bargs @var{switches}
8694 @cindex @option{-bargs} (@command{gnatmake})
8695 Binder switches. Here @var{switches} is a list of switches
8696 that are valid switches for @code{gnatbind}. They will be passed on to
8697 all bind steps performed by @command{gnatmake}.
8699 @item -largs @var{switches}
8700 @cindex @option{-largs} (@command{gnatmake})
8701 Linker switches. Here @var{switches} is a list of switches
8702 that are valid switches for @command{gnatlink}. They will be passed on to
8703 all link steps performed by @command{gnatmake}.
8705 @item -margs @var{switches}
8706 @cindex @option{-margs} (@command{gnatmake})
8707 Make switches. The switches are directly interpreted by @command{gnatmake},
8708 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
8712 @node Notes on the Command Line
8713 @section Notes on the Command Line
8716 This section contains some additional useful notes on the operation
8717 of the @command{gnatmake} command.
8721 @cindex Recompilation, by @command{gnatmake}
8722 If @command{gnatmake} finds no ALI files, it recompiles the main program
8723 and all other units required by the main program.
8724 This means that @command{gnatmake}
8725 can be used for the initial compile, as well as during subsequent steps of
8726 the development cycle.
8729 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
8730 is a subunit or body of a generic unit, @command{gnatmake} recompiles
8731 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
8735 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
8736 is used to specify both source and
8737 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8738 instead if you just want to specify
8739 source paths only and @option{^-aO^/OBJECT_SEARCH^}
8740 if you want to specify library paths
8744 @command{gnatmake} will ignore any files whose ALI file is write-protected.
8745 This may conveniently be used to exclude standard libraries from
8746 consideration and in particular it means that the use of the
8747 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
8748 unless @option{^-a^/ALL_FILES^} is also specified.
8751 @command{gnatmake} has been designed to make the use of Ada libraries
8752 particularly convenient. Assume you have an Ada library organized
8753 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
8754 of your Ada compilation units,
8755 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
8756 specs of these units, but no bodies. Then to compile a unit
8757 stored in @code{main.adb}, which uses this Ada library you would just type
8761 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
8764 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
8765 /SKIP_MISSING=@i{[OBJ_DIR]} main
8770 Using @command{gnatmake} along with the
8771 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
8772 switch provides a mechanism for avoiding unnecessary rcompilations. Using
8774 you can update the comments/format of your
8775 source files without having to recompile everything. Note, however, that
8776 adding or deleting lines in a source files may render its debugging
8777 info obsolete. If the file in question is a spec, the impact is rather
8778 limited, as that debugging info will only be useful during the
8779 elaboration phase of your program. For bodies the impact can be more
8780 significant. In all events, your debugger will warn you if a source file
8781 is more recent than the corresponding object, and alert you to the fact
8782 that the debugging information may be out of date.
8785 @node How gnatmake Works
8786 @section How @command{gnatmake} Works
8789 Generally @command{gnatmake} automatically performs all necessary
8790 recompilations and you don't need to worry about how it works. However,
8791 it may be useful to have some basic understanding of the @command{gnatmake}
8792 approach and in particular to understand how it uses the results of
8793 previous compilations without incorrectly depending on them.
8795 First a definition: an object file is considered @dfn{up to date} if the
8796 corresponding ALI file exists and if all the source files listed in the
8797 dependency section of this ALI file have time stamps matching those in
8798 the ALI file. This means that neither the source file itself nor any
8799 files that it depends on have been modified, and hence there is no need
8800 to recompile this file.
8802 @command{gnatmake} works by first checking if the specified main unit is up
8803 to date. If so, no compilations are required for the main unit. If not,
8804 @command{gnatmake} compiles the main program to build a new ALI file that
8805 reflects the latest sources. Then the ALI file of the main unit is
8806 examined to find all the source files on which the main program depends,
8807 and @command{gnatmake} recursively applies the above procedure on all these
8810 This process ensures that @command{gnatmake} only trusts the dependencies
8811 in an existing ALI file if they are known to be correct. Otherwise it
8812 always recompiles to determine a new, guaranteed accurate set of
8813 dependencies. As a result the program is compiled ``upside down'' from what may
8814 be more familiar as the required order of compilation in some other Ada
8815 systems. In particular, clients are compiled before the units on which
8816 they depend. The ability of GNAT to compile in any order is critical in
8817 allowing an order of compilation to be chosen that guarantees that
8818 @command{gnatmake} will recompute a correct set of new dependencies if
8821 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
8822 imported by several of the executables, it will be recompiled at most once.
8824 Note: when using non-standard naming conventions
8825 (@pxref{Using Other File Names}), changing through a configuration pragmas
8826 file the version of a source and invoking @command{gnatmake} to recompile may
8827 have no effect, if the previous version of the source is still accessible
8828 by @command{gnatmake}. It may be necessary to use the switch
8829 ^-f^/FORCE_COMPILE^.
8831 @node Examples of gnatmake Usage
8832 @section Examples of @command{gnatmake} Usage
8835 @item gnatmake hello.adb
8836 Compile all files necessary to bind and link the main program
8837 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
8838 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
8840 @item gnatmake main1 main2 main3
8841 Compile all files necessary to bind and link the main programs
8842 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
8843 (containing unit @code{Main2}) and @file{main3.adb}
8844 (containing unit @code{Main3}) and bind and link the resulting object files
8845 to generate three executable files @file{^main1^MAIN1.EXE^},
8846 @file{^main2^MAIN2.EXE^}
8847 and @file{^main3^MAIN3.EXE^}.
8850 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
8854 @item gnatmake Main_Unit /QUIET
8855 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
8856 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
8858 Compile all files necessary to bind and link the main program unit
8859 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
8860 be done with optimization level 2 and the order of elaboration will be
8861 listed by the binder. @command{gnatmake} will operate in quiet mode, not
8862 displaying commands it is executing.
8865 @c *************************
8866 @node Improving Performance
8867 @chapter Improving Performance
8868 @cindex Improving performance
8871 This chapter presents several topics related to program performance.
8872 It first describes some of the tradeoffs that need to be considered
8873 and some of the techniques for making your program run faster.
8874 It then documents the @command{gnatelim} tool and unused subprogram/data
8875 elimination feature, which can reduce the size of program executables.
8879 * Performance Considerations::
8880 * Reducing the Size of Ada Executables with gnatelim::
8881 * Reducing the Size of Executables with unused subprogram/data elimination::
8885 @c *****************************
8886 @node Performance Considerations
8887 @section Performance Considerations
8890 The GNAT system provides a number of options that allow a trade-off
8895 performance of the generated code
8898 speed of compilation
8901 minimization of dependences and recompilation
8904 the degree of run-time checking.
8908 The defaults (if no options are selected) aim at improving the speed
8909 of compilation and minimizing dependences, at the expense of performance
8910 of the generated code:
8917 no inlining of subprogram calls
8920 all run-time checks enabled except overflow and elaboration checks
8924 These options are suitable for most program development purposes. This
8925 chapter describes how you can modify these choices, and also provides
8926 some guidelines on debugging optimized code.
8929 * Controlling Run-Time Checks::
8930 * Use of Restrictions::
8931 * Optimization Levels::
8932 * Debugging Optimized Code::
8933 * Inlining of Subprograms::
8934 * Other Optimization Switches::
8935 * Optimization and Strict Aliasing::
8938 * Coverage Analysis::
8942 @node Controlling Run-Time Checks
8943 @subsection Controlling Run-Time Checks
8946 By default, GNAT generates all run-time checks, except arithmetic overflow
8947 checking for integer operations and checks for access before elaboration on
8948 subprogram calls. The latter are not required in default mode, because all
8949 necessary checking is done at compile time.
8950 @cindex @option{-gnatp} (@command{gcc})
8951 @cindex @option{-gnato} (@command{gcc})
8952 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
8953 be modified. @xref{Run-Time Checks}.
8955 Our experience is that the default is suitable for most development
8958 We treat integer overflow specially because these
8959 are quite expensive and in our experience are not as important as other
8960 run-time checks in the development process. Note that division by zero
8961 is not considered an overflow check, and divide by zero checks are
8962 generated where required by default.
8964 Elaboration checks are off by default, and also not needed by default, since
8965 GNAT uses a static elaboration analysis approach that avoids the need for
8966 run-time checking. This manual contains a full chapter discussing the issue
8967 of elaboration checks, and if the default is not satisfactory for your use,
8968 you should read this chapter.
8970 For validity checks, the minimal checks required by the Ada Reference
8971 Manual (for case statements and assignments to array elements) are on
8972 by default. These can be suppressed by use of the @option{-gnatVn} switch.
8973 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
8974 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
8975 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
8976 are also suppressed entirely if @option{-gnatp} is used.
8978 @cindex Overflow checks
8979 @cindex Checks, overflow
8982 @cindex pragma Suppress
8983 @cindex pragma Unsuppress
8984 Note that the setting of the switches controls the default setting of
8985 the checks. They may be modified using either @code{pragma Suppress} (to
8986 remove checks) or @code{pragma Unsuppress} (to add back suppressed
8987 checks) in the program source.
8989 @node Use of Restrictions
8990 @subsection Use of Restrictions
8993 The use of pragma Restrictions allows you to control which features are
8994 permitted in your program. Apart from the obvious point that if you avoid
8995 relatively expensive features like finalization (enforceable by the use
8996 of pragma Restrictions (No_Finalization), the use of this pragma does not
8997 affect the generated code in most cases.
8999 One notable exception to this rule is that the possibility of task abort
9000 results in some distributed overhead, particularly if finalization or
9001 exception handlers are used. The reason is that certain sections of code
9002 have to be marked as non-abortable.
9004 If you use neither the @code{abort} statement, nor asynchronous transfer
9005 of control (@code{select .. then abort}), then this distributed overhead
9006 is removed, which may have a general positive effect in improving
9007 overall performance. Especially code involving frequent use of tasking
9008 constructs and controlled types will show much improved performance.
9009 The relevant restrictions pragmas are
9011 @smallexample @c ada
9012 pragma Restrictions (No_Abort_Statements);
9013 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
9017 It is recommended that these restriction pragmas be used if possible. Note
9018 that this also means that you can write code without worrying about the
9019 possibility of an immediate abort at any point.
9021 @node Optimization Levels
9022 @subsection Optimization Levels
9023 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
9026 The default is optimization off. This results in the fastest compile
9027 times, but GNAT makes absolutely no attempt to optimize, and the
9028 generated programs are considerably larger and slower than when
9029 optimization is enabled. You can use the
9031 @option{-O@var{n}} switch, where @var{n} is an integer from 0 to 3,
9034 @code{OPTIMIZE} qualifier
9036 to @command{gcc} to control the optimization level:
9039 @item ^-O0^/OPTIMIZE=NONE^
9040 No optimization (the default);
9041 generates unoptimized code but has
9042 the fastest compilation time.
9044 Note that many other compilers do fairly extensive optimization
9045 even if "no optimization" is specified. When using gcc, it is
9046 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
9047 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
9048 really does mean no optimization at all. This difference between
9049 gcc and other compilers should be kept in mind when doing
9050 performance comparisons.
9052 @item ^-O1^/OPTIMIZE=SOME^
9053 Moderate optimization;
9054 optimizes reasonably well but does not
9055 degrade compilation time significantly.
9057 @item ^-O2^/OPTIMIZE=ALL^
9059 @itemx /OPTIMIZE=DEVELOPMENT
9062 generates highly optimized code and has
9063 the slowest compilation time.
9065 @item ^-O3^/OPTIMIZE=INLINING^
9066 Full optimization as in @option{-O2},
9067 and also attempts automatic inlining of small
9068 subprograms within a unit (@pxref{Inlining of Subprograms}).
9072 Higher optimization levels perform more global transformations on the
9073 program and apply more expensive analysis algorithms in order to generate
9074 faster and more compact code. The price in compilation time, and the
9075 resulting improvement in execution time,
9076 both depend on the particular application and the hardware environment.
9077 You should experiment to find the best level for your application.
9079 Since the precise set of optimizations done at each level will vary from
9080 release to release (and sometime from target to target), it is best to think
9081 of the optimization settings in general terms.
9082 The @cite{Using GNU GCC} manual contains details about
9083 ^the @option{-O} settings and a number of @option{-f} options that^how to^
9084 individually enable or disable specific optimizations.
9086 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
9087 been tested extensively at all optimization levels. There are some bugs
9088 which appear only with optimization turned on, but there have also been
9089 bugs which show up only in @emph{unoptimized} code. Selecting a lower
9090 level of optimization does not improve the reliability of the code
9091 generator, which in practice is highly reliable at all optimization
9094 Note regarding the use of @option{-O3}: The use of this optimization level
9095 is generally discouraged with GNAT, since it often results in larger
9096 executables which run more slowly. See further discussion of this point
9097 in @ref{Inlining of Subprograms}.
9099 @node Debugging Optimized Code
9100 @subsection Debugging Optimized Code
9101 @cindex Debugging optimized code
9102 @cindex Optimization and debugging
9105 Although it is possible to do a reasonable amount of debugging at
9107 nonzero optimization levels,
9108 the higher the level the more likely that
9111 @option{/OPTIMIZE} settings other than @code{NONE},
9112 such settings will make it more likely that
9114 source-level constructs will have been eliminated by optimization.
9115 For example, if a loop is strength-reduced, the loop
9116 control variable may be completely eliminated and thus cannot be
9117 displayed in the debugger.
9118 This can only happen at @option{-O2} or @option{-O3}.
9119 Explicit temporary variables that you code might be eliminated at
9120 ^level^setting^ @option{-O1} or higher.
9122 The use of the @option{^-g^/DEBUG^} switch,
9123 @cindex @option{^-g^/DEBUG^} (@command{gcc})
9124 which is needed for source-level debugging,
9125 affects the size of the program executable on disk,
9126 and indeed the debugging information can be quite large.
9127 However, it has no effect on the generated code (and thus does not
9128 degrade performance)
9130 Since the compiler generates debugging tables for a compilation unit before
9131 it performs optimizations, the optimizing transformations may invalidate some
9132 of the debugging data. You therefore need to anticipate certain
9133 anomalous situations that may arise while debugging optimized code.
9134 These are the most common cases:
9138 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
9140 the PC bouncing back and forth in the code. This may result from any of
9141 the following optimizations:
9145 @i{Common subexpression elimination:} using a single instance of code for a
9146 quantity that the source computes several times. As a result you
9147 may not be able to stop on what looks like a statement.
9150 @i{Invariant code motion:} moving an expression that does not change within a
9151 loop, to the beginning of the loop.
9154 @i{Instruction scheduling:} moving instructions so as to
9155 overlap loads and stores (typically) with other code, or in
9156 general to move computations of values closer to their uses. Often
9157 this causes you to pass an assignment statement without the assignment
9158 happening and then later bounce back to the statement when the
9159 value is actually needed. Placing a breakpoint on a line of code
9160 and then stepping over it may, therefore, not always cause all the
9161 expected side-effects.
9165 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
9166 two identical pieces of code are merged and the program counter suddenly
9167 jumps to a statement that is not supposed to be executed, simply because
9168 it (and the code following) translates to the same thing as the code
9169 that @emph{was} supposed to be executed. This effect is typically seen in
9170 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
9171 a @code{break} in a C @code{^switch^switch^} statement.
9174 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
9175 There are various reasons for this effect:
9179 In a subprogram prologue, a parameter may not yet have been moved to its
9183 A variable may be dead, and its register re-used. This is
9184 probably the most common cause.
9187 As mentioned above, the assignment of a value to a variable may
9191 A variable may be eliminated entirely by value propagation or
9192 other means. In this case, GCC may incorrectly generate debugging
9193 information for the variable
9197 In general, when an unexpected value appears for a local variable or parameter
9198 you should first ascertain if that value was actually computed by
9199 your program, as opposed to being incorrectly reported by the debugger.
9201 array elements in an object designated by an access value
9202 are generally less of a problem, once you have ascertained that the access
9204 Typically, this means checking variables in the preceding code and in the
9205 calling subprogram to verify that the value observed is explainable from other
9206 values (one must apply the procedure recursively to those
9207 other values); or re-running the code and stopping a little earlier
9208 (perhaps before the call) and stepping to better see how the variable obtained
9209 the value in question; or continuing to step @emph{from} the point of the
9210 strange value to see if code motion had simply moved the variable's
9215 In light of such anomalies, a recommended technique is to use @option{-O0}
9216 early in the software development cycle, when extensive debugging capabilities
9217 are most needed, and then move to @option{-O1} and later @option{-O2} as
9218 the debugger becomes less critical.
9219 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9220 a release management issue.
9222 Note that if you use @option{-g} you can then use the @command{strip} program
9223 on the resulting executable,
9224 which removes both debugging information and global symbols.
9227 @node Inlining of Subprograms
9228 @subsection Inlining of Subprograms
9231 A call to a subprogram in the current unit is inlined if all the
9232 following conditions are met:
9236 The optimization level is at least @option{-O1}.
9239 The called subprogram is suitable for inlining: It must be small enough
9240 and not contain nested subprograms or anything else that @command{gcc}
9241 cannot support in inlined subprograms.
9244 The call occurs after the definition of the body of the subprogram.
9247 @cindex pragma Inline
9249 Either @code{pragma Inline} applies to the subprogram or it is
9250 small and automatic inlining (optimization level @option{-O3}) is
9255 Calls to subprograms in @code{with}'ed units are normally not inlined.
9256 To achieve this level of inlining, the following conditions must all be
9261 The optimization level is at least @option{-O1}.
9264 The called subprogram is suitable for inlining: It must be small enough
9265 and not contain nested subprograms or anything else @command{gcc} cannot
9266 support in inlined subprograms.
9269 The call appears in a body (not in a package spec).
9272 There is a @code{pragma Inline} for the subprogram.
9275 @cindex @option{-gnatn} (@command{gcc})
9276 The @option{^-gnatn^/INLINE^} switch
9277 is used in the @command{gcc} command line
9280 Note that specifying the @option{-gnatn} switch causes additional
9281 compilation dependencies. Consider the following:
9283 @smallexample @c ada
9303 With the default behavior (no @option{-gnatn} switch specified), the
9304 compilation of the @code{Main} procedure depends only on its own source,
9305 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
9306 means that editing the body of @code{R} does not require recompiling
9309 On the other hand, the call @code{R.Q} is not inlined under these
9310 circumstances. If the @option{-gnatn} switch is present when @code{Main}
9311 is compiled, the call will be inlined if the body of @code{Q} is small
9312 enough, but now @code{Main} depends on the body of @code{R} in
9313 @file{r.adb} as well as on the spec. This means that if this body is edited,
9314 the main program must be recompiled. Note that this extra dependency
9315 occurs whether or not the call is in fact inlined by @command{gcc}.
9317 The use of front end inlining with @option{-gnatN} generates similar
9318 additional dependencies.
9320 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
9321 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
9322 can be used to prevent
9323 all inlining. This switch overrides all other conditions and ensures
9324 that no inlining occurs. The extra dependences resulting from
9325 @option{-gnatn} will still be active, even if
9326 this switch is used to suppress the resulting inlining actions.
9328 Note regarding the use of @option{-O3}: There is no difference in inlining
9329 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
9330 pragma @code{Inline} assuming the use of @option{-gnatn}
9331 or @option{-gnatN} (the switches that activate inlining). If you have used
9332 pragma @code{Inline} in appropriate cases, then it is usually much better
9333 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
9334 in this case only has the effect of inlining subprograms you did not
9335 think should be inlined. We often find that the use of @option{-O3} slows
9336 down code by performing excessive inlining, leading to increased instruction
9337 cache pressure from the increased code size. So the bottom line here is
9338 that you should not automatically assume that @option{-O3} is better than
9339 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
9340 it actually improves performance.
9342 @node Other Optimization Switches
9343 @subsection Other Optimization Switches
9344 @cindex Optimization Switches
9346 Since @code{GNAT} uses the @code{gcc} back end, all the specialized
9347 @code{gcc} optimization switches are potentially usable. These switches
9348 have not been extensively tested with GNAT but can generally be expected
9349 to work. Examples of switches in this category are
9350 @option{-funroll-loops} and
9351 the various target-specific @option{-m} options (in particular, it has been
9352 observed that @option{-march=pentium4} can significantly improve performance
9353 on appropriate machines). For full details of these switches, see the
9356 @node Optimization and Strict Aliasing
9357 @subsection Optimization and Strict Aliasing
9359 @cindex Strict Aliasing
9360 @cindex No_Strict_Aliasing
9363 The strong typing capabilities of Ada allow an optimizer to generate
9364 efficient code in situations where other languages would be forced to
9365 make worst case assumptions preventing such optimizations. Consider
9366 the following example:
9368 @smallexample @c ada
9371 type Int1 is new Integer;
9372 type Int2 is new Integer;
9373 type Int1A is access Int1;
9374 type Int2A is access Int2;
9381 for J in Data'Range loop
9382 if Data (J) = Int1V.all then
9383 Int2V.all := Int2V.all + 1;
9392 In this example, since the variable @code{Int1V} can only access objects
9393 of type @code{Int1}, and @code{Int2V} can only access objects of type
9394 @code{Int2}, there is no possibility that the assignment to
9395 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
9396 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
9397 for all iterations of the loop and avoid the extra memory reference
9398 required to dereference it each time through the loop.
9400 This kind of optimization, called strict aliasing analysis, is
9401 triggered by specifying an optimization level of @option{-O2} or
9402 higher and allows @code{GNAT} to generate more efficient code
9403 when access values are involved.
9405 However, although this optimization is always correct in terms of
9406 the formal semantics of the Ada Reference Manual, difficulties can
9407 arise if features like @code{Unchecked_Conversion} are used to break
9408 the typing system. Consider the following complete program example:
9410 @smallexample @c ada
9413 type int1 is new integer;
9414 type int2 is new integer;
9415 type a1 is access int1;
9416 type a2 is access int2;
9421 function to_a2 (Input : a1) return a2;
9424 with Unchecked_Conversion;
9426 function to_a2 (Input : a1) return a2 is
9428 new Unchecked_Conversion (a1, a2);
9430 return to_a2u (Input);
9436 with Text_IO; use Text_IO;
9438 v1 : a1 := new int1;
9439 v2 : a2 := to_a2 (v1);
9443 put_line (int1'image (v1.all));
9449 This program prints out 0 in @code{-O0} or @code{-O1}
9450 mode, but it prints out 1 in @code{-O2} mode. That's
9451 because in strict aliasing mode, the compiler can and
9452 does assume that the assignment to @code{v2.all} could not
9453 affect the value of @code{v1.all}, since different types
9456 This behavior is not a case of non-conformance with the standard, since
9457 the Ada RM specifies that an unchecked conversion where the resulting
9458 bit pattern is not a correct value of the target type can result in an
9459 abnormal value and attempting to reference an abnormal value makes the
9460 execution of a program erroneous. That's the case here since the result
9461 does not point to an object of type @code{int2}. This means that the
9462 effect is entirely unpredictable.
9464 However, although that explanation may satisfy a language
9465 lawyer, in practice an applications programmer expects an
9466 unchecked conversion involving pointers to create true
9467 aliases and the behavior of printing 1 seems plain wrong.
9468 In this case, the strict aliasing optimization is unwelcome.
9470 Indeed the compiler recognizes this possibility, and the
9471 unchecked conversion generates a warning:
9474 p2.adb:5:07: warning: possible aliasing problem with type "a2"
9475 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
9476 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
9480 Unfortunately the problem is recognized when compiling the body of
9481 package @code{p2}, but the actual "bad" code is generated while
9482 compiling the body of @code{m} and this latter compilation does not see
9483 the suspicious @code{Unchecked_Conversion}.
9485 As implied by the warning message, there are approaches you can use to
9486 avoid the unwanted strict aliasing optimization in a case like this.
9488 One possibility is to simply avoid the use of @code{-O2}, but
9489 that is a bit drastic, since it throws away a number of useful
9490 optimizations that do not involve strict aliasing assumptions.
9492 A less drastic approach is to compile the program using the
9493 option @code{-fno-strict-aliasing}. Actually it is only the
9494 unit containing the dereferencing of the suspicious pointer
9495 that needs to be compiled. So in this case, if we compile
9496 unit @code{m} with this switch, then we get the expected
9497 value of zero printed. Analyzing which units might need
9498 the switch can be painful, so a more reasonable approach
9499 is to compile the entire program with options @code{-O2}
9500 and @code{-fno-strict-aliasing}. If the performance is
9501 satisfactory with this combination of options, then the
9502 advantage is that the entire issue of possible "wrong"
9503 optimization due to strict aliasing is avoided.
9505 To avoid the use of compiler switches, the configuration
9506 pragma @code{No_Strict_Aliasing} with no parameters may be
9507 used to specify that for all access types, the strict
9508 aliasing optimization should be suppressed.
9510 However, these approaches are still overkill, in that they causes
9511 all manipulations of all access values to be deoptimized. A more
9512 refined approach is to concentrate attention on the specific
9513 access type identified as problematic.
9515 First, if a careful analysis of uses of the pointer shows
9516 that there are no possible problematic references, then
9517 the warning can be suppressed by bracketing the
9518 instantiation of @code{Unchecked_Conversion} to turn
9521 @smallexample @c ada
9522 pragma Warnings (Off);
9524 new Unchecked_Conversion (a1, a2);
9525 pragma Warnings (On);
9529 Of course that approach is not appropriate for this particular
9530 example, since indeed there is a problematic reference. In this
9531 case we can take one of two other approaches.
9533 The first possibility is to move the instantiation of unchecked
9534 conversion to the unit in which the type is declared. In
9535 this example, we would move the instantiation of
9536 @code{Unchecked_Conversion} from the body of package
9537 @code{p2} to the spec of package @code{p1}. Now the
9538 warning disappears. That's because any use of the
9539 access type knows there is a suspicious unchecked
9540 conversion, and the strict aliasing optimization
9541 is automatically suppressed for the type.
9543 If it is not practical to move the unchecked conversion to the same unit
9544 in which the destination access type is declared (perhaps because the
9545 source type is not visible in that unit), you may use pragma
9546 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
9547 same declarative sequence as the declaration of the access type:
9549 @smallexample @c ada
9550 type a2 is access int2;
9551 pragma No_Strict_Aliasing (a2);
9555 Here again, the compiler now knows that the strict aliasing optimization
9556 should be suppressed for any reference to type @code{a2} and the
9557 expected behavior is obtained.
9559 Finally, note that although the compiler can generate warnings for
9560 simple cases of unchecked conversions, there are tricker and more
9561 indirect ways of creating type incorrect aliases which the compiler
9562 cannot detect. Examples are the use of address overlays and unchecked
9563 conversions involving composite types containing access types as
9564 components. In such cases, no warnings are generated, but there can
9565 still be aliasing problems. One safe coding practice is to forbid the
9566 use of address clauses for type overlaying, and to allow unchecked
9567 conversion only for primitive types. This is not really a significant
9568 restriction since any possible desired effect can be achieved by
9569 unchecked conversion of access values.
9572 @node Coverage Analysis
9573 @subsection Coverage Analysis
9576 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
9577 the user to determine the distribution of execution time across a program,
9578 @pxref{Profiling} for details of usage.
9581 @node Reducing the Size of Ada Executables with gnatelim
9582 @section Reducing the Size of Ada Executables with @code{gnatelim}
9586 This section describes @command{gnatelim}, a tool which detects unused
9587 subprograms and helps the compiler to create a smaller executable for your
9592 * Running gnatelim::
9593 * Correcting the List of Eliminate Pragmas::
9594 * Making Your Executables Smaller::
9595 * Summary of the gnatelim Usage Cycle::
9598 @node About gnatelim
9599 @subsection About @code{gnatelim}
9602 When a program shares a set of Ada
9603 packages with other programs, it may happen that this program uses
9604 only a fraction of the subprograms defined in these packages. The code
9605 created for these unused subprograms increases the size of the executable.
9607 @code{gnatelim} tracks unused subprograms in an Ada program and
9608 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
9609 subprograms that are declared but never called. By placing the list of
9610 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
9611 recompiling your program, you may decrease the size of its executable,
9612 because the compiler will not generate the code for 'eliminated' subprograms.
9613 See GNAT Reference Manual for more information about this pragma.
9615 @code{gnatelim} needs as its input data the name of the main subprogram
9616 and a bind file for a main subprogram.
9618 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
9619 the main subprogram. @code{gnatelim} can work with both Ada and C
9620 bind files; when both are present, it uses the Ada bind file.
9621 The following commands will build the program and create the bind file:
9624 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
9625 $ gnatbind main_prog
9628 Note that @code{gnatelim} needs neither object nor ALI files.
9630 @node Running gnatelim
9631 @subsection Running @code{gnatelim}
9634 @code{gnatelim} has the following command-line interface:
9637 $ gnatelim [options] name
9641 @code{name} should be a name of a source file that contains the main subprogram
9642 of a program (partition).
9644 @code{gnatelim} has the following switches:
9649 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
9650 Quiet mode: by default @code{gnatelim} outputs to the standard error
9651 stream the number of program units left to be processed. This option turns
9655 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
9656 Verbose mode: @code{gnatelim} version information is printed as Ada
9657 comments to the standard output stream. Also, in addition to the number of
9658 program units left @code{gnatelim} will output the name of the current unit
9662 @cindex @option{^-a^/ALL^} (@command{gnatelim})
9663 Also look for subprograms from the GNAT run time that can be eliminated. Note
9664 that when @file{gnat.adc} is produced using this switch, the entire program
9665 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
9667 @item ^-I^/INCLUDE_DIRS=^@var{dir}
9668 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
9669 When looking for source files also look in directory @var{dir}. Specifying
9670 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
9671 sources in the current directory.
9673 @item ^-b^/BIND_FILE=^@var{bind_file}
9674 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
9675 Specifies @var{bind_file} as the bind file to process. If not set, the name
9676 of the bind file is computed from the full expanded Ada name
9677 of a main subprogram.
9679 @item ^-C^/CONFIG_FILE=^@var{config_file}
9680 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
9681 Specifies a file @var{config_file} that contains configuration pragmas. The
9682 file must be specified with full path.
9684 @item ^--GCC^/COMPILER^=@var{compiler_name}
9685 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
9686 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
9687 available on the path.
9689 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
9690 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
9691 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
9692 available on the path.
9696 @code{gnatelim} sends its output to the standard output stream, and all the
9697 tracing and debug information is sent to the standard error stream.
9698 In order to produce a proper GNAT configuration file
9699 @file{gnat.adc}, redirection must be used:
9703 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
9706 $ gnatelim main_prog.adb > gnat.adc
9715 $ gnatelim main_prog.adb >> gnat.adc
9719 in order to append the @code{gnatelim} output to the existing contents of
9723 @node Correcting the List of Eliminate Pragmas
9724 @subsection Correcting the List of Eliminate Pragmas
9727 In some rare cases @code{gnatelim} may try to eliminate
9728 subprograms that are actually called in the program. In this case, the
9729 compiler will generate an error message of the form:
9732 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
9736 You will need to manually remove the wrong @code{Eliminate} pragmas from
9737 the @file{gnat.adc} file. You should recompile your program
9738 from scratch after that, because you need a consistent @file{gnat.adc} file
9739 during the entire compilation.
9741 @node Making Your Executables Smaller
9742 @subsection Making Your Executables Smaller
9745 In order to get a smaller executable for your program you now have to
9746 recompile the program completely with the new @file{gnat.adc} file
9747 created by @code{gnatelim} in your current directory:
9750 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9754 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
9755 recompile everything
9756 with the set of pragmas @code{Eliminate} that you have obtained with
9757 @command{gnatelim}).
9759 Be aware that the set of @code{Eliminate} pragmas is specific to each
9760 program. It is not recommended to merge sets of @code{Eliminate}
9761 pragmas created for different programs in one @file{gnat.adc} file.
9763 @node Summary of the gnatelim Usage Cycle
9764 @subsection Summary of the gnatelim Usage Cycle
9767 Here is a quick summary of the steps to be taken in order to reduce
9768 the size of your executables with @code{gnatelim}. You may use
9769 other GNAT options to control the optimization level,
9770 to produce the debugging information, to set search path, etc.
9777 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
9778 $ gnatbind main_prog
9782 Generate a list of @code{Eliminate} pragmas
9785 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
9788 $ gnatelim main_prog >[>] gnat.adc
9793 Recompile the application
9796 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9801 @node Reducing the Size of Executables with unused subprogram/data elimination
9802 @section Reducing the Size of Executables with Unused Subprogram/Data Elimination
9803 @findex unused subprogram/data elimination
9806 This section describes how you can eliminate unused subprograms and data from
9807 your executable just by setting options at compilation time.
9810 * About unused subprogram/data elimination::
9811 * Compilation options::
9814 @node About unused subprogram/data elimination
9815 @subsection About unused subprogram/data elimination
9818 By default, an executable contains all code and data of its composing objects
9819 (directly linked or coming from statically linked libraries), even data or code
9820 never used by this executable.
9822 This feature will allow you to eliminate such unused code from your
9823 executable, making it smaller (in disk and in memory).
9825 This functionality is only available on native x86 GNU/Linux platform for the
9828 @node Compilation options
9829 @subsection Compilation options
9832 The operation of eliminating the unused code and data from the final executable
9833 is directly performed by the linker.
9835 In order to do this, it has to work with objects compiled with the
9837 @option{-ffunction-sections} @option{-fdata-sections}.
9838 @cindex @option{-ffunction-sections} (@command{gcc})
9839 @cindex @option{-fdata-sections} (@command{gcc})
9840 These options are usable with C and Ada files.
9841 They will place respectively each
9842 function or data in a separate section in the resulting object file.
9844 Once the objects and static libraries are created with these options, the
9845 linker can perform the dead code elimination. You can do this by setting
9846 the @option{-Wl,--gc-sections} option to gcc command or in the
9847 @option{-largs} section of gnatmake. This will create the final executable,
9848 without including the code and data determined as never accessed.
9850 Note that objects compiled without the @option{-ffunction-sections} and
9851 @option{-fdata-sections} options can still be linked with the executable.
9852 However, no dead code elimination will be performed on those objects (they will
9855 The GNAT static library is now compiled with -ffunction-sections and
9856 -fdata-sections. This allows you to eliminate the unused code of the GNAT
9857 library from your executable.
9859 @c ********************************
9860 @node Renaming Files Using gnatchop
9861 @chapter Renaming Files Using @code{gnatchop}
9865 This chapter discusses how to handle files with multiple units by using
9866 the @code{gnatchop} utility. This utility is also useful in renaming
9867 files to meet the standard GNAT default file naming conventions.
9870 * Handling Files with Multiple Units::
9871 * Operating gnatchop in Compilation Mode::
9872 * Command Line for gnatchop::
9873 * Switches for gnatchop::
9874 * Examples of gnatchop Usage::
9877 @node Handling Files with Multiple Units
9878 @section Handling Files with Multiple Units
9881 The basic compilation model of GNAT requires that a file submitted to the
9882 compiler have only one unit and there be a strict correspondence
9883 between the file name and the unit name.
9885 The @code{gnatchop} utility allows both of these rules to be relaxed,
9886 allowing GNAT to process files which contain multiple compilation units
9887 and files with arbitrary file names. @code{gnatchop}
9888 reads the specified file and generates one or more output files,
9889 containing one unit per file. The unit and the file name correspond,
9890 as required by GNAT.
9892 If you want to permanently restructure a set of ``foreign'' files so that
9893 they match the GNAT rules, and do the remaining development using the
9894 GNAT structure, you can simply use @command{gnatchop} once, generate the
9895 new set of files and work with them from that point on.
9897 Alternatively, if you want to keep your files in the ``foreign'' format,
9898 perhaps to maintain compatibility with some other Ada compilation
9899 system, you can set up a procedure where you use @command{gnatchop} each
9900 time you compile, regarding the source files that it writes as temporary
9901 files that you throw away.
9903 @node Operating gnatchop in Compilation Mode
9904 @section Operating gnatchop in Compilation Mode
9907 The basic function of @code{gnatchop} is to take a file with multiple units
9908 and split it into separate files. The boundary between files is reasonably
9909 clear, except for the issue of comments and pragmas. In default mode, the
9910 rule is that any pragmas between units belong to the previous unit, except
9911 that configuration pragmas always belong to the following unit. Any comments
9912 belong to the following unit. These rules
9913 almost always result in the right choice of
9914 the split point without needing to mark it explicitly and most users will
9915 find this default to be what they want. In this default mode it is incorrect to
9916 submit a file containing only configuration pragmas, or one that ends in
9917 configuration pragmas, to @code{gnatchop}.
9919 However, using a special option to activate ``compilation mode'',
9921 can perform another function, which is to provide exactly the semantics
9922 required by the RM for handling of configuration pragmas in a compilation.
9923 In the absence of configuration pragmas (at the main file level), this
9924 option has no effect, but it causes such configuration pragmas to be handled
9925 in a quite different manner.
9927 First, in compilation mode, if @code{gnatchop} is given a file that consists of
9928 only configuration pragmas, then this file is appended to the
9929 @file{gnat.adc} file in the current directory. This behavior provides
9930 the required behavior described in the RM for the actions to be taken
9931 on submitting such a file to the compiler, namely that these pragmas
9932 should apply to all subsequent compilations in the same compilation
9933 environment. Using GNAT, the current directory, possibly containing a
9934 @file{gnat.adc} file is the representation
9935 of a compilation environment. For more information on the
9936 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
9938 Second, in compilation mode, if @code{gnatchop}
9939 is given a file that starts with
9940 configuration pragmas, and contains one or more units, then these
9941 configuration pragmas are prepended to each of the chopped files. This
9942 behavior provides the required behavior described in the RM for the
9943 actions to be taken on compiling such a file, namely that the pragmas
9944 apply to all units in the compilation, but not to subsequently compiled
9947 Finally, if configuration pragmas appear between units, they are appended
9948 to the previous unit. This results in the previous unit being illegal,
9949 since the compiler does not accept configuration pragmas that follow
9950 a unit. This provides the required RM behavior that forbids configuration
9951 pragmas other than those preceding the first compilation unit of a
9954 For most purposes, @code{gnatchop} will be used in default mode. The
9955 compilation mode described above is used only if you need exactly
9956 accurate behavior with respect to compilations, and you have files
9957 that contain multiple units and configuration pragmas. In this
9958 circumstance the use of @code{gnatchop} with the compilation mode
9959 switch provides the required behavior, and is for example the mode
9960 in which GNAT processes the ACVC tests.
9962 @node Command Line for gnatchop
9963 @section Command Line for @code{gnatchop}
9966 The @code{gnatchop} command has the form:
9969 $ gnatchop switches @var{file name} [@var{file name} @var{file name} ...]
9974 The only required argument is the file name of the file to be chopped.
9975 There are no restrictions on the form of this file name. The file itself
9976 contains one or more Ada units, in normal GNAT format, concatenated
9977 together. As shown, more than one file may be presented to be chopped.
9979 When run in default mode, @code{gnatchop} generates one output file in
9980 the current directory for each unit in each of the files.
9982 @var{directory}, if specified, gives the name of the directory to which
9983 the output files will be written. If it is not specified, all files are
9984 written to the current directory.
9986 For example, given a
9987 file called @file{hellofiles} containing
9989 @smallexample @c ada
9994 with Text_IO; use Text_IO;
10007 $ gnatchop ^hellofiles^HELLOFILES.^
10011 generates two files in the current directory, one called
10012 @file{hello.ads} containing the single line that is the procedure spec,
10013 and the other called @file{hello.adb} containing the remaining text. The
10014 original file is not affected. The generated files can be compiled in
10018 When gnatchop is invoked on a file that is empty or that contains only empty
10019 lines and/or comments, gnatchop will not fail, but will not produce any
10022 For example, given a
10023 file called @file{toto.txt} containing
10025 @smallexample @c ada
10037 $ gnatchop ^toto.txt^TOT.TXT^
10041 will not produce any new file and will result in the following warnings:
10044 toto.txt:1:01: warning: empty file, contains no compilation units
10045 no compilation units found
10046 no source files written
10049 @node Switches for gnatchop
10050 @section Switches for @code{gnatchop}
10053 @command{gnatchop} recognizes the following switches:
10058 @item ^-c^/COMPILATION^
10059 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
10060 Causes @code{gnatchop} to operate in compilation mode, in which
10061 configuration pragmas are handled according to strict RM rules. See
10062 previous section for a full description of this mode.
10066 This passes the given @option{-gnatxxx} switch to @code{gnat} which is
10067 used to parse the given file. Not all @code{xxx} options make sense,
10068 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
10069 process a source file that uses Latin-2 coding for identifiers.
10073 Causes @code{gnatchop} to generate a brief help summary to the standard
10074 output file showing usage information.
10076 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
10077 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
10078 Limit generated file names to the specified number @code{mm}
10080 This is useful if the
10081 resulting set of files is required to be interoperable with systems
10082 which limit the length of file names.
10084 If no value is given, or
10085 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
10086 a default of 39, suitable for OpenVMS Alpha
10087 Systems, is assumed
10090 No space is allowed between the @option{-k} and the numeric value. The numeric
10091 value may be omitted in which case a default of @option{-k8},
10093 with DOS-like file systems, is used. If no @option{-k} switch
10095 there is no limit on the length of file names.
10098 @item ^-p^/PRESERVE^
10099 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
10100 Causes the file ^modification^creation^ time stamp of the input file to be
10101 preserved and used for the time stamp of the output file(s). This may be
10102 useful for preserving coherency of time stamps in an environment where
10103 @code{gnatchop} is used as part of a standard build process.
10106 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
10107 Causes output of informational messages indicating the set of generated
10108 files to be suppressed. Warnings and error messages are unaffected.
10110 @item ^-r^/REFERENCE^
10111 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
10112 @findex Source_Reference
10113 Generate @code{Source_Reference} pragmas. Use this switch if the output
10114 files are regarded as temporary and development is to be done in terms
10115 of the original unchopped file. This switch causes
10116 @code{Source_Reference} pragmas to be inserted into each of the
10117 generated files to refers back to the original file name and line number.
10118 The result is that all error messages refer back to the original
10120 In addition, the debugging information placed into the object file (when
10121 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
10123 also refers back to this original file so that tools like profilers and
10124 debuggers will give information in terms of the original unchopped file.
10126 If the original file to be chopped itself contains
10127 a @code{Source_Reference}
10128 pragma referencing a third file, then gnatchop respects
10129 this pragma, and the generated @code{Source_Reference} pragmas
10130 in the chopped file refer to the original file, with appropriate
10131 line numbers. This is particularly useful when @code{gnatchop}
10132 is used in conjunction with @code{gnatprep} to compile files that
10133 contain preprocessing statements and multiple units.
10135 @item ^-v^/VERBOSE^
10136 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
10137 Causes @code{gnatchop} to operate in verbose mode. The version
10138 number and copyright notice are output, as well as exact copies of
10139 the gnat1 commands spawned to obtain the chop control information.
10141 @item ^-w^/OVERWRITE^
10142 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
10143 Overwrite existing file names. Normally @code{gnatchop} regards it as a
10144 fatal error if there is already a file with the same name as a
10145 file it would otherwise output, in other words if the files to be
10146 chopped contain duplicated units. This switch bypasses this
10147 check, and causes all but the last instance of such duplicated
10148 units to be skipped.
10152 @cindex @option{--GCC=} (@code{gnatchop})
10153 Specify the path of the GNAT parser to be used. When this switch is used,
10154 no attempt is made to add the prefix to the GNAT parser executable.
10158 @node Examples of gnatchop Usage
10159 @section Examples of @code{gnatchop} Usage
10163 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
10166 @item gnatchop -w hello_s.ada prerelease/files
10169 Chops the source file @file{hello_s.ada}. The output files will be
10170 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
10172 files with matching names in that directory (no files in the current
10173 directory are modified).
10175 @item gnatchop ^archive^ARCHIVE.^
10176 Chops the source file @file{^archive^ARCHIVE.^}
10177 into the current directory. One
10178 useful application of @code{gnatchop} is in sending sets of sources
10179 around, for example in email messages. The required sources are simply
10180 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
10182 @code{gnatchop} is used at the other end to reconstitute the original
10185 @item gnatchop file1 file2 file3 direc
10186 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
10187 the resulting files in the directory @file{direc}. Note that if any units
10188 occur more than once anywhere within this set of files, an error message
10189 is generated, and no files are written. To override this check, use the
10190 @option{^-w^/OVERWRITE^} switch,
10191 in which case the last occurrence in the last file will
10192 be the one that is output, and earlier duplicate occurrences for a given
10193 unit will be skipped.
10196 @node Configuration Pragmas
10197 @chapter Configuration Pragmas
10198 @cindex Configuration pragmas
10199 @cindex Pragmas, configuration
10202 In Ada 95, configuration pragmas include those pragmas described as
10203 such in the Ada 95 Reference Manual, as well as
10204 implementation-dependent pragmas that are configuration pragmas. See the
10205 individual descriptions of pragmas in the GNAT Reference Manual for
10206 details on these additional GNAT-specific configuration pragmas. Most
10207 notably, the pragma @code{Source_File_Name}, which allows
10208 specifying non-default names for source files, is a configuration
10209 pragma. The following is a complete list of configuration pragmas
10210 recognized by @code{GNAT}:
10217 Component_Alignment
10223 External_Name_Casing
10224 Float_Representation
10235 Propagate_Exceptions
10238 Restricted_Run_Time
10240 Restrictions_Warnings
10245 Task_Dispatching_Policy
10254 * Handling of Configuration Pragmas::
10255 * The Configuration Pragmas Files::
10258 @node Handling of Configuration Pragmas
10259 @section Handling of Configuration Pragmas
10261 Configuration pragmas may either appear at the start of a compilation
10262 unit, in which case they apply only to that unit, or they may apply to
10263 all compilations performed in a given compilation environment.
10265 GNAT also provides the @code{gnatchop} utility to provide an automatic
10266 way to handle configuration pragmas following the semantics for
10267 compilations (that is, files with multiple units), described in the RM.
10268 See @ref{Operating gnatchop in Compilation Mode} for details.
10269 However, for most purposes, it will be more convenient to edit the
10270 @file{gnat.adc} file that contains configuration pragmas directly,
10271 as described in the following section.
10273 @node The Configuration Pragmas Files
10274 @section The Configuration Pragmas Files
10275 @cindex @file{gnat.adc}
10278 In GNAT a compilation environment is defined by the current
10279 directory at the time that a compile command is given. This current
10280 directory is searched for a file whose name is @file{gnat.adc}. If
10281 this file is present, it is expected to contain one or more
10282 configuration pragmas that will be applied to the current compilation.
10283 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
10286 Configuration pragmas may be entered into the @file{gnat.adc} file
10287 either by running @code{gnatchop} on a source file that consists only of
10288 configuration pragmas, or more conveniently by
10289 direct editing of the @file{gnat.adc} file, which is a standard format
10292 In addition to @file{gnat.adc}, one additional file containing configuration
10293 pragmas may be applied to the current compilation using the switch
10294 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
10295 contains only configuration pragmas. These configuration pragmas are
10296 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
10297 is present and switch @option{-gnatA} is not used).
10299 It is allowed to specify several switches @option{-gnatec}, however only
10300 the last one on the command line will be taken into account.
10302 If you are using project file, a separate mechanism is provided using
10303 project attributes, see @ref{Specifying Configuration Pragmas} for more
10307 Of special interest to GNAT OpenVMS Alpha is the following
10308 configuration pragma:
10310 @smallexample @c ada
10312 pragma Extend_System (Aux_DEC);
10317 In the presence of this pragma, GNAT adds to the definition of the
10318 predefined package SYSTEM all the additional types and subprograms that are
10319 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
10322 @node Handling Arbitrary File Naming Conventions Using gnatname
10323 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
10324 @cindex Arbitrary File Naming Conventions
10327 * Arbitrary File Naming Conventions::
10328 * Running gnatname::
10329 * Switches for gnatname::
10330 * Examples of gnatname Usage::
10333 @node Arbitrary File Naming Conventions
10334 @section Arbitrary File Naming Conventions
10337 The GNAT compiler must be able to know the source file name of a compilation
10338 unit. When using the standard GNAT default file naming conventions
10339 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
10340 does not need additional information.
10343 When the source file names do not follow the standard GNAT default file naming
10344 conventions, the GNAT compiler must be given additional information through
10345 a configuration pragmas file (@pxref{Configuration Pragmas})
10347 When the non standard file naming conventions are well-defined,
10348 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
10349 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
10350 if the file naming conventions are irregular or arbitrary, a number
10351 of pragma @code{Source_File_Name} for individual compilation units
10353 To help maintain the correspondence between compilation unit names and
10354 source file names within the compiler,
10355 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
10358 @node Running gnatname
10359 @section Running @code{gnatname}
10362 The usual form of the @code{gnatname} command is
10365 $ gnatname [@var{switches}] @var{naming_pattern} [@var{naming_patterns}]
10369 All of the arguments are optional. If invoked without any argument,
10370 @code{gnatname} will display its usage.
10373 When used with at least one naming pattern, @code{gnatname} will attempt to
10374 find all the compilation units in files that follow at least one of the
10375 naming patterns. To find these compilation units,
10376 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
10380 One or several Naming Patterns may be given as arguments to @code{gnatname}.
10381 Each Naming Pattern is enclosed between double quotes.
10382 A Naming Pattern is a regular expression similar to the wildcard patterns
10383 used in file names by the Unix shells or the DOS prompt.
10386 Examples of Naming Patterns are
10395 For a more complete description of the syntax of Naming Patterns,
10396 see the second kind of regular expressions described in @file{g-regexp.ads}
10397 (the ``Glob'' regular expressions).
10400 When invoked with no switches, @code{gnatname} will create a configuration
10401 pragmas file @file{gnat.adc} in the current working directory, with pragmas
10402 @code{Source_File_Name} for each file that contains a valid Ada unit.
10404 @node Switches for gnatname
10405 @section Switches for @code{gnatname}
10408 Switches for @code{gnatname} must precede any specified Naming Pattern.
10411 You may specify any of the following switches to @code{gnatname}:
10416 @item ^-c^/CONFIG_FILE=^@file{file}
10417 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
10418 Create a configuration pragmas file @file{file} (instead of the default
10421 There may be zero, one or more space between @option{-c} and
10424 @file{file} may include directory information. @file{file} must be
10425 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
10426 When a switch @option{^-c^/CONFIG_FILE^} is
10427 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
10429 @item ^-d^/SOURCE_DIRS=^@file{dir}
10430 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
10431 Look for source files in directory @file{dir}. There may be zero, one or more
10432 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
10433 When a switch @option{^-d^/SOURCE_DIRS^}
10434 is specified, the current working directory will not be searched for source
10435 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
10436 or @option{^-D^/DIR_FILES^} switch.
10437 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
10438 If @file{dir} is a relative path, it is relative to the directory of
10439 the configuration pragmas file specified with switch
10440 @option{^-c^/CONFIG_FILE^},
10441 or to the directory of the project file specified with switch
10442 @option{^-P^/PROJECT_FILE^} or,
10443 if neither switch @option{^-c^/CONFIG_FILE^}
10444 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
10445 current working directory. The directory
10446 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
10448 @item ^-D^/DIRS_FILE=^@file{file}
10449 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
10450 Look for source files in all directories listed in text file @file{file}.
10451 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
10453 @file{file} must be an existing, readable text file.
10454 Each non empty line in @file{file} must be a directory.
10455 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
10456 switches @option{^-d^/SOURCE_DIRS^} as there are non empty lines in
10459 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
10460 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
10461 Foreign patterns. Using this switch, it is possible to add sources of languages
10462 other than Ada to the list of sources of a project file.
10463 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
10466 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
10469 will look for Ada units in all files with the @file{.ada} extension,
10470 and will add to the list of file for project @file{prj.gpr} the C files
10471 with extension ".^c^C^".
10474 @cindex @option{^-h^/HELP^} (@code{gnatname})
10475 Output usage (help) information. The output is written to @file{stdout}.
10477 @item ^-P^/PROJECT_FILE=^@file{proj}
10478 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
10479 Create or update project file @file{proj}. There may be zero, one or more space
10480 between @option{-P} and @file{proj}. @file{proj} may include directory
10481 information. @file{proj} must be writable.
10482 There may be only one switch @option{^-P^/PROJECT_FILE^}.
10483 When a switch @option{^-P^/PROJECT_FILE^} is specified,
10484 no switch @option{^-c^/CONFIG_FILE^} may be specified.
10486 @item ^-v^/VERBOSE^
10487 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
10488 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
10489 This includes name of the file written, the name of the directories to search
10490 and, for each file in those directories whose name matches at least one of
10491 the Naming Patterns, an indication of whether the file contains a unit,
10492 and if so the name of the unit.
10494 @item ^-v -v^/VERBOSE /VERBOSE^
10495 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
10496 Very Verbose mode. In addition to the output produced in verbose mode,
10497 for each file in the searched directories whose name matches none of
10498 the Naming Patterns, an indication is given that there is no match.
10500 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
10501 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
10502 Excluded patterns. Using this switch, it is possible to exclude some files
10503 that would match the name patterns. For example,
10505 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
10508 will look for Ada units in all files with the @file{.ada} extension,
10509 except those whose names end with @file{_nt.ada}.
10513 @node Examples of gnatname Usage
10514 @section Examples of @code{gnatname} Usage
10518 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
10524 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
10529 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
10530 and be writable. In addition, the directory
10531 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
10532 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
10535 Note the optional spaces after @option{-c} and @option{-d}.
10540 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
10541 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
10544 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
10545 /EXCLUDED_PATTERN=*_nt_body.ada
10546 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
10547 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
10551 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
10552 even in conjunction with one or several switches
10553 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
10554 are used in this example.
10556 @c *****************************************
10557 @c * G N A T P r o j e c t M a n a g e r *
10558 @c *****************************************
10559 @node GNAT Project Manager
10560 @chapter GNAT Project Manager
10564 * Examples of Project Files::
10565 * Project File Syntax::
10566 * Objects and Sources in Project Files::
10567 * Importing Projects::
10568 * Project Extension::
10569 * Project Hierarchy Extension::
10570 * External References in Project Files::
10571 * Packages in Project Files::
10572 * Variables from Imported Projects::
10574 * Library Projects::
10575 * Stand-alone Library Projects::
10576 * Switches Related to Project Files::
10577 * Tools Supporting Project Files::
10578 * An Extended Example::
10579 * Project File Complete Syntax::
10582 @c ****************
10583 @c * Introduction *
10584 @c ****************
10587 @section Introduction
10590 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
10591 you to manage complex builds involving a number of source files, directories,
10592 and compilation options for different system configurations. In particular,
10593 project files allow you to specify:
10596 The directory or set of directories containing the source files, and/or the
10597 names of the specific source files themselves
10599 The directory in which the compiler's output
10600 (@file{ALI} files, object files, tree files) is to be placed
10602 The directory in which the executable programs is to be placed
10604 ^Switch^Switch^ settings for any of the project-enabled tools
10605 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
10606 @code{gnatfind}); you can apply these settings either globally or to individual
10609 The source files containing the main subprogram(s) to be built
10611 The source programming language(s) (currently Ada and/or C)
10613 Source file naming conventions; you can specify these either globally or for
10614 individual compilation units
10621 @node Project Files
10622 @subsection Project Files
10625 Project files are written in a syntax close to that of Ada, using familiar
10626 notions such as packages, context clauses, declarations, default values,
10627 assignments, and inheritance. Finally, project files can be built
10628 hierarchically from other project files, simplifying complex system
10629 integration and project reuse.
10631 A @dfn{project} is a specific set of values for various compilation properties.
10632 The settings for a given project are described by means of
10633 a @dfn{project file}, which is a text file written in an Ada-like syntax.
10634 Property values in project files are either strings or lists of strings.
10635 Properties that are not explicitly set receive default values. A project
10636 file may interrogate the values of @dfn{external variables} (user-defined
10637 command-line switches or environment variables), and it may specify property
10638 settings conditionally, based on the value of such variables.
10640 In simple cases, a project's source files depend only on other source files
10641 in the same project, or on the predefined libraries. (@emph{Dependence} is
10643 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
10644 the Project Manager also allows more sophisticated arrangements,
10645 where the source files in one project depend on source files in other
10649 One project can @emph{import} other projects containing needed source files.
10651 You can organize GNAT projects in a hierarchy: a @emph{child} project
10652 can extend a @emph{parent} project, inheriting the parent's source files and
10653 optionally overriding any of them with alternative versions
10657 More generally, the Project Manager lets you structure large development
10658 efforts into hierarchical subsystems, where build decisions are delegated
10659 to the subsystem level, and thus different compilation environments
10660 (^switch^switch^ settings) used for different subsystems.
10662 The Project Manager is invoked through the
10663 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
10664 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
10666 There may be zero, one or more spaces between @option{-P} and
10667 @option{@emph{projectfile}}.
10669 If you want to define (on the command line) an external variable that is
10670 queried by the project file, you must use the
10671 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
10672 The Project Manager parses and interprets the project file, and drives the
10673 invoked tool based on the project settings.
10675 The Project Manager supports a wide range of development strategies,
10676 for systems of all sizes. Here are some typical practices that are
10680 Using a common set of source files, but generating object files in different
10681 directories via different ^switch^switch^ settings
10683 Using a mostly-shared set of source files, but with different versions of
10688 The destination of an executable can be controlled inside a project file
10689 using the @option{^-o^-o^}
10691 In the absence of such a ^switch^switch^ either inside
10692 the project file or on the command line, any executable files generated by
10693 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
10694 in the project file. If no @code{Exec_Dir} is specified, they will be placed
10695 in the object directory of the project.
10697 You can use project files to achieve some of the effects of a source
10698 versioning system (for example, defining separate projects for
10699 the different sets of sources that comprise different releases) but the
10700 Project Manager is independent of any source configuration management tools
10701 that might be used by the developers.
10703 The next section introduces the main features of GNAT's project facility
10704 through a sequence of examples; subsequent sections will present the syntax
10705 and semantics in more detail. A more formal description of the project
10706 facility appears in the GNAT Reference Manual.
10708 @c *****************************
10709 @c * Examples of Project Files *
10710 @c *****************************
10712 @node Examples of Project Files
10713 @section Examples of Project Files
10715 This section illustrates some of the typical uses of project files and
10716 explains their basic structure and behavior.
10719 * Common Sources with Different ^Switches^Switches^ and Directories::
10720 * Using External Variables::
10721 * Importing Other Projects::
10722 * Extending a Project::
10725 @node Common Sources with Different ^Switches^Switches^ and Directories
10726 @subsection Common Sources with Different ^Switches^Switches^ and Directories
10730 * Specifying the Object Directory::
10731 * Specifying the Exec Directory::
10732 * Project File Packages::
10733 * Specifying ^Switch^Switch^ Settings::
10734 * Main Subprograms::
10735 * Executable File Names::
10736 * Source File Naming Conventions::
10737 * Source Language(s)::
10741 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
10742 @file{proc.adb} are in the @file{/common} directory. The file
10743 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
10744 package @code{Pack}. We want to compile these source files under two sets
10745 of ^switches^switches^:
10748 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
10749 and the @option{^-gnata^-gnata^},
10750 @option{^-gnato^-gnato^},
10751 and @option{^-gnatE^-gnatE^} switches to the
10752 compiler; the compiler's output is to appear in @file{/common/debug}
10754 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
10755 to the compiler; the compiler's output is to appear in @file{/common/release}
10759 The GNAT project files shown below, respectively @file{debug.gpr} and
10760 @file{release.gpr} in the @file{/common} directory, achieve these effects.
10773 ^/common/debug^[COMMON.DEBUG]^
10778 ^/common/release^[COMMON.RELEASE]^
10783 Here are the corresponding project files:
10785 @smallexample @c projectfile
10788 for Object_Dir use "debug";
10789 for Main use ("proc");
10792 for ^Default_Switches^Default_Switches^ ("Ada")
10794 for Executable ("proc.adb") use "proc1";
10799 package Compiler is
10800 for ^Default_Switches^Default_Switches^ ("Ada")
10801 use ("-fstack-check",
10804 "^-gnatE^-gnatE^");
10810 @smallexample @c projectfile
10813 for Object_Dir use "release";
10814 for Exec_Dir use ".";
10815 for Main use ("proc");
10817 package Compiler is
10818 for ^Default_Switches^Default_Switches^ ("Ada")
10826 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
10827 insensitive), and analogously the project defined by @file{release.gpr} is
10828 @code{"Release"}. For consistency the file should have the same name as the
10829 project, and the project file's extension should be @code{"gpr"}. These
10830 conventions are not required, but a warning is issued if they are not followed.
10832 If the current directory is @file{^/temp^[TEMP]^}, then the command
10834 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
10838 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
10839 as well as the @code{^proc1^PROC1.EXE^} executable,
10840 using the ^switch^switch^ settings defined in the project file.
10842 Likewise, the command
10844 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
10848 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
10849 and the @code{^proc^PROC.EXE^}
10850 executable in @file{^/common^[COMMON]^},
10851 using the ^switch^switch^ settings from the project file.
10854 @unnumberedsubsubsec Source Files
10857 If a project file does not explicitly specify a set of source directories or
10858 a set of source files, then by default the project's source files are the
10859 Ada source files in the project file directory. Thus @file{pack.ads},
10860 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
10862 @node Specifying the Object Directory
10863 @unnumberedsubsubsec Specifying the Object Directory
10866 Several project properties are modeled by Ada-style @emph{attributes};
10867 a property is defined by supplying the equivalent of an Ada attribute
10868 definition clause in the project file.
10869 A project's object directory is another such a property; the corresponding
10870 attribute is @code{Object_Dir}, and its value is also a string expression,
10871 specified either as absolute or relative. In the later case,
10872 it is relative to the project file directory. Thus the compiler's
10873 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
10874 (for the @code{Debug} project)
10875 and to @file{^/common/release^[COMMON.RELEASE]^}
10876 (for the @code{Release} project).
10877 If @code{Object_Dir} is not specified, then the default is the project file
10880 @node Specifying the Exec Directory
10881 @unnumberedsubsubsec Specifying the Exec Directory
10884 A project's exec directory is another property; the corresponding
10885 attribute is @code{Exec_Dir}, and its value is also a string expression,
10886 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
10887 then the default is the object directory (which may also be the project file
10888 directory if attribute @code{Object_Dir} is not specified). Thus the executable
10889 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
10890 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
10891 and in @file{^/common^[COMMON]^} for the @code{Release} project.
10893 @node Project File Packages
10894 @unnumberedsubsubsec Project File Packages
10897 A GNAT tool that is integrated with the Project Manager is modeled by a
10898 corresponding package in the project file. In the example above,
10899 The @code{Debug} project defines the packages @code{Builder}
10900 (for @command{gnatmake}) and @code{Compiler};
10901 the @code{Release} project defines only the @code{Compiler} package.
10903 The Ada-like package syntax is not to be taken literally. Although packages in
10904 project files bear a surface resemblance to packages in Ada source code, the
10905 notation is simply a way to convey a grouping of properties for a named
10906 entity. Indeed, the package names permitted in project files are restricted
10907 to a predefined set, corresponding to the project-aware tools, and the contents
10908 of packages are limited to a small set of constructs.
10909 The packages in the example above contain attribute definitions.
10911 @node Specifying ^Switch^Switch^ Settings
10912 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
10915 ^Switch^Switch^ settings for a project-aware tool can be specified through
10916 attributes in the package that corresponds to the tool.
10917 The example above illustrates one of the relevant attributes,
10918 @code{^Default_Switches^Default_Switches^}, which is defined in packages
10919 in both project files.
10920 Unlike simple attributes like @code{Source_Dirs},
10921 @code{^Default_Switches^Default_Switches^} is
10922 known as an @emph{associative array}. When you define this attribute, you must
10923 supply an ``index'' (a literal string), and the effect of the attribute
10924 definition is to set the value of the array at the specified index.
10925 For the @code{^Default_Switches^Default_Switches^} attribute,
10926 the index is a programming language (in our case, Ada),
10927 and the value specified (after @code{use}) must be a list
10928 of string expressions.
10930 The attributes permitted in project files are restricted to a predefined set.
10931 Some may appear at project level, others in packages.
10932 For any attribute that is an associative array, the index must always be a
10933 literal string, but the restrictions on this string (e.g., a file name or a
10934 language name) depend on the individual attribute.
10935 Also depending on the attribute, its specified value will need to be either a
10936 string or a string list.
10938 In the @code{Debug} project, we set the switches for two tools,
10939 @command{gnatmake} and the compiler, and thus we include the two corresponding
10940 packages; each package defines the @code{^Default_Switches^Default_Switches^}
10941 attribute with index @code{"Ada"}.
10942 Note that the package corresponding to
10943 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
10944 similar, but only includes the @code{Compiler} package.
10946 In project @code{Debug} above, the ^switches^switches^ starting with
10947 @option{-gnat} that are specified in package @code{Compiler}
10948 could have been placed in package @code{Builder}, since @command{gnatmake}
10949 transmits all such ^switches^switches^ to the compiler.
10951 @node Main Subprograms
10952 @unnumberedsubsubsec Main Subprograms
10955 One of the specifiable properties of a project is a list of files that contain
10956 main subprograms. This property is captured in the @code{Main} attribute,
10957 whose value is a list of strings. If a project defines the @code{Main}
10958 attribute, it is not necessary to identify the main subprogram(s) when
10959 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
10961 @node Executable File Names
10962 @unnumberedsubsubsec Executable File Names
10965 By default, the executable file name corresponding to a main source is
10966 deduced from the main source file name. Through the attributes
10967 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
10968 it is possible to change this default.
10969 In project @code{Debug} above, the executable file name
10970 for main source @file{^proc.adb^PROC.ADB^} is
10971 @file{^proc1^PROC1.EXE^}.
10972 Attribute @code{Executable_Suffix}, when specified, may change the suffix
10973 of the executable files, when no attribute @code{Executable} applies:
10974 its value replace the platform-specific executable suffix.
10975 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
10976 specify a non default executable file name when several mains are built at once
10977 in a single @command{gnatmake} command.
10979 @node Source File Naming Conventions
10980 @unnumberedsubsubsec Source File Naming Conventions
10983 Since the project files above do not specify any source file naming
10984 conventions, the GNAT defaults are used. The mechanism for defining source
10985 file naming conventions -- a package named @code{Naming} --
10986 is described below (@pxref{Naming Schemes}).
10988 @node Source Language(s)
10989 @unnumberedsubsubsec Source Language(s)
10992 Since the project files do not specify a @code{Languages} attribute, by
10993 default the GNAT tools assume that the language of the project file is Ada.
10994 More generally, a project can comprise source files
10995 in Ada, C, and/or other languages.
10997 @node Using External Variables
10998 @subsection Using External Variables
11001 Instead of supplying different project files for debug and release, we can
11002 define a single project file that queries an external variable (set either
11003 on the command line or via an ^environment variable^logical name^) in order to
11004 conditionally define the appropriate settings. Again, assume that the
11005 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
11006 located in directory @file{^/common^[COMMON]^}. The following project file,
11007 @file{build.gpr}, queries the external variable named @code{STYLE} and
11008 defines an object directory and ^switch^switch^ settings based on whether
11009 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
11010 the default is @code{"deb"}.
11012 @smallexample @c projectfile
11015 for Main use ("proc");
11017 type Style_Type is ("deb", "rel");
11018 Style : Style_Type := external ("STYLE", "deb");
11022 for Object_Dir use "debug";
11025 for Object_Dir use "release";
11026 for Exec_Dir use ".";
11035 for ^Default_Switches^Default_Switches^ ("Ada")
11037 for Executable ("proc") use "proc1";
11046 package Compiler is
11050 for ^Default_Switches^Default_Switches^ ("Ada")
11051 use ("^-gnata^-gnata^",
11053 "^-gnatE^-gnatE^");
11056 for ^Default_Switches^Default_Switches^ ("Ada")
11067 @code{Style_Type} is an example of a @emph{string type}, which is the project
11068 file analog of an Ada enumeration type but whose components are string literals
11069 rather than identifiers. @code{Style} is declared as a variable of this type.
11071 The form @code{external("STYLE", "deb")} is known as an
11072 @emph{external reference}; its first argument is the name of an
11073 @emph{external variable}, and the second argument is a default value to be
11074 used if the external variable doesn't exist. You can define an external
11075 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
11076 or you can use ^an environment variable^a logical name^
11077 as an external variable.
11079 Each @code{case} construct is expanded by the Project Manager based on the
11080 value of @code{Style}. Thus the command
11083 gnatmake -P/common/build.gpr -XSTYLE=deb
11089 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
11094 is equivalent to the @command{gnatmake} invocation using the project file
11095 @file{debug.gpr} in the earlier example. So is the command
11097 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
11101 since @code{"deb"} is the default for @code{STYLE}.
11107 gnatmake -P/common/build.gpr -XSTYLE=rel
11113 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
11118 is equivalent to the @command{gnatmake} invocation using the project file
11119 @file{release.gpr} in the earlier example.
11121 @node Importing Other Projects
11122 @subsection Importing Other Projects
11123 @cindex @code{ADA_PROJECT_PATH}
11126 A compilation unit in a source file in one project may depend on compilation
11127 units in source files in other projects. To compile this unit under
11128 control of a project file, the
11129 dependent project must @emph{import} the projects containing the needed source
11131 This effect is obtained using syntax similar to an Ada @code{with} clause,
11132 but where @code{with}ed entities are strings that denote project files.
11134 As an example, suppose that the two projects @code{GUI_Proj} and
11135 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
11136 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
11137 and @file{^/comm^[COMM]^}, respectively.
11138 Suppose that the source files for @code{GUI_Proj} are
11139 @file{gui.ads} and @file{gui.adb}, and that the source files for
11140 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
11141 files is located in its respective project file directory. Schematically:
11160 We want to develop an application in directory @file{^/app^[APP]^} that
11161 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
11162 the corresponding project files (e.g. the ^switch^switch^ settings
11163 and object directory).
11164 Skeletal code for a main procedure might be something like the following:
11166 @smallexample @c ada
11169 procedure App_Main is
11178 Here is a project file, @file{app_proj.gpr}, that achieves the desired
11181 @smallexample @c projectfile
11183 with "/gui/gui_proj", "/comm/comm_proj";
11184 project App_Proj is
11185 for Main use ("app_main");
11191 Building an executable is achieved through the command:
11193 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
11196 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
11197 in the directory where @file{app_proj.gpr} resides.
11199 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
11200 (as illustrated above) the @code{with} clause can omit the extension.
11202 Our example specified an absolute path for each imported project file.
11203 Alternatively, the directory name of an imported object can be omitted
11207 The imported project file is in the same directory as the importing project
11210 You have defined ^an environment variable^a logical name^
11211 that includes the directory containing
11212 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
11213 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
11214 directory names separated by colons (semicolons on Windows).
11218 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
11219 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
11222 @smallexample @c projectfile
11224 with "gui_proj", "comm_proj";
11225 project App_Proj is
11226 for Main use ("app_main");
11232 Importing other projects can create ambiguities.
11233 For example, the same unit might be present in different imported projects, or
11234 it might be present in both the importing project and in an imported project.
11235 Both of these conditions are errors. Note that in the current version of
11236 the Project Manager, it is illegal to have an ambiguous unit even if the
11237 unit is never referenced by the importing project. This restriction may be
11238 relaxed in a future release.
11240 @node Extending a Project
11241 @subsection Extending a Project
11244 In large software systems it is common to have multiple
11245 implementations of a common interface; in Ada terms, multiple versions of a
11246 package body for the same specification. For example, one implementation
11247 might be safe for use in tasking programs, while another might only be used
11248 in sequential applications. This can be modeled in GNAT using the concept
11249 of @emph{project extension}. If one project (the ``child'') @emph{extends}
11250 another project (the ``parent'') then by default all source files of the
11251 parent project are inherited by the child, but the child project can
11252 override any of the parent's source files with new versions, and can also
11253 add new files. This facility is the project analog of a type extension in
11254 Object-Oriented Programming. Project hierarchies are permitted (a child
11255 project may be the parent of yet another project), and a project that
11256 inherits one project can also import other projects.
11258 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
11259 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
11260 @file{pack.adb}, and @file{proc.adb}:
11273 Note that the project file can simply be empty (that is, no attribute or
11274 package is defined):
11276 @smallexample @c projectfile
11278 project Seq_Proj is
11284 implying that its source files are all the Ada source files in the project
11287 Suppose we want to supply an alternate version of @file{pack.adb}, in
11288 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
11289 @file{pack.ads} and @file{proc.adb}. We can define a project
11290 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
11294 ^/tasking^[TASKING]^
11300 project Tasking_Proj extends "/seq/seq_proj" is
11306 The version of @file{pack.adb} used in a build depends on which project file
11309 Note that we could have obtained the desired behavior using project import
11310 rather than project inheritance; a @code{base} project would contain the
11311 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
11312 import @code{base} and add @file{pack.adb}, and likewise a tasking project
11313 would import @code{base} and add a different version of @file{pack.adb}. The
11314 choice depends on whether other sources in the original project need to be
11315 overridden. If they do, then project extension is necessary, otherwise,
11316 importing is sufficient.
11319 In a project file that extends another project file, it is possible to
11320 indicate that an inherited source is not part of the sources of the extending
11321 project. This is necessary sometimes when a package spec has been overloaded
11322 and no longer requires a body: in this case, it is necessary to indicate that
11323 the inherited body is not part of the sources of the project, otherwise there
11324 will be a compilation error when compiling the spec.
11326 For that purpose, the attribute @code{Locally_Removed_Files} is used.
11327 Its value is a string list: a list of file names.
11329 @smallexample @c @projectfile
11330 project B extends "a" is
11331 for Source_Files use ("pkg.ads");
11332 -- New spec of Pkg does not need a completion
11333 for Locally_Removed_Files use ("pkg.adb");
11337 Attribute @code{Locally_Removed_Files} may also be used to check if a source
11338 is still needed: if it is possible to build using @command{gnatmake} when such
11339 a source is put in attribute @code{Locally_Removed_Files} of a project P, then
11340 it is possible to remove the source completely from a system that includes
11343 @c ***********************
11344 @c * Project File Syntax *
11345 @c ***********************
11347 @node Project File Syntax
11348 @section Project File Syntax
11357 * Associative Array Attributes::
11358 * case Constructions::
11362 This section describes the structure of project files.
11364 A project may be an @emph{independent project}, entirely defined by a single
11365 project file. Any Ada source file in an independent project depends only
11366 on the predefined library and other Ada source files in the same project.
11369 A project may also @dfn{depend on} other projects, in either or both of
11370 the following ways:
11372 @item It may import any number of projects
11373 @item It may extend at most one other project
11377 The dependence relation is a directed acyclic graph (the subgraph reflecting
11378 the ``extends'' relation is a tree).
11380 A project's @dfn{immediate sources} are the source files directly defined by
11381 that project, either implicitly by residing in the project file's directory,
11382 or explicitly through any of the source-related attributes described below.
11383 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
11384 of @var{proj} together with the immediate sources (unless overridden) of any
11385 project on which @var{proj} depends (either directly or indirectly).
11388 @subsection Basic Syntax
11391 As seen in the earlier examples, project files have an Ada-like syntax.
11392 The minimal project file is:
11393 @smallexample @c projectfile
11402 The identifier @code{Empty} is the name of the project.
11403 This project name must be present after the reserved
11404 word @code{end} at the end of the project file, followed by a semi-colon.
11406 Any name in a project file, such as the project name or a variable name,
11407 has the same syntax as an Ada identifier.
11409 The reserved words of project files are the Ada reserved words plus
11410 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
11411 reserved words currently used in project file syntax are:
11439 Comments in project files have the same syntax as in Ada, two consecutives
11440 hyphens through the end of the line.
11443 @subsection Packages
11446 A project file may contain @emph{packages}. The name of a package must be one
11447 of the identifiers from the following list. A package
11448 with a given name may only appear once in a project file. Package names are
11449 case insensitive. The following package names are legal:
11465 @code{Cross_Reference}
11469 @code{Pretty_Printer}
11479 @code{Language_Processing}
11483 In its simplest form, a package may be empty:
11485 @smallexample @c projectfile
11495 A package may contain @emph{attribute declarations},
11496 @emph{variable declarations} and @emph{case constructions}, as will be
11499 When there is ambiguity between a project name and a package name,
11500 the name always designates the project. To avoid possible confusion, it is
11501 always a good idea to avoid naming a project with one of the
11502 names allowed for packages or any name that starts with @code{gnat}.
11505 @subsection Expressions
11508 An @emph{expression} is either a @emph{string expression} or a
11509 @emph{string list expression}.
11511 A @emph{string expression} is either a @emph{simple string expression} or a
11512 @emph{compound string expression}.
11514 A @emph{simple string expression} is one of the following:
11516 @item A literal string; e.g.@code{"comm/my_proj.gpr"}
11517 @item A string-valued variable reference (@pxref{Variables})
11518 @item A string-valued attribute reference (@pxref{Attributes})
11519 @item An external reference (@pxref{External References in Project Files})
11523 A @emph{compound string expression} is a concatenation of string expressions,
11524 using the operator @code{"&"}
11526 Path & "/" & File_Name & ".ads"
11530 A @emph{string list expression} is either a
11531 @emph{simple string list expression} or a
11532 @emph{compound string list expression}.
11534 A @emph{simple string list expression} is one of the following:
11536 @item A parenthesized list of zero or more string expressions,
11537 separated by commas
11539 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
11542 @item A string list-valued variable reference
11543 @item A string list-valued attribute reference
11547 A @emph{compound string list expression} is the concatenation (using
11548 @code{"&"}) of a simple string list expression and an expression. Note that
11549 each term in a compound string list expression, except the first, may be
11550 either a string expression or a string list expression.
11552 @smallexample @c projectfile
11554 File_Name_List := () & File_Name; -- One string in this list
11555 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
11557 Big_List := File_Name_List & Extended_File_Name_List;
11558 -- Concatenation of two string lists: three strings
11559 Illegal_List := "gnat.adc" & Extended_File_Name_List;
11560 -- Illegal: must start with a string list
11565 @subsection String Types
11568 A @emph{string type declaration} introduces a discrete set of string literals.
11569 If a string variable is declared to have this type, its value
11570 is restricted to the given set of literals.
11572 Here is an example of a string type declaration:
11574 @smallexample @c projectfile
11575 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
11579 Variables of a string type are called @emph{typed variables}; all other
11580 variables are called @emph{untyped variables}. Typed variables are
11581 particularly useful in @code{case} constructions, to support conditional
11582 attribute declarations.
11583 (@pxref{case Constructions}).
11585 The string literals in the list are case sensitive and must all be different.
11586 They may include any graphic characters allowed in Ada, including spaces.
11588 A string type may only be declared at the project level, not inside a package.
11590 A string type may be referenced by its name if it has been declared in the same
11591 project file, or by an expanded name whose prefix is the name of the project
11592 in which it is declared.
11595 @subsection Variables
11598 A variable may be declared at the project file level, or within a package.
11599 Here are some examples of variable declarations:
11601 @smallexample @c projectfile
11603 This_OS : OS := external ("OS"); -- a typed variable declaration
11604 That_OS := "GNU/Linux"; -- an untyped variable declaration
11609 The syntax of a @emph{typed variable declaration} is identical to the Ada
11610 syntax for an object declaration. By contrast, the syntax of an untyped
11611 variable declaration is identical to an Ada assignment statement. In fact,
11612 variable declarations in project files have some of the characteristics of
11613 an assignment, in that successive declarations for the same variable are
11614 allowed. Untyped variable declarations do establish the expected kind of the
11615 variable (string or string list), and successive declarations for it must
11616 respect the initial kind.
11619 A string variable declaration (typed or untyped) declares a variable
11620 whose value is a string. This variable may be used as a string expression.
11621 @smallexample @c projectfile
11622 File_Name := "readme.txt";
11623 Saved_File_Name := File_Name & ".saved";
11627 A string list variable declaration declares a variable whose value is a list
11628 of strings. The list may contain any number (zero or more) of strings.
11630 @smallexample @c projectfile
11632 List_With_One_Element := ("^-gnaty^-gnaty^");
11633 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
11634 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
11635 "pack2.ada", "util_.ada", "util.ada");
11639 The same typed variable may not be declared more than once at project level,
11640 and it may not be declared more than once in any package; it is in effect
11643 The same untyped variable may be declared several times. Declarations are
11644 elaborated in the order in which they appear, so the new value replaces
11645 the old one, and any subsequent reference to the variable uses the new value.
11646 However, as noted above, if a variable has been declared as a string, all
11648 declarations must give it a string value. Similarly, if a variable has
11649 been declared as a string list, all subsequent declarations
11650 must give it a string list value.
11652 A @emph{variable reference} may take several forms:
11655 @item The simple variable name, for a variable in the current package (if any)
11656 or in the current project
11657 @item An expanded name, whose prefix is a context name.
11661 A @emph{context} may be one of the following:
11664 @item The name of an existing package in the current project
11665 @item The name of an imported project of the current project
11666 @item The name of an ancestor project (i.e., a project extended by the current
11667 project, either directly or indirectly)
11668 @item An expanded name whose prefix is an imported/parent project name, and
11669 whose selector is a package name in that project.
11673 A variable reference may be used in an expression.
11676 @subsection Attributes
11679 A project (and its packages) may have @emph{attributes} that define
11680 the project's properties. Some attributes have values that are strings;
11681 others have values that are string lists.
11683 There are two categories of attributes: @emph{simple attributes}
11684 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
11686 Legal project attribute names, and attribute names for each legal package are
11687 listed below. Attributes names are case-insensitive.
11689 The following attributes are defined on projects (all are simple attributes):
11691 @multitable @columnfractions .4 .3
11692 @item @emph{Attribute Name}
11694 @item @code{Source_Files}
11696 @item @code{Source_Dirs}
11698 @item @code{Source_List_File}
11700 @item @code{Object_Dir}
11702 @item @code{Exec_Dir}
11704 @item @code{Locally_Removed_Files}
11706 @item @code{Languages}
11710 @item @code{Library_Dir}
11712 @item @code{Library_Name}
11714 @item @code{Library_Kind}
11716 @item @code{Library_Version}
11718 @item @code{Library_Interface}
11720 @item @code{Library_Auto_Init}
11722 @item @code{Library_Options}
11724 @item @code{Library_Src_Dir}
11726 @item @code{Library_ALI_Dir}
11728 @item @code{Library_GCC}
11730 @item @code{Library_Symbol_File}
11732 @item @code{Library_Symbol_Policy}
11734 @item @code{Library_Reference_Symbol_File}
11736 @item @code{Externally_Built}
11741 The following attributes are defined for package @code{Naming}
11742 (@pxref{Naming Schemes}):
11744 @multitable @columnfractions .4 .2 .2 .2
11745 @item Attribute Name @tab Category @tab Index @tab Value
11746 @item @code{Spec_Suffix}
11747 @tab associative array
11750 @item @code{Body_Suffix}
11751 @tab associative array
11754 @item @code{Separate_Suffix}
11755 @tab simple attribute
11758 @item @code{Casing}
11759 @tab simple attribute
11762 @item @code{Dot_Replacement}
11763 @tab simple attribute
11767 @tab associative array
11771 @tab associative array
11774 @item @code{Specification_Exceptions}
11775 @tab associative array
11778 @item @code{Implementation_Exceptions}
11779 @tab associative array
11785 The following attributes are defined for packages @code{Builder},
11786 @code{Compiler}, @code{Binder},
11787 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
11788 (@pxref{^Switches^Switches^ and Project Files}).
11790 @multitable @columnfractions .4 .2 .2 .2
11791 @item Attribute Name @tab Category @tab Index @tab Value
11792 @item @code{^Default_Switches^Default_Switches^}
11793 @tab associative array
11796 @item @code{^Switches^Switches^}
11797 @tab associative array
11803 In addition, package @code{Compiler} has a single string attribute
11804 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
11805 string attribute @code{Global_Configuration_Pragmas}.
11808 Each simple attribute has a default value: the empty string (for string-valued
11809 attributes) and the empty list (for string list-valued attributes).
11811 An attribute declaration defines a new value for an attribute.
11813 Examples of simple attribute declarations:
11815 @smallexample @c projectfile
11816 for Object_Dir use "objects";
11817 for Source_Dirs use ("units", "test/drivers");
11821 The syntax of a @dfn{simple attribute declaration} is similar to that of an
11822 attribute definition clause in Ada.
11824 Attributes references may be appear in expressions.
11825 The general form for such a reference is @code{<entity>'<attribute>}:
11826 Associative array attributes are functions. Associative
11827 array attribute references must have an argument that is a string literal.
11831 @smallexample @c projectfile
11833 Naming'Dot_Replacement
11834 Imported_Project'Source_Dirs
11835 Imported_Project.Naming'Casing
11836 Builder'^Default_Switches^Default_Switches^("Ada")
11840 The prefix of an attribute may be:
11842 @item @code{project} for an attribute of the current project
11843 @item The name of an existing package of the current project
11844 @item The name of an imported project
11845 @item The name of a parent project that is extended by the current project
11846 @item An expanded name whose prefix is imported/parent project name,
11847 and whose selector is a package name
11852 @smallexample @c projectfile
11855 for Source_Dirs use project'Source_Dirs & "units";
11856 for Source_Dirs use project'Source_Dirs & "test/drivers"
11862 In the first attribute declaration, initially the attribute @code{Source_Dirs}
11863 has the default value: an empty string list. After this declaration,
11864 @code{Source_Dirs} is a string list of one element: @code{"units"}.
11865 After the second attribute declaration @code{Source_Dirs} is a string list of
11866 two elements: @code{"units"} and @code{"test/drivers"}.
11868 Note: this example is for illustration only. In practice,
11869 the project file would contain only one attribute declaration:
11871 @smallexample @c projectfile
11872 for Source_Dirs use ("units", "test/drivers");
11875 @node Associative Array Attributes
11876 @subsection Associative Array Attributes
11879 Some attributes are defined as @emph{associative arrays}. An associative
11880 array may be regarded as a function that takes a string as a parameter
11881 and delivers a string or string list value as its result.
11883 Here are some examples of single associative array attribute associations:
11885 @smallexample @c projectfile
11886 for Body ("main") use "Main.ada";
11887 for ^Switches^Switches^ ("main.ada")
11889 "^-gnatv^-gnatv^");
11890 for ^Switches^Switches^ ("main.ada")
11891 use Builder'^Switches^Switches^ ("main.ada")
11896 Like untyped variables and simple attributes, associative array attributes
11897 may be declared several times. Each declaration supplies a new value for the
11898 attribute, and replaces the previous setting.
11901 An associative array attribute may be declared as a full associative array
11902 declaration, with the value of the same attribute in an imported or extended
11905 @smallexample @c projectfile
11907 for Default_Switches use Default.Builder'Default_Switches;
11912 In this example, @code{Default} must be either a project imported by the
11913 current project, or the project that the current project extends. If the
11914 attribute is in a package (in this case, in package @code{Builder}), the same
11915 package needs to be specified.
11918 A full associative array declaration replaces any other declaration for the
11919 attribute, including other full associative array declaration. Single
11920 associative array associations may be declare after a full associative
11921 declaration, modifying the value for a single association of the attribute.
11923 @node case Constructions
11924 @subsection @code{case} Constructions
11927 A @code{case} construction is used in a project file to effect conditional
11929 Here is a typical example:
11931 @smallexample @c projectfile
11934 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
11936 OS : OS_Type := external ("OS", "GNU/Linux");
11940 package Compiler is
11942 when "GNU/Linux" | "Unix" =>
11943 for ^Default_Switches^Default_Switches^ ("Ada")
11944 use ("^-gnath^-gnath^");
11946 for ^Default_Switches^Default_Switches^ ("Ada")
11947 use ("^-gnatP^-gnatP^");
11956 The syntax of a @code{case} construction is based on the Ada case statement
11957 (although there is no @code{null} construction for empty alternatives).
11959 The case expression must be a typed string variable.
11960 Each alternative comprises the reserved word @code{when}, either a list of
11961 literal strings separated by the @code{"|"} character or the reserved word
11962 @code{others}, and the @code{"=>"} token.
11963 Each literal string must belong to the string type that is the type of the
11965 An @code{others} alternative, if present, must occur last.
11967 After each @code{=>}, there are zero or more constructions. The only
11968 constructions allowed in a case construction are other case constructions and
11969 attribute declarations. String type declarations, variable declarations and
11970 package declarations are not allowed.
11972 The value of the case variable is often given by an external reference
11973 (@pxref{External References in Project Files}).
11975 @c ****************************************
11976 @c * Objects and Sources in Project Files *
11977 @c ****************************************
11979 @node Objects and Sources in Project Files
11980 @section Objects and Sources in Project Files
11983 * Object Directory::
11985 * Source Directories::
11986 * Source File Names::
11990 Each project has exactly one object directory and one or more source
11991 directories. The source directories must contain at least one source file,
11992 unless the project file explicitly specifies that no source files are present
11993 (@pxref{Source File Names}).
11995 @node Object Directory
11996 @subsection Object Directory
11999 The object directory for a project is the directory containing the compiler's
12000 output (such as @file{ALI} files and object files) for the project's immediate
12003 The object directory is given by the value of the attribute @code{Object_Dir}
12004 in the project file.
12006 @smallexample @c projectfile
12007 for Object_Dir use "objects";
12011 The attribute @var{Object_Dir} has a string value, the path name of the object
12012 directory. The path name may be absolute or relative to the directory of the
12013 project file. This directory must already exist, and be readable and writable.
12015 By default, when the attribute @code{Object_Dir} is not given an explicit value
12016 or when its value is the empty string, the object directory is the same as the
12017 directory containing the project file.
12019 @node Exec Directory
12020 @subsection Exec Directory
12023 The exec directory for a project is the directory containing the executables
12024 for the project's main subprograms.
12026 The exec directory is given by the value of the attribute @code{Exec_Dir}
12027 in the project file.
12029 @smallexample @c projectfile
12030 for Exec_Dir use "executables";
12034 The attribute @var{Exec_Dir} has a string value, the path name of the exec
12035 directory. The path name may be absolute or relative to the directory of the
12036 project file. This directory must already exist, and be writable.
12038 By default, when the attribute @code{Exec_Dir} is not given an explicit value
12039 or when its value is the empty string, the exec directory is the same as the
12040 object directory of the project file.
12042 @node Source Directories
12043 @subsection Source Directories
12046 The source directories of a project are specified by the project file
12047 attribute @code{Source_Dirs}.
12049 This attribute's value is a string list. If the attribute is not given an
12050 explicit value, then there is only one source directory, the one where the
12051 project file resides.
12053 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
12056 @smallexample @c projectfile
12057 for Source_Dirs use ();
12061 indicates that the project contains no source files.
12063 Otherwise, each string in the string list designates one or more
12064 source directories.
12066 @smallexample @c projectfile
12067 for Source_Dirs use ("sources", "test/drivers");
12071 If a string in the list ends with @code{"/**"}, then the directory whose path
12072 name precedes the two asterisks, as well as all its subdirectories
12073 (recursively), are source directories.
12075 @smallexample @c projectfile
12076 for Source_Dirs use ("/system/sources/**");
12080 Here the directory @code{/system/sources} and all of its subdirectories
12081 (recursively) are source directories.
12083 To specify that the source directories are the directory of the project file
12084 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
12085 @smallexample @c projectfile
12086 for Source_Dirs use ("./**");
12090 Each of the source directories must exist and be readable.
12092 @node Source File Names
12093 @subsection Source File Names
12096 In a project that contains source files, their names may be specified by the
12097 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
12098 (a string). Source file names never include any directory information.
12100 If the attribute @code{Source_Files} is given an explicit value, then each
12101 element of the list is a source file name.
12103 @smallexample @c projectfile
12104 for Source_Files use ("main.adb");
12105 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
12109 If the attribute @code{Source_Files} is not given an explicit value,
12110 but the attribute @code{Source_List_File} is given a string value,
12111 then the source file names are contained in the text file whose path name
12112 (absolute or relative to the directory of the project file) is the
12113 value of the attribute @code{Source_List_File}.
12115 Each line in the file that is not empty or is not a comment
12116 contains a source file name.
12118 @smallexample @c projectfile
12119 for Source_List_File use "source_list.txt";
12123 By default, if neither the attribute @code{Source_Files} nor the attribute
12124 @code{Source_List_File} is given an explicit value, then each file in the
12125 source directories that conforms to the project's naming scheme
12126 (@pxref{Naming Schemes}) is an immediate source of the project.
12128 A warning is issued if both attributes @code{Source_Files} and
12129 @code{Source_List_File} are given explicit values. In this case, the attribute
12130 @code{Source_Files} prevails.
12132 Each source file name must be the name of one existing source file
12133 in one of the source directories.
12135 A @code{Source_Files} attribute whose value is an empty list
12136 indicates that there are no source files in the project.
12138 If the order of the source directories is known statically, that is if
12139 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
12140 be several files with the same source file name. In this case, only the file
12141 in the first directory is considered as an immediate source of the project
12142 file. If the order of the source directories is not known statically, it is
12143 an error to have several files with the same source file name.
12145 Projects can be specified to have no Ada source
12146 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
12147 list, or the @code{"Ada"} may be absent from @code{Languages}:
12149 @smallexample @c projectfile
12150 for Source_Dirs use ();
12151 for Source_Files use ();
12152 for Languages use ("C", "C++");
12156 Otherwise, a project must contain at least one immediate source.
12158 Projects with no source files are useful as template packages
12159 (@pxref{Packages in Project Files}) for other projects; in particular to
12160 define a package @code{Naming} (@pxref{Naming Schemes}).
12162 @c ****************************
12163 @c * Importing Projects *
12164 @c ****************************
12166 @node Importing Projects
12167 @section Importing Projects
12168 @cindex @code{ADA_PROJECT_PATH}
12171 An immediate source of a project P may depend on source files that
12172 are neither immediate sources of P nor in the predefined library.
12173 To get this effect, P must @emph{import} the projects that contain the needed
12176 @smallexample @c projectfile
12178 with "project1", "utilities.gpr";
12179 with "/namings/apex.gpr";
12186 As can be seen in this example, the syntax for importing projects is similar
12187 to the syntax for importing compilation units in Ada. However, project files
12188 use literal strings instead of names, and the @code{with} clause identifies
12189 project files rather than packages.
12191 Each literal string is the file name or path name (absolute or relative) of a
12192 project file. If a string corresponds to a file name, with no path or a
12193 relative path, then its location is determined by the @emph{project path}. The
12194 latter can be queried using @code{gnatls -v}. It contains:
12198 In first position, the directory containing the current project file.
12200 In last position, the default project directory. This default project directory
12201 is part of the GNAT installation and is the standard place to install project
12202 files giving access to standard support libraries.
12204 @ref{Installing a library}
12208 In between, all the directories referenced in the
12209 ^environment variable^logical name^ @env{ADA_PROJECT_PATH} if it exists.
12213 If a relative pathname is used, as in
12215 @smallexample @c projectfile
12220 then the full path for the project is constructed by concatenating this
12221 relative path to those in the project path, in order, until a matching file is
12222 found. Any symbolic link will be fully resolved in the directory of the
12223 importing project file before the imported project file is examined.
12225 If the @code{with}'ed project file name does not have an extension,
12226 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
12227 then the file name as specified in the @code{with} clause (no extension) will
12228 be used. In the above example, if a file @code{project1.gpr} is found, then it
12229 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
12230 then it will be used; if neither file exists, this is an error.
12232 A warning is issued if the name of the project file does not match the
12233 name of the project; this check is case insensitive.
12235 Any source file that is an immediate source of the imported project can be
12236 used by the immediate sources of the importing project, transitively. Thus
12237 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
12238 sources of @code{A} may depend on the immediate sources of @code{C}, even if
12239 @code{A} does not import @code{C} explicitly. However, this is not recommended,
12240 because if and when @code{B} ceases to import @code{C}, some sources in
12241 @code{A} will no longer compile.
12243 A side effect of this capability is that normally cyclic dependencies are not
12244 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
12245 is not allowed to import @code{A}. However, there are cases when cyclic
12246 dependencies would be beneficial. For these cases, another form of import
12247 between projects exists, the @code{limited with}: a project @code{A} that
12248 imports a project @code{B} with a straight @code{with} may also be imported,
12249 directly or indirectly, by @code{B} on the condition that imports from @code{B}
12250 to @code{A} include at least one @code{limited with}.
12252 @smallexample @c 0projectfile
12258 limited with "../a/a.gpr";
12266 limited with "../a/a.gpr";
12272 In the above legal example, there are two project cycles:
12275 @item A -> C -> D -> A
12279 In each of these cycle there is one @code{limited with}: import of @code{A}
12280 from @code{B} and import of @code{A} from @code{D}.
12282 The difference between straight @code{with} and @code{limited with} is that
12283 the name of a project imported with a @code{limited with} cannot be used in the
12284 project that imports it. In particular, its packages cannot be renamed and
12285 its variables cannot be referred to.
12287 An exception to the above rules for @code{limited with} is that for the main
12288 project specified to @command{gnatmake} or to the @command{GNAT} driver a
12289 @code{limited with} is equivalent to a straight @code{with}. For example,
12290 in the example above, projects @code{B} and @code{D} could not be main
12291 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
12292 each have a @code{limited with} that is the only one in a cycle of importing
12295 @c *********************
12296 @c * Project Extension *
12297 @c *********************
12299 @node Project Extension
12300 @section Project Extension
12303 During development of a large system, it is sometimes necessary to use
12304 modified versions of some of the source files, without changing the original
12305 sources. This can be achieved through the @emph{project extension} facility.
12307 @smallexample @c projectfile
12308 project Modified_Utilities extends "/baseline/utilities.gpr" is ...
12312 A project extension declaration introduces an extending project
12313 (the @emph{child}) and a project being extended (the @emph{parent}).
12315 By default, a child project inherits all the sources of its parent.
12316 However, inherited sources can be overridden: a unit in a parent is hidden
12317 by a unit of the same name in the child.
12319 Inherited sources are considered to be sources (but not immediate sources)
12320 of the child project; see @ref{Project File Syntax}.
12322 An inherited source file retains any switches specified in the parent project.
12324 For example if the project @code{Utilities} contains the specification and the
12325 body of an Ada package @code{Util_IO}, then the project
12326 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
12327 The original body of @code{Util_IO} will not be considered in program builds.
12328 However, the package specification will still be found in the project
12331 A child project can have only one parent but it may import any number of other
12334 A project is not allowed to import directly or indirectly at the same time a
12335 child project and any of its ancestors.
12337 @c *******************************
12338 @c * Project Hierarchy Extension *
12339 @c *******************************
12341 @node Project Hierarchy Extension
12342 @section Project Hierarchy Extension
12345 When extending a large system spanning multiple projects, it is often
12346 inconvenient to extend every project in the hierarchy that is impacted by a
12347 small change introduced. In such cases, it is possible to create a virtual
12348 extension of entire hierarchy using @code{extends all} relationship.
12350 When the project is extended using @code{extends all} inheritance, all projects
12351 that are imported by it, both directly and indirectly, are considered virtually
12352 extended. That is, the Project Manager creates "virtual projects"
12353 that extend every project in the hierarchy; all these virtual projects have
12354 no sources of their own and have as object directory the object directory of
12355 the root of "extending all" project.
12357 It is possible to explicitly extend one or more projects in the hierarchy
12358 in order to modify the sources. These extending projects must be imported by
12359 the "extending all" project, which will replace the corresponding virtual
12360 projects with the explicit ones.
12362 When building such a project hierarchy extension, the Project Manager will
12363 ensure that both modified sources and sources in virtual extending projects
12364 that depend on them, are recompiled.
12366 By means of example, consider the following hierarchy of projects.
12370 project A, containing package P1
12372 project B importing A and containing package P2 which depends on P1
12374 project C importing B and containing package P3 which depends on P2
12378 We want to modify packages P1 and P3.
12380 This project hierarchy will need to be extended as follows:
12384 Create project A1 that extends A, placing modified P1 there:
12386 @smallexample @c 0projectfile
12387 project A1 extends "(...)/A" is
12392 Create project C1 that "extends all" C and imports A1, placing modified
12395 @smallexample @c 0projectfile
12397 project C1 extends all "(...)/C" is
12402 When you build project C1, your entire modified project space will be
12403 recompiled, including the virtual project B1 that has been impacted by the
12404 "extending all" inheritance of project C.
12406 Note that if a Library Project in the hierarchy is virtually extended,
12407 the virtual project that extends the Library Project is not a Library Project.
12409 @c ****************************************
12410 @c * External References in Project Files *
12411 @c ****************************************
12413 @node External References in Project Files
12414 @section External References in Project Files
12417 A project file may contain references to external variables; such references
12418 are called @emph{external references}.
12420 An external variable is either defined as part of the environment (an
12421 environment variable in Unix, for example) or else specified on the command
12422 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
12423 If both, then the command line value is used.
12425 The value of an external reference is obtained by means of the built-in
12426 function @code{external}, which returns a string value.
12427 This function has two forms:
12429 @item @code{external (external_variable_name)}
12430 @item @code{external (external_variable_name, default_value)}
12434 Each parameter must be a string literal. For example:
12436 @smallexample @c projectfile
12438 external ("OS", "GNU/Linux")
12442 In the form with one parameter, the function returns the value of
12443 the external variable given as parameter. If this name is not present in the
12444 environment, the function returns an empty string.
12446 In the form with two string parameters, the second argument is
12447 the value returned when the variable given as the first argument is not
12448 present in the environment. In the example above, if @code{"OS"} is not
12449 the name of ^an environment variable^a logical name^ and is not passed on
12450 the command line, then the returned value is @code{"GNU/Linux"}.
12452 An external reference may be part of a string expression or of a string
12453 list expression, and can therefore appear in a variable declaration or
12454 an attribute declaration.
12456 @smallexample @c projectfile
12458 type Mode_Type is ("Debug", "Release");
12459 Mode : Mode_Type := external ("MODE");
12466 @c *****************************
12467 @c * Packages in Project Files *
12468 @c *****************************
12470 @node Packages in Project Files
12471 @section Packages in Project Files
12474 A @emph{package} defines the settings for project-aware tools within a
12476 For each such tool one can declare a package; the names for these
12477 packages are preset (@pxref{Packages}).
12478 A package may contain variable declarations, attribute declarations, and case
12481 @smallexample @c projectfile
12484 package Builder is -- used by gnatmake
12485 for ^Default_Switches^Default_Switches^ ("Ada")
12494 The syntax of package declarations mimics that of package in Ada.
12496 Most of the packages have an attribute
12497 @code{^Default_Switches^Default_Switches^}.
12498 This attribute is an associative array, and its value is a string list.
12499 The index of the associative array is the name of a programming language (case
12500 insensitive). This attribute indicates the ^switch^switch^
12501 or ^switches^switches^ to be used
12502 with the corresponding tool.
12504 Some packages also have another attribute, @code{^Switches^Switches^},
12505 an associative array whose value is a string list.
12506 The index is the name of a source file.
12507 This attribute indicates the ^switch^switch^
12508 or ^switches^switches^ to be used by the corresponding
12509 tool when dealing with this specific file.
12511 Further information on these ^switch^switch^-related attributes is found in
12512 @ref{^Switches^Switches^ and Project Files}.
12514 A package may be declared as a @emph{renaming} of another package; e.g., from
12515 the project file for an imported project.
12517 @smallexample @c projectfile
12519 with "/global/apex.gpr";
12521 package Naming renames Apex.Naming;
12528 Packages that are renamed in other project files often come from project files
12529 that have no sources: they are just used as templates. Any modification in the
12530 template will be reflected automatically in all the project files that rename
12531 a package from the template.
12533 In addition to the tool-oriented packages, you can also declare a package
12534 named @code{Naming} to establish specialized source file naming conventions
12535 (@pxref{Naming Schemes}).
12537 @c ************************************
12538 @c * Variables from Imported Projects *
12539 @c ************************************
12541 @node Variables from Imported Projects
12542 @section Variables from Imported Projects
12545 An attribute or variable defined in an imported or parent project can
12546 be used in expressions in the importing / extending project.
12547 Such an attribute or variable is denoted by an expanded name whose prefix
12548 is either the name of the project or the expanded name of a package within
12551 @smallexample @c projectfile
12554 project Main extends "base" is
12555 Var1 := Imported.Var;
12556 Var2 := Base.Var & ".new";
12561 for ^Default_Switches^Default_Switches^ ("Ada")
12562 use Imported.Builder.Ada_^Switches^Switches^ &
12563 "^-gnatg^-gnatg^" &
12569 package Compiler is
12570 for ^Default_Switches^Default_Switches^ ("Ada")
12571 use Base.Compiler.Ada_^Switches^Switches^;
12582 The value of @code{Var1} is a copy of the variable @code{Var} defined
12583 in the project file @file{"imported.gpr"}
12585 the value of @code{Var2} is a copy of the value of variable @code{Var}
12586 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
12588 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12589 @code{Builder} is a string list that includes in its value a copy of the value
12590 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
12591 in project file @file{imported.gpr} plus two new elements:
12592 @option{"^-gnatg^-gnatg^"}
12593 and @option{"^-v^-v^"};
12595 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12596 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
12597 defined in the @code{Compiler} package in project file @file{base.gpr},
12598 the project being extended.
12601 @c ******************
12602 @c * Naming Schemes *
12603 @c ******************
12605 @node Naming Schemes
12606 @section Naming Schemes
12609 Sometimes an Ada software system is ported from a foreign compilation
12610 environment to GNAT, and the file names do not use the default GNAT
12611 conventions. Instead of changing all the file names (which for a variety
12612 of reasons might not be possible), you can define the relevant file
12613 naming scheme in the @code{Naming} package in your project file.
12616 Note that the use of pragmas described in
12617 @ref{Alternative File Naming Schemes} by mean of a configuration
12618 pragmas file is not supported when using project files. You must use
12619 the features described in this paragraph. You can however use specify
12620 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
12623 For example, the following
12624 package models the Apex file naming rules:
12626 @smallexample @c projectfile
12629 for Casing use "lowercase";
12630 for Dot_Replacement use ".";
12631 for Spec_Suffix ("Ada") use ".1.ada";
12632 for Body_Suffix ("Ada") use ".2.ada";
12639 For example, the following package models the HP Ada file naming rules:
12641 @smallexample @c projectfile
12644 for Casing use "lowercase";
12645 for Dot_Replacement use "__";
12646 for Spec_Suffix ("Ada") use "_.^ada^ada^";
12647 for Body_Suffix ("Ada") use ".^ada^ada^";
12653 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
12654 names in lower case)
12658 You can define the following attributes in package @code{Naming}:
12663 This must be a string with one of the three values @code{"lowercase"},
12664 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
12667 If @var{Casing} is not specified, then the default is @code{"lowercase"}.
12669 @item @var{Dot_Replacement}
12670 This must be a string whose value satisfies the following conditions:
12673 @item It must not be empty
12674 @item It cannot start or end with an alphanumeric character
12675 @item It cannot be a single underscore
12676 @item It cannot start with an underscore followed by an alphanumeric
12677 @item It cannot contain a dot @code{'.'} except if the entire string
12682 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
12684 @item @var{Spec_Suffix}
12685 This is an associative array (indexed by the programming language name, case
12686 insensitive) whose value is a string that must satisfy the following
12690 @item It must not be empty
12691 @item It must include at least one dot
12694 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
12695 @code{"^.ads^.ADS^"}.
12697 @item @var{Body_Suffix}
12698 This is an associative array (indexed by the programming language name, case
12699 insensitive) whose value is a string that must satisfy the following
12703 @item It must not be empty
12704 @item It must include at least one dot
12705 @item It cannot end with the same string as @code{Spec_Suffix ("Ada")}
12708 If @code{Body_Suffix ("Ada")} is not specified, then the default is
12709 @code{"^.adb^.ADB^"}.
12711 @item @var{Separate_Suffix}
12712 This must be a string whose value satisfies the same conditions as
12713 @code{Body_Suffix}.
12716 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
12717 value as @code{Body_Suffix ("Ada")}.
12721 You can use the associative array attribute @code{Spec} to define
12722 the source file name for an individual Ada compilation unit's spec. The array
12723 index must be a string literal that identifies the Ada unit (case insensitive).
12724 The value of this attribute must be a string that identifies the file that
12725 contains this unit's spec (case sensitive or insensitive depending on the
12728 @smallexample @c projectfile
12729 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
12734 You can use the associative array attribute @code{Body} to
12735 define the source file name for an individual Ada compilation unit's body
12736 (possibly a subunit). The array index must be a string literal that identifies
12737 the Ada unit (case insensitive). The value of this attribute must be a string
12738 that identifies the file that contains this unit's body or subunit (case
12739 sensitive or insensitive depending on the operating system).
12741 @smallexample @c projectfile
12742 for Body ("MyPack.MyChild") use "mypack.mychild.body";
12746 @c ********************
12747 @c * Library Projects *
12748 @c ********************
12750 @node Library Projects
12751 @section Library Projects
12754 @emph{Library projects} are projects whose object code is placed in a library.
12755 (Note that this facility is not yet supported on all platforms)
12757 To create a library project, you need to define in its project file
12758 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
12759 Additionally, you may define other library-related attributes such as
12760 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
12761 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
12763 The @code{Library_Name} attribute has a string value. There is no restriction
12764 on the name of a library. It is the responsibility of the developer to
12765 choose a name that will be accepted by the platform. It is recommended to
12766 choose names that could be Ada identifiers; such names are almost guaranteed
12767 to be acceptable on all platforms.
12769 The @code{Library_Dir} attribute has a string value that designates the path
12770 (absolute or relative) of the directory where the library will reside.
12771 It must designate an existing directory, and this directory must be writable,
12772 different from the project's object directory and from any source directory
12773 in the project tree.
12775 If both @code{Library_Name} and @code{Library_Dir} are specified and
12776 are legal, then the project file defines a library project. The optional
12777 library-related attributes are checked only for such project files.
12779 The @code{Library_Kind} attribute has a string value that must be one of the
12780 following (case insensitive): @code{"static"}, @code{"dynamic"} or
12781 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
12782 attribute is not specified, the library is a static library, that is
12783 an archive of object files that can be potentially linked into a
12784 static executable. Otherwise, the library may be dynamic or
12785 relocatable, that is a library that is loaded only at the start of execution.
12787 If you need to build both a static and a dynamic library, you should use two
12788 different object directories, since in some cases some extra code needs to
12789 be generated for the latter. For such cases, it is recommended to either use
12790 two different project files, or a single one which uses external variables
12791 to indicate what kind of library should be build.
12793 The @code{Library_ALI_Dir} attribute may be specified to indicate the
12794 directory where the ALI files of the library will be copied. When it is
12795 not specified, the ALI files are copied ti the directory specified in
12796 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
12797 must be writable and different from the project's object directory and from
12798 any source directory in the project tree.
12800 The @code{Library_Version} attribute has a string value whose interpretation
12801 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
12802 used only for dynamic/relocatable libraries as the internal name of the
12803 library (the @code{"soname"}). If the library file name (built from the
12804 @code{Library_Name}) is different from the @code{Library_Version}, then the
12805 library file will be a symbolic link to the actual file whose name will be
12806 @code{Library_Version}.
12810 @smallexample @c projectfile
12816 for Library_Dir use "lib_dir";
12817 for Library_Name use "dummy";
12818 for Library_Kind use "relocatable";
12819 for Library_Version use "libdummy.so." & Version;
12826 Directory @file{lib_dir} will contain the internal library file whose name
12827 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
12828 @file{libdummy.so.1}.
12830 When @command{gnatmake} detects that a project file
12831 is a library project file, it will check all immediate sources of the project
12832 and rebuild the library if any of the sources have been recompiled.
12834 Standard project files can import library project files. In such cases,
12835 the libraries will only be rebuilt if some of its sources are recompiled
12836 because they are in the closure of some other source in an importing project.
12837 Sources of the library project files that are not in such a closure will
12838 not be checked, unless the full library is checked, because one of its sources
12839 needs to be recompiled.
12841 For instance, assume the project file @code{A} imports the library project file
12842 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
12843 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
12844 @file{l2.ads}, @file{l2.adb}.
12846 If @file{l1.adb} has been modified, then the library associated with @code{L}
12847 will be rebuilt when compiling all the immediate sources of @code{A} only
12848 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
12851 To be sure that all the sources in the library associated with @code{L} are
12852 up to date, and that all the sources of project @code{A} are also up to date,
12853 the following two commands needs to be used:
12860 When a library is built or rebuilt, an attempt is made first to delete all
12861 files in the library directory.
12862 All @file{ALI} files will also be copied from the object directory to the
12863 library directory. To build executables, @command{gnatmake} will use the
12864 library rather than the individual object files.
12867 It is also possible to create library project files for third-party libraries
12868 that are precompiled and cannot be compiled locally thanks to the
12869 @code{externally_built} attribute. (See @ref{Installing a library}).
12872 @c *******************************
12873 @c * Stand-alone Library Projects *
12874 @c *******************************
12876 @node Stand-alone Library Projects
12877 @section Stand-alone Library Projects
12880 A Stand-alone Library is a library that contains the necessary code to
12881 elaborate the Ada units that are included in the library. A Stand-alone
12882 Library is suitable to be used in an executable when the main is not
12883 in Ada. However, Stand-alone Libraries may also be used with an Ada main
12886 A Stand-alone Library Project is a Library Project where the library is
12887 a Stand-alone Library.
12889 To be a Stand-alone Library Project, in addition to the two attributes
12890 that make a project a Library Project (@code{Library_Name} and
12891 @code{Library_Dir}, see @ref{Library Projects}), the attribute
12892 @code{Library_Interface} must be defined.
12894 @smallexample @c projectfile
12896 for Library_Dir use "lib_dir";
12897 for Library_Name use "dummy";
12898 for Library_Interface use ("int1", "int1.child");
12902 Attribute @code{Library_Interface} has a non empty string list value,
12903 each string in the list designating a unit contained in an immediate source
12904 of the project file.
12906 When a Stand-alone Library is built, first the binder is invoked to build
12907 a package whose name depends on the library name
12908 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
12909 This binder-generated package includes initialization and
12910 finalization procedures whose
12911 names depend on the library name (dummyinit and dummyfinal in the example
12912 above). The object corresponding to this package is included in the library.
12914 A dynamic or relocatable Stand-alone Library is automatically initialized
12915 if automatic initialization of Stand-alone Libraries is supported on the
12916 platform and if attribute @code{Library_Auto_Init} is not specified or
12917 is specified with the value "true". A static Stand-alone Library is never
12918 automatically initialized.
12920 Single string attribute @code{Library_Auto_Init} may be specified with only
12921 two possible values: "false" or "true" (case-insensitive). Specifying
12922 "false" for attribute @code{Library_Auto_Init} will prevent automatic
12923 initialization of dynamic or relocatable libraries.
12925 When a non automatically initialized Stand-alone Library is used
12926 in an executable, its initialization procedure must be called before
12927 any service of the library is used.
12928 When the main subprogram is in Ada, it may mean that the initialization
12929 procedure has to be called during elaboration of another package.
12931 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
12932 (those that are listed in attribute @code{Library_Interface}) are copied to
12933 the Library Directory. As a consequence, only the Interface Units may be
12934 imported from Ada units outside of the library. If other units are imported,
12935 the binding phase will fail.
12937 When a Stand-Alone Library is bound, the switches that are specified in
12938 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
12939 used in the call to @command{gnatbind}.
12941 The string list attribute @code{Library_Options} may be used to specified
12942 additional switches to the call to @command{gcc} to link the library.
12944 The attribute @code{Library_Src_Dir}, may be specified for a
12945 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
12946 single string value. Its value must be the path (absolute or relative to the
12947 project directory) of an existing directory. This directory cannot be the
12948 object directory or one of the source directories, but it can be the same as
12949 the library directory. The sources of the Interface
12950 Units of the library, necessary to an Ada client of the library, will be
12951 copied to the designated directory, called Interface Copy directory.
12952 These sources includes the specs of the Interface Units, but they may also
12953 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
12954 are used, or when there is a generic units in the spec. Before the sources
12955 are copied to the Interface Copy directory, an attempt is made to delete all
12956 files in the Interface Copy directory.
12958 @c *************************************
12959 @c * Switches Related to Project Files *
12960 @c *************************************
12961 @node Switches Related to Project Files
12962 @section Switches Related to Project Files
12965 The following switches are used by GNAT tools that support project files:
12969 @item ^-P^/PROJECT_FILE=^@var{project}
12970 @cindex @option{^-P^/PROJECT_FILE^} (any tool supporting project files)
12971 Indicates the name of a project file. This project file will be parsed with
12972 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
12973 if any, and using the external references indicated
12974 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
12976 There may zero, one or more spaces between @option{-P} and @var{project}.
12980 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
12983 Since the Project Manager parses the project file only after all the switches
12984 on the command line are checked, the order of the switches
12985 @option{^-P^/PROJECT_FILE^},
12986 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
12987 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
12989 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
12990 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any tool supporting project files)
12991 Indicates that external variable @var{name} has the value @var{value}.
12992 The Project Manager will use this value for occurrences of
12993 @code{external(name)} when parsing the project file.
12997 If @var{name} or @var{value} includes a space, then @var{name=value} should be
12998 put between quotes.
13006 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
13007 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
13008 @var{name}, only the last one is used.
13011 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
13012 takes precedence over the value of the same name in the environment.
13014 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
13015 @cindex @code{^-vP^/MESSAGES_PROJECT_FILE^} (any tool supporting project files)
13016 @c Previous line uses code vs option command, to stay less than 80 chars
13017 Indicates the verbosity of the parsing of GNAT project files.
13020 @option{-vP0} means Default;
13021 @option{-vP1} means Medium;
13022 @option{-vP2} means High.
13026 There are three possible options for this qualifier: DEFAULT, MEDIUM and
13031 The default is ^Default^DEFAULT^: no output for syntactically correct
13034 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
13035 only the last one is used.
13039 @c **********************************
13040 @c * Tools Supporting Project Files *
13041 @c **********************************
13043 @node Tools Supporting Project Files
13044 @section Tools Supporting Project Files
13047 * gnatmake and Project Files::
13048 * The GNAT Driver and Project Files::
13050 * Glide and Project Files::
13054 @node gnatmake and Project Files
13055 @subsection gnatmake and Project Files
13058 This section covers several topics related to @command{gnatmake} and
13059 project files: defining ^switches^switches^ for @command{gnatmake}
13060 and for the tools that it invokes; specifying configuration pragmas;
13061 the use of the @code{Main} attribute; building and rebuilding library project
13065 * ^Switches^Switches^ and Project Files::
13066 * Specifying Configuration Pragmas::
13067 * Project Files and Main Subprograms::
13068 * Library Project Files::
13071 @node ^Switches^Switches^ and Project Files
13072 @subsubsection ^Switches^Switches^ and Project Files
13075 It is not currently possible to specify VMS style qualifiers in the project
13076 files; only Unix style ^switches^switches^ may be specified.
13080 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
13081 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
13082 attribute, a @code{^Switches^Switches^} attribute, or both;
13083 as their names imply, these ^switch^switch^-related
13084 attributes affect the ^switches^switches^ that are used for each of these GNAT
13086 @command{gnatmake} is invoked. As will be explained below, these
13087 component-specific ^switches^switches^ precede
13088 the ^switches^switches^ provided on the @command{gnatmake} command line.
13090 The @code{^Default_Switches^Default_Switches^} attribute is an associative
13091 array indexed by language name (case insensitive) whose value is a string list.
13094 @smallexample @c projectfile
13096 package Compiler is
13097 for ^Default_Switches^Default_Switches^ ("Ada")
13098 use ("^-gnaty^-gnaty^",
13105 The @code{^Switches^Switches^} attribute is also an associative array,
13106 indexed by a file name (which may or may not be case sensitive, depending
13107 on the operating system) whose value is a string list. For example:
13109 @smallexample @c projectfile
13112 for ^Switches^Switches^ ("main1.adb")
13114 for ^Switches^Switches^ ("main2.adb")
13121 For the @code{Builder} package, the file names must designate source files
13122 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
13123 file names must designate @file{ALI} or source files for main subprograms.
13124 In each case just the file name without an explicit extension is acceptable.
13126 For each tool used in a program build (@command{gnatmake}, the compiler, the
13127 binder, and the linker), the corresponding package @dfn{contributes} a set of
13128 ^switches^switches^ for each file on which the tool is invoked, based on the
13129 ^switch^switch^-related attributes defined in the package.
13130 In particular, the ^switches^switches^
13131 that each of these packages contributes for a given file @var{f} comprise:
13135 the value of attribute @code{^Switches^Switches^ (@var{f})},
13136 if it is specified in the package for the given file,
13138 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
13139 if it is specified in the package.
13143 If neither of these attributes is defined in the package, then the package does
13144 not contribute any ^switches^switches^ for the given file.
13146 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
13147 two sets, in the following order: those contributed for the file
13148 by the @code{Builder} package;
13149 and the switches passed on the command line.
13151 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
13152 the ^switches^switches^ passed to the tool comprise three sets,
13153 in the following order:
13157 the applicable ^switches^switches^ contributed for the file
13158 by the @code{Builder} package in the project file supplied on the command line;
13161 those contributed for the file by the package (in the relevant project file --
13162 see below) corresponding to the tool; and
13165 the applicable switches passed on the command line.
13169 The term @emph{applicable ^switches^switches^} reflects the fact that
13170 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
13171 tools, depending on the individual ^switch^switch^.
13173 @command{gnatmake} may invoke the compiler on source files from different
13174 projects. The Project Manager will use the appropriate project file to
13175 determine the @code{Compiler} package for each source file being compiled.
13176 Likewise for the @code{Binder} and @code{Linker} packages.
13178 As an example, consider the following package in a project file:
13180 @smallexample @c projectfile
13183 package Compiler is
13184 for ^Default_Switches^Default_Switches^ ("Ada")
13186 for ^Switches^Switches^ ("a.adb")
13188 for ^Switches^Switches^ ("b.adb")
13190 "^-gnaty^-gnaty^");
13197 If @command{gnatmake} is invoked with this project file, and it needs to
13198 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
13199 @file{a.adb} will be compiled with the ^switch^switch^
13200 @option{^-O1^-O1^},
13201 @file{b.adb} with ^switches^switches^
13203 and @option{^-gnaty^-gnaty^},
13204 and @file{c.adb} with @option{^-g^-g^}.
13206 The following example illustrates the ordering of the ^switches^switches^
13207 contributed by different packages:
13209 @smallexample @c projectfile
13213 for ^Switches^Switches^ ("main.adb")
13221 package Compiler is
13222 for ^Switches^Switches^ ("main.adb")
13230 If you issue the command:
13233 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
13237 then the compiler will be invoked on @file{main.adb} with the following
13238 sequence of ^switches^switches^
13241 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
13244 with the last @option{^-O^-O^}
13245 ^switch^switch^ having precedence over the earlier ones;
13246 several other ^switches^switches^
13247 (such as @option{^-c^-c^}) are added implicitly.
13249 The ^switches^switches^
13251 and @option{^-O1^-O1^} are contributed by package
13252 @code{Builder}, @option{^-O2^-O2^} is contributed
13253 by the package @code{Compiler}
13254 and @option{^-O0^-O0^} comes from the command line.
13256 The @option{^-g^-g^}
13257 ^switch^switch^ will also be passed in the invocation of
13258 @command{Gnatlink.}
13260 A final example illustrates switch contributions from packages in different
13263 @smallexample @c projectfile
13266 for Source_Files use ("pack.ads", "pack.adb");
13267 package Compiler is
13268 for ^Default_Switches^Default_Switches^ ("Ada")
13269 use ("^-gnata^-gnata^");
13277 for Source_Files use ("foo_main.adb", "bar_main.adb");
13279 for ^Switches^Switches^ ("foo_main.adb")
13287 -- Ada source file:
13289 procedure Foo_Main is
13297 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
13301 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
13302 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
13303 @option{^-gnato^-gnato^} (passed on the command line).
13304 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
13305 are @option{^-g^-g^} from @code{Proj4.Builder},
13306 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
13307 and @option{^-gnato^-gnato^} from the command line.
13310 When using @command{gnatmake} with project files, some ^switches^switches^ or
13311 arguments may be expressed as relative paths. As the working directory where
13312 compilation occurs may change, these relative paths are converted to absolute
13313 paths. For the ^switches^switches^ found in a project file, the relative paths
13314 are relative to the project file directory, for the switches on the command
13315 line, they are relative to the directory where @command{gnatmake} is invoked.
13316 The ^switches^switches^ for which this occurs are:
13322 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
13324 ^-o^-o^, object files specified in package @code{Linker} or after
13325 -largs on the command line). The exception to this rule is the ^switch^switch^
13326 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
13328 @node Specifying Configuration Pragmas
13329 @subsubsection Specifying Configuration Pragmas
13331 When using @command{gnatmake} with project files, if there exists a file
13332 @file{gnat.adc} that contains configuration pragmas, this file will be
13335 Configuration pragmas can be defined by means of the following attributes in
13336 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
13337 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
13339 Both these attributes are single string attributes. Their values is the path
13340 name of a file containing configuration pragmas. If a path name is relative,
13341 then it is relative to the project directory of the project file where the
13342 attribute is defined.
13344 When compiling a source, the configuration pragmas used are, in order,
13345 those listed in the file designated by attribute
13346 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
13347 project file, if it is specified, and those listed in the file designated by
13348 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
13349 the project file of the source, if it exists.
13351 @node Project Files and Main Subprograms
13352 @subsubsection Project Files and Main Subprograms
13355 When using a project file, you can invoke @command{gnatmake}
13356 with one or several main subprograms, by specifying their source files on the
13360 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
13364 Each of these needs to be a source file of the same project, except
13365 when the switch ^-u^/UNIQUE^ is used.
13368 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
13369 same project, one of the project in the tree rooted at the project specified
13370 on the command line. The package @code{Builder} of this common project, the
13371 "main project" is the one that is considered by @command{gnatmake}.
13374 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
13375 imported directly or indirectly by the project specified on the command line.
13376 Note that if such a source file is not part of the project specified on the
13377 command line, the ^switches^switches^ found in package @code{Builder} of the
13378 project specified on the command line, if any, that are transmitted
13379 to the compiler will still be used, not those found in the project file of
13383 When using a project file, you can also invoke @command{gnatmake} without
13384 explicitly specifying any main, and the effect depends on whether you have
13385 defined the @code{Main} attribute. This attribute has a string list value,
13386 where each element in the list is the name of a source file (the file
13387 extension is optional) that contains a unit that can be a main subprogram.
13389 If the @code{Main} attribute is defined in a project file as a non-empty
13390 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
13391 line, then invoking @command{gnatmake} with this project file but without any
13392 main on the command line is equivalent to invoking @command{gnatmake} with all
13393 the file names in the @code{Main} attribute on the command line.
13396 @smallexample @c projectfile
13399 for Main use ("main1", "main2", "main3");
13405 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
13407 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
13409 When the project attribute @code{Main} is not specified, or is specified
13410 as an empty string list, or when the switch @option{-u} is used on the command
13411 line, then invoking @command{gnatmake} with no main on the command line will
13412 result in all immediate sources of the project file being checked, and
13413 potentially recompiled. Depending on the presence of the switch @option{-u},
13414 sources from other project files on which the immediate sources of the main
13415 project file depend are also checked and potentially recompiled. In other
13416 words, the @option{-u} switch is applied to all of the immediate sources of the
13419 When no main is specified on the command line and attribute @code{Main} exists
13420 and includes several mains, or when several mains are specified on the
13421 command line, the default ^switches^switches^ in package @code{Builder} will
13422 be used for all mains, even if there are specific ^switches^switches^
13423 specified for one or several mains.
13425 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
13426 the specific ^switches^switches^ for each main, if they are specified.
13428 @node Library Project Files
13429 @subsubsection Library Project Files
13432 When @command{gnatmake} is invoked with a main project file that is a library
13433 project file, it is not allowed to specify one or more mains on the command
13437 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
13438 ^-l^/ACTION=LINK^ have special meanings.
13441 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
13442 to @command{gnatmake} that @command{gnatbind} should be invoked for the
13445 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
13446 to @command{gnatmake} that the binder generated file should be compiled
13447 (in the case of a stand-alone library) and that the library should be built.
13451 @node The GNAT Driver and Project Files
13452 @subsection The GNAT Driver and Project Files
13455 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
13457 @command{^gnatbind^gnatbind^},
13458 @command{^gnatfind^gnatfind^},
13459 @command{^gnatlink^gnatlink^},
13460 @command{^gnatls^gnatls^},
13461 @command{^gnatelim^gnatelim^},
13462 @command{^gnatpp^gnatpp^},
13463 @command{^gnatmetric^gnatmetric^},
13464 @command{^gnatstub^gnatstub^},
13465 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
13466 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
13467 They must be invoked through the @command{gnat} driver.
13469 The @command{gnat} driver is a front-end that accepts a number of commands and
13470 call the corresponding tool. It has been designed initially for VMS to convert
13471 VMS style qualifiers to Unix style switches, but it is now available to all
13472 the GNAT supported platforms.
13474 On non VMS platforms, the @command{gnat} driver accepts the following commands
13475 (case insensitive):
13479 BIND to invoke @command{^gnatbind^gnatbind^}
13481 CHOP to invoke @command{^gnatchop^gnatchop^}
13483 CLEAN to invoke @command{^gnatclean^gnatclean^}
13485 COMP or COMPILE to invoke the compiler
13487 ELIM to invoke @command{^gnatelim^gnatelim^}
13489 FIND to invoke @command{^gnatfind^gnatfind^}
13491 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
13493 LINK to invoke @command{^gnatlink^gnatlink^}
13495 LS or LIST to invoke @command{^gnatls^gnatls^}
13497 MAKE to invoke @command{^gnatmake^gnatmake^}
13499 NAME to invoke @command{^gnatname^gnatname^}
13501 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
13503 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
13505 METRIC to invoke @command{^gnatmetric^gnatmetric^}
13507 STUB to invoke @command{^gnatstub^gnatstub^}
13509 XREF to invoke @command{^gnatxref^gnatxref^}
13513 (note that the compiler is invoked using the command
13514 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
13517 On non VMS platforms, between @command{gnat} and the command, two
13518 special switches may be used:
13522 @command{-v} to display the invocation of the tool.
13524 @command{-dn} to prevent the @command{gnat} driver from removing
13525 the temporary files it has created. These temporary files are
13526 configuration files and temporary file list files.
13530 The command may be followed by switches and arguments for the invoked
13534 gnat bind -C main.ali
13540 Switches may also be put in text files, one switch per line, and the text
13541 files may be specified with their path name preceded by '@@'.
13544 gnat bind @@args.txt main.ali
13548 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
13549 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
13550 (@option{^-P^/PROJECT_FILE^},
13551 @option{^-X^/EXTERNAL_REFERENCE^} and
13552 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
13553 the switches of the invoking tool.
13556 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
13557 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
13558 the immediate sources of the specified project file.
13561 When GNAT METRIC is used with a project file, but with no source
13562 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
13563 with all the immediate sources of the specified project file and with
13564 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
13568 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
13569 a project file, no source is specified on the command line and
13570 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
13571 the underlying tool (^gnatpp^gnatpp^ or
13572 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
13573 not only for the immediate sources of the main project.
13575 (-U stands for Universal or Union of the project files of the project tree)
13579 For each of the following commands, there is optionally a corresponding
13580 package in the main project.
13584 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
13587 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
13590 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
13593 package @code{Eliminate} for command ELIM (invoking
13594 @code{^gnatelim^gnatelim^})
13597 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
13600 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
13603 package @code{Metrics} for command METRIC
13604 (invoking @code{^gnatmetric^gnatmetric^})
13607 package @code{Pretty_Printer} for command PP or PRETTY
13608 (invoking @code{^gnatpp^gnatpp^})
13611 package @code{Gnatstub} for command STUB
13612 (invoking @code{^gnatstub^gnatstub^})
13615 package @code{Cross_Reference} for command XREF (invoking
13616 @code{^gnatxref^gnatxref^})
13621 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
13622 a simple variable with a string list value. It contains ^switches^switches^
13623 for the invocation of @code{^gnatls^gnatls^}.
13625 @smallexample @c projectfile
13629 for ^Switches^Switches^
13638 All other packages have two attribute @code{^Switches^Switches^} and
13639 @code{^Default_Switches^Default_Switches^}.
13642 @code{^Switches^Switches^} is an associated array attribute, indexed by the
13643 source file name, that has a string list value: the ^switches^switches^ to be
13644 used when the tool corresponding to the package is invoked for the specific
13648 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
13649 indexed by the programming language that has a string list value.
13650 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
13651 ^switches^switches^ for the invocation of the tool corresponding
13652 to the package, except if a specific @code{^Switches^Switches^} attribute
13653 is specified for the source file.
13655 @smallexample @c projectfile
13659 for Source_Dirs use ("./**");
13662 for ^Switches^Switches^ use
13669 package Compiler is
13670 for ^Default_Switches^Default_Switches^ ("Ada")
13671 use ("^-gnatv^-gnatv^",
13672 "^-gnatwa^-gnatwa^");
13678 for ^Default_Switches^Default_Switches^ ("Ada")
13686 for ^Default_Switches^Default_Switches^ ("Ada")
13688 for ^Switches^Switches^ ("main.adb")
13697 for ^Default_Switches^Default_Switches^ ("Ada")
13704 package Cross_Reference is
13705 for ^Default_Switches^Default_Switches^ ("Ada")
13710 end Cross_Reference;
13716 With the above project file, commands such as
13719 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
13720 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
13721 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
13722 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
13723 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
13727 will set up the environment properly and invoke the tool with the switches
13728 found in the package corresponding to the tool:
13729 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
13730 except @code{^Switches^Switches^ ("main.adb")}
13731 for @code{^gnatlink^gnatlink^}.
13734 @node Glide and Project Files
13735 @subsection Glide and Project Files
13738 Glide will automatically recognize the @file{.gpr} extension for
13739 project files, and will
13740 convert them to its own internal format automatically. However, it
13741 doesn't provide a syntax-oriented editor for modifying these
13743 The project file will be loaded as text when you select the menu item
13744 @code{Ada} @result{} @code{Project} @result{} @code{Edit}.
13745 You can edit this text and save the @file{gpr} file;
13746 when you next select this project file in Glide it
13747 will be automatically reloaded.
13750 @c **********************
13751 @node An Extended Example
13752 @section An Extended Example
13755 Suppose that we have two programs, @var{prog1} and @var{prog2},
13756 whose sources are in corresponding directories. We would like
13757 to build them with a single @command{gnatmake} command, and we want to place
13758 their object files into @file{build} subdirectories of the source directories.
13759 Furthermore, we want to have to have two separate subdirectories
13760 in @file{build} -- @file{release} and @file{debug} -- which will contain
13761 the object files compiled with different set of compilation flags.
13763 In other words, we have the following structure:
13780 Here are the project files that we must place in a directory @file{main}
13781 to maintain this structure:
13785 @item We create a @code{Common} project with a package @code{Compiler} that
13786 specifies the compilation ^switches^switches^:
13791 @b{project} Common @b{is}
13793 @b{for} Source_Dirs @b{use} (); -- No source files
13797 @b{type} Build_Type @b{is} ("release", "debug");
13798 Build : Build_Type := External ("BUILD", "debug");
13801 @b{package} Compiler @b{is}
13802 @b{case} Build @b{is}
13803 @b{when} "release" =>
13804 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13805 @b{use} ("^-O2^-O2^");
13806 @b{when} "debug" =>
13807 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13808 @b{use} ("^-g^-g^");
13816 @item We create separate projects for the two programs:
13823 @b{project} Prog1 @b{is}
13825 @b{for} Source_Dirs @b{use} ("prog1");
13826 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
13828 @b{package} Compiler @b{renames} Common.Compiler;
13839 @b{project} Prog2 @b{is}
13841 @b{for} Source_Dirs @b{use} ("prog2");
13842 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
13844 @b{package} Compiler @b{renames} Common.Compiler;
13850 @item We create a wrapping project @code{Main}:
13859 @b{project} Main @b{is}
13861 @b{package} Compiler @b{renames} Common.Compiler;
13867 @item Finally we need to create a dummy procedure that @code{with}s (either
13868 explicitly or implicitly) all the sources of our two programs.
13873 Now we can build the programs using the command
13876 gnatmake ^-P^/PROJECT_FILE=^main dummy
13880 for the Debug mode, or
13884 gnatmake -Pmain -XBUILD=release
13890 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
13895 for the Release mode.
13897 @c ********************************
13898 @c * Project File Complete Syntax *
13899 @c ********************************
13901 @node Project File Complete Syntax
13902 @section Project File Complete Syntax
13906 context_clause project_declaration
13912 @b{with} path_name @{ , path_name @} ;
13917 project_declaration ::=
13918 simple_project_declaration | project_extension
13920 simple_project_declaration ::=
13921 @b{project} <project_>simple_name @b{is}
13922 @{declarative_item@}
13923 @b{end} <project_>simple_name;
13925 project_extension ::=
13926 @b{project} <project_>simple_name @b{extends} path_name @b{is}
13927 @{declarative_item@}
13928 @b{end} <project_>simple_name;
13930 declarative_item ::=
13931 package_declaration |
13932 typed_string_declaration |
13933 other_declarative_item
13935 package_declaration ::=
13936 package_specification | package_renaming
13938 package_specification ::=
13939 @b{package} package_identifier @b{is}
13940 @{simple_declarative_item@}
13941 @b{end} package_identifier ;
13943 package_identifier ::=
13944 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
13945 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
13946 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
13948 package_renaming ::==
13949 @b{package} package_identifier @b{renames}
13950 <project_>simple_name.package_identifier ;
13952 typed_string_declaration ::=
13953 @b{type} <typed_string_>_simple_name @b{is}
13954 ( string_literal @{, string_literal@} );
13956 other_declarative_item ::=
13957 attribute_declaration |
13958 typed_variable_declaration |
13959 variable_declaration |
13962 attribute_declaration ::=
13963 full_associative_array_declaration |
13964 @b{for} attribute_designator @b{use} expression ;
13966 full_associative_array_declaration ::=
13967 @b{for} <associative_array_attribute_>simple_name @b{use}
13968 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
13970 attribute_designator ::=
13971 <simple_attribute_>simple_name |
13972 <associative_array_attribute_>simple_name ( string_literal )
13974 typed_variable_declaration ::=
13975 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
13977 variable_declaration ::=
13978 <variable_>simple_name := expression;
13988 attribute_reference
13994 ( <string_>expression @{ , <string_>expression @} )
13997 @b{external} ( string_literal [, string_literal] )
13999 attribute_reference ::=
14000 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
14002 attribute_prefix ::=
14004 <project_>simple_name | package_identifier |
14005 <project_>simple_name . package_identifier
14007 case_construction ::=
14008 @b{case} <typed_variable_>name @b{is}
14013 @b{when} discrete_choice_list =>
14014 @{case_construction | attribute_declaration@}
14016 discrete_choice_list ::=
14017 string_literal @{| string_literal@} |
14021 simple_name @{. simple_name@}
14024 identifier (same as Ada)
14028 @node The Cross-Referencing Tools gnatxref and gnatfind
14029 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
14034 The compiler generates cross-referencing information (unless
14035 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
14036 This information indicates where in the source each entity is declared and
14037 referenced. Note that entities in package Standard are not included, but
14038 entities in all other predefined units are included in the output.
14040 Before using any of these two tools, you need to compile successfully your
14041 application, so that GNAT gets a chance to generate the cross-referencing
14044 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
14045 information to provide the user with the capability to easily locate the
14046 declaration and references to an entity. These tools are quite similar,
14047 the difference being that @code{gnatfind} is intended for locating
14048 definitions and/or references to a specified entity or entities, whereas
14049 @code{gnatxref} is oriented to generating a full report of all
14052 To use these tools, you must not compile your application using the
14053 @option{-gnatx} switch on the @command{gnatmake} command line
14054 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
14055 information will not be generated.
14058 * gnatxref Switches::
14059 * gnatfind Switches::
14060 * Project Files for gnatxref and gnatfind::
14061 * Regular Expressions in gnatfind and gnatxref::
14062 * Examples of gnatxref Usage::
14063 * Examples of gnatfind Usage::
14066 @node gnatxref Switches
14067 @section @code{gnatxref} Switches
14070 The command invocation for @code{gnatxref} is:
14072 $ gnatxref [switches] sourcefile1 [sourcefile2 ...]
14079 @item sourcefile1, sourcefile2
14080 identifies the source files for which a report is to be generated. The
14081 ``with''ed units will be processed too. You must provide at least one file.
14083 These file names are considered to be regular expressions, so for instance
14084 specifying @file{source*.adb} is the same as giving every file in the current
14085 directory whose name starts with @file{source} and whose extension is
14088 You shouldn't specify any directory name, just base names. @command{gnatxref}
14089 and @command{gnatfind} will be able to locate these files by themselves using
14090 the source path. If you specify directories, no result is produced.
14095 The switches can be :
14098 @item ^-a^/ALL_FILES^
14099 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
14100 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14101 the read-only files found in the library search path. Otherwise, these files
14102 will be ignored. This option can be used to protect Gnat sources or your own
14103 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
14104 much faster, and their output much smaller. Read-only here refers to access
14105 or permissions status in the file system for the current user.
14108 @cindex @option{-aIDIR} (@command{gnatxref})
14109 When looking for source files also look in directory DIR. The order in which
14110 source file search is undertaken is the same as for @command{gnatmake}.
14113 @cindex @option{-aODIR} (@command{gnatxref})
14114 When searching for library and object files, look in directory
14115 DIR. The order in which library files are searched is the same as for
14116 @command{gnatmake}.
14119 @cindex @option{-nostdinc} (@command{gnatxref})
14120 Do not look for sources in the system default directory.
14123 @cindex @option{-nostdlib} (@command{gnatxref})
14124 Do not look for library files in the system default directory.
14126 @item --RTS=@var{rts-path}
14127 @cindex @option{--RTS} (@command{gnatxref})
14128 Specifies the default location of the runtime library. Same meaning as the
14129 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
14131 @item ^-d^/DERIVED_TYPES^
14132 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
14133 If this switch is set @code{gnatxref} will output the parent type
14134 reference for each matching derived types.
14136 @item ^-f^/FULL_PATHNAME^
14137 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
14138 If this switch is set, the output file names will be preceded by their
14139 directory (if the file was found in the search path). If this switch is
14140 not set, the directory will not be printed.
14142 @item ^-g^/IGNORE_LOCALS^
14143 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
14144 If this switch is set, information is output only for library-level
14145 entities, ignoring local entities. The use of this switch may accelerate
14146 @code{gnatfind} and @code{gnatxref}.
14149 @cindex @option{-IDIR} (@command{gnatxref})
14150 Equivalent to @samp{-aODIR -aIDIR}.
14153 @cindex @option{-pFILE} (@command{gnatxref})
14154 Specify a project file to use @xref{Project Files}. These project files are
14155 the @file{.adp} files used by Glide. If you need to use the @file{.gpr}
14156 project files, you should use gnatxref through the GNAT driver
14157 (@command{gnat xref -Pproject}).
14159 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
14160 project file in the current directory.
14162 If a project file is either specified or found by the tools, then the content
14163 of the source directory and object directory lines are added as if they
14164 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
14165 and @samp{^-aO^OBJECT_SEARCH^}.
14167 Output only unused symbols. This may be really useful if you give your
14168 main compilation unit on the command line, as @code{gnatxref} will then
14169 display every unused entity and 'with'ed package.
14173 Instead of producing the default output, @code{gnatxref} will generate a
14174 @file{tags} file that can be used by vi. For examples how to use this
14175 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
14176 to the standard output, thus you will have to redirect it to a file.
14182 All these switches may be in any order on the command line, and may even
14183 appear after the file names. They need not be separated by spaces, thus
14184 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
14185 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
14187 @node gnatfind Switches
14188 @section @code{gnatfind} Switches
14191 The command line for @code{gnatfind} is:
14194 $ gnatfind [switches] pattern[:sourcefile[:line[:column]]]
14203 An entity will be output only if it matches the regular expression found
14204 in @samp{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
14206 Omitting the pattern is equivalent to specifying @samp{*}, which
14207 will match any entity. Note that if you do not provide a pattern, you
14208 have to provide both a sourcefile and a line.
14210 Entity names are given in Latin-1, with uppercase/lowercase equivalence
14211 for matching purposes. At the current time there is no support for
14212 8-bit codes other than Latin-1, or for wide characters in identifiers.
14215 @code{gnatfind} will look for references, bodies or declarations
14216 of symbols referenced in @file{sourcefile}, at line @samp{line}
14217 and column @samp{column}. See @ref{Examples of gnatfind Usage}
14218 for syntax examples.
14221 is a decimal integer identifying the line number containing
14222 the reference to the entity (or entities) to be located.
14225 is a decimal integer identifying the exact location on the
14226 line of the first character of the identifier for the
14227 entity reference. Columns are numbered from 1.
14229 @item file1 file2 ...
14230 The search will be restricted to these source files. If none are given, then
14231 the search will be done for every library file in the search path.
14232 These file must appear only after the pattern or sourcefile.
14234 These file names are considered to be regular expressions, so for instance
14235 specifying 'source*.adb' is the same as giving every file in the current
14236 directory whose name starts with 'source' and whose extension is 'adb'.
14238 The location of the spec of the entity will always be displayed, even if it
14239 isn't in one of file1, file2,... The occurrences of the entity in the
14240 separate units of the ones given on the command line will also be displayed.
14242 Note that if you specify at least one file in this part, @code{gnatfind} may
14243 sometimes not be able to find the body of the subprograms...
14248 At least one of 'sourcefile' or 'pattern' has to be present on
14251 The following switches are available:
14255 @item ^-a^/ALL_FILES^
14256 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
14257 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
14258 the read-only files found in the library search path. Otherwise, these files
14259 will be ignored. This option can be used to protect Gnat sources or your own
14260 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
14261 much faster, and their output much smaller. Read-only here refers to access
14262 or permission status in the file system for the current user.
14265 @cindex @option{-aIDIR} (@command{gnatfind})
14266 When looking for source files also look in directory DIR. The order in which
14267 source file search is undertaken is the same as for @command{gnatmake}.
14270 @cindex @option{-aODIR} (@command{gnatfind})
14271 When searching for library and object files, look in directory
14272 DIR. The order in which library files are searched is the same as for
14273 @command{gnatmake}.
14276 @cindex @option{-nostdinc} (@command{gnatfind})
14277 Do not look for sources in the system default directory.
14280 @cindex @option{-nostdlib} (@command{gnatfind})
14281 Do not look for library files in the system default directory.
14283 @item --RTS=@var{rts-path}
14284 @cindex @option{--RTS} (@command{gnatfind})
14285 Specifies the default location of the runtime library. Same meaning as the
14286 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
14288 @item ^-d^/DERIVED_TYPE_INFORMATION^
14289 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
14290 If this switch is set, then @code{gnatfind} will output the parent type
14291 reference for each matching derived types.
14293 @item ^-e^/EXPRESSIONS^
14294 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
14295 By default, @code{gnatfind} accept the simple regular expression set for
14296 @samp{pattern}. If this switch is set, then the pattern will be
14297 considered as full Unix-style regular expression.
14299 @item ^-f^/FULL_PATHNAME^
14300 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
14301 If this switch is set, the output file names will be preceded by their
14302 directory (if the file was found in the search path). If this switch is
14303 not set, the directory will not be printed.
14305 @item ^-g^/IGNORE_LOCALS^
14306 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
14307 If this switch is set, information is output only for library-level
14308 entities, ignoring local entities. The use of this switch may accelerate
14309 @code{gnatfind} and @code{gnatxref}.
14312 @cindex @option{-IDIR} (@command{gnatfind})
14313 Equivalent to @samp{-aODIR -aIDIR}.
14316 @cindex @option{-pFILE} (@command{gnatfind})
14317 Specify a project file (@pxref{Project Files}) to use.
14318 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
14319 project file in the current directory.
14321 If a project file is either specified or found by the tools, then the content
14322 of the source directory and object directory lines are added as if they
14323 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
14324 @samp{^-aO^/OBJECT_SEARCH^}.
14326 @item ^-r^/REFERENCES^
14327 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
14328 By default, @code{gnatfind} will output only the information about the
14329 declaration, body or type completion of the entities. If this switch is
14330 set, the @code{gnatfind} will locate every reference to the entities in
14331 the files specified on the command line (or in every file in the search
14332 path if no file is given on the command line).
14334 @item ^-s^/PRINT_LINES^
14335 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
14336 If this switch is set, then @code{gnatfind} will output the content
14337 of the Ada source file lines were the entity was found.
14339 @item ^-t^/TYPE_HIERARCHY^
14340 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
14341 If this switch is set, then @code{gnatfind} will output the type hierarchy for
14342 the specified type. It act like -d option but recursively from parent
14343 type to parent type. When this switch is set it is not possible to
14344 specify more than one file.
14349 All these switches may be in any order on the command line, and may even
14350 appear after the file names. They need not be separated by spaces, thus
14351 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
14352 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
14354 As stated previously, gnatfind will search in every directory in the
14355 search path. You can force it to look only in the current directory if
14356 you specify @code{*} at the end of the command line.
14358 @node Project Files for gnatxref and gnatfind
14359 @section Project Files for @command{gnatxref} and @command{gnatfind}
14362 Project files allow a programmer to specify how to compile its
14363 application, where to find sources, etc. These files are used
14365 primarily by the Glide Ada mode, but they can also be used
14368 @code{gnatxref} and @code{gnatfind}.
14370 A project file name must end with @file{.gpr}. If a single one is
14371 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
14372 extract the information from it. If multiple project files are found, none of
14373 them is read, and you have to use the @samp{-p} switch to specify the one
14376 The following lines can be included, even though most of them have default
14377 values which can be used in most cases.
14378 The lines can be entered in any order in the file.
14379 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
14380 each line. If you have multiple instances, only the last one is taken into
14385 [default: @code{"^./^[]^"}]
14386 specifies a directory where to look for source files. Multiple @code{src_dir}
14387 lines can be specified and they will be searched in the order they
14391 [default: @code{"^./^[]^"}]
14392 specifies a directory where to look for object and library files. Multiple
14393 @code{obj_dir} lines can be specified, and they will be searched in the order
14396 @item comp_opt=SWITCHES
14397 [default: @code{""}]
14398 creates a variable which can be referred to subsequently by using
14399 the @code{$@{comp_opt@}} notation. This is intended to store the default
14400 switches given to @command{gnatmake} and @command{gcc}.
14402 @item bind_opt=SWITCHES
14403 [default: @code{""}]
14404 creates a variable which can be referred to subsequently by using
14405 the @samp{$@{bind_opt@}} notation. This is intended to store the default
14406 switches given to @command{gnatbind}.
14408 @item link_opt=SWITCHES
14409 [default: @code{""}]
14410 creates a variable which can be referred to subsequently by using
14411 the @samp{$@{link_opt@}} notation. This is intended to store the default
14412 switches given to @command{gnatlink}.
14414 @item main=EXECUTABLE
14415 [default: @code{""}]
14416 specifies the name of the executable for the application. This variable can
14417 be referred to in the following lines by using the @samp{$@{main@}} notation.
14420 @item comp_cmd=COMMAND
14421 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
14424 @item comp_cmd=COMMAND
14425 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
14427 specifies the command used to compile a single file in the application.
14430 @item make_cmd=COMMAND
14431 [default: @code{"GNAT MAKE $@{main@}
14432 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
14433 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
14434 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
14437 @item make_cmd=COMMAND
14438 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
14439 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
14440 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
14442 specifies the command used to recompile the whole application.
14444 @item run_cmd=COMMAND
14445 [default: @code{"$@{main@}"}]
14446 specifies the command used to run the application.
14448 @item debug_cmd=COMMAND
14449 [default: @code{"gdb $@{main@}"}]
14450 specifies the command used to debug the application
14455 @command{gnatxref} and @command{gnatfind} only take into account the
14456 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
14458 @node Regular Expressions in gnatfind and gnatxref
14459 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
14462 As specified in the section about @command{gnatfind}, the pattern can be a
14463 regular expression. Actually, there are to set of regular expressions
14464 which are recognized by the program :
14467 @item globbing patterns
14468 These are the most usual regular expression. They are the same that you
14469 generally used in a Unix shell command line, or in a DOS session.
14471 Here is a more formal grammar :
14478 term ::= elmt -- matches elmt
14479 term ::= elmt elmt -- concatenation (elmt then elmt)
14480 term ::= * -- any string of 0 or more characters
14481 term ::= ? -- matches any character
14482 term ::= [char @{char@}] -- matches any character listed
14483 term ::= [char - char] -- matches any character in range
14487 @item full regular expression
14488 The second set of regular expressions is much more powerful. This is the
14489 type of regular expressions recognized by utilities such a @file{grep}.
14491 The following is the form of a regular expression, expressed in Ada
14492 reference manual style BNF is as follows
14499 regexp ::= term @{| term@} -- alternation (term or term ...)
14501 term ::= item @{item@} -- concatenation (item then item)
14503 item ::= elmt -- match elmt
14504 item ::= elmt * -- zero or more elmt's
14505 item ::= elmt + -- one or more elmt's
14506 item ::= elmt ? -- matches elmt or nothing
14509 elmt ::= nschar -- matches given character
14510 elmt ::= [nschar @{nschar@}] -- matches any character listed
14511 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
14512 elmt ::= [char - char] -- matches chars in given range
14513 elmt ::= \ char -- matches given character
14514 elmt ::= . -- matches any single character
14515 elmt ::= ( regexp ) -- parens used for grouping
14517 char ::= any character, including special characters
14518 nschar ::= any character except ()[].*+?^^^
14522 Following are a few examples :
14526 will match any of the two strings 'abcde' and 'fghi'.
14529 will match any string like 'abd', 'abcd', 'abccd', 'abcccd', and so on
14532 will match any string which has only lowercase characters in it (and at
14533 least one character
14538 @node Examples of gnatxref Usage
14539 @section Examples of @code{gnatxref} Usage
14541 @subsection General Usage
14544 For the following examples, we will consider the following units :
14546 @smallexample @c ada
14552 3: procedure Foo (B : in Integer);
14559 1: package body Main is
14560 2: procedure Foo (B : in Integer) is
14571 2: procedure Print (B : Integer);
14580 The first thing to do is to recompile your application (for instance, in
14581 that case just by doing a @samp{gnatmake main}, so that GNAT generates
14582 the cross-referencing information.
14583 You can then issue any of the following commands:
14585 @item gnatxref main.adb
14586 @code{gnatxref} generates cross-reference information for main.adb
14587 and every unit 'with'ed by main.adb.
14589 The output would be:
14597 Decl: main.ads 3:20
14598 Body: main.adb 2:20
14599 Ref: main.adb 4:13 5:13 6:19
14602 Ref: main.adb 6:8 7:8
14612 Decl: main.ads 3:15
14613 Body: main.adb 2:15
14616 Body: main.adb 1:14
14619 Ref: main.adb 6:12 7:12
14623 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
14624 its body is in main.adb, line 1, column 14 and is not referenced any where.
14626 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
14627 it referenced in main.adb, line 6 column 12 and line 7 column 12.
14629 @item gnatxref package1.adb package2.ads
14630 @code{gnatxref} will generates cross-reference information for
14631 package1.adb, package2.ads and any other package 'with'ed by any
14637 @subsection Using gnatxref with vi
14639 @code{gnatxref} can generate a tags file output, which can be used
14640 directly from @file{vi}. Note that the standard version of @file{vi}
14641 will not work properly with overloaded symbols. Consider using another
14642 free implementation of @file{vi}, such as @file{vim}.
14645 $ gnatxref -v gnatfind.adb > tags
14649 will generate the tags file for @code{gnatfind} itself (if the sources
14650 are in the search path!).
14652 From @file{vi}, you can then use the command @samp{:tag @i{entity}}
14653 (replacing @i{entity} by whatever you are looking for), and vi will
14654 display a new file with the corresponding declaration of entity.
14657 @node Examples of gnatfind Usage
14658 @section Examples of @code{gnatfind} Usage
14662 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
14663 Find declarations for all entities xyz referenced at least once in
14664 main.adb. The references are search in every library file in the search
14667 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
14670 The output will look like:
14672 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14673 ^directory/^[directory]^main.adb:24:10: xyz <= body
14674 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14678 that is to say, one of the entities xyz found in main.adb is declared at
14679 line 12 of main.ads (and its body is in main.adb), and another one is
14680 declared at line 45 of foo.ads
14682 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
14683 This is the same command as the previous one, instead @code{gnatfind} will
14684 display the content of the Ada source file lines.
14686 The output will look like:
14689 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14691 ^directory/^[directory]^main.adb:24:10: xyz <= body
14693 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14698 This can make it easier to find exactly the location your are looking
14701 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
14702 Find references to all entities containing an x that are
14703 referenced on line 123 of main.ads.
14704 The references will be searched only in main.ads and foo.adb.
14706 @item gnatfind main.ads:123
14707 Find declarations and bodies for all entities that are referenced on
14708 line 123 of main.ads.
14710 This is the same as @code{gnatfind "*":main.adb:123}.
14712 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
14713 Find the declaration for the entity referenced at column 45 in
14714 line 123 of file main.adb in directory mydir. Note that it
14715 is usual to omit the identifier name when the column is given,
14716 since the column position identifies a unique reference.
14718 The column has to be the beginning of the identifier, and should not
14719 point to any character in the middle of the identifier.
14723 @c *********************************
14724 @node The GNAT Pretty-Printer gnatpp
14725 @chapter The GNAT Pretty-Printer @command{gnatpp}
14727 @cindex Pretty-Printer
14730 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
14731 for source reformatting / pretty-printing.
14732 It takes an Ada source file as input and generates a reformatted
14734 You can specify various style directives via switches; e.g.,
14735 identifier case conventions, rules of indentation, and comment layout.
14737 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
14738 tree for the input source and thus requires the input to be syntactically and
14739 semantically legal.
14740 If this condition is not met, @command{gnatpp} will terminate with an
14741 error message; no output file will be generated.
14743 If the compilation unit
14744 contained in the input source depends semantically upon units located
14745 outside the current directory, you have to provide the source search path
14746 when invoking @command{gnatpp}, if these units are contained in files with
14747 names that do not follow the GNAT file naming rules, you have to provide
14748 the configuration file describing the corresponding naming scheme;
14749 see the description of the @command{gnatpp}
14750 switches below. Another possibility is to use a project file and to
14751 call @command{gnatpp} through the @command{gnat} driver
14753 The @command{gnatpp} command has the form
14756 $ gnatpp [@var{switches}] @var{filename}
14763 @var{switches} is an optional sequence of switches defining such properties as
14764 the formatting rules, the source search path, and the destination for the
14768 @var{filename} is the name (including the extension) of the source file to
14769 reformat; ``wildcards'' or several file names on the same gnatpp command are
14770 allowed. The file name may contain path information; it does not have to
14771 follow the GNAT file naming rules
14775 * Switches for gnatpp::
14776 * Formatting Rules::
14779 @node Switches for gnatpp
14780 @section Switches for @command{gnatpp}
14783 The following subsections describe the various switches accepted by
14784 @command{gnatpp}, organized by category.
14787 You specify a switch by supplying a name and generally also a value.
14788 In many cases the values for a switch with a given name are incompatible with
14790 (for example the switch that controls the casing of a reserved word may have
14791 exactly one value: upper case, lower case, or
14792 mixed case) and thus exactly one such switch can be in effect for an
14793 invocation of @command{gnatpp}.
14794 If more than one is supplied, the last one is used.
14795 However, some values for the same switch are mutually compatible.
14796 You may supply several such switches to @command{gnatpp}, but then
14797 each must be specified in full, with both the name and the value.
14798 Abbreviated forms (the name appearing once, followed by each value) are
14800 For example, to set
14801 the alignment of the assignment delimiter both in declarations and in
14802 assignment statements, you must write @option{-A2A3}
14803 (or @option{-A2 -A3}), but not @option{-A23}.
14807 In many cases the set of options for a given qualifier are incompatible with
14808 each other (for example the qualifier that controls the casing of a reserved
14809 word may have exactly one option, which specifies either upper case, lower
14810 case, or mixed case), and thus exactly one such option can be in effect for
14811 an invocation of @command{gnatpp}.
14812 If more than one is supplied, the last one is used.
14813 However, some qualifiers have options that are mutually compatible,
14814 and then you may then supply several such options when invoking
14818 In most cases, it is obvious whether or not the
14819 ^values for a switch with a given name^options for a given qualifier^
14820 are compatible with each other.
14821 When the semantics might not be evident, the summaries below explicitly
14822 indicate the effect.
14825 * Alignment Control::
14827 * Construct Layout Control::
14828 * General Text Layout Control::
14829 * Other Formatting Options::
14830 * Setting the Source Search Path::
14831 * Output File Control::
14832 * Other gnatpp Switches::
14835 @node Alignment Control
14836 @subsection Alignment Control
14837 @cindex Alignment control in @command{gnatpp}
14840 Programs can be easier to read if certain constructs are vertically aligned.
14841 By default all alignments are set ON.
14842 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
14843 OFF, and then use one or more of the other
14844 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
14845 to activate alignment for specific constructs.
14848 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
14852 Set all alignments to ON
14855 @item ^-A0^/ALIGN=OFF^
14856 Set all alignments to OFF
14858 @item ^-A1^/ALIGN=COLONS^
14859 Align @code{:} in declarations
14861 @item ^-A2^/ALIGN=DECLARATIONS^
14862 Align @code{:=} in initializations in declarations
14864 @item ^-A3^/ALIGN=STATEMENTS^
14865 Align @code{:=} in assignment statements
14867 @item ^-A4^/ALIGN=ARROWS^
14868 Align @code{=>} in associations
14870 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
14871 Align @code{at} keywords in the component clauses in record
14872 representation clauses
14876 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
14879 @node Casing Control
14880 @subsection Casing Control
14881 @cindex Casing control in @command{gnatpp}
14884 @command{gnatpp} allows you to specify the casing for reserved words,
14885 pragma names, attribute designators and identifiers.
14886 For identifiers you may define a
14887 general rule for name casing but also override this rule
14888 via a set of dictionary files.
14890 Three types of casing are supported: lower case, upper case, and mixed case.
14891 Lower and upper case are self-explanatory (but since some letters in
14892 Latin1 and other GNAT-supported character sets
14893 exist only in lower-case form, an upper case conversion will have no
14895 ``Mixed case'' means that the first letter, and also each letter immediately
14896 following an underscore, are converted to their uppercase forms;
14897 all the other letters are converted to their lowercase forms.
14900 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
14901 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
14902 Attribute designators are lower case
14904 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
14905 Attribute designators are upper case
14907 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
14908 Attribute designators are mixed case (this is the default)
14910 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
14911 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
14912 Keywords (technically, these are known in Ada as @emph{reserved words}) are
14913 lower case (this is the default)
14915 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
14916 Keywords are upper case
14918 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
14919 @item ^-nD^/NAME_CASING=AS_DECLARED^
14920 Name casing for defining occurrences are as they appear in the source file
14921 (this is the default)
14923 @item ^-nU^/NAME_CASING=UPPER_CASE^
14924 Names are in upper case
14926 @item ^-nL^/NAME_CASING=LOWER_CASE^
14927 Names are in lower case
14929 @item ^-nM^/NAME_CASING=MIXED_CASE^
14930 Names are in mixed case
14932 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
14933 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
14934 Pragma names are lower case
14936 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
14937 Pragma names are upper case
14939 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
14940 Pragma names are mixed case (this is the default)
14942 @item ^-D@var{file}^/DICTIONARY=@var{file}^
14943 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
14944 Use @var{file} as a @emph{dictionary file} that defines
14945 the casing for a set of specified names,
14946 thereby overriding the effect on these names by
14947 any explicit or implicit
14948 ^-n^/NAME_CASING^ switch.
14949 To supply more than one dictionary file,
14950 use ^several @option{-D} switches^a list of files as options^.
14953 @option{gnatpp} implicitly uses a @emph{default dictionary file}
14954 to define the casing for the Ada predefined names and
14955 the names declared in the GNAT libraries.
14957 @item ^-D-^/SPECIFIC_CASING^
14958 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
14959 Do not use the default dictionary file;
14960 instead, use the casing
14961 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
14966 The structure of a dictionary file, and details on the conventions
14967 used in the default dictionary file, are defined in @ref{Name Casing}.
14969 The @option{^-D-^/SPECIFIC_CASING^} and
14970 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
14973 @node Construct Layout Control
14974 @subsection Construct Layout Control
14975 @cindex Layout control in @command{gnatpp}
14978 This group of @command{gnatpp} switches controls the layout of comments and
14979 complex syntactic constructs. See @ref{Formatting Comments} for details
14983 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
14984 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
14985 All the comments remain unchanged
14987 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
14988 GNAT-style comment line indentation (this is the default).
14990 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
14991 Reference-manual comment line indentation.
14993 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
14994 GNAT-style comment beginning
14996 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
14997 Reformat comment blocks
14999 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
15000 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
15001 GNAT-style layout (this is the default)
15003 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
15006 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
15009 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
15011 All the VT characters are removed from the comment text. All the HT characters
15012 are expanded with the sequences of space characters to get to the next tab
15015 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
15016 @item ^--no-separate-is^/NO_SEPARATE_IS^
15017 Do not place the keyword @code{is} on a separate line in a subprogram body in
15018 case if the specification occupies more then one line.
15024 The @option{-c1} and @option{-c2} switches are incompatible.
15025 The @option{-c3} and @option{-c4} switches are compatible with each other and
15026 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
15027 the other comment formatting switches.
15029 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
15034 For the @option{/COMMENTS_LAYOUT} qualifier:
15037 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
15039 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
15040 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
15044 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
15045 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
15048 @node General Text Layout Control
15049 @subsection General Text Layout Control
15052 These switches allow control over line length and indentation.
15055 @item ^-M@i{nnn}^/LINE_LENGTH_MAX=@i{nnn}^
15056 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
15057 Maximum line length, @i{nnn} from 32 ..256, the default value is 79
15059 @item ^-i@i{nnn}^/INDENTATION_LEVEL=@i{nnn}^
15060 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
15061 Indentation level, @i{nnn} from 1 .. 9, the default value is 3
15063 @item ^-cl@i{nnn}^/CONTINUATION_INDENT=@i{nnn}^
15064 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
15065 Indentation level for continuation lines (relative to the line being
15066 continued), @i{nnn} from 1 .. 9.
15068 value is one less then the (normal) indentation level, unless the
15069 indentation is set to 1 (in which case the default value for continuation
15070 line indentation is also 1)
15073 @node Other Formatting Options
15074 @subsection Other Formatting Options
15077 These switches control the inclusion of missing end/exit labels, and
15078 the indentation level in @b{case} statements.
15081 @item ^-e^/NO_MISSED_LABELS^
15082 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
15083 Do not insert missing end/exit labels. An end label is the name of
15084 a construct that may optionally be repeated at the end of the
15085 construct's declaration;
15086 e.g., the names of packages, subprograms, and tasks.
15087 An exit label is the name of a loop that may appear as target
15088 of an exit statement within the loop.
15089 By default, @command{gnatpp} inserts these end/exit labels when
15090 they are absent from the original source. This option suppresses such
15091 insertion, so that the formatted source reflects the original.
15093 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
15094 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
15095 Insert a Form Feed character after a pragma Page.
15097 @item ^-T@i{nnn}^/MAX_INDENT=@i{nnn}^
15098 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
15099 Do not use an additional indentation level for @b{case} alternatives
15100 and variants if there are @i{nnn} or more (the default
15102 If @i{nnn} is 0, an additional indentation level is
15103 used for @b{case} alternatives and variants regardless of their number.
15106 @node Setting the Source Search Path
15107 @subsection Setting the Source Search Path
15110 To define the search path for the input source file, @command{gnatpp}
15111 uses the same switches as the GNAT compiler, with the same effects.
15114 @item ^-I^/SEARCH=^@var{dir}
15115 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
15116 The same as the corresponding gcc switch
15118 @item ^-I-^/NOCURRENT_DIRECTORY^
15119 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
15120 The same as the corresponding gcc switch
15122 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
15123 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
15124 The same as the corresponding gcc switch
15126 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
15127 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
15128 The same as the corresponding gcc switch
15132 @node Output File Control
15133 @subsection Output File Control
15136 By default the output is sent to the file whose name is obtained by appending
15137 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
15138 (if the file with this name already exists, it is unconditionally overwritten).
15139 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
15140 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
15142 The output may be redirected by the following switches:
15145 @item ^-pipe^/STANDARD_OUTPUT^
15146 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
15147 Send the output to @code{Standard_Output}
15149 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
15150 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
15151 Write the output into @var{output_file}.
15152 If @var{output_file} already exists, @command{gnatpp} terminates without
15153 reading or processing the input file.
15155 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
15156 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
15157 Write the output into @var{output_file}, overwriting the existing file
15158 (if one is present).
15160 @item ^-r^/REPLACE^
15161 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
15162 Replace the input source file with the reformatted output, and copy the
15163 original input source into the file whose name is obtained by appending the
15164 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
15165 If a file with this name already exists, @command{gnatpp} terminates without
15166 reading or processing the input file.
15168 @item ^-rf^/OVERRIDING_REPLACE^
15169 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
15170 Like @option{^-r^/REPLACE^} except that if the file with the specified name
15171 already exists, it is overwritten.
15173 @item ^-rnb^/NO_BACKUP^
15174 @cindex @option{^-rnb^/NO_BACKUP^} (@code{gnatpp})
15175 Replace the input source file with the reformatted output without
15176 creating any backup copy of the input source.
15178 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
15179 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
15180 Specifies the format of the reformatted output file. The @var{xxx}
15181 ^string specified with the switch^option^ may be either
15183 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
15184 @item ``@option{^crlf^CRLF^}''
15185 the same as @option{^crlf^CRLF^}
15186 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
15187 @item ``@option{^lf^LF^}''
15188 the same as @option{^unix^UNIX^}
15194 Options @option{^-pipe^/STANDARD_OUTPUT^},
15195 @option{^-o^/OUTPUT^} and
15196 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
15197 contains only one file to reformat.
15199 @option{^--eol^/END_OF_LINE^}
15200 cannot be used together
15201 with @option{^-pipe^/STANDARD_OUTPUT^} option.
15203 @node Other gnatpp Switches
15204 @subsection Other @code{gnatpp} Switches
15207 The additional @command{gnatpp} switches are defined in this subsection.
15210 @item ^-files @var{filename}^/FILES=@var{output_file}^
15211 @cindex @option{^-files^/FILES^} (@code{gnatpp})
15212 Take the argument source files from the specified file. This file should be an
15213 ordinary textual file containing file names separated by spaces or
15214 line breaks. You can use this switch more then once in the same call to
15215 @command{gnatpp}. You also can combine this switch with explicit list of
15218 @item ^-v^/VERBOSE^
15219 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
15221 @command{gnatpp} generates version information and then
15222 a trace of the actions it takes to produce or obtain the ASIS tree.
15224 @item ^-w^/WARNINGS^
15225 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
15227 @command{gnatpp} generates a warning whenever it cannot provide
15228 a required layout in the result source.
15231 @node Formatting Rules
15232 @section Formatting Rules
15235 The following subsections show how @command{gnatpp} treats ``white space'',
15236 comments, program layout, and name casing.
15237 They provide the detailed descriptions of the switches shown above.
15240 * White Space and Empty Lines::
15241 * Formatting Comments::
15242 * Construct Layout::
15246 @node White Space and Empty Lines
15247 @subsection White Space and Empty Lines
15250 @command{gnatpp} does not have an option to control space characters.
15251 It will add or remove spaces according to the style illustrated by the
15252 examples in the @cite{Ada Reference Manual}.
15254 The only format effectors
15255 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
15256 that will appear in the output file are platform-specific line breaks,
15257 and also format effectors within (but not at the end of) comments.
15258 In particular, each horizontal tab character that is not inside
15259 a comment will be treated as a space and thus will appear in the
15260 output file as zero or more spaces depending on
15261 the reformatting of the line in which it appears.
15262 The only exception is a Form Feed character, which is inserted after a
15263 pragma @code{Page} when @option{-ff} is set.
15265 The output file will contain no lines with trailing ``white space'' (spaces,
15268 Empty lines in the original source are preserved
15269 only if they separate declarations or statements.
15270 In such contexts, a
15271 sequence of two or more empty lines is replaced by exactly one empty line.
15272 Note that a blank line will be removed if it separates two ``comment blocks''
15273 (a comment block is a sequence of whole-line comments).
15274 In order to preserve a visual separation between comment blocks, use an
15275 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
15276 Likewise, if for some reason you wish to have a sequence of empty lines,
15277 use a sequence of empty comments instead.
15279 @node Formatting Comments
15280 @subsection Formatting Comments
15283 Comments in Ada code are of two kinds:
15286 a @emph{whole-line comment}, which appears by itself (possibly preceded by
15287 ``white space'') on a line
15290 an @emph{end-of-line comment}, which follows some other Ada lexical element
15295 The indentation of a whole-line comment is that of either
15296 the preceding or following line in
15297 the formatted source, depending on switch settings as will be described below.
15299 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
15300 between the end of the preceding Ada lexical element and the beginning
15301 of the comment as appear in the original source,
15302 unless either the comment has to be split to
15303 satisfy the line length limitation, or else the next line contains a
15304 whole line comment that is considered a continuation of this end-of-line
15305 comment (because it starts at the same position).
15307 cases, the start of the end-of-line comment is moved right to the nearest
15308 multiple of the indentation level.
15309 This may result in a ``line overflow'' (the right-shifted comment extending
15310 beyond the maximum line length), in which case the comment is split as
15313 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
15314 (GNAT-style comment line indentation)
15315 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
15316 (reference-manual comment line indentation).
15317 With reference-manual style, a whole-line comment is indented as if it
15318 were a declaration or statement at the same place
15319 (i.e., according to the indentation of the preceding line(s)).
15320 With GNAT style, a whole-line comment that is immediately followed by an
15321 @b{if} or @b{case} statement alternative, a record variant, or the reserved
15322 word @b{begin}, is indented based on the construct that follows it.
15325 @smallexample @c ada
15337 Reference-manual indentation produces:
15339 @smallexample @c ada
15351 while GNAT-style indentation produces:
15353 @smallexample @c ada
15365 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
15366 (GNAT style comment beginning) has the following
15371 For each whole-line comment that does not end with two hyphens,
15372 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
15373 to ensure that there are at least two spaces between these hyphens and the
15374 first non-blank character of the comment.
15378 For an end-of-line comment, if in the original source the next line is a
15379 whole-line comment that starts at the same position
15380 as the end-of-line comment,
15381 then the whole-line comment (and all whole-line comments
15382 that follow it and that start at the same position)
15383 will start at this position in the output file.
15386 That is, if in the original source we have:
15388 @smallexample @c ada
15391 A := B + C; -- B must be in the range Low1..High1
15392 -- C must be in the range Low2..High2
15393 --B+C will be in the range Low1+Low2..High1+High2
15399 Then in the formatted source we get
15401 @smallexample @c ada
15404 A := B + C; -- B must be in the range Low1..High1
15405 -- C must be in the range Low2..High2
15406 -- B+C will be in the range Low1+Low2..High1+High2
15412 A comment that exceeds the line length limit will be split.
15414 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
15415 the line belongs to a reformattable block, splitting the line generates a
15416 @command{gnatpp} warning.
15417 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
15418 comments may be reformatted in typical
15419 word processor style (that is, moving words between lines and putting as
15420 many words in a line as possible).
15422 @node Construct Layout
15423 @subsection Construct Layout
15426 In several cases the suggested layout in the Ada Reference Manual includes
15427 an extra level of indentation that many programmers prefer to avoid. The
15428 affected cases include:
15432 @item Record type declaration (RM 3.8)
15434 @item Record representation clause (RM 13.5.1)
15436 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
15438 @item Block statement in case if a block has a statement identifier (RM 5.6)
15442 In compact mode (when GNAT style layout or compact layout is set),
15443 the pretty printer uses one level of indentation instead
15444 of two. This is achieved in the record definition and record representation
15445 clause cases by putting the @code{record} keyword on the same line as the
15446 start of the declaration or representation clause, and in the block and loop
15447 case by putting the block or loop header on the same line as the statement
15451 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
15452 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
15453 layout on the one hand, and uncompact layout
15454 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
15455 can be illustrated by the following examples:
15459 @multitable @columnfractions .5 .5
15460 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
15463 @smallexample @c ada
15470 @smallexample @c ada
15479 @smallexample @c ada
15481 a at 0 range 0 .. 31;
15482 b at 4 range 0 .. 31;
15486 @smallexample @c ada
15489 a at 0 range 0 .. 31;
15490 b at 4 range 0 .. 31;
15495 @smallexample @c ada
15503 @smallexample @c ada
15513 @smallexample @c ada
15514 Clear : for J in 1 .. 10 loop
15519 @smallexample @c ada
15521 for J in 1 .. 10 loop
15532 GNAT style, compact layout Uncompact layout
15534 type q is record type q is
15535 a : integer; record
15536 b : integer; a : integer;
15537 end record; b : integer;
15540 for q use record for q use
15541 a at 0 range 0 .. 31; record
15542 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
15543 end record; b at 4 range 0 .. 31;
15546 Block : declare Block :
15547 A : Integer := 3; declare
15548 begin A : Integer := 3;
15550 end Block; Proc (A, A);
15553 Clear : for J in 1 .. 10 loop Clear :
15554 A (J) := 0; for J in 1 .. 10 loop
15555 end loop Clear; A (J) := 0;
15562 A further difference between GNAT style layout and compact layout is that
15563 GNAT style layout inserts empty lines as separation for
15564 compound statements, return statements and bodies.
15567 @subsection Name Casing
15570 @command{gnatpp} always converts the usage occurrence of a (simple) name to
15571 the same casing as the corresponding defining identifier.
15573 You control the casing for defining occurrences via the
15574 @option{^-n^/NAME_CASING^} switch.
15576 With @option{-nD} (``as declared'', which is the default),
15579 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
15581 defining occurrences appear exactly as in the source file
15582 where they are declared.
15583 The other ^values for this switch^options for this qualifier^ ---
15584 @option{^-nU^UPPER_CASE^},
15585 @option{^-nL^LOWER_CASE^},
15586 @option{^-nM^MIXED_CASE^} ---
15588 ^upper, lower, or mixed case, respectively^the corresponding casing^.
15589 If @command{gnatpp} changes the casing of a defining
15590 occurrence, it analogously changes the casing of all the
15591 usage occurrences of this name.
15593 If the defining occurrence of a name is not in the source compilation unit
15594 currently being processed by @command{gnatpp}, the casing of each reference to
15595 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
15596 switch (subject to the dictionary file mechanism described below).
15597 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
15599 casing for the defining occurrence of the name.
15601 Some names may need to be spelled with casing conventions that are not
15602 covered by the upper-, lower-, and mixed-case transformations.
15603 You can arrange correct casing by placing such names in a
15604 @emph{dictionary file},
15605 and then supplying a @option{^-D^/DICTIONARY^} switch.
15606 The casing of names from dictionary files overrides
15607 any @option{^-n^/NAME_CASING^} switch.
15609 To handle the casing of Ada predefined names and the names from GNAT libraries,
15610 @command{gnatpp} assumes a default dictionary file.
15611 The name of each predefined entity is spelled with the same casing as is used
15612 for the entity in the @cite{Ada Reference Manual}.
15613 The name of each entity in the GNAT libraries is spelled with the same casing
15614 as is used in the declaration of that entity.
15616 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
15617 default dictionary file.
15618 Instead, the casing for predefined and GNAT-defined names will be established
15619 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
15620 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
15621 will appear as just shown,
15622 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
15623 To ensure that even such names are rendered in uppercase,
15624 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
15625 (or else, less conveniently, place these names in upper case in a dictionary
15628 A dictionary file is
15629 a plain text file; each line in this file can be either a blank line
15630 (containing only space characters and ASCII.HT characters), an Ada comment
15631 line, or the specification of exactly one @emph{casing schema}.
15633 A casing schema is a string that has the following syntax:
15637 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
15639 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
15644 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
15645 @var{identifier} lexical element and the @var{letter_or_digit} category.)
15647 The casing schema string can be followed by white space and/or an Ada-style
15648 comment; any amount of white space is allowed before the string.
15650 If a dictionary file is passed as
15652 the value of a @option{-D@var{file}} switch
15655 an option to the @option{/DICTIONARY} qualifier
15658 simple name and every identifier, @command{gnatpp} checks if the dictionary
15659 defines the casing for the name or for some of its parts (the term ``subword''
15660 is used below to denote the part of a name which is delimited by ``_'' or by
15661 the beginning or end of the word and which does not contain any ``_'' inside):
15665 if the whole name is in the dictionary, @command{gnatpp} uses for this name
15666 the casing defined by the dictionary; no subwords are checked for this word
15669 for every subword @command{gnatpp} checks if the dictionary contains the
15670 corresponding string of the form @code{*@var{simple_identifier}*},
15671 and if it does, the casing of this @var{simple_identifier} is used
15675 if the whole name does not contain any ``_'' inside, and if for this name
15676 the dictionary contains two entries - one of the form @var{identifier},
15677 and another - of the form *@var{simple_identifier}*, then the first one
15678 is applied to define the casing of this name
15681 if more than one dictionary file is passed as @command{gnatpp} switches, each
15682 dictionary adds new casing exceptions and overrides all the existing casing
15683 exceptions set by the previous dictionaries
15686 when @command{gnatpp} checks if the word or subword is in the dictionary,
15687 this check is not case sensitive
15691 For example, suppose we have the following source to reformat:
15693 @smallexample @c ada
15696 name1 : integer := 1;
15697 name4_name3_name2 : integer := 2;
15698 name2_name3_name4 : Boolean;
15701 name2_name3_name4 := name4_name3_name2 > name1;
15707 And suppose we have two dictionaries:
15724 If @command{gnatpp} is called with the following switches:
15728 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
15731 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
15736 then we will get the following name casing in the @command{gnatpp} output:
15738 @smallexample @c ada
15741 NAME1 : Integer := 1;
15742 Name4_NAME3_Name2 : Integer := 2;
15743 Name2_NAME3_Name4 : Boolean;
15746 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
15751 @c *********************************
15752 @node The GNAT Metric Tool gnatmetric
15753 @chapter The GNAT Metric Tool @command{gnatmetric}
15755 @cindex Metric tool
15758 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
15759 for computing various program metrics.
15760 It takes an Ada source file as input and generates a file containing the
15761 metrics data as output. Various switches control which
15762 metrics are computed and output.
15764 @command{gnatmetric} generates and uses the ASIS
15765 tree for the input source and thus requires the input to be syntactically and
15766 semantically legal.
15767 If this condition is not met, @command{gnatmetric} will generate
15768 an error message; no metric information for this file will be
15769 computed and reported.
15771 If the compilation unit contained in the input source depends semantically
15772 upon units in files located outside the current directory, you have to provide
15773 the source search path when invoking @command{gnatmetric}.
15774 If it depends semantically upon units that are contained
15775 in files with names that do not follow the GNAT file naming rules, you have to
15776 provide the configuration file describing the corresponding naming scheme (see
15777 the description of the @command{gnatmetric} switches below.)
15778 Alternatively, you may use a project file and invoke @command{gnatmetric}
15779 through the @command{gnat} driver.
15782 The @command{gnatmetric} command has the form
15785 $ gnatmetric [@i{switches}] @{@i{filename}@} [@i{-cargs gcc_switches}]
15792 @i{switches} specify the metrics to compute and define the destination for
15796 Each @i{filename} is the name (including the extension) of a source
15797 file to process. ``Wildcards'' are allowed, and
15798 the file name may contain path information.
15799 If no @i{filename} is supplied, then the @i{switches} list must contain
15801 @option{-files} switch (@pxref{Other gnatmetric Switches}).
15802 Including both a @option{-files} switch and one or more
15803 @i{filename} arguments is permitted.
15806 @i{-cargs gcc_switches} is a list of switches for
15807 @command{gcc}. They will be passed on to all compiler invocations made by
15808 @command{gnatmetric} to generate the ASIS trees. Here you can provide
15809 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
15810 and use the @option{-gnatec} switch to set the configuration file.
15814 * Switches for gnatmetric::
15817 @node Switches for gnatmetric
15818 @section Switches for @command{gnatmetric}
15821 The following subsections describe the various switches accepted by
15822 @command{gnatmetric}, organized by category.
15825 * Output Files Control::
15826 * Disable Metrics For Local Units::
15827 * Line Metrics Control::
15828 * Syntax Metrics Control::
15829 * Complexity Metrics Control::
15830 * Other gnatmetric Switches::
15833 @node Output Files Control
15834 @subsection Output File Control
15835 @cindex Output file control in @command{gnatmetric}
15838 @command{gnatmetric} has two output formats. It can generate a
15839 textual (human-readable) form, and also XML. By default only textual
15840 output is generated.
15842 When generating the output in textual form, @command{gnatmetric} creates
15843 for each Ada source file a corresponding text file
15844 containing the computed metrics. By default, this file
15845 is placed in the same directory as where the source file is located, and
15846 its name is obtained
15847 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
15850 All the output information generated in XML format is placed in a single
15851 file. By default this file is placed in the current directory and has the
15852 name ^@file{metrix.xml}^@file{METRIX$XML}^.
15854 Some of the computed metrics are summed over the units passed to
15855 @command{gnatmetric}; for example, the total number of lines of code.
15856 By default this information is sent to @file{stdout}, but a file
15857 can be specified with the @option{-og} switch.
15859 The following switches control the @command{gnatmetric} output:
15862 @cindex @option{^-x^/XML^} (@command{gnatmetric})
15864 Generate the XML output
15866 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
15867 @item ^-nt^/NO_TEXT^
15868 Do not generate the output in text form (implies @option{^-x^/XML^})
15870 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
15871 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
15872 Put textual files with detailed metrics into @var{output_dir}
15874 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
15875 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
15876 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
15877 in the name of the output file.
15879 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
15880 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
15881 Put global metrics into @var{file_name}
15883 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
15884 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
15885 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
15887 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
15888 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
15889 Use ``short'' source file names in the output. (The @command{gnatmetric}
15890 output includes the name(s) of the Ada source file(s) from which the metrics
15891 are computed. By default each name includes the absolute path. The
15892 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
15893 to exclude all directory information from the file names that are output.)
15897 @node Disable Metrics For Local Units
15898 @subsection Disable Metrics For Local Units
15899 @cindex Disable Metrics For Local Units in @command{gnatmetric}
15902 @command{gnatmetric} relies on the GNAT compilation model @minus{}
15904 unit per one source file. It computes line metrics for the whole source
15905 file, and it also computes syntax
15906 and complexity metrics for the file's outermost unit.
15908 By default, @command{gnatmetric} will also compute all metrics for certain
15909 kinds of locally declared program units:
15913 subprogram (and generic subprogram) bodies;
15916 package (and generic package) specifications and bodies;
15919 task object and type specifications and bodies;
15922 protected object and type specifications and bodies.
15926 These kinds of entities will be referred to as
15927 @emph{eligible local program units}, or simply @emph{eligible local units},
15928 @cindex Eligible local unit (for @command{gnatmetric})
15929 in the discussion below.
15931 Note that a subprogram declaration, generic instantiation,
15932 or renaming declaration only receives metrics
15933 computation when it appear as the outermost entity
15936 Suppression of metrics computation for eligible local units can be
15937 obtained via the following switch:
15940 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
15941 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
15942 Do not compute detailed metrics for eligible local program units
15946 @node Line Metrics Control
15947 @subsection Line Metrics Control
15948 @cindex Line metrics control in @command{gnatmetric}
15951 For any (legal) source file, and for each of its
15952 eligible local program units, @command{gnatmetric} computes the following
15957 the total number of lines;
15960 the total number of code lines (i.e., non-blank lines that are not comments)
15963 the number of comment lines
15966 the number of code lines containing end-of-line comments;
15969 the number of empty lines and lines containing only space characters and/or
15970 format effectors (blank lines)
15974 If @command{gnatmetric} is invoked on more than one source file, it sums the
15975 values of the line metrics for all the files being processed and then
15976 generates the cumulative results.
15978 By default, all the line metrics are computed and reported. You can use the
15979 following switches to select the specific line metrics to be computed and
15980 reported (if any of these parameters is set, only explicitly specified line
15981 metrics are computed).
15984 @cindex @option{^-la^/LINES_ALL^} (@command{gnatmetric})
15985 @item ^-la^/LINES_ALL^
15986 The number of all lines
15988 @cindex @option{^-lcode^/CODE_LINES^} (@command{gnatmetric})
15989 @item ^-lcode^/CODE_LINES^
15990 The number of code lines
15992 @cindex @option{^-lcomm^/COMMENT_LINES^} (@command{gnatmetric})
15993 @item ^-lcomm^/COMENT_LINES^
15994 The number of comment lines
15996 @cindex @option{^-leol^/MIXED_CODE_COMMENTS^} (@command{gnatmetric})
15997 @item ^-leol^/MIXED_CODE_COMMENTS^
15998 The number of code lines containing
15999 end-of-line comments
16001 @cindex @option{^-lb^/BLANK_LINES^} (@command{gnatmetric})
16002 @item ^-lb^/BLANK_LINES^
16003 The number of blank lines
16008 @node Syntax Metrics Control
16009 @subsection Syntax Metrics Control
16010 @cindex Syntax metrics control in @command{gnatmetric}
16013 @command{gnatmetric} computes various syntactic metrics for the
16014 outermost unit and for each eligible local unit:
16017 @item LSLOC (``Logical Source Lines Of Code'')
16018 The total number of declarations and the total number of statements
16020 @item Maximal static nesting level of inner program units
16022 @cite{Ada 95 Language Reference Manual}, 10.1(1), ``A program unit is either a
16023 package, a task unit, a protected unit, a
16024 protected entry, a generic unit, or an explicitly declared subprogram other
16025 than an enumeration literal.''
16027 @item Maximal nesting level of composite syntactic constructs
16028 This corresponds to the notion of the
16029 maximum nesting level in the GNAT built-in style checks
16030 (@pxref{Style Checking})
16034 For the outermost unit in the file, @command{gnatmetric} additionally computes
16035 the following metrics:
16038 @item Public subprograms
16039 This metric is computed for package specifications. It is the
16040 number of subprograms and generic subprograms declared in the visible
16041 part (including in nested packages, protected objects, and
16044 @item All subprograms
16045 This metric is computed for bodies and subunits. The
16046 metric is equal to a total number of subprogram bodies in the compilation
16048 Neither generic instantiations nor renamings-as-a-body nor body stubs
16049 are counted. Any subprogram body is counted, independently of its nesting
16050 level and enclosing constructs. Generic bodies and bodies of protected
16051 subprograms are counted in the same way as ``usual'' subprogram bodies.
16054 This metric is computed for package specifications and
16055 generic package declarations. It is the total number of types
16056 that can be referenced from outside this compilation unit, plus the
16057 number of types from all the visible parts of all the visible generic packages.
16058 Generic formal types are not counted. Only types, not subtypes,
16062 Along with the total number of public types, the following
16063 types are counted and reported separately:
16070 Root tagged types (abstract, non-abstract, private, non-private). Type
16071 extensions are @emph{not} counted
16074 Private types (including private extensions)
16085 This metric is computed for any compilation unit. It is equal to the total
16086 number of the declarations of different types given in the compilation unit.
16087 The private and the corresponding full type declaration are counted as one
16088 type declaration. Incomplete type declarations and generic formal types
16090 No distinction is made among different kinds of types (abstract,
16091 private etc.); the total number of types is computed and reported.
16096 By default, all the syntax metrics are computed and reported. You can use the
16097 following switches to select specific syntax metrics;
16098 if any of these is set, only the explicitly specified metrics are computed.
16101 @cindex @option{^-ed^/DECLARATION_TOTAL^} (@command{gnatmetric})
16102 @item ^-ed^/DECLARATION_TOTAL^
16103 The total number of declarations
16105 @cindex @option{^-es^/STATEMENT_TOTAL^} (@command{gnatmetric})
16106 @item ^-es^/STATEMENT_TOTAL^
16107 The total number of statements
16109 @cindex @option{^-eps^/^} (@command{gnatmetric})
16110 @item ^-eps^/INT_SUBPROGRAMS^
16111 The number of public subprograms in a compilation unit
16113 @cindex @option{^-eas^/SUBPROGRAMS_ALL^} (@command{gnatmetric})
16114 @item ^-eas^/SUBPROGRAMS_ALL^
16115 The number of all the subprograms in a compilation unit
16117 @cindex @option{^-ept^/INT_TYPES^} (@command{gnatmetric})
16118 @item ^-ept^/INT_TYPES^
16119 The number of public types in a compilation unit
16121 @cindex @option{^-eat^/TYPES_ALL^} (@command{gnatmetric})
16122 @item ^-eat^/TYPES_ALL^
16123 The number of all the types in a compilation unit
16125 @cindex @option{^-enu^/PROGRAM_NESTING_MAX^} (@command{gnatmetric})
16126 @item ^-enu^/PROGRAM_NESTING_MAX^
16127 The maximal program unit nesting level
16129 @cindex @option{^-ec^/CONSTRUCT_NESTING_MAX^} (@command{gnatmetric})
16130 @item ^-ec^/CONSTRUCT_NESTING_MAX^
16131 The maximal construct nesting level
16135 @node Complexity Metrics Control
16136 @subsection Complexity Metrics Control
16137 @cindex Complexity metrics control in @command{gnatmetric}
16140 For a program unit that is an executable body (a subprogram body (including
16141 generic bodies), task body, entry body or a package body containing
16142 its own statement sequence ) @command{gnatmetric} computes the following
16143 complexity metrics:
16147 McCabe cyclomatic complexity;
16150 McCabe essential complexity;
16153 maximal loop nesting level
16158 The McCabe complexity metrics are defined
16159 in @url{www.mccabe.com/pdf/nist235r.pdf}
16161 According to McCabe, both control statements and short-circuit control forms
16162 should be taken into account when computing cyclomatic complexity. For each
16163 body, we compute three metric values:
16167 the complexity introduced by control
16168 statements only, without taking into account short-circuit forms,
16171 the complexity introduced by short-circuit control forms only, and
16175 cyclomatic complexity, which is the sum of these two values.
16179 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
16180 the code in the exception handlers and in all the nested program units.
16182 By default, all the complexity metrics are computed and reported.
16183 For more finely-grained control you can use
16184 the following switches:
16187 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
16189 @item ^-nocc^/SUPPRESS=CYCLOMATIC_COMPLEXITY^
16190 Do not compute the McCabe Cyclomatic Complexity
16192 @item ^-noec^/SUPPRESS=ESSENTIAL_COMPLEXITY^
16193 Do not compute the Essential Complexity
16195 @item ^-nonl^/SUPPRESS=MAXIMAL_LOOP_NESTING^
16196 Do not compute maximal loop nesting level
16198 @item ^-ne^/SUPPRESS=EXITS_AS_GOTOS^
16199 Do not consider @code{exit} statements as @code{goto}s when
16200 computing Essential Complexity
16204 @node Other gnatmetric Switches
16205 @subsection Other @code{gnatmetric} Switches
16208 Additional @command{gnatmetric} switches are as follows:
16211 @item ^-files @var{filename}^/FILES=@var{filename}^
16212 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
16213 Take the argument source files from the specified file. This file should be an
16214 ordinary textual file containing file names separated by spaces or
16215 line breaks. You can use this switch more then once in the same call to
16216 @command{gnatmetric}. You also can combine this switch with
16217 an explicit list of files.
16219 @item ^-v^/VERBOSE^
16220 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
16222 @command{gnatmetric} generates version information and then
16223 a trace of sources being processed.
16225 @item ^-dv^/DEBUG_OUTPUT^
16226 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
16228 @command{gnatmetric} generates various messages useful to understand what
16229 happens during the metrics computation
16232 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
16236 @c ***********************************
16237 @node File Name Krunching Using gnatkr
16238 @chapter File Name Krunching Using @code{gnatkr}
16242 This chapter discusses the method used by the compiler to shorten
16243 the default file names chosen for Ada units so that they do not
16244 exceed the maximum length permitted. It also describes the
16245 @code{gnatkr} utility that can be used to determine the result of
16246 applying this shortening.
16250 * Krunching Method::
16251 * Examples of gnatkr Usage::
16255 @section About @code{gnatkr}
16258 The default file naming rule in GNAT
16259 is that the file name must be derived from
16260 the unit name. The exact default rule is as follows:
16263 Take the unit name and replace all dots by hyphens.
16265 If such a replacement occurs in the
16266 second character position of a name, and the first character is
16267 ^a, g, s, or i^A, G, S, or I^ then replace the dot by the character
16268 ^~ (tilde)^$ (dollar sign)^
16269 instead of a minus.
16271 The reason for this exception is to avoid clashes
16272 with the standard names for children of System, Ada, Interfaces,
16273 and GNAT, which use the prefixes ^s- a- i- and g-^S- A- I- and G-^
16276 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
16277 switch of the compiler activates a ``krunching''
16278 circuit that limits file names to nn characters (where nn is a decimal
16279 integer). For example, using OpenVMS,
16280 where the maximum file name length is
16281 39, the value of nn is usually set to 39, but if you want to generate
16282 a set of files that would be usable if ported to a system with some
16283 different maximum file length, then a different value can be specified.
16284 The default value of 39 for OpenVMS need not be specified.
16286 The @code{gnatkr} utility can be used to determine the krunched name for
16287 a given file, when krunched to a specified maximum length.
16290 @section Using @code{gnatkr}
16293 The @code{gnatkr} command has the form
16297 $ gnatkr @var{name} [@var{length}]
16303 $ gnatkr @var{name} /COUNT=nn
16308 @var{name} is the uncrunched file name, derived from the name of the unit
16309 in the standard manner described in the previous section (i.e. in particular
16310 all dots are replaced by hyphens). The file name may or may not have an
16311 extension (defined as a suffix of the form period followed by arbitrary
16312 characters other than period). If an extension is present then it will
16313 be preserved in the output. For example, when krunching @file{hellofile.ads}
16314 to eight characters, the result will be hellofil.ads.
16316 Note: for compatibility with previous versions of @code{gnatkr} dots may
16317 appear in the name instead of hyphens, but the last dot will always be
16318 taken as the start of an extension. So if @code{gnatkr} is given an argument
16319 such as @file{Hello.World.adb} it will be treated exactly as if the first
16320 period had been a hyphen, and for example krunching to eight characters
16321 gives the result @file{hellworl.adb}.
16323 Note that the result is always all lower case (except on OpenVMS where it is
16324 all upper case). Characters of the other case are folded as required.
16326 @var{length} represents the length of the krunched name. The default
16327 when no argument is given is ^8^39^ characters. A length of zero stands for
16328 unlimited, in other words do not chop except for system files where the
16329 implied crunching length is always eight characters.
16332 The output is the krunched name. The output has an extension only if the
16333 original argument was a file name with an extension.
16335 @node Krunching Method
16336 @section Krunching Method
16339 The initial file name is determined by the name of the unit that the file
16340 contains. The name is formed by taking the full expanded name of the
16341 unit and replacing the separating dots with hyphens and
16342 using ^lowercase^uppercase^
16343 for all letters, except that a hyphen in the second character position is
16344 replaced by a ^tilde^dollar sign^ if the first character is
16345 ^a, i, g, or s^A, I, G, or S^.
16346 The extension is @code{.ads} for a
16347 specification and @code{.adb} for a body.
16348 Krunching does not affect the extension, but the file name is shortened to
16349 the specified length by following these rules:
16353 The name is divided into segments separated by hyphens, tildes or
16354 underscores and all hyphens, tildes, and underscores are
16355 eliminated. If this leaves the name short enough, we are done.
16358 If the name is too long, the longest segment is located (left-most
16359 if there are two of equal length), and shortened by dropping
16360 its last character. This is repeated until the name is short enough.
16362 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
16363 to fit the name into 8 characters as required by some operating systems.
16366 our-strings-wide_fixed 22
16367 our strings wide fixed 19
16368 our string wide fixed 18
16369 our strin wide fixed 17
16370 our stri wide fixed 16
16371 our stri wide fixe 15
16372 our str wide fixe 14
16373 our str wid fixe 13
16379 Final file name: oustwifi.adb
16383 The file names for all predefined units are always krunched to eight
16384 characters. The krunching of these predefined units uses the following
16385 special prefix replacements:
16389 replaced by @file{^a^A^-}
16392 replaced by @file{^g^G^-}
16395 replaced by @file{^i^I^-}
16398 replaced by @file{^s^S^-}
16401 These system files have a hyphen in the second character position. That
16402 is why normal user files replace such a character with a
16403 ^tilde^dollar sign^, to
16404 avoid confusion with system file names.
16406 As an example of this special rule, consider
16407 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
16410 ada-strings-wide_fixed 22
16411 a- strings wide fixed 18
16412 a- string wide fixed 17
16413 a- strin wide fixed 16
16414 a- stri wide fixed 15
16415 a- stri wide fixe 14
16416 a- str wide fixe 13
16422 Final file name: a-stwifi.adb
16426 Of course no file shortening algorithm can guarantee uniqueness over all
16427 possible unit names, and if file name krunching is used then it is your
16428 responsibility to ensure that no name clashes occur. The utility
16429 program @code{gnatkr} is supplied for conveniently determining the
16430 krunched name of a file.
16432 @node Examples of gnatkr Usage
16433 @section Examples of @code{gnatkr} Usage
16440 $ gnatkr very_long_unit_name.ads --> velounna.ads
16441 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
16442 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
16443 $ gnatkr grandparent-parent-child --> grparchi
16445 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
16446 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
16449 @node Preprocessing Using gnatprep
16450 @chapter Preprocessing Using @code{gnatprep}
16454 The @code{gnatprep} utility provides
16455 a simple preprocessing capability for Ada programs.
16456 It is designed for use with GNAT, but is not dependent on any special
16461 * Switches for gnatprep::
16462 * Form of Definitions File::
16463 * Form of Input Text for gnatprep::
16466 @node Using gnatprep
16467 @section Using @code{gnatprep}
16470 To call @code{gnatprep} use
16473 $ gnatprep [switches] infile outfile [deffile]
16480 is an optional sequence of switches as described in the next section.
16483 is the full name of the input file, which is an Ada source
16484 file containing preprocessor directives.
16487 is the full name of the output file, which is an Ada source
16488 in standard Ada form. When used with GNAT, this file name will
16489 normally have an ads or adb suffix.
16492 is the full name of a text file containing definitions of
16493 symbols to be referenced by the preprocessor. This argument is
16494 optional, and can be replaced by the use of the @option{-D} switch.
16498 @node Switches for gnatprep
16499 @section Switches for @code{gnatprep}
16504 @item ^-b^/BLANK_LINES^
16505 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
16506 Causes both preprocessor lines and the lines deleted by
16507 preprocessing to be replaced by blank lines in the output source file,
16508 preserving line numbers in the output file.
16510 @item ^-c^/COMMENTS^
16511 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
16512 Causes both preprocessor lines and the lines deleted
16513 by preprocessing to be retained in the output source as comments marked
16514 with the special string @code{"--! "}. This option will result in line numbers
16515 being preserved in the output file.
16517 @item ^-C^/REPLACE_IN_COMMENTS^
16518 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
16519 Causes comments to be scanned. Normally comments are ignored by gnatprep.
16520 If this option is specified, then comments are scanned and any $symbol
16521 substitutions performed as in program text. This is particularly useful
16522 when structured comments are used (e.g. when writing programs in the
16523 SPARK dialect of Ada). Note that this switch is not available when
16524 doing integrated preprocessing (it would be useless in this context
16525 since comments are ignored by the compiler in any case).
16527 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
16528 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
16529 Defines a new symbol, associated with value. If no value is given on the
16530 command line, then symbol is considered to be @code{True}. This switch
16531 can be used in place of a definition file.
16535 @cindex @option{/REMOVE} (@command{gnatprep})
16536 This is the default setting which causes lines deleted by preprocessing
16537 to be entirely removed from the output file.
16540 @item ^-r^/REFERENCE^
16541 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
16542 Causes a @code{Source_Reference} pragma to be generated that
16543 references the original input file, so that error messages will use
16544 the file name of this original file. The use of this switch implies
16545 that preprocessor lines are not to be removed from the file, so its
16546 use will force @option{^-b^/BLANK_LINES^} mode if
16547 @option{^-c^/COMMENTS^}
16548 has not been specified explicitly.
16550 Note that if the file to be preprocessed contains multiple units, then
16551 it will be necessary to @code{gnatchop} the output file from
16552 @code{gnatprep}. If a @code{Source_Reference} pragma is present
16553 in the preprocessed file, it will be respected by
16554 @code{gnatchop ^-r^/REFERENCE^}
16555 so that the final chopped files will correctly refer to the original
16556 input source file for @code{gnatprep}.
16558 @item ^-s^/SYMBOLS^
16559 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
16560 Causes a sorted list of symbol names and values to be
16561 listed on the standard output file.
16563 @item ^-u^/UNDEFINED^
16564 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
16565 Causes undefined symbols to be treated as having the value FALSE in the context
16566 of a preprocessor test. In the absence of this option, an undefined symbol in
16567 a @code{#if} or @code{#elsif} test will be treated as an error.
16573 Note: if neither @option{-b} nor @option{-c} is present,
16574 then preprocessor lines and
16575 deleted lines are completely removed from the output, unless -r is
16576 specified, in which case -b is assumed.
16579 @node Form of Definitions File
16580 @section Form of Definitions File
16583 The definitions file contains lines of the form
16590 where symbol is an identifier, following normal Ada (case-insensitive)
16591 rules for its syntax, and value is one of the following:
16595 Empty, corresponding to a null substitution
16597 A string literal using normal Ada syntax
16599 Any sequence of characters from the set
16600 (letters, digits, period, underline).
16604 Comment lines may also appear in the definitions file, starting with
16605 the usual @code{--},
16606 and comments may be added to the definitions lines.
16608 @node Form of Input Text for gnatprep
16609 @section Form of Input Text for @code{gnatprep}
16612 The input text may contain preprocessor conditional inclusion lines,
16613 as well as general symbol substitution sequences.
16615 The preprocessor conditional inclusion commands have the form
16620 #if @i{expression} [then]
16622 #elsif @i{expression} [then]
16624 #elsif @i{expression} [then]
16635 In this example, @i{expression} is defined by the following grammar:
16637 @i{expression} ::= <symbol>
16638 @i{expression} ::= <symbol> = "<value>"
16639 @i{expression} ::= <symbol> = <symbol>
16640 @i{expression} ::= <symbol> 'Defined
16641 @i{expression} ::= not @i{expression}
16642 @i{expression} ::= @i{expression} and @i{expression}
16643 @i{expression} ::= @i{expression} or @i{expression}
16644 @i{expression} ::= @i{expression} and then @i{expression}
16645 @i{expression} ::= @i{expression} or else @i{expression}
16646 @i{expression} ::= ( @i{expression} )
16650 For the first test (@i{expression} ::= <symbol>) the symbol must have
16651 either the value true or false, that is to say the right-hand of the
16652 symbol definition must be one of the (case-insensitive) literals
16653 @code{True} or @code{False}. If the value is true, then the
16654 corresponding lines are included, and if the value is false, they are
16657 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
16658 the symbol has been defined in the definition file or by a @option{-D}
16659 switch on the command line. Otherwise, the test is false.
16661 The equality tests are case insensitive, as are all the preprocessor lines.
16663 If the symbol referenced is not defined in the symbol definitions file,
16664 then the effect depends on whether or not switch @option{-u}
16665 is specified. If so, then the symbol is treated as if it had the value
16666 false and the test fails. If this switch is not specified, then
16667 it is an error to reference an undefined symbol. It is also an error to
16668 reference a symbol that is defined with a value other than @code{True}
16671 The use of the @code{not} operator inverts the sense of this logical test, so
16672 that the lines are included only if the symbol is not defined.
16673 The @code{then} keyword is optional as shown
16675 The @code{#} must be the first non-blank character on a line, but
16676 otherwise the format is free form. Spaces or tabs may appear between
16677 the @code{#} and the keyword. The keywords and the symbols are case
16678 insensitive as in normal Ada code. Comments may be used on a
16679 preprocessor line, but other than that, no other tokens may appear on a
16680 preprocessor line. Any number of @code{elsif} clauses can be present,
16681 including none at all. The @code{else} is optional, as in Ada.
16683 The @code{#} marking the start of a preprocessor line must be the first
16684 non-blank character on the line, i.e. it must be preceded only by
16685 spaces or horizontal tabs.
16687 Symbol substitution outside of preprocessor lines is obtained by using
16695 anywhere within a source line, except in a comment or within a
16696 string literal. The identifier
16697 following the @code{$} must match one of the symbols defined in the symbol
16698 definition file, and the result is to substitute the value of the
16699 symbol in place of @code{$symbol} in the output file.
16701 Note that although the substitution of strings within a string literal
16702 is not possible, it is possible to have a symbol whose defined value is
16703 a string literal. So instead of setting XYZ to @code{hello} and writing:
16706 Header : String := "$XYZ";
16710 you should set XYZ to @code{"hello"} and write:
16713 Header : String := $XYZ;
16717 and then the substitution will occur as desired.
16720 @node The GNAT Run-Time Library Builder gnatlbr
16721 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
16723 @cindex Library builder
16726 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
16727 supplied configuration pragmas.
16730 * Running gnatlbr::
16731 * Switches for gnatlbr::
16732 * Examples of gnatlbr Usage::
16735 @node Running gnatlbr
16736 @section Running @code{gnatlbr}
16739 The @code{gnatlbr} command has the form
16742 $ GNAT LIBRARY /[CREATE | SET | DELETE]=directory [/CONFIG=file]
16745 @node Switches for gnatlbr
16746 @section Switches for @code{gnatlbr}
16749 @code{gnatlbr} recognizes the following switches:
16753 @item /CREATE=directory
16754 @cindex @code{/CREATE} (@code{gnatlbr})
16755 Create the new run-time library in the specified directory.
16757 @item /SET=directory
16758 @cindex @code{/SET} (@code{gnatlbr})
16759 Make the library in the specified directory the current run-time
16762 @item /DELETE=directory
16763 @cindex @code{/DELETE} (@code{gnatlbr})
16764 Delete the run-time library in the specified directory.
16767 @cindex @code{/CONFIG} (@code{gnatlbr})
16769 Use the configuration pragmas in the specified file when building
16773 Use the configuration pragmas in the specified file when compiling.
16777 @node Examples of gnatlbr Usage
16778 @section Example of @code{gnatlbr} Usage
16781 Contents of VAXFLOAT.ADC:
16782 pragma Float_Representation (VAX_Float);
16784 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
16786 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
16791 @node The GNAT Library Browser gnatls
16792 @chapter The GNAT Library Browser @code{gnatls}
16794 @cindex Library browser
16797 @code{gnatls} is a tool that outputs information about compiled
16798 units. It gives the relationship between objects, unit names and source
16799 files. It can also be used to check the source dependencies of a unit
16800 as well as various characteristics.
16804 * Switches for gnatls::
16805 * Examples of gnatls Usage::
16808 @node Running gnatls
16809 @section Running @code{gnatls}
16812 The @code{gnatls} command has the form
16815 $ gnatls switches @var{object_or_ali_file}
16819 The main argument is the list of object or @file{ali} files
16820 (@pxref{The Ada Library Information Files})
16821 for which information is requested.
16823 In normal mode, without additional option, @code{gnatls} produces a
16824 four-column listing. Each line represents information for a specific
16825 object. The first column gives the full path of the object, the second
16826 column gives the name of the principal unit in this object, the third
16827 column gives the status of the source and the fourth column gives the
16828 full path of the source representing this unit.
16829 Here is a simple example of use:
16833 ^./^[]^demo1.o demo1 DIF demo1.adb
16834 ^./^[]^demo2.o demo2 OK demo2.adb
16835 ^./^[]^hello.o h1 OK hello.adb
16836 ^./^[]^instr-child.o instr.child MOK instr-child.adb
16837 ^./^[]^instr.o instr OK instr.adb
16838 ^./^[]^tef.o tef DIF tef.adb
16839 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
16840 ^./^[]^tgef.o tgef DIF tgef.adb
16844 The first line can be interpreted as follows: the main unit which is
16846 object file @file{demo1.o} is demo1, whose main source is in
16847 @file{demo1.adb}. Furthermore, the version of the source used for the
16848 compilation of demo1 has been modified (DIF). Each source file has a status
16849 qualifier which can be:
16852 @item OK (unchanged)
16853 The version of the source file used for the compilation of the
16854 specified unit corresponds exactly to the actual source file.
16856 @item MOK (slightly modified)
16857 The version of the source file used for the compilation of the
16858 specified unit differs from the actual source file but not enough to
16859 require recompilation. If you use gnatmake with the qualifier
16860 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
16861 MOK will not be recompiled.
16863 @item DIF (modified)
16864 No version of the source found on the path corresponds to the source
16865 used to build this object.
16867 @item ??? (file not found)
16868 No source file was found for this unit.
16870 @item HID (hidden, unchanged version not first on PATH)
16871 The version of the source that corresponds exactly to the source used
16872 for compilation has been found on the path but it is hidden by another
16873 version of the same source that has been modified.
16877 @node Switches for gnatls
16878 @section Switches for @code{gnatls}
16881 @code{gnatls} recognizes the following switches:
16885 @item ^-a^/ALL_UNITS^
16886 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
16887 Consider all units, including those of the predefined Ada library.
16888 Especially useful with @option{^-d^/DEPENDENCIES^}.
16890 @item ^-d^/DEPENDENCIES^
16891 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
16892 List sources from which specified units depend on.
16894 @item ^-h^/OUTPUT=OPTIONS^
16895 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
16896 Output the list of options.
16898 @item ^-o^/OUTPUT=OBJECTS^
16899 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
16900 Only output information about object files.
16902 @item ^-s^/OUTPUT=SOURCES^
16903 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
16904 Only output information about source files.
16906 @item ^-u^/OUTPUT=UNITS^
16907 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
16908 Only output information about compilation units.
16910 @item ^-files^/FILES^=@var{file}
16911 @cindex @option{^-files^/FILES^} (@code{gnatls})
16912 Take as arguments the files listed in text file @var{file}.
16913 Text file @var{file} may contain empty lines that are ignored.
16914 Each non empty line should contain the name of an existing file.
16915 Several such switches may be specified simultaneously.
16917 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
16918 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
16919 @itemx ^-I^/SEARCH=^@var{dir}
16920 @itemx ^-I-^/NOCURRENT_DIRECTORY^
16922 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
16923 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
16924 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
16925 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
16926 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
16927 flags (@pxref{Switches for gnatmake}).
16929 @item --RTS=@var{rts-path}
16930 @cindex @option{--RTS} (@code{gnatls})
16931 Specifies the default location of the runtime library. Same meaning as the
16932 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
16934 @item ^-v^/OUTPUT=VERBOSE^
16935 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
16936 Verbose mode. Output the complete source, object and project paths. Do not use
16937 the default column layout but instead use long format giving as much as
16938 information possible on each requested units, including special
16939 characteristics such as:
16942 @item Preelaborable
16943 The unit is preelaborable in the Ada 95 sense.
16946 No elaboration code has been produced by the compiler for this unit.
16949 The unit is pure in the Ada 95 sense.
16951 @item Elaborate_Body
16952 The unit contains a pragma Elaborate_Body.
16955 The unit contains a pragma Remote_Types.
16957 @item Shared_Passive
16958 The unit contains a pragma Shared_Passive.
16961 This unit is part of the predefined environment and cannot be modified
16964 @item Remote_Call_Interface
16965 The unit contains a pragma Remote_Call_Interface.
16971 @node Examples of gnatls Usage
16972 @section Example of @code{gnatls} Usage
16976 Example of using the verbose switch. Note how the source and
16977 object paths are affected by the -I switch.
16980 $ gnatls -v -I.. demo1.o
16982 GNATLS 5.03w (20041123-34)
16983 Copyright 1997-2004 Free Software Foundation, Inc.
16985 Source Search Path:
16986 <Current_Directory>
16988 /home/comar/local/adainclude/
16990 Object Search Path:
16991 <Current_Directory>
16993 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
16995 Project Search Path:
16996 <Current_Directory>
16997 /home/comar/local/lib/gnat/
17002 Kind => subprogram body
17003 Flags => No_Elab_Code
17004 Source => demo1.adb modified
17008 The following is an example of use of the dependency list.
17009 Note the use of the -s switch
17010 which gives a straight list of source files. This can be useful for
17011 building specialized scripts.
17014 $ gnatls -d demo2.o
17015 ./demo2.o demo2 OK demo2.adb
17021 $ gnatls -d -s -a demo1.o
17023 /home/comar/local/adainclude/ada.ads
17024 /home/comar/local/adainclude/a-finali.ads
17025 /home/comar/local/adainclude/a-filico.ads
17026 /home/comar/local/adainclude/a-stream.ads
17027 /home/comar/local/adainclude/a-tags.ads
17030 /home/comar/local/adainclude/gnat.ads
17031 /home/comar/local/adainclude/g-io.ads
17033 /home/comar/local/adainclude/system.ads
17034 /home/comar/local/adainclude/s-exctab.ads
17035 /home/comar/local/adainclude/s-finimp.ads
17036 /home/comar/local/adainclude/s-finroo.ads
17037 /home/comar/local/adainclude/s-secsta.ads
17038 /home/comar/local/adainclude/s-stalib.ads
17039 /home/comar/local/adainclude/s-stoele.ads
17040 /home/comar/local/adainclude/s-stratt.ads
17041 /home/comar/local/adainclude/s-tasoli.ads
17042 /home/comar/local/adainclude/s-unstyp.ads
17043 /home/comar/local/adainclude/unchconv.ads
17049 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
17051 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
17052 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
17053 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
17054 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
17055 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
17059 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
17060 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
17062 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
17063 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
17064 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
17065 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
17066 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
17067 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
17068 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
17069 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
17070 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
17071 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
17072 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
17076 @node Cleaning Up Using gnatclean
17077 @chapter Cleaning Up Using @code{gnatclean}
17079 @cindex Cleaning tool
17082 @code{gnatclean} is a tool that allows the deletion of files produced by the
17083 compiler, binder and linker, including ALI files, object files, tree files,
17084 expanded source files, library files, interface copy source files, binder
17085 generated files and executable files.
17088 * Running gnatclean::
17089 * Switches for gnatclean::
17090 @c * Examples of gnatclean Usage::
17093 @node Running gnatclean
17094 @section Running @code{gnatclean}
17097 The @code{gnatclean} command has the form:
17100 $ gnatclean switches @var{names}
17104 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
17105 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
17106 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
17109 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17110 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
17111 the linker. In informative-only mode, specified by switch
17112 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
17113 normal mode is listed, but no file is actually deleted.
17115 @node Switches for gnatclean
17116 @section Switches for @code{gnatclean}
17119 @code{gnatclean} recognizes the following switches:
17123 @item ^-c^/COMPILER_FILES_ONLY^
17124 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
17125 Only attempt to delete the files produced by the compiler, not those produced
17126 by the binder or the linker. The files that are not to be deleted are library
17127 files, interface copy files, binder generated files and executable files.
17129 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
17130 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
17131 Indicate that ALI and object files should normally be found in directory
17134 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
17135 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
17136 When using project files, if some errors or warnings are detected during
17137 parsing and verbose mode is not in effect (no use of switch
17138 ^-v^/VERBOSE^), then error lines start with the full path name of the project
17139 file, rather than its simple file name.
17142 @cindex @option{^-h^/HELP^} (@code{gnatclean})
17143 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
17145 @item ^-n^/NODELETE^
17146 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
17147 Informative-only mode. Do not delete any files. Output the list of the files
17148 that would have been deleted if this switch was not specified.
17150 @item ^-P^/PROJECT_FILE=^@var{project}
17151 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
17152 Use project file @var{project}. Only one such switch can be used.
17153 When cleaning a project file, the files produced by the compilation of the
17154 immediate sources or inherited sources of the project files are to be
17155 deleted. This is not depending on the presence or not of executable names
17156 on the command line.
17159 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
17160 Quiet output. If there are no errors, do not output anything, except in
17161 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
17162 (switch ^-n^/NODELETE^).
17164 @item ^-r^/RECURSIVE^
17165 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
17166 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
17167 clean all imported and extended project files, recursively. If this switch
17168 is not specified, only the files related to the main project file are to be
17169 deleted. This switch has no effect if no project file is specified.
17171 @item ^-v^/VERBOSE^
17172 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
17175 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
17176 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
17177 Indicates the verbosity of the parsing of GNAT project files.
17178 @xref{Switches Related to Project Files}.
17180 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
17181 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
17182 Indicates that external variable @var{name} has the value @var{value}.
17183 The Project Manager will use this value for occurrences of
17184 @code{external(name)} when parsing the project file.
17185 @xref{Switches Related to Project Files}.
17187 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
17188 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
17189 When searching for ALI and object files, look in directory
17192 @item ^-I^/SEARCH=^@var{dir}
17193 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
17194 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
17196 @item ^-I-^/NOCURRENT_DIRECTORY^
17197 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
17198 @cindex Source files, suppressing search
17199 Do not look for ALI or object files in the directory
17200 where @code{gnatclean} was invoked.
17204 @c @node Examples of gnatclean Usage
17205 @c @section Examples of @code{gnatclean} Usage
17208 @node GNAT and Libraries
17209 @chapter GNAT and Libraries
17210 @cindex Library, building, installing, using
17213 This chapter describes how to build and use libraries with GNAT, and also shows
17214 how to recompile the GNAT run-time library. You should be familiar with the
17215 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
17219 * Introduction to Libraries in GNAT::
17220 * General Ada Libraries::
17221 * Stand-alone Ada Libraries::
17222 * Rebuilding the GNAT Run-Time Library::
17225 @node Introduction to Libraries in GNAT
17226 @section Introduction to Libraries in GNAT
17229 A library is, conceptually, a collection of objects which does not have its
17230 own main thread of execution, but rather provides certain services to the
17231 applications that use it. A library can be either statically linked with the
17232 application, in which case its code is directly included in the application,
17233 or, on platforms that support it, be dynamically linked, in which case
17234 its code is shared by all applications making use of this library.
17236 GNAT supports both types of libraries.
17237 In the static case, the compiled code can be provided in different ways. The
17238 simplest approach is to provide directly the set of objects resulting from
17239 compilation of the library source files. Alternatively, you can group the
17240 objects into an archive using whatever commands are provided by the operating
17241 system. For the latter case, the objects are grouped into a shared library.
17243 In the GNAT environment, a library has three types of components:
17249 @xref{The Ada Library Information Files}.
17251 Object files, an archive or a shared library.
17255 A GNAT library may expose all its source files, which is useful for
17256 documentation purposes. Alternatively, it may expose only the units needed by
17257 an external user to make use of the library. That is to say, the specs
17258 reflecting the library services along with all the units needed to compile
17259 those specs, which can include generic bodies or any body implementing an
17260 inlined routine. In the case of @emph{stand-alone libraries} those exposed
17261 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
17263 All compilation units comprising an application, including those in a library,
17264 need to be elaborated in an order partially defined by Ada's semantics. GNAT
17265 computes the elaboration order from the @file{ALI} files and this is why they
17266 constitute a mandatory part of GNAT libraries. Except in the case of
17267 @emph{stand-alone libraries}, where a specific library elaboration routine is
17268 produced independently of the application(s) using the library.
17270 @node General Ada Libraries
17271 @section General Ada Libraries
17274 * Building a library::
17275 * Installing a library::
17276 * Using a library::
17279 @node Building a library
17280 @subsection Building a library
17283 The easiest way to build a library is to use the Project Manager,
17284 which supports a special type of project called a @emph{Library Project}
17285 (@pxref{Library Projects}).
17287 A project is considered a library project, when two project-level attributes
17288 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
17289 control different aspects of library configuration, additional optional
17290 project-level attributes can be specified:
17293 This attribute controls whether the library is to be static or dynamic
17295 @item Library_Version
17296 This attribute specifies the library version; this value is used
17297 during dynamic linking of shared libraries to determine if the currently
17298 installed versions of the binaries are compatible.
17300 @item Library_Options
17302 These attributes specify additional low-level options to be used during
17303 library generation, and redefine the actual application used to generate
17308 The GNAT Project Manager takes full care of the library maintenance task,
17309 including recompilation of the source files for which objects do not exist
17310 or are not up to date, assembly of the library archive, and installation of
17311 the library (i.e., copying associated source, object and @file{ALI} files
17312 to the specified location).
17314 Here is a simple library project file:
17315 @smallexample @c ada
17317 for Source_Dirs use ("src1", "src2");
17318 for Object_Dir use "obj";
17319 for Library_Name use "mylib";
17320 for Library_Dir use "lib";
17321 for Library_Kind use "dynamic";
17326 and the compilation command to build and install the library:
17328 @smallexample @c ada
17329 $ gnatmake -Pmy_lib
17333 It is not entirely trivial to perform manually all the steps required to
17334 produce a library. We recommend that you use the GNAT Project Manager
17335 for this task. In special cases where this is not desired, the necessary
17336 steps are discussed below.
17338 There are various possibilities for compiling the units that make up the
17339 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
17340 with a conventional script. For simple libraries, it is also possible to create
17341 a dummy main program which depends upon all the packages that comprise the
17342 interface of the library. This dummy main program can then be given to
17343 @command{gnatmake}, which will ensure that all necessary objects are built.
17345 After this task is accomplished, you should follow the standard procedure
17346 of the underlying operating system to produce the static or shared library.
17348 Here is an example of such a dummy program:
17349 @smallexample @c ada
17351 with My_Lib.Service1;
17352 with My_Lib.Service2;
17353 with My_Lib.Service3;
17354 procedure My_Lib_Dummy is
17362 Here are the generic commands that will build an archive or a shared library.
17365 # compiling the library
17366 $ gnatmake -c my_lib_dummy.adb
17368 # we don't need the dummy object itself
17369 $ rm my_lib_dummy.o my_lib_dummy.ali
17371 # create an archive with the remaining objects
17372 $ ar rc libmy_lib.a *.o
17373 # some systems may require "ranlib" to be run as well
17375 # or create a shared library
17376 $ gcc -shared -o libmy_lib.so *.o
17377 # some systems may require the code to have been compiled with -fPIC
17379 # remove the object files that are now in the library
17382 # Make the ALI files read-only so that gnatmake will not try to
17383 # regenerate the objects that are in the library
17388 Please note that the library must have a name of the form @file{libxxx.a} or
17389 @file{libxxx.so} (or @file{libxxx.dll} on Windows) in order to be accessed by
17390 the directive @option{-lxxx} at link time.
17392 @node Installing a library
17393 @subsection Installing a library
17394 @cindex @code{ADA_PROJECT_PATH}
17397 If you use project files, library installation is part of the library build
17398 process. Thus no further action is needed in order to make use of the
17399 libraries that are built as part of the general application build. A usable
17400 version of the library is installed in the directory specified by the
17401 @code{Library_Dir} attribute of the library project file.
17403 You may want to install a library in a context different from where the library
17404 is built. This situation arises with third party suppliers, who may want
17405 to distribute a library in binary form where the user is not expected to be
17406 able to recompile the library. The simplest option in this case is to provide
17407 a project file slightly different from the one used to build the library, by
17408 using the @code{externally_built} attribute. For instance, the project
17409 file used to build the library in the previous section can be changed into the
17410 following one when the library is installed:
17412 @smallexample @c projectfile
17414 for Source_Dirs use ("src1", "src2");
17415 for Library_Name use "mylib";
17416 for Library_Dir use "lib";
17417 for Library_Kind use "dynamic";
17418 for Externally_Built use "true";
17423 This project file assumes that the directories @file{src1},
17424 @file{src2}, and @file{lib} exist in
17425 the directory containing the project file. The @code{externally_built}
17426 attribute makes it clear to the GNAT builder that it should not attempt to
17427 recompile any of the units from this library. It allows the library provider to
17428 restrict the source set to the minimum necessary for clients to make use of the
17429 library as described in the first section of this chapter. It is the
17430 responsibility of the library provider to install the necessary sources, ALI
17431 files and libraries in the directories mentioned in the project file. For
17432 convenience, the user's library project file should be installed in a location
17433 that will be searched automatically by the GNAT
17434 builder. These are the directories referenced in the @code{ADA_PROJECT_PATH}
17435 environment variable (@pxref{Importing Projects}), and also the default GNAT
17436 library location that can be queried with @command{gnatls -v} and is usually of
17437 the form $gnat_install_root/lib/gnat.
17439 When project files are not an option, it is also possible, but not recommended,
17440 to install the library so that the sources needed to use the library are on the
17441 Ada source path and the ALI files & libraries be on the Ada Object path (see
17442 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
17443 administrator can place general-purpose libraries in the default compiler
17444 paths, by specifying the libraries' location in the configuration files
17445 @file{ada_source_path} and @file{ada_object_path}. These configuration files
17446 must be located in the GNAT installation tree at the same place as the gcc spec
17447 file. The location of the gcc spec file can be determined as follows:
17453 The configuration files mentioned above have a simple format: each line
17454 must contain one unique directory name.
17455 Those names are added to the corresponding path
17456 in their order of appearance in the file. The names can be either absolute
17457 or relative; in the latter case, they are relative to where theses files
17460 The files @file{ada_source_path} and @file{ada_object_path} might not be
17462 GNAT installation, in which case, GNAT will look for its run-time library in
17463 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
17464 objects and @file{ALI} files). When the files exist, the compiler does not
17465 look in @file{adainclude} and @file{adalib}, and thus the
17466 @file{ada_source_path} file
17467 must contain the location for the GNAT run-time sources (which can simply
17468 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
17469 contain the location for the GNAT run-time objects (which can simply
17472 You can also specify a new default path to the run-time library at compilation
17473 time with the switch @option{--RTS=rts-path}. You can thus choose / change
17474 the run-time library you want your program to be compiled with. This switch is
17475 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
17476 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
17478 It is possible to install a library before or after the standard GNAT
17479 library, by reordering the lines in the configuration files. In general, a
17480 library must be installed before the GNAT library if it redefines
17483 @node Using a library
17484 @subsection Using a library
17486 @noindent Once again, the project facility greatly simplifies the use of
17487 libraries. In this context, using a library is just a matter of adding a
17488 @code{with} clause in the user project. For instance, to make use of the
17489 library @code{My_Lib} shown in examples in earlier sections, you can
17492 @smallexample @c projectfile
17499 Even if you have a third-party, non-Ada library, you can still use GNAT's
17500 Project Manager facility to provide a wrapper for it. For example, the
17501 following project, when @code{with}ed by your main project, will link with the
17502 third-party library @file{liba.a}:
17504 @smallexample @c projectfile
17507 for Externally_Built use "true";
17508 for Library_Dir use "lib";
17509 for Library_Name use "a";
17510 for Library_Kind use "static";
17514 This is an alternative to the use of @code{pragma Linker_Options}. It is
17515 especially interesting in the context of systems with several interdependent
17516 static libraries where finding a proper linker order is not easy and best be
17517 left to the tools having visibility over project dependence information.
17520 In order to use an Ada library manually, you need to make sure that this
17521 library is on both your source and object path
17522 (see @ref{Search Paths and the Run-Time Library (RTL)}
17523 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
17524 in an archive or a shared library, you need to specify the desired
17525 library at link time.
17527 For example, you can use the library @file{mylib} installed in
17528 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
17531 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
17536 This can be expressed more simply:
17541 when the following conditions are met:
17544 @file{/dir/my_lib_src} has been added by the user to the environment
17545 variable @code{ADA_INCLUDE_PATH}, or by the administrator to the file
17546 @file{ada_source_path}
17548 @file{/dir/my_lib_obj} has been added by the user to the environment
17549 variable @code{ADA_OBJECTS_PATH}, or by the administrator to the file
17550 @file{ada_object_path}
17552 a pragma @code{Linker_Options} has been added to one of the sources.
17555 @smallexample @c ada
17556 pragma Linker_Options ("-lmy_lib");
17560 @node Stand-alone Ada Libraries
17561 @section Stand-alone Ada Libraries
17562 @cindex Stand-alone library, building, using
17565 * Introduction to Stand-alone Libraries::
17566 * Building a Stand-alone Library::
17567 * Creating a Stand-alone Library to be used in a non-Ada context::
17568 * Restrictions in Stand-alone Libraries::
17571 @node Introduction to Stand-alone Libraries
17572 @subsection Introduction to Stand-alone Libraries
17575 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
17577 elaborate the Ada units that are included in the library. In contrast with
17578 an ordinary library, which consists of all sources, objects and @file{ALI}
17580 library, a SAL may specify a restricted subset of compilation units
17581 to serve as a library interface. In this case, the fully
17582 self-sufficient set of files will normally consist of an objects
17583 archive, the sources of interface units' specs, and the @file{ALI}
17584 files of interface units.
17585 If an interface spec contains a generic unit or an inlined subprogram,
17587 source must also be provided; if the units that must be provided in the source
17588 form depend on other units, the source and @file{ALI} files of those must
17591 The main purpose of a SAL is to minimize the recompilation overhead of client
17592 applications when a new version of the library is installed. Specifically,
17593 if the interface sources have not changed, client applications do not need to
17594 be recompiled. If, furthermore, a SAL is provided in the shared form and its
17595 version, controlled by @code{Library_Version} attribute, is not changed,
17596 then the clients do not need to be relinked.
17598 SALs also allow the library providers to minimize the amount of library source
17599 text exposed to the clients. Such ``information hiding'' might be useful or
17600 necessary for various reasons.
17602 Stand-alone libraries are also well suited to be used in an executable whose
17603 main routine is not written in Ada.
17605 @node Building a Stand-alone Library
17606 @subsection Building a Stand-alone Library
17609 GNAT's Project facility provides a simple way of building and installing
17610 stand-alone libraries; see @ref{Stand-alone Library Projects}.
17611 To be a Stand-alone Library Project, in addition to the two attributes
17612 that make a project a Library Project (@code{Library_Name} and
17613 @code{Library_Dir}; see @ref{Library Projects}), the attribute
17614 @code{Library_Interface} must be defined. For example:
17616 @smallexample @c projectfile
17618 for Library_Dir use "lib_dir";
17619 for Library_Name use "dummy";
17620 for Library_Interface use ("int1", "int1.child");
17625 Attribute @code{Library_Interface} has a non-empty string list value,
17626 each string in the list designating a unit contained in an immediate source
17627 of the project file.
17629 When a Stand-alone Library is built, first the binder is invoked to build
17630 a package whose name depends on the library name
17631 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
17632 This binder-generated package includes initialization and
17633 finalization procedures whose
17634 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
17636 above). The object corresponding to this package is included in the library.
17638 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
17639 calling of these procedures if a static SAL is built, or if a shared SAL
17641 with the project-level attribute @code{Library_Auto_Init} set to
17644 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
17645 (those that are listed in attribute @code{Library_Interface}) are copied to
17646 the Library Directory. As a consequence, only the Interface Units may be
17647 imported from Ada units outside of the library. If other units are imported,
17648 the binding phase will fail.
17650 The attribute @code{Library_Src_Dir} may be specified for a
17651 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
17652 single string value. Its value must be the path (absolute or relative to the
17653 project directory) of an existing directory. This directory cannot be the
17654 object directory or one of the source directories, but it can be the same as
17655 the library directory. The sources of the Interface
17656 Units of the library that are needed by an Ada client of the library will be
17657 copied to the designated directory, called the Interface Copy directory.
17658 These sources include the specs of the Interface Units, but they may also
17659 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
17660 are used, or when there is a generic unit in the spec. Before the sources
17661 are copied to the Interface Copy directory, an attempt is made to delete all
17662 files in the Interface Copy directory.
17664 Building stand-alone libraries by hand is somewhat tedious, but for those
17665 occasions when it is necessary here are the steps that you need to perform:
17668 Compile all library sources.
17671 Invoke the binder with the switch @option{-n} (No Ada main program),
17672 with all the @file{ALI} files of the interfaces, and
17673 with the switch @option{-L} to give specific names to the @code{init}
17674 and @code{final} procedures. For example:
17676 gnatbind -n int1.ali int2.ali -Lsal1
17680 Compile the binder generated file:
17686 Link the dynamic library with all the necessary object files,
17687 indicating to the linker the names of the @code{init} (and possibly
17688 @code{final}) procedures for automatic initialization (and finalization).
17689 The built library should be placed in a directory different from
17690 the object directory.
17693 Copy the @code{ALI} files of the interface to the library directory,
17694 add in this copy an indication that it is an interface to a SAL
17695 (i.e. add a word @option{SL} on the line in the @file{ALI} file that starts
17696 with letter ``P'') and make the modified copy of the @file{ALI} file
17701 Using SALs is not different from using other libraries
17702 (see @ref{Using a library}).
17704 @node Creating a Stand-alone Library to be used in a non-Ada context
17705 @subsection Creating a Stand-alone Library to be used in a non-Ada context
17708 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
17711 The only extra step required is to ensure that library interface subprograms
17712 are compatible with the main program, by means of @code{pragma Export}
17713 or @code{pragma Convention}.
17715 Here is an example of simple library interface for use with C main program:
17717 @smallexample @c ada
17718 package Interface is
17720 procedure Do_Something;
17721 pragma Export (C, Do_Something, "do_something");
17723 procedure Do_Something_Else;
17724 pragma Export (C, Do_Something_Else, "do_something_else");
17730 On the foreign language side, you must provide a ``foreign'' view of the
17731 library interface; remember that it should contain elaboration routines in
17732 addition to interface subprograms.
17734 The example below shows the content of @code{mylib_interface.h} (note
17735 that there is no rule for the naming of this file, any name can be used)
17737 /* the library elaboration procedure */
17738 extern void mylibinit (void);
17740 /* the library finalization procedure */
17741 extern void mylibfinal (void);
17743 /* the interface exported by the library */
17744 extern void do_something (void);
17745 extern void do_something_else (void);
17749 Libraries built as explained above can be used from any program, provided
17750 that the elaboration procedures (named @code{mylibinit} in the previous
17751 example) are called before the library services are used. Any number of
17752 libraries can be used simultaneously, as long as the elaboration
17753 procedure of each library is called.
17755 Below is an example of a C program that uses the @code{mylib} library.
17758 #include "mylib_interface.h"
17763 /* First, elaborate the library before using it */
17766 /* Main program, using the library exported entities */
17768 do_something_else ();
17770 /* Library finalization at the end of the program */
17777 Note that invoking any library finalization procedure generated by
17778 @code{gnatbind} shuts down the Ada run-time environment.
17780 finalization of all Ada libraries must be performed at the end of the program.
17781 No call to these libraries or to the Ada run-time library should be made
17782 after the finalization phase.
17784 @node Restrictions in Stand-alone Libraries
17785 @subsection Restrictions in Stand-alone Libraries
17788 The pragmas listed below should be used with caution inside libraries,
17789 as they can create incompatibilities with other Ada libraries:
17791 @item pragma @code{Locking_Policy}
17792 @item pragma @code{Queuing_Policy}
17793 @item pragma @code{Task_Dispatching_Policy}
17794 @item pragma @code{Unreserve_All_Interrupts}
17798 When using a library that contains such pragmas, the user must make sure
17799 that all libraries use the same pragmas with the same values. Otherwise,
17800 @code{Program_Error} will
17801 be raised during the elaboration of the conflicting
17802 libraries. The usage of these pragmas and its consequences for the user
17803 should therefore be well documented.
17805 Similarly, the traceback in the exception occurrence mechanism should be
17806 enabled or disabled in a consistent manner across all libraries.
17807 Otherwise, Program_Error will be raised during the elaboration of the
17808 conflicting libraries.
17810 If the @code{Version} or @code{Body_Version}
17811 attributes are used inside a library, then you need to
17812 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
17813 libraries, so that version identifiers can be properly computed.
17814 In practice these attributes are rarely used, so this is unlikely
17815 to be a consideration.
17817 @node Rebuilding the GNAT Run-Time Library
17818 @section Rebuilding the GNAT Run-Time Library
17819 @cindex GNAT Run-Time Library, rebuilding
17820 @cindex Building the GNAT Run-Time Library
17821 @cindex Rebuilding the GNAT Run-Time Library
17822 @cindex Run-Time Library, rebuilding
17825 It may be useful to recompile the GNAT library in various contexts, the
17826 most important one being the use of partition-wide configuration pragmas
17827 such as @code{Normalize_Scalars}. A special Makefile called
17828 @code{Makefile.adalib} is provided to that effect and can be found in
17829 the directory containing the GNAT library. The location of this
17830 directory depends on the way the GNAT environment has been installed and can
17831 be determined by means of the command:
17838 The last entry in the object search path usually contains the
17839 gnat library. This Makefile contains its own documentation and in
17840 particular the set of instructions needed to rebuild a new library and
17843 @node Using the GNU make Utility
17844 @chapter Using the GNU @code{make} Utility
17848 This chapter offers some examples of makefiles that solve specific
17849 problems. It does not explain how to write a makefile (see the GNU make
17850 documentation), nor does it try to replace the @command{gnatmake} utility
17851 (@pxref{The GNAT Make Program gnatmake}).
17853 All the examples in this section are specific to the GNU version of
17854 make. Although @code{make} is a standard utility, and the basic language
17855 is the same, these examples use some advanced features found only in
17859 * Using gnatmake in a Makefile::
17860 * Automatically Creating a List of Directories::
17861 * Generating the Command Line Switches::
17862 * Overcoming Command Line Length Limits::
17865 @node Using gnatmake in a Makefile
17866 @section Using gnatmake in a Makefile
17871 Complex project organizations can be handled in a very powerful way by
17872 using GNU make combined with gnatmake. For instance, here is a Makefile
17873 which allows you to build each subsystem of a big project into a separate
17874 shared library. Such a makefile allows you to significantly reduce the link
17875 time of very big applications while maintaining full coherence at
17876 each step of the build process.
17878 The list of dependencies are handled automatically by
17879 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
17880 the appropriate directories.
17882 Note that you should also read the example on how to automatically
17883 create the list of directories
17884 (@pxref{Automatically Creating a List of Directories})
17885 which might help you in case your project has a lot of subdirectories.
17890 @font@heightrm=cmr8
17893 ## This Makefile is intended to be used with the following directory
17895 ## - The sources are split into a series of csc (computer software components)
17896 ## Each of these csc is put in its own directory.
17897 ## Their name are referenced by the directory names.
17898 ## They will be compiled into shared library (although this would also work
17899 ## with static libraries
17900 ## - The main program (and possibly other packages that do not belong to any
17901 ## csc is put in the top level directory (where the Makefile is).
17902 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
17903 ## \_ second_csc (sources) __ lib (will contain the library)
17905 ## Although this Makefile is build for shared library, it is easy to modify
17906 ## to build partial link objects instead (modify the lines with -shared and
17909 ## With this makefile, you can change any file in the system or add any new
17910 ## file, and everything will be recompiled correctly (only the relevant shared
17911 ## objects will be recompiled, and the main program will be re-linked).
17913 # The list of computer software component for your project. This might be
17914 # generated automatically.
17917 # Name of the main program (no extension)
17920 # If we need to build objects with -fPIC, uncomment the following line
17923 # The following variable should give the directory containing libgnat.so
17924 # You can get this directory through 'gnatls -v'. This is usually the last
17925 # directory in the Object_Path.
17928 # The directories for the libraries
17929 # (This macro expands the list of CSC to the list of shared libraries, you
17930 # could simply use the expanded form :
17931 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17932 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17934 $@{MAIN@}: objects $@{LIB_DIR@}
17935 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17936 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17939 # recompile the sources
17940 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17942 # Note: In a future version of GNAT, the following commands will be simplified
17943 # by a new tool, gnatmlib
17945 mkdir -p $@{dir $@@ @}
17946 cd $@{dir $@@ @}; gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17947 cd $@{dir $@@ @}; cp -f ../*.ali .
17949 # The dependencies for the modules
17950 # Note that we have to force the expansion of *.o, since in some cases
17951 # make won't be able to do it itself.
17952 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17953 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17954 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17956 # Make sure all of the shared libraries are in the path before starting the
17959 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17962 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17963 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17964 $@{RM@} $@{CSC_LIST:%=%/*.o@}
17965 $@{RM@} *.o *.ali $@{MAIN@}
17968 @node Automatically Creating a List of Directories
17969 @section Automatically Creating a List of Directories
17972 In most makefiles, you will have to specify a list of directories, and
17973 store it in a variable. For small projects, it is often easier to
17974 specify each of them by hand, since you then have full control over what
17975 is the proper order for these directories, which ones should be
17978 However, in larger projects, which might involve hundreds of
17979 subdirectories, it might be more convenient to generate this list
17982 The example below presents two methods. The first one, although less
17983 general, gives you more control over the list. It involves wildcard
17984 characters, that are automatically expanded by @code{make}. Its
17985 shortcoming is that you need to explicitly specify some of the
17986 organization of your project, such as for instance the directory tree
17987 depth, whether some directories are found in a separate tree,...
17989 The second method is the most general one. It requires an external
17990 program, called @code{find}, which is standard on all Unix systems. All
17991 the directories found under a given root directory will be added to the
17997 @font@heightrm=cmr8
18000 # The examples below are based on the following directory hierarchy:
18001 # All the directories can contain any number of files
18002 # ROOT_DIRECTORY -> a -> aa -> aaa
18005 # -> b -> ba -> baa
18008 # This Makefile creates a variable called DIRS, that can be reused any time
18009 # you need this list (see the other examples in this section)
18011 # The root of your project's directory hierarchy
18015 # First method: specify explicitly the list of directories
18016 # This allows you to specify any subset of all the directories you need.
18019 DIRS := a/aa/ a/ab/ b/ba/
18022 # Second method: use wildcards
18023 # Note that the argument(s) to wildcard below should end with a '/'.
18024 # Since wildcards also return file names, we have to filter them out
18025 # to avoid duplicate directory names.
18026 # We thus use make's @code{dir} and @code{sort} functions.
18027 # It sets DIRs to the following value (note that the directories aaa and baa
18028 # are not given, unless you change the arguments to wildcard).
18029 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
18032 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
18033 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
18036 # Third method: use an external program
18037 # This command is much faster if run on local disks, avoiding NFS slowdowns.
18038 # This is the most complete command: it sets DIRs to the following value:
18039 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
18042 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
18046 @node Generating the Command Line Switches
18047 @section Generating the Command Line Switches
18050 Once you have created the list of directories as explained in the
18051 previous section (@pxref{Automatically Creating a List of Directories}),
18052 you can easily generate the command line arguments to pass to gnatmake.
18054 For the sake of completeness, this example assumes that the source path
18055 is not the same as the object path, and that you have two separate lists
18059 # see "Automatically creating a list of directories" to create
18064 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
18065 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
18068 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
18071 @node Overcoming Command Line Length Limits
18072 @section Overcoming Command Line Length Limits
18075 One problem that might be encountered on big projects is that many
18076 operating systems limit the length of the command line. It is thus hard to give
18077 gnatmake the list of source and object directories.
18079 This example shows how you can set up environment variables, which will
18080 make @command{gnatmake} behave exactly as if the directories had been
18081 specified on the command line, but have a much higher length limit (or
18082 even none on most systems).
18084 It assumes that you have created a list of directories in your Makefile,
18085 using one of the methods presented in
18086 @ref{Automatically Creating a List of Directories}.
18087 For the sake of completeness, we assume that the object
18088 path (where the ALI files are found) is different from the sources patch.
18090 Note a small trick in the Makefile below: for efficiency reasons, we
18091 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
18092 expanded immediately by @code{make}. This way we overcome the standard
18093 make behavior which is to expand the variables only when they are
18096 On Windows, if you are using the standard Windows command shell, you must
18097 replace colons with semicolons in the assignments to these variables.
18102 @font@heightrm=cmr8
18105 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
18106 # This is the same thing as putting the -I arguments on the command line.
18107 # (the equivalent of using -aI on the command line would be to define
18108 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
18109 # You can of course have different values for these variables.
18111 # Note also that we need to keep the previous values of these variables, since
18112 # they might have been set before running 'make' to specify where the GNAT
18113 # library is installed.
18115 # see "Automatically creating a list of directories" to create these
18121 space:=$@{empty@} $@{empty@}
18122 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
18123 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
18124 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
18125 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
18126 export ADA_INCLUDE_PATH
18127 export ADA_OBJECT_PATH
18134 @node Memory Management Issues
18135 @chapter Memory Management Issues
18138 This chapter describes some useful memory pools provided in the GNAT library
18139 and in particular the GNAT Debug Pool facility, which can be used to detect
18140 incorrect uses of access values (including ``dangling references'').
18142 It also describes the @command{gnatmem} tool, which can be used to track down
18147 * Some Useful Memory Pools::
18148 * The GNAT Debug Pool Facility::
18150 * The gnatmem Tool::
18154 @node Some Useful Memory Pools
18155 @section Some Useful Memory Pools
18156 @findex Memory Pool
18157 @cindex storage, pool
18160 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
18161 storage pool. Allocations use the standard system call @code{malloc} while
18162 deallocations use the standard system call @code{free}. No reclamation is
18163 performed when the pool goes out of scope. For performance reasons, the
18164 standard default Ada allocators/deallocators do not use any explicit storage
18165 pools but if they did, they could use this storage pool without any change in
18166 behavior. That is why this storage pool is used when the user
18167 manages to make the default implicit allocator explicit as in this example:
18168 @smallexample @c ada
18169 type T1 is access Something;
18170 -- no Storage pool is defined for T2
18171 type T2 is access Something_Else;
18172 for T2'Storage_Pool use T1'Storage_Pool;
18173 -- the above is equivalent to
18174 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
18178 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
18179 pool. The allocation strategy is similar to @code{Pool_Local}'s
18180 except that the all
18181 storage allocated with this pool is reclaimed when the pool object goes out of
18182 scope. This pool provides a explicit mechanism similar to the implicit one
18183 provided by several Ada 83 compilers for allocations performed through a local
18184 access type and whose purpose was to reclaim memory when exiting the
18185 scope of a given local access. As an example, the following program does not
18186 leak memory even though it does not perform explicit deallocation:
18188 @smallexample @c ada
18189 with System.Pool_Local;
18190 procedure Pooloc1 is
18191 procedure Internal is
18192 type A is access Integer;
18193 X : System.Pool_Local.Unbounded_Reclaim_Pool;
18194 for A'Storage_Pool use X;
18197 for I in 1 .. 50 loop
18202 for I in 1 .. 100 loop
18209 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
18210 @code{Storage_Size} is specified for an access type.
18211 The whole storage for the pool is
18212 allocated at once, usually on the stack at the point where the access type is
18213 elaborated. It is automatically reclaimed when exiting the scope where the
18214 access type is defined. This package is not intended to be used directly by the
18215 user and it is implicitly used for each such declaration:
18217 @smallexample @c ada
18218 type T1 is access Something;
18219 for T1'Storage_Size use 10_000;
18223 @node The GNAT Debug Pool Facility
18224 @section The GNAT Debug Pool Facility
18226 @cindex storage, pool, memory corruption
18229 The use of unchecked deallocation and unchecked conversion can easily
18230 lead to incorrect memory references. The problems generated by such
18231 references are usually difficult to tackle because the symptoms can be
18232 very remote from the origin of the problem. In such cases, it is
18233 very helpful to detect the problem as early as possible. This is the
18234 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
18236 In order to use the GNAT specific debugging pool, the user must
18237 associate a debug pool object with each of the access types that may be
18238 related to suspected memory problems. See Ada Reference Manual 13.11.
18239 @smallexample @c ada
18240 type Ptr is access Some_Type;
18241 Pool : GNAT.Debug_Pools.Debug_Pool;
18242 for Ptr'Storage_Pool use Pool;
18246 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
18247 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
18248 allow the user to redefine allocation and deallocation strategies. They
18249 also provide a checkpoint for each dereference, through the use of
18250 the primitive operation @code{Dereference} which is implicitly called at
18251 each dereference of an access value.
18253 Once an access type has been associated with a debug pool, operations on
18254 values of the type may raise four distinct exceptions,
18255 which correspond to four potential kinds of memory corruption:
18258 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
18260 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
18262 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
18264 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
18268 For types associated with a Debug_Pool, dynamic allocation is performed using
18269 the standard GNAT allocation routine. References to all allocated chunks of
18270 memory are kept in an internal dictionary. Several deallocation strategies are
18271 provided, whereupon the user can choose to release the memory to the system,
18272 keep it allocated for further invalid access checks, or fill it with an easily
18273 recognizable pattern for debug sessions. The memory pattern is the old IBM
18274 hexadecimal convention: @code{16#DEADBEEF#}.
18276 See the documentation in the file g-debpoo.ads for more information on the
18277 various strategies.
18279 Upon each dereference, a check is made that the access value denotes a
18280 properly allocated memory location. Here is a complete example of use of
18281 @code{Debug_Pools}, that includes typical instances of memory corruption:
18282 @smallexample @c ada
18286 with Gnat.Io; use Gnat.Io;
18287 with Unchecked_Deallocation;
18288 with Unchecked_Conversion;
18289 with GNAT.Debug_Pools;
18290 with System.Storage_Elements;
18291 with Ada.Exceptions; use Ada.Exceptions;
18292 procedure Debug_Pool_Test is
18294 type T is access Integer;
18295 type U is access all T;
18297 P : GNAT.Debug_Pools.Debug_Pool;
18298 for T'Storage_Pool use P;
18300 procedure Free is new Unchecked_Deallocation (Integer, T);
18301 function UC is new Unchecked_Conversion (U, T);
18304 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
18314 Put_Line (Integer'Image(B.all));
18316 when E : others => Put_Line ("raised: " & Exception_Name (E));
18321 when E : others => Put_Line ("raised: " & Exception_Name (E));
18325 Put_Line (Integer'Image(B.all));
18327 when E : others => Put_Line ("raised: " & Exception_Name (E));
18332 when E : others => Put_Line ("raised: " & Exception_Name (E));
18335 end Debug_Pool_Test;
18339 The debug pool mechanism provides the following precise diagnostics on the
18340 execution of this erroneous program:
18343 Total allocated bytes : 0
18344 Total deallocated bytes : 0
18345 Current Water Mark: 0
18349 Total allocated bytes : 8
18350 Total deallocated bytes : 0
18351 Current Water Mark: 8
18354 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
18355 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
18356 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
18357 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
18359 Total allocated bytes : 8
18360 Total deallocated bytes : 4
18361 Current Water Mark: 4
18366 @node The gnatmem Tool
18367 @section The @command{gnatmem} Tool
18371 The @code{gnatmem} utility monitors dynamic allocation and
18372 deallocation activity in a program, and displays information about
18373 incorrect deallocations and possible sources of memory leaks.
18374 It provides three type of information:
18377 General information concerning memory management, such as the total
18378 number of allocations and deallocations, the amount of allocated
18379 memory and the high water mark, i.e. the largest amount of allocated
18380 memory in the course of program execution.
18383 Backtraces for all incorrect deallocations, that is to say deallocations
18384 which do not correspond to a valid allocation.
18387 Information on each allocation that is potentially the origin of a memory
18392 * Running gnatmem::
18393 * Switches for gnatmem::
18394 * Example of gnatmem Usage::
18397 @node Running gnatmem
18398 @subsection Running @code{gnatmem}
18401 @code{gnatmem} makes use of the output created by the special version of
18402 allocation and deallocation routines that record call information. This
18403 allows to obtain accurate dynamic memory usage history at a minimal cost to
18404 the execution speed. Note however, that @code{gnatmem} is not supported on
18405 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux x86,
18406 32-bit Solaris (sparc and x86) and Windows NT/2000/XP (x86).
18409 The @code{gnatmem} command has the form
18412 $ gnatmem [switches] user_program
18416 The program must have been linked with the instrumented version of the
18417 allocation and deallocation routines. This is done by linking with the
18418 @file{libgmem.a} library. For correct symbolic backtrace information,
18419 the user program should be compiled with debugging options
18420 (see @ref{Switches for gcc}). For example to build @file{my_program}:
18423 $ gnatmake -g my_program -largs -lgmem
18427 When @file{my_program} is executed, the file @file{gmem.out} is produced.
18428 This file contains information about all allocations and deallocations
18429 performed by the program. It is produced by the instrumented allocations and
18430 deallocations routines and will be used by @code{gnatmem}.
18432 In order to produce symbolic backtrace information for allocations and
18433 deallocations performed by the GNAT run-time library, you need to use a
18434 version of that library that has been compiled with the @option{-g} switch
18435 (see @ref{Rebuilding the GNAT Run-Time Library}).
18437 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
18438 examine. If the location of @file{gmem.out} file was not explicitly supplied by
18439 @code{-i} switch, gnatmem will assume that this file can be found in the
18440 current directory. For example, after you have executed @file{my_program},
18441 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
18444 $ gnatmem my_program
18448 This will produce the output with the following format:
18450 *************** debut cc
18452 $ gnatmem my_program
18456 Total number of allocations : 45
18457 Total number of deallocations : 6
18458 Final Water Mark (non freed mem) : 11.29 Kilobytes
18459 High Water Mark : 11.40 Kilobytes
18464 Allocation Root # 2
18465 -------------------
18466 Number of non freed allocations : 11
18467 Final Water Mark (non freed mem) : 1.16 Kilobytes
18468 High Water Mark : 1.27 Kilobytes
18470 my_program.adb:23 my_program.alloc
18476 The first block of output gives general information. In this case, the
18477 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
18478 Unchecked_Deallocation routine occurred.
18481 Subsequent paragraphs display information on all allocation roots.
18482 An allocation root is a specific point in the execution of the program
18483 that generates some dynamic allocation, such as a ``@code{@b{new}}''
18484 construct. This root is represented by an execution backtrace (or subprogram
18485 call stack). By default the backtrace depth for allocations roots is 1, so
18486 that a root corresponds exactly to a source location. The backtrace can
18487 be made deeper, to make the root more specific.
18489 @node Switches for gnatmem
18490 @subsection Switches for @code{gnatmem}
18493 @code{gnatmem} recognizes the following switches:
18498 @cindex @option{-q} (@code{gnatmem})
18499 Quiet. Gives the minimum output needed to identify the origin of the
18500 memory leaks. Omits statistical information.
18503 @cindex @var{N} (@code{gnatmem})
18504 N is an integer literal (usually between 1 and 10) which controls the
18505 depth of the backtraces defining allocation root. The default value for
18506 N is 1. The deeper the backtrace, the more precise the localization of
18507 the root. Note that the total number of roots can depend on this
18508 parameter. This parameter must be specified @emph{before} the name of the
18509 executable to be analyzed, to avoid ambiguity.
18512 @cindex @option{-b} (@code{gnatmem})
18513 This switch has the same effect as just depth parameter.
18515 @item -i @var{file}
18516 @cindex @option{-i} (@code{gnatmem})
18517 Do the @code{gnatmem} processing starting from @file{file}, rather than
18518 @file{gmem.out} in the current directory.
18521 @cindex @option{-m} (@code{gnatmem})
18522 This switch causes @code{gnatmem} to mask the allocation roots that have less
18523 than n leaks. The default value is 1. Specifying the value of 0 will allow to
18524 examine even the roots that didn't result in leaks.
18527 @cindex @option{-s} (@code{gnatmem})
18528 This switch causes @code{gnatmem} to sort the allocation roots according to the
18529 specified order of sort criteria, each identified by a single letter. The
18530 currently supported criteria are @code{n, h, w} standing respectively for
18531 number of unfreed allocations, high watermark, and final watermark
18532 corresponding to a specific root. The default order is @code{nwh}.
18536 @node Example of gnatmem Usage
18537 @subsection Example of @code{gnatmem} Usage
18540 The following example shows the use of @code{gnatmem}
18541 on a simple memory-leaking program.
18542 Suppose that we have the following Ada program:
18544 @smallexample @c ada
18547 with Unchecked_Deallocation;
18548 procedure Test_Gm is
18550 type T is array (1..1000) of Integer;
18551 type Ptr is access T;
18552 procedure Free is new Unchecked_Deallocation (T, Ptr);
18555 procedure My_Alloc is
18560 procedure My_DeAlloc is
18568 for I in 1 .. 5 loop
18569 for J in I .. 5 loop
18580 The program needs to be compiled with debugging option and linked with
18581 @code{gmem} library:
18584 $ gnatmake -g test_gm -largs -lgmem
18588 Then we execute the program as usual:
18595 Then @code{gnatmem} is invoked simply with
18601 which produces the following output (result may vary on different platforms):
18606 Total number of allocations : 18
18607 Total number of deallocations : 5
18608 Final Water Mark (non freed mem) : 53.00 Kilobytes
18609 High Water Mark : 56.90 Kilobytes
18611 Allocation Root # 1
18612 -------------------
18613 Number of non freed allocations : 11
18614 Final Water Mark (non freed mem) : 42.97 Kilobytes
18615 High Water Mark : 46.88 Kilobytes
18617 test_gm.adb:11 test_gm.my_alloc
18619 Allocation Root # 2
18620 -------------------
18621 Number of non freed allocations : 1
18622 Final Water Mark (non freed mem) : 10.02 Kilobytes
18623 High Water Mark : 10.02 Kilobytes
18625 s-secsta.adb:81 system.secondary_stack.ss_init
18627 Allocation Root # 3
18628 -------------------
18629 Number of non freed allocations : 1
18630 Final Water Mark (non freed mem) : 12 Bytes
18631 High Water Mark : 12 Bytes
18633 s-secsta.adb:181 system.secondary_stack.ss_init
18637 Note that the GNAT run time contains itself a certain number of
18638 allocations that have no corresponding deallocation,
18639 as shown here for root #2 and root
18640 #3. This is a normal behavior when the number of non freed allocations
18641 is one, it allocates dynamic data structures that the run time needs for
18642 the complete lifetime of the program. Note also that there is only one
18643 allocation root in the user program with a single line back trace:
18644 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
18645 program shows that 'My_Alloc' is called at 2 different points in the
18646 source (line 21 and line 24). If those two allocation roots need to be
18647 distinguished, the backtrace depth parameter can be used:
18650 $ gnatmem 3 test_gm
18654 which will give the following output:
18659 Total number of allocations : 18
18660 Total number of deallocations : 5
18661 Final Water Mark (non freed mem) : 53.00 Kilobytes
18662 High Water Mark : 56.90 Kilobytes
18664 Allocation Root # 1
18665 -------------------
18666 Number of non freed allocations : 10
18667 Final Water Mark (non freed mem) : 39.06 Kilobytes
18668 High Water Mark : 42.97 Kilobytes
18670 test_gm.adb:11 test_gm.my_alloc
18671 test_gm.adb:24 test_gm
18672 b_test_gm.c:52 main
18674 Allocation Root # 2
18675 -------------------
18676 Number of non freed allocations : 1
18677 Final Water Mark (non freed mem) : 10.02 Kilobytes
18678 High Water Mark : 10.02 Kilobytes
18680 s-secsta.adb:81 system.secondary_stack.ss_init
18681 s-secsta.adb:283 <system__secondary_stack___elabb>
18682 b_test_gm.c:33 adainit
18684 Allocation Root # 3
18685 -------------------
18686 Number of non freed allocations : 1
18687 Final Water Mark (non freed mem) : 3.91 Kilobytes
18688 High Water Mark : 3.91 Kilobytes
18690 test_gm.adb:11 test_gm.my_alloc
18691 test_gm.adb:21 test_gm
18692 b_test_gm.c:52 main
18694 Allocation Root # 4
18695 -------------------
18696 Number of non freed allocations : 1
18697 Final Water Mark (non freed mem) : 12 Bytes
18698 High Water Mark : 12 Bytes
18700 s-secsta.adb:181 system.secondary_stack.ss_init
18701 s-secsta.adb:283 <system__secondary_stack___elabb>
18702 b_test_gm.c:33 adainit
18706 The allocation root #1 of the first example has been split in 2 roots #1
18707 and #3 thanks to the more precise associated backtrace.
18711 @node Stack Related Facilities
18712 @chapter Stack Related Facilities
18715 This chapter describes some useful tools associated with stack
18716 checking and analysis. In
18717 particular, it deals with dynamic and static stack usage measurements.
18720 * Stack Overflow Checking::
18721 * Static Stack Usage Analysis::
18722 * Dynamic Stack Usage Analysis::
18725 @node Stack Overflow Checking
18726 @section Stack Overflow Checking
18727 @cindex Stack Overflow Checking
18728 @cindex -fstack-check
18731 For most operating systems, @command{gcc} does not perform stack overflow
18732 checking by default. This means that if the main environment task or
18733 some other task exceeds the available stack space, then unpredictable
18734 behavior will occur. Most native systems offer some level of protection by
18735 adding a guard page at the end of each task stack. This mechanism is usually
18736 not enough for dealing properly with stack overflow situations because
18737 a large local variable could ``jump'' above the guard page.
18738 Furthermore, when the
18739 guard page is hit, there may not be any space left on the stack for executing
18740 the exception propagation code. Enabling stack checking avoids
18743 To activate stack checking, compile all units with the gcc option
18744 @option{-fstack-check}. For example:
18747 gcc -c -fstack-check package1.adb
18751 Units compiled with this option will generate extra instructions to check
18752 that any use of the stack (for procedure calls or for declaring local
18753 variables in declare blocks) does not exceed the available stack space.
18754 If the space is exceeded, then a @code{Storage_Error} exception is raised.
18756 For declared tasks, the stack size is controlled by the size
18757 given in an applicable @code{Storage_Size} pragma or by the value specified
18758 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
18759 the default size as defined in the GNAT runtime otherwise.
18761 For the environment task, the stack size depends on
18762 system defaults and is unknown to the compiler. Stack checking
18763 may still work correctly if a fixed
18764 size stack is allocated, but this cannot be guaranteed.
18765 To ensure that a clean exception is signalled for stack
18766 overflow, set the environment variable
18767 @code{GNAT_STACK_LIMIT} to indicate the maximum
18768 stack area that can be used, as in:
18769 @cindex GNAT_STACK_LIMIT
18772 SET GNAT_STACK_LIMIT 1600
18776 The limit is given in kilobytes, so the above declaration would
18777 set the stack limit of the environment task to 1.6 megabytes.
18778 Note that the only purpose of this usage is to limit the amount
18779 of stack used by the environment task. If it is necessary to
18780 increase the amount of stack for the environment task, then this
18781 is an operating systems issue, and must be addressed with the
18782 appropriate operating systems commands.
18784 @node Static Stack Usage Analysis
18785 @section Static Stack Usage Analysis
18786 @cindex Static Stack Usage Analysis
18787 @cindex -fstack-usage
18790 A unit compiled with @option{-fstack-usage} will generate an extra file
18792 the maximum amount of stack used, on a per-function basis.
18793 The file has the same
18794 basename as the target object file with a @file{.su} extension.
18795 Each line of this file is made up of three fields:
18799 The name of the function.
18803 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
18806 The second field corresponds to the size of the known part of the function
18809 The qualifier @code{static} means that the function frame size
18811 It usually means that all local variables have a static size.
18812 In this case, the second field is a reliable measure of the function stack
18815 The qualifier @code{dynamic} means that the function frame size is not static.
18816 It happens mainly when some local variables have a dynamic size. When this
18817 qualifier appears alone, the second field is not a reliable measure
18818 of the function stack analysis. When it is qualified with @code{bounded}, it
18819 means that the second field is a reliable maximum of the function stack
18822 @node Dynamic Stack Usage Analysis
18823 @section Dynamic Stack Usage Analysis
18826 It is possible to measure the maximum amount of stack used by a task, by
18827 adding a switch to @command{gnatbind}, as:
18830 $ gnatbind -u0 file
18834 With this option, at each task termination, its stack usage is output on
18836 It is not always convenient to output the stack usage when the program
18837 is still running. Hence, it is possible to delay this output until program
18838 termination. for a given number of tasks specified as the argument of the
18839 @code{-u} option. For instance:
18842 $ gnatbind -u100 file
18846 will buffer the stack usage information of the first 100 tasks to terminate and
18847 output this info at program termination. Results are displayed in four
18851 Index | Task Name | Stack Size | Actual Use
18858 is a number associated with each task.
18861 is the name of the task analyzed.
18864 is the maximum size for the stack. In order to prevent overflow,
18865 the real stack limit is slightly larger than the Stack Size in order to allow
18869 is the measure done by the stack analyzer.
18874 The environment task stack, e.g. the stack that contains the main unit, is
18875 only processed when the environment variable GNAT_STACK_LIMIT is set.
18877 @c *********************************
18878 @node Verifying properties using gnatcheck
18879 @chapter Verifying properties using @command{gnatcheck}
18883 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
18884 of Ada source files according to a given set of semantic rules.
18886 In order to check compliance with a given rule, @command{gnatcheck} has to
18887 semantically analyze the Ada sources.
18888 Therefore, checks can only be performed on
18889 legal Ada units. Moreover, when a unit depends semantically upon units located
18890 outside the current directory, the source search path has to be provided when
18891 calling @command{gnatcheck}, either through a specified project file or
18892 through @command{gnatcheck} switches as described below.
18894 The project support for @command{gnatcheck} is provided by the @command{gnat}
18897 Several rules are already implemented in @command{gnatcheck}. The list of such
18898 rules can be obtained with option @option{^-h^/HELP^} as described in the next
18899 section. A user can add new rules by modifying the @command{gnatcheck} code and
18900 rebuilding the tool. For adding a simple rule making some local checks, a small
18901 amount of straightforward ASIS-based programming is usually needed.
18904 @command{gnatcheck} has the command-line interface of the form
18907 $ gnatcheck [@i{switches}] @{@i{filename}@} [@i{^-files^/FILES^=@{arg_list_filename@}}]
18908 [@i{-cargs gcc_switches}] [@i{-rules rule_options}]
18916 @i{switches} specify the general tool options
18919 Each @i{filename} is the name (including the extension) of a source
18920 file to process. ``Wildcards'' are allowed, and
18921 the file name may contain path information.
18924 Each @i{arg_list_filename} is the name (including the extension) of a text
18925 file containing the names of the source files to process, separated by spaces
18929 @i{-cargs gcc_switches} is a list of switches for
18930 @command{gcc}. They will be passed on to all compiler invocations made by
18931 @command{gnatcheck} to generate the ASIS trees. Here you can provide
18932 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
18933 and use the @option{-gnatec} switch to set the configuration file.
18936 @i{-rules rule_options} is a list of options for controlling a set of
18937 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options})
18941 Either a @i{filename} or an @i{arg_list_filename} needs to be supplied.
18944 * Format of the Report File::
18945 * General gnatcheck Switches::
18946 * gnatcheck Rule Options::
18947 * Add the Results of Compiler Checks to gnatcheck Output::
18950 @node Format of the Report File
18951 @section Format of the Report File
18954 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
18956 It also creates, in the current
18957 directory, a text file named @file{^gnatcheck.out^GNATCHECK.OUT^} that
18958 contains the complete report of the last gnatcheck run. This report contains:
18960 @item a list of the Ada source files being checked,
18961 @item a list of enabled and disabled rules,
18962 @item a list of the diagnostic messages, ordered in three different ways
18963 and collected in three separate
18964 sections. Section 1 contains the raw list of diagnostic messages. It
18965 corresponds to the output going to @file{stdout}. Section 2 contains
18966 messages ordered by rules.
18967 Section 3 contains messages ordered by source files.
18971 @node General gnatcheck Switches
18972 @section General @command{gnatcheck} Switches
18975 The following switches control the general @command{gnatcheck} behavior
18978 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
18980 Process all units including those with read-only ALI files such as
18981 those from GNAT Run-Time library.
18983 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
18985 Print out the list of the currently implemented rules. For more details see
18986 the README file in the @command{gnatcheck} sources.
18988 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
18990 Use full source locations references in the report file. For a construct from
18991 a generic instantiation a full source location is a chain from the location
18992 of this construct in the generic unit to the place where this unit is
18995 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
18997 Quiet mode. All the diagnoses about rule violations are placed in the
18998 @command{gnatcheck} report file only, without duplicating in @file{stdout}.
19000 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
19002 Short format of the report file (no version information, no list of applied
19003 rules, no list of checked sources is included)
19005 @cindex @option{^-s1^/COMPILER_STYLE^} (@command{gnatcheck})
19006 @item ^-s1^/COMPILER_STYLE^
19007 Include the compiler-style section in the report file
19009 @cindex @option{^-s2^/BY_RULES^} (@command{gnatcheck})
19010 @item ^-s2^/BY_RULES^
19011 Include the section containing diagnoses ordered by rules in the report file
19013 @cindex @option{^-s3^/BY_FILES_BY_RULES^} (@command{gnatcheck})
19014 @item ^-s3^/BY_FILES_BY_RULES^
19015 Include the section containing diagnoses ordered by files and then by rules
19018 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
19019 @item ^-v^/VERBOSE^
19020 Verbose mode; @command{gnatcheck} generates version information and then
19021 a trace of sources being processed.
19026 Note, that if either of the options @option{^-s1^/COMPILER_STYLE^},
19027 @option{^-s2^/BY_RULES^} or
19028 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
19029 then the @command{gnatcheck} report file will contain only sections
19030 explicitly stated by these options.
19032 @node gnatcheck Rule Options
19033 @section @command{gnatcheck} Rule Options
19036 The following options control the processing performed by
19037 @command{gnatcheck}.
19040 @cindex @option{+ALL} (@command{gnatcheck})
19042 Turn all the rule checks ON
19044 @cindex @option{-ALL} (@command{gnatcheck})
19046 Turn all the rule checks OFF
19048 @cindex @option{+R} (@command{gnatcheck})
19049 @item +R@i{rule_id[:param]}
19050 Turn on the check for a specified rule with the specified parameter, if any.
19051 @i{rule_id} should be the identifier of one of the currently implemented rules
19052 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
19053 are not case-sensitive. The @i{:param} item should
19054 be a string representing a valid parameter(s) for the specified rule.
19055 If it contains any space characters then this string must be enclosed in
19058 @cindex @option{-R} (@command{gnatcheck})
19059 @item -R@i{rule_id}
19060 Turn off the check for a specified rule
19064 @node Add the Results of Compiler Checks to gnatcheck Output
19065 @section Add the Results of Compiler Checks to @command{gnatcheck} Output
19068 The @command{gnatcheck} tool can include in the generated diagnostic messages
19070 the report file the results of the checks performed by the compiler. Though
19071 disabled by default, this effect may be obtained by using @option{+R} with
19072 the following rule identifiers and parameters:
19076 To record restrictions violations (that are performed by the compiler if the
19077 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
19079 @i{Restrictions} with the same parameters as pragma
19080 @code{Restrictions} or @code{Restriction_Warnings}
19083 To record compiler style checks, use the rule named
19084 @i{Style_Checks}. A parameter of this rule can be either @i{All_Checks}, that
19085 turns ON all the style checks, or a string that has exactly the same structure
19086 and semantics as @code{string_LITERAL} parameter of GNAT pragma
19087 @code{Style_Checks}.
19090 To record compiler warnings (@pxref{Warning Message Control}), use the rule
19091 named @i{Warnings} with a parameter that is a valid
19092 @code{static_string_expression} argument of GNAT pragma @code{Warnings}.
19096 @c *********************************
19097 @node Creating Sample Bodies Using gnatstub
19098 @chapter Creating Sample Bodies Using @command{gnatstub}
19102 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
19103 for library unit declarations.
19105 To create a body stub, @command{gnatstub} has to compile the library
19106 unit declaration. Therefore, bodies can be created only for legal
19107 library units. Moreover, if a library unit depends semantically upon
19108 units located outside the current directory, you have to provide
19109 the source search path when calling @command{gnatstub}, see the description
19110 of @command{gnatstub} switches below.
19113 * Running gnatstub::
19114 * Switches for gnatstub::
19117 @node Running gnatstub
19118 @section Running @command{gnatstub}
19121 @command{gnatstub} has the command-line interface of the form
19124 $ gnatstub [switches] filename [directory]
19131 is the name of the source file that contains a library unit declaration
19132 for which a body must be created. The file name may contain the path
19134 The file name does not have to follow the GNAT file name conventions. If the
19136 does not follow GNAT file naming conventions, the name of the body file must
19138 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
19139 If the file name follows the GNAT file naming
19140 conventions and the name of the body file is not provided,
19143 of the body file from the argument file name by replacing the @file{.ads}
19145 with the @file{.adb} suffix.
19148 indicates the directory in which the body stub is to be placed (the default
19153 is an optional sequence of switches as described in the next section
19156 @node Switches for gnatstub
19157 @section Switches for @command{gnatstub}
19163 @cindex @option{^-f^/FULL^} (@command{gnatstub})
19164 If the destination directory already contains a file with the name of the
19166 for the argument spec file, replace it with the generated body stub.
19168 @item ^-hs^/HEADER=SPEC^
19169 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
19170 Put the comment header (i.e., all the comments preceding the
19171 compilation unit) from the source of the library unit declaration
19172 into the body stub.
19174 @item ^-hg^/HEADER=GENERAL^
19175 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
19176 Put a sample comment header into the body stub.
19180 @cindex @option{-IDIR} (@command{gnatstub})
19182 @cindex @option{-I-} (@command{gnatstub})
19185 @item /NOCURRENT_DIRECTORY
19186 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
19188 ^These switches have ^This switch has^ the same meaning as in calls to
19190 ^They define ^It defines ^ the source search path in the call to
19191 @command{gcc} issued
19192 by @command{gnatstub} to compile an argument source file.
19194 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
19195 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
19196 This switch has the same meaning as in calls to @command{gcc}.
19197 It defines the additional configuration file to be passed to the call to
19198 @command{gcc} issued
19199 by @command{gnatstub} to compile an argument source file.
19201 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
19202 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
19203 (@var{n} is a non-negative integer). Set the maximum line length in the
19204 body stub to @var{n}; the default is 79. The maximum value that can be
19205 specified is 32767. Note that in the special case of configuration
19206 pragma files, the maximum is always 32767 regardless of whether or
19207 not this switch appears.
19209 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
19210 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
19211 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
19212 the generated body sample to @var{n}.
19213 The default indentation is 3.
19215 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
19216 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
19217 Order local bodies alphabetically. (By default local bodies are ordered
19218 in the same way as the corresponding local specs in the argument spec file.)
19220 @item ^-i^/INDENTATION=^@var{n}
19221 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
19222 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
19224 @item ^-k^/TREE_FILE=SAVE^
19225 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
19226 Do not remove the tree file (i.e., the snapshot of the compiler internal
19227 structures used by @command{gnatstub}) after creating the body stub.
19229 @item ^-l^/LINE_LENGTH=^@var{n}
19230 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
19231 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
19233 @item ^-o^/BODY=^@var{body-name}
19234 @cindex @option{^-o^/BODY^} (@command{gnatstub})
19235 Body file name. This should be set if the argument file name does not
19237 the GNAT file naming
19238 conventions. If this switch is omitted the default name for the body will be
19240 from the argument file name according to the GNAT file naming conventions.
19243 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
19244 Quiet mode: do not generate a confirmation when a body is
19245 successfully created, and do not generate a message when a body is not
19249 @item ^-r^/TREE_FILE=REUSE^
19250 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
19251 Reuse the tree file (if it exists) instead of creating it. Instead of
19252 creating the tree file for the library unit declaration, @command{gnatstub}
19253 tries to find it in the current directory and use it for creating
19254 a body. If the tree file is not found, no body is created. This option
19255 also implies @option{^-k^/SAVE^}, whether or not
19256 the latter is set explicitly.
19258 @item ^-t^/TREE_FILE=OVERWRITE^
19259 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
19260 Overwrite the existing tree file. If the current directory already
19261 contains the file which, according to the GNAT file naming rules should
19262 be considered as a tree file for the argument source file,
19264 will refuse to create the tree file needed to create a sample body
19265 unless this option is set.
19267 @item ^-v^/VERBOSE^
19268 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
19269 Verbose mode: generate version information.
19273 @node Other Utility Programs
19274 @chapter Other Utility Programs
19277 This chapter discusses some other utility programs available in the Ada
19281 * Using Other Utility Programs with GNAT::
19282 * The External Symbol Naming Scheme of GNAT::
19284 * Ada Mode for Glide::
19286 * Converting Ada Files to html with gnathtml::
19287 * Installing gnathtml::
19294 @node Using Other Utility Programs with GNAT
19295 @section Using Other Utility Programs with GNAT
19298 The object files generated by GNAT are in standard system format and in
19299 particular the debugging information uses this format. This means
19300 programs generated by GNAT can be used with existing utilities that
19301 depend on these formats.
19304 In general, any utility program that works with C will also often work with
19305 Ada programs generated by GNAT. This includes software utilities such as
19306 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
19310 @node The External Symbol Naming Scheme of GNAT
19311 @section The External Symbol Naming Scheme of GNAT
19314 In order to interpret the output from GNAT, when using tools that are
19315 originally intended for use with other languages, it is useful to
19316 understand the conventions used to generate link names from the Ada
19319 All link names are in all lowercase letters. With the exception of library
19320 procedure names, the mechanism used is simply to use the full expanded
19321 Ada name with dots replaced by double underscores. For example, suppose
19322 we have the following package spec:
19324 @smallexample @c ada
19335 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
19336 the corresponding link name is @code{qrs__mn}.
19338 Of course if a @code{pragma Export} is used this may be overridden:
19340 @smallexample @c ada
19345 pragma Export (Var1, C, External_Name => "var1_name");
19347 pragma Export (Var2, C, Link_Name => "var2_link_name");
19354 In this case, the link name for @var{Var1} is whatever link name the
19355 C compiler would assign for the C function @var{var1_name}. This typically
19356 would be either @var{var1_name} or @var{_var1_name}, depending on operating
19357 system conventions, but other possibilities exist. The link name for
19358 @var{Var2} is @var{var2_link_name}, and this is not operating system
19362 One exception occurs for library level procedures. A potential ambiguity
19363 arises between the required name @code{_main} for the C main program,
19364 and the name we would otherwise assign to an Ada library level procedure
19365 called @code{Main} (which might well not be the main program).
19367 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
19368 names. So if we have a library level procedure such as
19370 @smallexample @c ada
19373 procedure Hello (S : String);
19379 the external name of this procedure will be @var{_ada_hello}.
19382 @node Ada Mode for Glide
19383 @section Ada Mode for @code{Glide}
19384 @cindex Ada mode (for Glide)
19387 The Glide mode for programming in Ada (both Ada83 and Ada95) helps the
19388 user to understand and navigate existing code, and facilitates writing
19389 new code. It furthermore provides some utility functions for easier
19390 integration of standard Emacs features when programming in Ada.
19392 Its general features include:
19396 An Integrated Development Environment with functionality such as the
19401 ``Project files'' for configuration-specific aspects
19402 (e.g. directories and compilation options)
19405 Compiling and stepping through error messages.
19408 Running and debugging an applications within Glide.
19415 User configurability
19418 Some of the specific Ada mode features are:
19422 Functions for easy and quick stepping through Ada code
19425 Getting cross reference information for identifiers (e.g., finding a
19426 defining occurrence)
19429 Displaying an index menu of types and subprograms, allowing
19430 direct selection for browsing
19433 Automatic color highlighting of the various Ada entities
19436 Glide directly supports writing Ada code, via several facilities:
19440 Switching between spec and body files with possible
19441 autogeneration of body files
19444 Automatic formating of subprogram parameter lists
19447 Automatic indentation according to Ada syntax
19450 Automatic completion of identifiers
19453 Automatic (and configurable) casing of identifiers, keywords, and attributes
19456 Insertion of syntactic templates
19459 Block commenting / uncommenting
19463 For more information, please refer to the online documentation
19464 available in the @code{Glide} @result{} @code{Help} menu.
19467 @node Converting Ada Files to html with gnathtml
19468 @section Converting Ada Files to HTML with @code{gnathtml}
19471 This @code{Perl} script allows Ada source files to be browsed using
19472 standard Web browsers. For installation procedure, see the section
19473 @xref{Installing gnathtml}.
19475 Ada reserved keywords are highlighted in a bold font and Ada comments in
19476 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
19477 switch to suppress the generation of cross-referencing information, user
19478 defined variables and types will appear in a different color; you will
19479 be able to click on any identifier and go to its declaration.
19481 The command line is as follow:
19483 $ perl gnathtml.pl [^switches^options^] ada-files
19487 You can pass it as many Ada files as you want. @code{gnathtml} will generate
19488 an html file for every ada file, and a global file called @file{index.htm}.
19489 This file is an index of every identifier defined in the files.
19491 The available ^switches^options^ are the following ones :
19495 @cindex @option{-83} (@code{gnathtml})
19496 Only the subset on the Ada 83 keywords will be highlighted, not the full
19497 Ada 95 keywords set.
19499 @item -cc @var{color}
19500 @cindex @option{-cc} (@code{gnathtml})
19501 This option allows you to change the color used for comments. The default
19502 value is green. The color argument can be any name accepted by html.
19505 @cindex @option{-d} (@code{gnathtml})
19506 If the Ada files depend on some other files (for instance through
19507 @code{with} clauses, the latter files will also be converted to html.
19508 Only the files in the user project will be converted to html, not the files
19509 in the run-time library itself.
19512 @cindex @option{-D} (@code{gnathtml})
19513 This command is the same as @option{-d} above, but @command{gnathtml} will
19514 also look for files in the run-time library, and generate html files for them.
19516 @item -ext @var{extension}
19517 @cindex @option{-ext} (@code{gnathtml})
19518 This option allows you to change the extension of the generated HTML files.
19519 If you do not specify an extension, it will default to @file{htm}.
19522 @cindex @option{-f} (@code{gnathtml})
19523 By default, gnathtml will generate html links only for global entities
19524 ('with'ed units, global variables and types,...). If you specify
19525 @option{-f} on the command line, then links will be generated for local
19528 @item -l @var{number}
19529 @cindex @option{-l} (@code{gnathtml})
19530 If this ^switch^option^ is provided and @var{number} is not 0, then
19531 @code{gnathtml} will number the html files every @var{number} line.
19534 @cindex @option{-I} (@code{gnathtml})
19535 Specify a directory to search for library files (@file{.ALI} files) and
19536 source files. You can provide several -I switches on the command line,
19537 and the directories will be parsed in the order of the command line.
19540 @cindex @option{-o} (@code{gnathtml})
19541 Specify the output directory for html files. By default, gnathtml will
19542 saved the generated html files in a subdirectory named @file{html/}.
19544 @item -p @var{file}
19545 @cindex @option{-p} (@code{gnathtml})
19546 If you are using Emacs and the most recent Emacs Ada mode, which provides
19547 a full Integrated Development Environment for compiling, checking,
19548 running and debugging applications, you may use @file{.gpr} files
19549 to give the directories where Emacs can find sources and object files.
19551 Using this ^switch^option^, you can tell gnathtml to use these files.
19552 This allows you to get an html version of your application, even if it
19553 is spread over multiple directories.
19555 @item -sc @var{color}
19556 @cindex @option{-sc} (@code{gnathtml})
19557 This ^switch^option^ allows you to change the color used for symbol
19559 The default value is red. The color argument can be any name accepted by html.
19561 @item -t @var{file}
19562 @cindex @option{-t} (@code{gnathtml})
19563 This ^switch^option^ provides the name of a file. This file contains a list of
19564 file names to be converted, and the effect is exactly as though they had
19565 appeared explicitly on the command line. This
19566 is the recommended way to work around the command line length limit on some
19571 @node Installing gnathtml
19572 @section Installing @code{gnathtml}
19575 @code{Perl} needs to be installed on your machine to run this script.
19576 @code{Perl} is freely available for almost every architecture and
19577 Operating System via the Internet.
19579 On Unix systems, you may want to modify the first line of the script
19580 @code{gnathtml}, to explicitly tell the Operating system where Perl
19581 is. The syntax of this line is :
19583 #!full_path_name_to_perl
19587 Alternatively, you may run the script using the following command line:
19590 $ perl gnathtml.pl [switches] files
19599 The GNAT distribution provides an Ada 95 template for the HP Language
19600 Sensitive Editor (LSE), a component of DECset. In order to
19601 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
19608 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
19609 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
19610 the collection phase with the /DEBUG qualifier.
19613 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
19614 $ DEFINE LIB$DEBUG PCA$COLLECTOR
19615 $ RUN/DEBUG <PROGRAM_NAME>
19620 @node Running and Debugging Ada Programs
19621 @chapter Running and Debugging Ada Programs
19625 This chapter discusses how to debug Ada programs.
19627 It applies to the Alpha OpenVMS platform;
19628 the debugger for I64 OpenVMS is scheduled for a subsequent release.
19631 An incorrect Ada program may be handled in three ways by the GNAT compiler:
19635 The illegality may be a violation of the static semantics of Ada. In
19636 that case GNAT diagnoses the constructs in the program that are illegal.
19637 It is then a straightforward matter for the user to modify those parts of
19641 The illegality may be a violation of the dynamic semantics of Ada. In
19642 that case the program compiles and executes, but may generate incorrect
19643 results, or may terminate abnormally with some exception.
19646 When presented with a program that contains convoluted errors, GNAT
19647 itself may terminate abnormally without providing full diagnostics on
19648 the incorrect user program.
19652 * The GNAT Debugger GDB::
19654 * Introduction to GDB Commands::
19655 * Using Ada Expressions::
19656 * Calling User-Defined Subprograms::
19657 * Using the Next Command in a Function::
19660 * Debugging Generic Units::
19661 * GNAT Abnormal Termination or Failure to Terminate::
19662 * Naming Conventions for GNAT Source Files::
19663 * Getting Internal Debugging Information::
19664 * Stack Traceback::
19670 @node The GNAT Debugger GDB
19671 @section The GNAT Debugger GDB
19674 @code{GDB} is a general purpose, platform-independent debugger that
19675 can be used to debug mixed-language programs compiled with @command{gcc},
19676 and in particular is capable of debugging Ada programs compiled with
19677 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
19678 complex Ada data structures.
19680 The manual @cite{Debugging with GDB}
19682 , located in the GNU:[DOCS] directory,
19684 contains full details on the usage of @code{GDB}, including a section on
19685 its usage on programs. This manual should be consulted for full
19686 details. The section that follows is a brief introduction to the
19687 philosophy and use of @code{GDB}.
19689 When GNAT programs are compiled, the compiler optionally writes debugging
19690 information into the generated object file, including information on
19691 line numbers, and on declared types and variables. This information is
19692 separate from the generated code. It makes the object files considerably
19693 larger, but it does not add to the size of the actual executable that
19694 will be loaded into memory, and has no impact on run-time performance. The
19695 generation of debug information is triggered by the use of the
19696 ^-g^/DEBUG^ switch in the gcc or gnatmake command used to carry out
19697 the compilations. It is important to emphasize that the use of these
19698 options does not change the generated code.
19700 The debugging information is written in standard system formats that
19701 are used by many tools, including debuggers and profilers. The format
19702 of the information is typically designed to describe C types and
19703 semantics, but GNAT implements a translation scheme which allows full
19704 details about Ada types and variables to be encoded into these
19705 standard C formats. Details of this encoding scheme may be found in
19706 the file exp_dbug.ads in the GNAT source distribution. However, the
19707 details of this encoding are, in general, of no interest to a user,
19708 since @code{GDB} automatically performs the necessary decoding.
19710 When a program is bound and linked, the debugging information is
19711 collected from the object files, and stored in the executable image of
19712 the program. Again, this process significantly increases the size of
19713 the generated executable file, but it does not increase the size of
19714 the executable program itself. Furthermore, if this program is run in
19715 the normal manner, it runs exactly as if the debug information were
19716 not present, and takes no more actual memory.
19718 However, if the program is run under control of @code{GDB}, the
19719 debugger is activated. The image of the program is loaded, at which
19720 point it is ready to run. If a run command is given, then the program
19721 will run exactly as it would have if @code{GDB} were not present. This
19722 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
19723 entirely non-intrusive until a breakpoint is encountered. If no
19724 breakpoint is ever hit, the program will run exactly as it would if no
19725 debugger were present. When a breakpoint is hit, @code{GDB} accesses
19726 the debugging information and can respond to user commands to inspect
19727 variables, and more generally to report on the state of execution.
19731 @section Running GDB
19734 The debugger can be launched directly and simply from @code{glide} or
19735 through its graphical interface: @code{gvd}. It can also be used
19736 directly in text mode. Here is described the basic use of @code{GDB}
19737 in text mode. All the commands described below can be used in the
19738 @code{gvd} console window even though there is usually other more
19739 graphical ways to achieve the same goals.
19743 The command to run the graphical interface of the debugger is
19750 The command to run @code{GDB} in text mode is
19753 $ ^gdb program^$ GDB PROGRAM^
19757 where @code{^program^PROGRAM^} is the name of the executable file. This
19758 activates the debugger and results in a prompt for debugger commands.
19759 The simplest command is simply @code{run}, which causes the program to run
19760 exactly as if the debugger were not present. The following section
19761 describes some of the additional commands that can be given to @code{GDB}.
19763 @c *******************************
19764 @node Introduction to GDB Commands
19765 @section Introduction to GDB Commands
19768 @code{GDB} contains a large repertoire of commands. The manual
19769 @cite{Debugging with GDB}
19771 , located in the GNU:[DOCS] directory,
19773 includes extensive documentation on the use
19774 of these commands, together with examples of their use. Furthermore,
19775 the command @var{help} invoked from within @code{GDB} activates a simple help
19776 facility which summarizes the available commands and their options.
19777 In this section we summarize a few of the most commonly
19778 used commands to give an idea of what @code{GDB} is about. You should create
19779 a simple program with debugging information and experiment with the use of
19780 these @code{GDB} commands on the program as you read through the
19784 @item set args @var{arguments}
19785 The @var{arguments} list above is a list of arguments to be passed to
19786 the program on a subsequent run command, just as though the arguments
19787 had been entered on a normal invocation of the program. The @code{set args}
19788 command is not needed if the program does not require arguments.
19791 The @code{run} command causes execution of the program to start from
19792 the beginning. If the program is already running, that is to say if
19793 you are currently positioned at a breakpoint, then a prompt will ask
19794 for confirmation that you want to abandon the current execution and
19797 @item breakpoint @var{location}
19798 The breakpoint command sets a breakpoint, that is to say a point at which
19799 execution will halt and @code{GDB} will await further
19800 commands. @var{location} is
19801 either a line number within a file, given in the format @code{file:linenumber},
19802 or it is the name of a subprogram. If you request that a breakpoint be set on
19803 a subprogram that is overloaded, a prompt will ask you to specify on which of
19804 those subprograms you want to breakpoint. You can also
19805 specify that all of them should be breakpointed. If the program is run
19806 and execution encounters the breakpoint, then the program
19807 stops and @code{GDB} signals that the breakpoint was encountered by
19808 printing the line of code before which the program is halted.
19810 @item breakpoint exception @var{name}
19811 A special form of the breakpoint command which breakpoints whenever
19812 exception @var{name} is raised.
19813 If @var{name} is omitted,
19814 then a breakpoint will occur when any exception is raised.
19816 @item print @var{expression}
19817 This will print the value of the given expression. Most simple
19818 Ada expression formats are properly handled by @code{GDB}, so the expression
19819 can contain function calls, variables, operators, and attribute references.
19822 Continues execution following a breakpoint, until the next breakpoint or the
19823 termination of the program.
19826 Executes a single line after a breakpoint. If the next statement
19827 is a subprogram call, execution continues into (the first statement of)
19828 the called subprogram.
19831 Executes a single line. If this line is a subprogram call, executes and
19832 returns from the call.
19835 Lists a few lines around the current source location. In practice, it
19836 is usually more convenient to have a separate edit window open with the
19837 relevant source file displayed. Successive applications of this command
19838 print subsequent lines. The command can be given an argument which is a
19839 line number, in which case it displays a few lines around the specified one.
19842 Displays a backtrace of the call chain. This command is typically
19843 used after a breakpoint has occurred, to examine the sequence of calls that
19844 leads to the current breakpoint. The display includes one line for each
19845 activation record (frame) corresponding to an active subprogram.
19848 At a breakpoint, @code{GDB} can display the values of variables local
19849 to the current frame. The command @code{up} can be used to
19850 examine the contents of other active frames, by moving the focus up
19851 the stack, that is to say from callee to caller, one frame at a time.
19854 Moves the focus of @code{GDB} down from the frame currently being
19855 examined to the frame of its callee (the reverse of the previous command),
19857 @item frame @var{n}
19858 Inspect the frame with the given number. The value 0 denotes the frame
19859 of the current breakpoint, that is to say the top of the call stack.
19863 The above list is a very short introduction to the commands that
19864 @code{GDB} provides. Important additional capabilities, including conditional
19865 breakpoints, the ability to execute command sequences on a breakpoint,
19866 the ability to debug at the machine instruction level and many other
19867 features are described in detail in @cite{Debugging with GDB}.
19868 Note that most commands can be abbreviated
19869 (for example, c for continue, bt for backtrace).
19871 @node Using Ada Expressions
19872 @section Using Ada Expressions
19873 @cindex Ada expressions
19876 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
19877 extensions. The philosophy behind the design of this subset is
19881 That @code{GDB} should provide basic literals and access to operations for
19882 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
19883 leaving more sophisticated computations to subprograms written into the
19884 program (which therefore may be called from @code{GDB}).
19887 That type safety and strict adherence to Ada language restrictions
19888 are not particularly important to the @code{GDB} user.
19891 That brevity is important to the @code{GDB} user.
19894 Thus, for brevity, the debugger acts as if there were
19895 implicit @code{with} and @code{use} clauses in effect for all user-written
19896 packages, thus making it unnecessary to fully qualify most names with
19897 their packages, regardless of context. Where this causes ambiguity,
19898 @code{GDB} asks the user's intent.
19900 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
19902 @node Calling User-Defined Subprograms
19903 @section Calling User-Defined Subprograms
19906 An important capability of @code{GDB} is the ability to call user-defined
19907 subprograms while debugging. This is achieved simply by entering
19908 a subprogram call statement in the form:
19911 call subprogram-name (parameters)
19915 The keyword @code{call} can be omitted in the normal case where the
19916 @code{subprogram-name} does not coincide with any of the predefined
19917 @code{GDB} commands.
19919 The effect is to invoke the given subprogram, passing it the
19920 list of parameters that is supplied. The parameters can be expressions and
19921 can include variables from the program being debugged. The
19922 subprogram must be defined
19923 at the library level within your program, and @code{GDB} will call the
19924 subprogram within the environment of your program execution (which
19925 means that the subprogram is free to access or even modify variables
19926 within your program).
19928 The most important use of this facility is in allowing the inclusion of
19929 debugging routines that are tailored to particular data structures
19930 in your program. Such debugging routines can be written to provide a suitably
19931 high-level description of an abstract type, rather than a low-level dump
19932 of its physical layout. After all, the standard
19933 @code{GDB print} command only knows the physical layout of your
19934 types, not their abstract meaning. Debugging routines can provide information
19935 at the desired semantic level and are thus enormously useful.
19937 For example, when debugging GNAT itself, it is crucial to have access to
19938 the contents of the tree nodes used to represent the program internally.
19939 But tree nodes are represented simply by an integer value (which in turn
19940 is an index into a table of nodes).
19941 Using the @code{print} command on a tree node would simply print this integer
19942 value, which is not very useful. But the PN routine (defined in file
19943 treepr.adb in the GNAT sources) takes a tree node as input, and displays
19944 a useful high level representation of the tree node, which includes the
19945 syntactic category of the node, its position in the source, the integers
19946 that denote descendant nodes and parent node, as well as varied
19947 semantic information. To study this example in more detail, you might want to
19948 look at the body of the PN procedure in the stated file.
19950 @node Using the Next Command in a Function
19951 @section Using the Next Command in a Function
19954 When you use the @code{next} command in a function, the current source
19955 location will advance to the next statement as usual. A special case
19956 arises in the case of a @code{return} statement.
19958 Part of the code for a return statement is the ``epilog'' of the function.
19959 This is the code that returns to the caller. There is only one copy of
19960 this epilog code, and it is typically associated with the last return
19961 statement in the function if there is more than one return. In some
19962 implementations, this epilog is associated with the first statement
19965 The result is that if you use the @code{next} command from a return
19966 statement that is not the last return statement of the function you
19967 may see a strange apparent jump to the last return statement or to
19968 the start of the function. You should simply ignore this odd jump.
19969 The value returned is always that from the first return statement
19970 that was stepped through.
19972 @node Ada Exceptions
19973 @section Breaking on Ada Exceptions
19977 You can set breakpoints that trip when your program raises
19978 selected exceptions.
19981 @item break exception
19982 Set a breakpoint that trips whenever (any task in the) program raises
19985 @item break exception @var{name}
19986 Set a breakpoint that trips whenever (any task in the) program raises
19987 the exception @var{name}.
19989 @item break exception unhandled
19990 Set a breakpoint that trips whenever (any task in the) program raises an
19991 exception for which there is no handler.
19993 @item info exceptions
19994 @itemx info exceptions @var{regexp}
19995 The @code{info exceptions} command permits the user to examine all defined
19996 exceptions within Ada programs. With a regular expression, @var{regexp}, as
19997 argument, prints out only those exceptions whose name matches @var{regexp}.
20005 @code{GDB} allows the following task-related commands:
20009 This command shows a list of current Ada tasks, as in the following example:
20016 ID TID P-ID Thread Pri State Name
20017 1 8088000 0 807e000 15 Child Activation Wait main_task
20018 2 80a4000 1 80ae000 15 Accept/Select Wait b
20019 3 809a800 1 80a4800 15 Child Activation Wait a
20020 * 4 80ae800 3 80b8000 15 Running c
20024 In this listing, the asterisk before the first task indicates it to be the
20025 currently running task. The first column lists the task ID that is used
20026 to refer to tasks in the following commands.
20028 @item break @var{linespec} task @var{taskid}
20029 @itemx break @var{linespec} task @var{taskid} if @dots{}
20030 @cindex Breakpoints and tasks
20031 These commands are like the @code{break @dots{} thread @dots{}}.
20032 @var{linespec} specifies source lines.
20034 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
20035 to specify that you only want @code{GDB} to stop the program when a
20036 particular Ada task reaches this breakpoint. @var{taskid} is one of the
20037 numeric task identifiers assigned by @code{GDB}, shown in the first
20038 column of the @samp{info tasks} display.
20040 If you do not specify @samp{task @var{taskid}} when you set a
20041 breakpoint, the breakpoint applies to @emph{all} tasks of your
20044 You can use the @code{task} qualifier on conditional breakpoints as
20045 well; in this case, place @samp{task @var{taskid}} before the
20046 breakpoint condition (before the @code{if}).
20048 @item task @var{taskno}
20049 @cindex Task switching
20051 This command allows to switch to the task referred by @var{taskno}. In
20052 particular, This allows to browse the backtrace of the specified
20053 task. It is advised to switch back to the original task before
20054 continuing execution otherwise the scheduling of the program may be
20059 For more detailed information on the tasking support,
20060 see @cite{Debugging with GDB}.
20062 @node Debugging Generic Units
20063 @section Debugging Generic Units
20064 @cindex Debugging Generic Units
20068 GNAT always uses code expansion for generic instantiation. This means that
20069 each time an instantiation occurs, a complete copy of the original code is
20070 made, with appropriate substitutions of formals by actuals.
20072 It is not possible to refer to the original generic entities in
20073 @code{GDB}, but it is always possible to debug a particular instance of
20074 a generic, by using the appropriate expanded names. For example, if we have
20076 @smallexample @c ada
20081 generic package k is
20082 procedure kp (v1 : in out integer);
20086 procedure kp (v1 : in out integer) is
20092 package k1 is new k;
20093 package k2 is new k;
20095 var : integer := 1;
20108 Then to break on a call to procedure kp in the k2 instance, simply
20112 (gdb) break g.k2.kp
20116 When the breakpoint occurs, you can step through the code of the
20117 instance in the normal manner and examine the values of local variables, as for
20120 @node GNAT Abnormal Termination or Failure to Terminate
20121 @section GNAT Abnormal Termination or Failure to Terminate
20122 @cindex GNAT Abnormal Termination or Failure to Terminate
20125 When presented with programs that contain serious errors in syntax
20127 GNAT may on rare occasions experience problems in operation, such
20129 segmentation fault or illegal memory access, raising an internal
20130 exception, terminating abnormally, or failing to terminate at all.
20131 In such cases, you can activate
20132 various features of GNAT that can help you pinpoint the construct in your
20133 program that is the likely source of the problem.
20135 The following strategies are presented in increasing order of
20136 difficulty, corresponding to your experience in using GNAT and your
20137 familiarity with compiler internals.
20141 Run @command{gcc} with the @option{-gnatf}. This first
20142 switch causes all errors on a given line to be reported. In its absence,
20143 only the first error on a line is displayed.
20145 The @option{-gnatdO} switch causes errors to be displayed as soon as they
20146 are encountered, rather than after compilation is terminated. If GNAT
20147 terminates prematurely or goes into an infinite loop, the last error
20148 message displayed may help to pinpoint the culprit.
20151 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
20152 mode, @command{gcc} produces ongoing information about the progress of the
20153 compilation and provides the name of each procedure as code is
20154 generated. This switch allows you to find which Ada procedure was being
20155 compiled when it encountered a code generation problem.
20158 @cindex @option{-gnatdc} switch
20159 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
20160 switch that does for the front-end what @option{^-v^VERBOSE^} does
20161 for the back end. The system prints the name of each unit,
20162 either a compilation unit or nested unit, as it is being analyzed.
20164 Finally, you can start
20165 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
20166 front-end of GNAT, and can be run independently (normally it is just
20167 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
20168 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
20169 @code{where} command is the first line of attack; the variable
20170 @code{lineno} (seen by @code{print lineno}), used by the second phase of
20171 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
20172 which the execution stopped, and @code{input_file name} indicates the name of
20176 @node Naming Conventions for GNAT Source Files
20177 @section Naming Conventions for GNAT Source Files
20180 In order to examine the workings of the GNAT system, the following
20181 brief description of its organization may be helpful:
20185 Files with prefix @file{^sc^SC^} contain the lexical scanner.
20188 All files prefixed with @file{^par^PAR^} are components of the parser. The
20189 numbers correspond to chapters of the Ada 95 Reference Manual. For example,
20190 parsing of select statements can be found in @file{par-ch9.adb}.
20193 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
20194 numbers correspond to chapters of the Ada standard. For example, all
20195 issues involving context clauses can be found in @file{sem_ch10.adb}. In
20196 addition, some features of the language require sufficient special processing
20197 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
20198 dynamic dispatching, etc.
20201 All files prefixed with @file{^exp^EXP^} perform normalization and
20202 expansion of the intermediate representation (abstract syntax tree, or AST).
20203 these files use the same numbering scheme as the parser and semantics files.
20204 For example, the construction of record initialization procedures is done in
20205 @file{exp_ch3.adb}.
20208 The files prefixed with @file{^bind^BIND^} implement the binder, which
20209 verifies the consistency of the compilation, determines an order of
20210 elaboration, and generates the bind file.
20213 The files @file{atree.ads} and @file{atree.adb} detail the low-level
20214 data structures used by the front-end.
20217 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
20218 the abstract syntax tree as produced by the parser.
20221 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
20222 all entities, computed during semantic analysis.
20225 Library management issues are dealt with in files with prefix
20231 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
20232 defined in Annex A.
20237 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
20238 defined in Annex B.
20242 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
20243 both language-defined children and GNAT run-time routines.
20247 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
20248 general-purpose packages, fully documented in their specifications. All
20249 the other @file{.c} files are modifications of common @command{gcc} files.
20252 @node Getting Internal Debugging Information
20253 @section Getting Internal Debugging Information
20256 Most compilers have internal debugging switches and modes. GNAT
20257 does also, except GNAT internal debugging switches and modes are not
20258 secret. A summary and full description of all the compiler and binder
20259 debug flags are in the file @file{debug.adb}. You must obtain the
20260 sources of the compiler to see the full detailed effects of these flags.
20262 The switches that print the source of the program (reconstructed from
20263 the internal tree) are of general interest for user programs, as are the
20265 the full internal tree, and the entity table (the symbol table
20266 information). The reconstructed source provides a readable version of the
20267 program after the front-end has completed analysis and expansion,
20268 and is useful when studying the performance of specific constructs.
20269 For example, constraint checks are indicated, complex aggregates
20270 are replaced with loops and assignments, and tasking primitives
20271 are replaced with run-time calls.
20273 @node Stack Traceback
20274 @section Stack Traceback
20276 @cindex stack traceback
20277 @cindex stack unwinding
20280 Traceback is a mechanism to display the sequence of subprogram calls that
20281 leads to a specified execution point in a program. Often (but not always)
20282 the execution point is an instruction at which an exception has been raised.
20283 This mechanism is also known as @i{stack unwinding} because it obtains
20284 its information by scanning the run-time stack and recovering the activation
20285 records of all active subprograms. Stack unwinding is one of the most
20286 important tools for program debugging.
20288 The first entry stored in traceback corresponds to the deepest calling level,
20289 that is to say the subprogram currently executing the instruction
20290 from which we want to obtain the traceback.
20292 Note that there is no runtime performance penalty when stack traceback
20293 is enabled, and no exception is raised during program execution.
20296 * Non-Symbolic Traceback::
20297 * Symbolic Traceback::
20300 @node Non-Symbolic Traceback
20301 @subsection Non-Symbolic Traceback
20302 @cindex traceback, non-symbolic
20305 Note: this feature is not supported on all platforms. See
20306 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
20310 * Tracebacks From an Unhandled Exception::
20311 * Tracebacks From Exception Occurrences (non-symbolic)::
20312 * Tracebacks From Anywhere in a Program (non-symbolic)::
20315 @node Tracebacks From an Unhandled Exception
20316 @subsubsection Tracebacks From an Unhandled Exception
20319 A runtime non-symbolic traceback is a list of addresses of call instructions.
20320 To enable this feature you must use the @option{-E}
20321 @code{gnatbind}'s option. With this option a stack traceback is stored as part
20322 of exception information. You can retrieve this information using the
20323 @code{addr2line} tool.
20325 Here is a simple example:
20327 @smallexample @c ada
20333 raise Constraint_Error;
20348 $ gnatmake stb -bargs -E
20351 Execution terminated by unhandled exception
20352 Exception name: CONSTRAINT_ERROR
20354 Call stack traceback locations:
20355 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20359 As we see the traceback lists a sequence of addresses for the unhandled
20360 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
20361 guess that this exception come from procedure P1. To translate these
20362 addresses into the source lines where the calls appear, the
20363 @code{addr2line} tool, described below, is invaluable. The use of this tool
20364 requires the program to be compiled with debug information.
20367 $ gnatmake -g stb -bargs -E
20370 Execution terminated by unhandled exception
20371 Exception name: CONSTRAINT_ERROR
20373 Call stack traceback locations:
20374 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20376 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
20377 0x4011f1 0x77e892a4
20379 00401373 at d:/stb/stb.adb:5
20380 0040138B at d:/stb/stb.adb:10
20381 0040139C at d:/stb/stb.adb:14
20382 00401335 at d:/stb/b~stb.adb:104
20383 004011C4 at /build/.../crt1.c:200
20384 004011F1 at /build/.../crt1.c:222
20385 77E892A4 in ?? at ??:0
20389 The @code{addr2line} tool has several other useful options:
20393 to get the function name corresponding to any location
20395 @item --demangle=gnat
20396 to use the gnat decoding mode for the function names. Note that
20397 for binutils version 2.9.x the option is simply @option{--demangle}.
20401 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
20402 0x40139c 0x401335 0x4011c4 0x4011f1
20404 00401373 in stb.p1 at d:/stb/stb.adb:5
20405 0040138B in stb.p2 at d:/stb/stb.adb:10
20406 0040139C in stb at d:/stb/stb.adb:14
20407 00401335 in main at d:/stb/b~stb.adb:104
20408 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
20409 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
20413 From this traceback we can see that the exception was raised in
20414 @file{stb.adb} at line 5, which was reached from a procedure call in
20415 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
20416 which contains the call to the main program.
20417 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
20418 and the output will vary from platform to platform.
20420 It is also possible to use @code{GDB} with these traceback addresses to debug
20421 the program. For example, we can break at a given code location, as reported
20422 in the stack traceback:
20428 Furthermore, this feature is not implemented inside Windows DLL. Only
20429 the non-symbolic traceback is reported in this case.
20432 (gdb) break *0x401373
20433 Breakpoint 1 at 0x401373: file stb.adb, line 5.
20437 It is important to note that the stack traceback addresses
20438 do not change when debug information is included. This is particularly useful
20439 because it makes it possible to release software without debug information (to
20440 minimize object size), get a field report that includes a stack traceback
20441 whenever an internal bug occurs, and then be able to retrieve the sequence
20442 of calls with the same program compiled with debug information.
20444 @node Tracebacks From Exception Occurrences (non-symbolic)
20445 @subsubsection Tracebacks From Exception Occurrences
20448 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
20449 The stack traceback is attached to the exception information string, and can
20450 be retrieved in an exception handler within the Ada program, by means of the
20451 Ada95 facilities defined in @code{Ada.Exceptions}. Here is a simple example:
20453 @smallexample @c ada
20455 with Ada.Exceptions;
20460 use Ada.Exceptions;
20468 Text_IO.Put_Line (Exception_Information (E));
20482 This program will output:
20487 Exception name: CONSTRAINT_ERROR
20488 Message: stb.adb:12
20489 Call stack traceback locations:
20490 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
20493 @node Tracebacks From Anywhere in a Program (non-symbolic)
20494 @subsubsection Tracebacks From Anywhere in a Program
20497 It is also possible to retrieve a stack traceback from anywhere in a
20498 program. For this you need to
20499 use the @code{GNAT.Traceback} API. This package includes a procedure called
20500 @code{Call_Chain} that computes a complete stack traceback, as well as useful
20501 display procedures described below. It is not necessary to use the
20502 @option{-E gnatbind} option in this case, because the stack traceback mechanism
20503 is invoked explicitly.
20506 In the following example we compute a traceback at a specific location in
20507 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
20508 convert addresses to strings:
20510 @smallexample @c ada
20512 with GNAT.Traceback;
20513 with GNAT.Debug_Utilities;
20519 use GNAT.Traceback;
20522 TB : Tracebacks_Array (1 .. 10);
20523 -- We are asking for a maximum of 10 stack frames.
20525 -- Len will receive the actual number of stack frames returned.
20527 Call_Chain (TB, Len);
20529 Text_IO.Put ("In STB.P1 : ");
20531 for K in 1 .. Len loop
20532 Text_IO.Put (Debug_Utilities.Image (TB (K)));
20553 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
20554 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
20558 You can then get further information by invoking the @code{addr2line}
20559 tool as described earlier (note that the hexadecimal addresses
20560 need to be specified in C format, with a leading ``0x'').
20562 @node Symbolic Traceback
20563 @subsection Symbolic Traceback
20564 @cindex traceback, symbolic
20567 A symbolic traceback is a stack traceback in which procedure names are
20568 associated with each code location.
20571 Note that this feature is not supported on all platforms. See
20572 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
20573 list of currently supported platforms.
20576 Note that the symbolic traceback requires that the program be compiled
20577 with debug information. If it is not compiled with debug information
20578 only the non-symbolic information will be valid.
20581 * Tracebacks From Exception Occurrences (symbolic)::
20582 * Tracebacks From Anywhere in a Program (symbolic)::
20585 @node Tracebacks From Exception Occurrences (symbolic)
20586 @subsubsection Tracebacks From Exception Occurrences
20588 @smallexample @c ada
20590 with GNAT.Traceback.Symbolic;
20596 raise Constraint_Error;
20613 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
20618 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
20621 0040149F in stb.p1 at stb.adb:8
20622 004014B7 in stb.p2 at stb.adb:13
20623 004014CF in stb.p3 at stb.adb:18
20624 004015DD in ada.stb at stb.adb:22
20625 00401461 in main at b~stb.adb:168
20626 004011C4 in __mingw_CRTStartup at crt1.c:200
20627 004011F1 in mainCRTStartup at crt1.c:222
20628 77E892A4 in ?? at ??:0
20632 In the above example the ``.\'' syntax in the @command{gnatmake} command
20633 is currently required by @command{addr2line} for files that are in
20634 the current working directory.
20635 Moreover, the exact sequence of linker options may vary from platform
20637 The above @option{-largs} section is for Windows platforms. By contrast,
20638 under Unix there is no need for the @option{-largs} section.
20639 Differences across platforms are due to details of linker implementation.
20641 @node Tracebacks From Anywhere in a Program (symbolic)
20642 @subsubsection Tracebacks From Anywhere in a Program
20645 It is possible to get a symbolic stack traceback
20646 from anywhere in a program, just as for non-symbolic tracebacks.
20647 The first step is to obtain a non-symbolic
20648 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
20649 information. Here is an example:
20651 @smallexample @c ada
20653 with GNAT.Traceback;
20654 with GNAT.Traceback.Symbolic;
20659 use GNAT.Traceback;
20660 use GNAT.Traceback.Symbolic;
20663 TB : Tracebacks_Array (1 .. 10);
20664 -- We are asking for a maximum of 10 stack frames.
20666 -- Len will receive the actual number of stack frames returned.
20668 Call_Chain (TB, Len);
20669 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
20683 @c ******************************
20685 @node Compatibility with HP Ada
20686 @chapter Compatibility with HP Ada
20687 @cindex Compatibility
20692 @cindex Compatibility between GNAT and HP Ada
20693 This chapter compares HP Ada (formerly known as ``DEC Ada'')
20694 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
20695 GNAT is highly compatible
20696 with HP Ada, and it should generally be straightforward to port code
20697 from the HP Ada environment to GNAT. However, there are a few language
20698 and implementation differences of which the user must be aware. These
20699 differences are discussed in this chapter. In
20700 addition, the operating environment and command structure for the
20701 compiler are different, and these differences are also discussed.
20703 For further details on these and other compatibility issues,
20704 see Appendix E of the HP publication
20705 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
20707 Except where otherwise indicated, the description of GNAT for OpenVMS
20708 applies to both the Alpha and I64 platforms.
20710 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
20711 I64 OpenVMS, see @ref{Transitioning from Alpha to I64 OpenVMS}.
20713 The discussion in this chapter addresses specifically the implementation
20714 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
20715 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
20716 GNAT always follows the Alpha implementation.
20718 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
20719 attributes are recognized, although only a subset of them can sensibly
20720 be implemented. The description of pragmas in the
20721 @cite{GNAT Reference Manual} indicates whether or not they are applicable
20722 to non-VMS systems.
20726 * Ada 95 Compatibility::
20727 * Differences in the Definition of Package System::
20728 * Language-Related Features::
20729 * The Package STANDARD::
20730 * The Package SYSTEM::
20731 * Tasking and Task-Related Features::
20732 * Pragmas and Pragma-Related Features::
20733 * Library of Predefined Units::
20735 * Main Program Definition::
20736 * Implementation-Defined Attributes::
20737 * Compiler and Run-Time Interfacing::
20738 * Program Compilation and Library Management::
20740 * Implementation Limits::
20741 * Tools and Utilities::
20744 @node Ada 95 Compatibility
20745 @section Ada 95 Compatibility
20748 GNAT is an Ada 95 compiler, and HP Ada is an Ada 83
20749 compiler. Ada 95 is almost completely upwards compatible
20750 with Ada 83, and therefore Ada 83 programs will compile
20751 and run under GNAT with
20752 no changes or only minor changes. The Ada 95 Reference
20753 Manual provides details on specific incompatibilities.
20755 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
20756 as well as the pragma @code{ADA_83}, to force the compiler to
20757 operate in Ada 83 mode. This mode does not guarantee complete
20758 conformance to Ada 83, but in practice is sufficient to
20759 eliminate most sources of incompatibilities.
20760 In particular, it eliminates the recognition of the
20761 additional Ada 95 keywords, so that their use as identifiers
20762 in Ada 83 programs is legal, and handles the cases of packages
20763 with optional bodies, and generics that instantiate unconstrained
20764 types without the use of @code{(<>)}.
20766 @node Differences in the Definition of Package System
20767 @section Differences in the Definition of Package @code{System}
20770 Both Ada 95 and Ada 83 permit a compiler to add
20771 implementation-dependent declarations to package @code{System}.
20773 GNAT does not take advantage of this permission, and the version of
20774 @code{System} provided by GNAT exactly matches that in Ada 95.
20776 However, HP Ada adds an extensive set of declarations to package
20778 as fully documented in the HP Ada manuals. To minimize changes required
20779 for programs that make use of these extensions, GNAT provides the pragma
20780 @code{Extend_System} for extending the definition of package System. By using:
20781 @cindex pragma @code{Extend_System}
20782 @cindex @code{Extend_System} pragma
20784 @smallexample @c ada
20787 pragma Extend_System (Aux_DEC);
20793 the set of definitions in @code{System} is extended to include those in
20794 package @code{System.Aux_DEC}.
20795 @cindex @code{System.Aux_DEC} package
20796 @cindex @code{Aux_DEC} package (child of @code{System})
20797 These definitions are incorporated directly into package @code{System},
20798 as though they had been declared there. For a
20799 list of the declarations added, see the specification of this package,
20800 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
20801 @cindex @file{s-auxdec.ads} file
20802 The pragma @code{Extend_System} is a configuration pragma, which means that
20803 it can be placed in the file @file{gnat.adc}, so that it will automatically
20804 apply to all subsequent compilations. See @ref{Configuration Pragmas},
20805 for further details.
20807 An alternative approach that avoids the use of the non-standard
20808 @code{Extend_System} pragma is to add a context clause to the unit that
20809 references these facilities:
20811 @smallexample @c ada
20813 with System.Aux_DEC;
20814 use System.Aux_DEC;
20819 The effect is not quite semantically identical to incorporating
20820 the declarations directly into package @code{System},
20821 but most programs will not notice a difference
20822 unless they use prefix notation (e.g. @code{System.Integer_8})
20823 to reference the entities directly in package @code{System}.
20824 For units containing such references,
20825 the prefixes must either be removed, or the pragma @code{Extend_System}
20828 @node Language-Related Features
20829 @section Language-Related Features
20832 The following sections highlight differences in types,
20833 representations of types, operations, alignment, and
20837 * Integer Types and Representations::
20838 * Floating-Point Types and Representations::
20839 * Pragmas Float_Representation and Long_Float::
20840 * Fixed-Point Types and Representations::
20841 * Record and Array Component Alignment::
20842 * Address Clauses::
20843 * Other Representation Clauses::
20846 @node Integer Types and Representations
20847 @subsection Integer Types and Representations
20850 The set of predefined integer types is identical in HP Ada and GNAT.
20851 Furthermore the representation of these integer types is also identical,
20852 including the capability of size clauses forcing biased representation.
20855 HP Ada for OpenVMS Alpha systems has defined the
20856 following additional integer types in package @code{System}:
20873 @code{LARGEST_INTEGER}
20877 In GNAT, the first four of these types may be obtained from the
20878 standard Ada 95 package @code{Interfaces}.
20879 Alternatively, by use of the pragma @code{Extend_System}, identical
20880 declarations can be referenced directly in package @code{System}.
20881 On both GNAT and HP Ada, the maximum integer size is 64 bits.
20883 @node Floating-Point Types and Representations
20884 @subsection Floating-Point Types and Representations
20885 @cindex Floating-Point types
20888 The set of predefined floating-point types is identical in HP Ada and GNAT.
20889 Furthermore the representation of these floating-point
20890 types is also identical. One important difference is that the default
20891 representation for HP Ada is @code{VAX_Float}, but the default representation
20894 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
20895 pragma @code{Float_Representation} as described in the HP Ada
20897 For example, the declarations:
20899 @smallexample @c ada
20901 type F_Float is digits 6;
20902 pragma Float_Representation (VAX_Float, F_Float);
20907 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
20909 This set of declarations actually appears in @code{System.Aux_DEC},
20911 the full set of additional floating-point declarations provided in
20912 the HP Ada version of package @code{System}.
20913 This and similar declarations may be accessed in a user program
20914 by using pragma @code{Extend_System}. The use of this
20915 pragma, and the related pragma @code{Long_Float} is described in further
20916 detail in the following section.
20918 @node Pragmas Float_Representation and Long_Float
20919 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
20922 HP Ada provides the pragma @code{Float_Representation}, which
20923 acts as a program library switch to allow control over
20924 the internal representation chosen for the predefined
20925 floating-point types declared in the package @code{Standard}.
20926 The format of this pragma is as follows:
20928 @smallexample @c ada
20930 pragma Float_Representation(VAX_Float | IEEE_Float);
20935 This pragma controls the representation of floating-point
20940 @code{VAX_Float} specifies that floating-point
20941 types are represented by default with the VAX system hardware types
20942 @code{F-floating}, @code{D-floating}, @code{G-floating}.
20943 Note that the @code{H-floating}
20944 type was available only on VAX systems, and is not available
20945 in either HP Ada or GNAT.
20948 @code{IEEE_Float} specifies that floating-point
20949 types are represented by default with the IEEE single and
20950 double floating-point types.
20954 GNAT provides an identical implementation of the pragma
20955 @code{Float_Representation}, except that it functions as a
20956 configuration pragma. Note that the
20957 notion of configuration pragma corresponds closely to the
20958 HP Ada notion of a program library switch.
20960 When no pragma is used in GNAT, the default is @code{IEEE_Float},
20962 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
20963 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
20964 advisable to change the format of numbers passed to standard library
20965 routines, and if necessary explicit type conversions may be needed.
20967 The use of @code{IEEE_Float} is recommended in GNAT since it is more
20968 efficient, and (given that it conforms to an international standard)
20969 potentially more portable.
20970 The situation in which @code{VAX_Float} may be useful is in interfacing
20971 to existing code and data that expect the use of @code{VAX_Float}.
20972 In such a situation use the predefined @code{VAX_Float}
20973 types in package @code{System}, as extended by
20974 @code{Extend_System}. For example, use @code{System.F_Float}
20975 to specify the 32-bit @code{F-Float} format.
20978 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
20979 to allow control over the internal representation chosen
20980 for the predefined type @code{Long_Float} and for floating-point
20981 type declarations with digits specified in the range 7 .. 15.
20982 The format of this pragma is as follows:
20984 @smallexample @c ada
20986 pragma Long_Float (D_FLOAT | G_FLOAT);
20990 @node Fixed-Point Types and Representations
20991 @subsection Fixed-Point Types and Representations
20994 On HP Ada for OpenVMS Alpha systems, rounding is
20995 away from zero for both positive and negative numbers.
20996 Therefore, @code{+0.5} rounds to @code{1},
20997 and @code{-0.5} rounds to @code{-1}.
20999 On GNAT the results of operations
21000 on fixed-point types are in accordance with the Ada 95
21001 rules. In particular, results of operations on decimal
21002 fixed-point types are truncated.
21004 @node Record and Array Component Alignment
21005 @subsection Record and Array Component Alignment
21008 On HP Ada for OpenVMS Alpha, all non composite components
21009 are aligned on natural boundaries. For example, 1-byte
21010 components are aligned on byte boundaries, 2-byte
21011 components on 2-byte boundaries, 4-byte components on 4-byte
21012 byte boundaries, and so on. The OpenVMS Alpha hardware
21013 runs more efficiently with naturally aligned data.
21015 On GNAT, alignment rules are compatible
21016 with HP Ada for OpenVMS Alpha.
21018 @node Address Clauses
21019 @subsection Address Clauses
21022 In HP Ada and GNAT, address clauses are supported for
21023 objects and imported subprograms.
21024 The predefined type @code{System.Address} is a private type
21025 in both compilers on Alpha OpenVMS, with the same representation
21026 (it is simply a machine pointer). Addition, subtraction, and comparison
21027 operations are available in the standard Ada 95 package
21028 @code{System.Storage_Elements}, or in package @code{System}
21029 if it is extended to include @code{System.Aux_DEC} using a
21030 pragma @code{Extend_System} as previously described.
21032 Note that code that @code{with}'s both this extended package @code{System}
21033 and the package @code{System.Storage_Elements} should not @code{use}
21034 both packages, or ambiguities will result. In general it is better
21035 not to mix these two sets of facilities. The Ada 95 package was
21036 designed specifically to provide the kind of features that HP Ada
21037 adds directly to package @code{System}.
21039 The type @code{System.Address} is a 64-bit integer type in GNAT for
21040 I64 OpenVMS. For more information,
21041 see @ref{Transitioning from Alpha to I64 OpenVMS}.
21043 GNAT is compatible with HP Ada in its handling of address
21044 clauses, except for some limitations in
21045 the form of address clauses for composite objects with
21046 initialization. Such address clauses are easily replaced
21047 by the use of an explicitly-defined constant as described
21048 in the Ada 95 Reference Manual (13.1(22)). For example, the sequence
21051 @smallexample @c ada
21053 X, Y : Integer := Init_Func;
21054 Q : String (X .. Y) := "abc";
21056 for Q'Address use Compute_Address;
21061 will be rejected by GNAT, since the address cannot be computed at the time
21062 that @code{Q} is declared. To achieve the intended effect, write instead:
21064 @smallexample @c ada
21067 X, Y : Integer := Init_Func;
21068 Q_Address : constant Address := Compute_Address;
21069 Q : String (X .. Y) := "abc";
21071 for Q'Address use Q_Address;
21077 which will be accepted by GNAT (and other Ada 95 compilers), and is also
21078 compatible with Ada 83. A fuller description of the restrictions
21079 on address specifications is found in the @cite{GNAT Reference Manual}.
21081 @node Other Representation Clauses
21082 @subsection Other Representation Clauses
21085 GNAT implements in a compatible manner all the representation
21086 clauses supported by HP Ada. In addition, GNAT
21087 implements the representation clause forms that were introduced in Ada 95,
21088 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
21090 @node The Package STANDARD
21091 @section The Package @code{STANDARD}
21094 The package @code{STANDARD}, as implemented by HP Ada, is fully
21095 described in the Ada 95 Reference Manual and in the HP Ada
21096 Language Reference Manual. As implemented by GNAT, the
21097 package @code{STANDARD} is described in the Ada 95 Reference
21100 In addition, HP Ada supports the Latin-1 character set in
21101 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
21102 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
21103 the type @code{WIDE_CHARACTER}.
21105 The floating-point types supported by GNAT are those
21106 supported by HP Ada, but the defaults are different, and are controlled by
21107 pragmas. See @ref{Floating-Point Types and Representations}, for details.
21109 @node The Package SYSTEM
21110 @section The Package @code{SYSTEM}
21113 HP Ada provides a specific version of the package
21114 @code{SYSTEM} for each platform on which the language is implemented.
21115 For the complete specification of the package @code{SYSTEM}, see
21116 Appendix F of the @cite{HP Ada Language Reference Manual}.
21118 On HP Ada, the package @code{SYSTEM} includes the following conversion
21121 @item @code{TO_ADDRESS(INTEGER)}
21123 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
21125 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
21127 @item @code{TO_INTEGER(ADDRESS)}
21129 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
21131 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
21132 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
21136 By default, GNAT supplies a version of @code{SYSTEM} that matches
21137 the definition given in the Ada 95 Reference Manual.
21139 is a subset of the HP system definitions, which is as
21140 close as possible to the original definitions. The only difference
21141 is that the definition of @code{SYSTEM_NAME} is different:
21143 @smallexample @c ada
21145 type Name is (SYSTEM_NAME_GNAT);
21146 System_Name : constant Name := SYSTEM_NAME_GNAT;
21151 Also, GNAT adds the new Ada 95 declarations for
21152 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
21154 However, the use of the following pragma causes GNAT
21155 to extend the definition of package @code{SYSTEM} so that it
21156 encompasses the full set of HP-specific extensions,
21157 including the functions listed above:
21159 @smallexample @c ada
21161 pragma Extend_System (Aux_DEC);
21166 The pragma @code{Extend_System} is a configuration pragma that
21167 is most conveniently placed in the @file{gnat.adc} file. See the
21168 @cite{GNAT Reference Manual} for further details.
21170 HP Ada does not allow the recompilation of the package
21171 @code{SYSTEM}. Instead HP Ada provides several pragmas
21172 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
21173 to modify values in the package @code{SYSTEM}.
21174 On OpenVMS Alpha systems, the pragma
21175 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
21176 its single argument.
21178 GNAT does permit the recompilation of package @code{SYSTEM} using
21179 the special switch @option{-gnatg}, and this switch can be used if
21180 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
21181 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
21182 or @code{MEMORY_SIZE} by any other means.
21184 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
21185 enumeration literal @code{SYSTEM_NAME_GNAT}.
21187 The definitions provided by the use of
21189 @smallexample @c ada
21190 pragma Extend_System (AUX_Dec);
21194 are virtually identical to those provided by the HP Ada 83 package
21195 @code{SYSTEM}. One important difference is that the name of the
21197 function for type @code{UNSIGNED_LONGWORD} is changed to
21198 @code{TO_ADDRESS_LONG}.
21199 See the @cite{GNAT Reference Manual} for a discussion of why this change was
21203 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
21205 an extension to Ada 83 not strictly compatible with the reference manual.
21206 GNAT, in order to be exactly compatible with the standard,
21207 does not provide this capability. In HP Ada 83, the
21208 point of this definition is to deal with a call like:
21210 @smallexample @c ada
21211 TO_ADDRESS (16#12777#);
21215 Normally, according to Ada 83 semantics, one would expect this to be
21216 ambiguous, since it matches both the @code{INTEGER} and
21217 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
21218 However, in HP Ada 83, there is no ambiguity, since the
21219 definition using @i{universal_integer} takes precedence.
21221 In GNAT, since the version with @i{universal_integer} cannot be supplied,
21223 not possible to be 100% compatible. Since there are many programs using
21224 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
21226 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
21227 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
21229 @smallexample @c ada
21230 function To_Address (X : Integer) return Address;
21231 pragma Pure_Function (To_Address);
21233 function To_Address_Long (X : Unsigned_Longword) return Address;
21234 pragma Pure_Function (To_Address_Long);
21238 This means that programs using @code{TO_ADDRESS} for
21239 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
21241 @node Tasking and Task-Related Features
21242 @section Tasking and Task-Related Features
21245 This section compares the treatment of tasking in GNAT
21246 and in HP Ada for OpenVMS Alpha.
21247 The GNAT description applies to both Alpha and I64 OpenVMS.
21248 For detailed information on tasking in
21249 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
21250 relevant run-time reference manual.
21253 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
21254 * Assigning Task IDs::
21255 * Task IDs and Delays::
21256 * Task-Related Pragmas::
21257 * Scheduling and Task Priority::
21259 * External Interrupts::
21262 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
21263 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
21266 On OpenVMS Alpha systems, each Ada task (except a passive
21267 task) is implemented as a single stream of execution
21268 that is created and managed by the kernel. On these
21269 systems, HP Ada tasking support is based on DECthreads,
21270 an implementation of the POSIX standard for threads.
21272 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
21273 code that calls DECthreads routines can be used together.
21274 The interaction between Ada tasks and DECthreads routines
21275 can have some benefits. For example when on OpenVMS Alpha,
21276 HP Ada can call C code that is already threaded.
21278 GNAT uses the facilities of DECthreads,
21279 and Ada tasks are mapped to threads.
21282 @node Assigning Task IDs
21283 @subsection Assigning Task IDs
21286 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
21287 the environment task that executes the main program. On
21288 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
21289 that have been created but are not yet activated.
21291 On OpenVMS Alpha systems, task IDs are assigned at
21292 activation. On GNAT systems, task IDs are also assigned at
21293 task creation but do not have the same form or values as
21294 task ID values in HP Ada. There is no null task, and the
21295 environment task does not have a specific task ID value.
21297 @node Task IDs and Delays
21298 @subsection Task IDs and Delays
21301 On OpenVMS Alpha systems, tasking delays are implemented
21302 using Timer System Services. The Task ID is used for the
21303 identification of the timer request (the @code{REQIDT} parameter).
21304 If Timers are used in the application take care not to use
21305 @code{0} for the identification, because cancelling such a timer
21306 will cancel all timers and may lead to unpredictable results.
21308 @node Task-Related Pragmas
21309 @subsection Task-Related Pragmas
21312 Ada supplies the pragma @code{TASK_STORAGE}, which allows
21313 specification of the size of the guard area for a task
21314 stack. (The guard area forms an area of memory that has no
21315 read or write access and thus helps in the detection of
21316 stack overflow.) On OpenVMS Alpha systems, if the pragma
21317 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
21318 area is created. In the absence of a pragma @code{TASK_STORAGE},
21319 a default guard area is created.
21321 GNAT supplies the following task-related pragmas:
21324 @item @code{TASK_INFO}
21326 This pragma appears within a task definition and
21327 applies to the task in which it appears. The argument
21328 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
21330 @item @code{TASK_STORAGE}
21332 GNAT implements pragma @code{TASK_STORAGE} in the same way as
21334 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
21335 @code{SUPPRESS}, and @code{VOLATILE}.
21337 @node Scheduling and Task Priority
21338 @subsection Scheduling and Task Priority
21341 HP Ada implements the Ada language requirement that
21342 when two tasks are eligible for execution and they have
21343 different priorities, the lower priority task does not
21344 execute while the higher priority task is waiting. The HP
21345 Ada Run-Time Library keeps a task running until either the
21346 task is suspended or a higher priority task becomes ready.
21348 On OpenVMS Alpha systems, the default strategy is round-
21349 robin with preemption. Tasks of equal priority take turns
21350 at the processor. A task is run for a certain period of
21351 time and then placed at the tail of the ready queue for
21352 its priority level.
21354 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
21355 which can be used to enable or disable round-robin
21356 scheduling of tasks with the same priority.
21357 See the relevant HP Ada run-time reference manual for
21358 information on using the pragmas to control HP Ada task
21361 GNAT follows the scheduling rules of Annex D (Real-Time
21362 Annex) of the Ada 95 Reference Manual. In general, this
21363 scheduling strategy is fully compatible with HP Ada
21364 although it provides some additional constraints (as
21365 fully documented in Annex D).
21366 GNAT implements time slicing control in a manner compatible with
21367 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
21368 are identical to the HP Ada 83 pragma of the same name.
21369 Note that it is not possible to mix GNAT tasking and
21370 HP Ada 83 tasking in the same program, since the two run-time
21371 libraries are not compatible.
21373 @node The Task Stack
21374 @subsection The Task Stack
21377 In HP Ada, a task stack is allocated each time a
21378 non-passive task is activated. As soon as the task is
21379 terminated, the storage for the task stack is deallocated.
21380 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
21381 a default stack size is used. Also, regardless of the size
21382 specified, some additional space is allocated for task
21383 management purposes. On OpenVMS Alpha systems, at least
21384 one page is allocated.
21386 GNAT handles task stacks in a similar manner. In accordance with
21387 the Ada 95 rules, it provides the pragma @code{STORAGE_SIZE} as
21388 an alternative method for controlling the task stack size.
21389 The specification of the attribute @code{T'STORAGE_SIZE} is also
21390 supported in a manner compatible with HP Ada.
21392 @node External Interrupts
21393 @subsection External Interrupts
21396 On HP Ada, external interrupts can be associated with task entries.
21397 GNAT is compatible with HP Ada in its handling of external interrupts.
21399 @node Pragmas and Pragma-Related Features
21400 @section Pragmas and Pragma-Related Features
21403 Both HP Ada and GNAT supply all language-defined pragmas
21404 as specified by the Ada 83 standard. GNAT also supplies all
21405 language-defined pragmas specified in the Ada 95 Reference Manual.
21406 In addition, GNAT implements the implementation-defined pragmas
21410 @item @code{AST_ENTRY}
21412 @item @code{COMMON_OBJECT}
21414 @item @code{COMPONENT_ALIGNMENT}
21416 @item @code{EXPORT_EXCEPTION}
21418 @item @code{EXPORT_FUNCTION}
21420 @item @code{EXPORT_OBJECT}
21422 @item @code{EXPORT_PROCEDURE}
21424 @item @code{EXPORT_VALUED_PROCEDURE}
21426 @item @code{FLOAT_REPRESENTATION}
21430 @item @code{IMPORT_EXCEPTION}
21432 @item @code{IMPORT_FUNCTION}
21434 @item @code{IMPORT_OBJECT}
21436 @item @code{IMPORT_PROCEDURE}
21438 @item @code{IMPORT_VALUED_PROCEDURE}
21440 @item @code{INLINE_GENERIC}
21442 @item @code{INTERFACE_NAME}
21444 @item @code{LONG_FLOAT}
21446 @item @code{MAIN_STORAGE}
21448 @item @code{PASSIVE}
21450 @item @code{PSET_OBJECT}
21452 @item @code{SHARE_GENERIC}
21454 @item @code{SUPPRESS_ALL}
21456 @item @code{TASK_STORAGE}
21458 @item @code{TIME_SLICE}
21464 These pragmas are all fully implemented, with the exception of @code{TITLE},
21465 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
21466 recognized, but which have no
21467 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
21468 use of protected objects in Ada 95. In GNAT, all generics are inlined.
21470 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
21471 a separate subprogram specification which must appear before the
21474 GNAT also supplies a number of implementation-defined pragmas as follows:
21476 @item @code{ABORT_DEFER}
21478 @item @code{ADA_83}
21480 @item @code{ADA_95}
21482 @item @code{ADA_05}
21484 @item @code{ANNOTATE}
21486 @item @code{ASSERT}
21488 @item @code{C_PASS_BY_COPY}
21490 @item @code{CPP_CLASS}
21492 @item @code{CPP_CONSTRUCTOR}
21494 @item @code{CPP_DESTRUCTOR}
21496 @item @code{CPP_VIRTUAL}
21498 @item @code{CPP_VTABLE}
21502 @item @code{EXTEND_SYSTEM}
21504 @item @code{LINKER_ALIAS}
21506 @item @code{LINKER_SECTION}
21508 @item @code{MACHINE_ATTRIBUTE}
21510 @item @code{NO_RETURN}
21512 @item @code{PURE_FUNCTION}
21514 @item @code{SOURCE_FILE_NAME}
21516 @item @code{SOURCE_REFERENCE}
21518 @item @code{TASK_INFO}
21520 @item @code{UNCHECKED_UNION}
21522 @item @code{UNIMPLEMENTED_UNIT}
21524 @item @code{UNIVERSAL_DATA}
21526 @item @code{UNSUPPRESS}
21528 @item @code{WARNINGS}
21530 @item @code{WEAK_EXTERNAL}
21534 For full details on these GNAT implementation-defined pragmas, see
21535 the GNAT Reference Manual.
21538 * Restrictions on the Pragma INLINE::
21539 * Restrictions on the Pragma INTERFACE::
21540 * Restrictions on the Pragma SYSTEM_NAME::
21543 @node Restrictions on the Pragma INLINE
21544 @subsection Restrictions on Pragma @code{INLINE}
21547 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
21549 @item Parameters cannot have a task type.
21551 @item Function results cannot be task types, unconstrained
21552 array types, or unconstrained types with discriminants.
21554 @item Bodies cannot declare the following:
21556 @item Subprogram body or stub (imported subprogram is allowed)
21560 @item Generic declarations
21562 @item Instantiations
21566 @item Access types (types derived from access types allowed)
21568 @item Array or record types
21570 @item Dependent tasks
21572 @item Direct recursive calls of subprogram or containing
21573 subprogram, directly or via a renaming
21579 In GNAT, the only restriction on pragma @code{INLINE} is that the
21580 body must occur before the call if both are in the same
21581 unit, and the size must be appropriately small. There are
21582 no other specific restrictions which cause subprograms to
21583 be incapable of being inlined.
21585 @node Restrictions on the Pragma INTERFACE
21586 @subsection Restrictions on Pragma @code{INTERFACE}
21589 The following restrictions on pragma @code{INTERFACE}
21590 are enforced by both HP Ada and GNAT:
21592 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
21593 Default is the default on OpenVMS Alpha systems.
21595 @item Parameter passing: Language specifies default
21596 mechanisms but can be overridden with an @code{EXPORT} pragma.
21599 @item Ada: Use internal Ada rules.
21601 @item Bliss, C: Parameters must be mode @code{in}; cannot be
21602 record or task type. Result cannot be a string, an
21603 array, or a record.
21605 @item Fortran: Parameters cannot have a task type. Result cannot
21606 be a string, an array, or a record.
21611 GNAT is entirely upwards compatible with HP Ada, and in addition allows
21612 record parameters for all languages.
21614 @node Restrictions on the Pragma SYSTEM_NAME
21615 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
21618 For HP Ada for OpenVMS Alpha, the enumeration literal
21619 for the type @code{NAME} is @code{OPENVMS_AXP}.
21620 In GNAT, the enumeration
21621 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
21623 @node Library of Predefined Units
21624 @section Library of Predefined Units
21627 A library of predefined units is provided as part of the
21628 HP Ada and GNAT implementations. HP Ada does not provide
21629 the package @code{MACHINE_CODE} but instead recommends importing
21632 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
21633 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
21635 The HP Ada Predefined Library units are modified to remove Ada 95
21636 incompatibilities and to make them interoperable with GNAT
21637 (@pxref{Changes to DECLIB}, for details).
21638 The units are located in the @file{DECLIB} directory.
21641 The GNAT RTL is contained in
21642 the @file{ADALIB} directory, and
21643 the default search path is set up to find @code{DECLIB} units in preference
21644 to @code{ADALIB} units with the same name (@code{TEXT_IO},
21645 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
21649 * Changes to DECLIB::
21652 @node Changes to DECLIB
21653 @subsection Changes to @code{DECLIB}
21656 The changes made to the HP Ada predefined library for GNAT and Ada 95
21657 compatibility are minor and include the following:
21660 @item Adjusting the location of pragmas and record representation
21661 clauses to obey Ada 95 rules
21663 @item Adding the proper notation to generic formal parameters
21664 that take unconstrained types in instantiation
21666 @item Adding pragma @code{ELABORATE_BODY} to package specifications
21667 that have package bodies not otherwise allowed
21669 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
21670 ``@code{PROTECTD}''.
21671 Currently these are found only in the @code{STARLET} package spec.
21673 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
21674 where the address size is constrained to 32 bits.
21678 None of the above changes is visible to users.
21684 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
21687 @item Command Language Interpreter (CLI interface)
21689 @item DECtalk Run-Time Library (DTK interface)
21691 @item Librarian utility routines (LBR interface)
21693 @item General Purpose Run-Time Library (LIB interface)
21695 @item Math Run-Time Library (MTH interface)
21697 @item National Character Set Run-Time Library (NCS interface)
21699 @item Compiled Code Support Run-Time Library (OTS interface)
21701 @item Parallel Processing Run-Time Library (PPL interface)
21703 @item Screen Management Run-Time Library (SMG interface)
21705 @item Sort Run-Time Library (SOR interface)
21707 @item String Run-Time Library (STR interface)
21709 @item STARLET System Library
21712 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
21714 @item X Windows Toolkit (XT interface)
21716 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
21720 GNAT provides implementations of these HP bindings in the @code{DECLIB}
21723 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
21725 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
21726 A pragma @code{Linker_Options} has been added to packages @code{Xm},
21727 @code{Xt}, and @code{X_Lib}
21728 causing the default X/Motif sharable image libraries to be linked in. This
21729 is done via options files named @file{xm.opt}, @file{xt.opt}, and
21730 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
21732 It may be necessary to edit these options files to update or correct the
21733 library names if, for example, the newer X/Motif bindings from
21734 @file{ADA$EXAMPLES}
21735 had been (previous to installing GNAT) copied and renamed to supersede the
21736 default @file{ADA$PREDEFINED} versions.
21739 * Shared Libraries and Options Files::
21740 * Interfaces to C::
21743 @node Shared Libraries and Options Files
21744 @subsection Shared Libraries and Options Files
21747 When using the HP Ada
21748 predefined X and Motif bindings, the linking with their sharable images is
21749 done automatically by @command{GNAT LINK}.
21750 When using other X and Motif bindings, you need
21751 to add the corresponding sharable images to the command line for
21752 @code{GNAT LINK}. When linking with shared libraries, or with
21753 @file{.OPT} files, you must
21754 also add them to the command line for @command{GNAT LINK}.
21756 A shared library to be used with GNAT is built in the same way as other
21757 libraries under VMS. The VMS Link command can be used in standard fashion.
21759 @node Interfaces to C
21760 @subsection Interfaces to C
21764 provides the following Ada types and operations:
21767 @item C types package (@code{C_TYPES})
21769 @item C strings (@code{C_TYPES.NULL_TERMINATED})
21771 @item Other_types (@code{SHORT_INT})
21775 Interfacing to C with GNAT, you can use the above approach
21776 described for HP Ada or the facilities of Annex B of
21777 the Ada 95 Reference Manual (packages @code{INTERFACES.C},
21778 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
21779 information, see the section ``Interfacing to C'' in the
21780 @cite{GNAT Reference Manual}.
21782 The @option{-gnatF} qualifier forces default and explicit
21783 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
21784 to be uppercased for compatibility with the default behavior
21785 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
21787 @node Main Program Definition
21788 @section Main Program Definition
21791 The following section discusses differences in the
21792 definition of main programs on HP Ada and GNAT.
21793 On HP Ada, main programs are defined to meet the
21794 following conditions:
21796 @item Procedure with no formal parameters (returns @code{0} upon
21799 @item Procedure with no formal parameters (returns @code{42} when
21800 an unhandled exception is raised)
21802 @item Function with no formal parameters whose returned value
21803 is of a discrete type
21805 @item Procedure with one @code{out} formal of a discrete type for
21806 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE}
21812 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
21813 a main function or main procedure returns a discrete
21814 value whose size is less than 64 bits (32 on VAX systems),
21815 the value is zero- or sign-extended as appropriate.
21816 On GNAT, main programs are defined as follows:
21818 @item Must be a non-generic, parameterless subprogram that
21819 is either a procedure or function returning an Ada
21820 @code{STANDARD.INTEGER} (the predefined type)
21822 @item Cannot be a generic subprogram or an instantiation of a
21826 @node Implementation-Defined Attributes
21827 @section Implementation-Defined Attributes
21830 GNAT provides all HP Ada implementation-defined
21833 @node Compiler and Run-Time Interfacing
21834 @section Compiler and Run-Time Interfacing
21837 HP Ada provides the following qualifiers to pass options to the linker
21840 @item @option{/WAIT} and @option{/SUBMIT}
21842 @item @option{/COMMAND}
21844 @item @option{/[NO]MAP}
21846 @item @option{/OUTPUT=@i{file-spec}}
21848 @item @option{/[NO]DEBUG} and @option{/[NO]TRACEBACK}
21852 To pass options to the linker, GNAT provides the following
21856 @item @option{/EXECUTABLE=@i{exec-name}}
21858 @item @option{/VERBOSE}
21860 @item @option{/[NO]DEBUG} and @option{/[NO]TRACEBACK}
21864 For more information on these switches, see
21865 @ref{Switches for gnatlink}.
21866 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
21867 to control optimization. HP Ada also supplies the
21870 @item @code{OPTIMIZE}
21872 @item @code{INLINE}
21874 @item @code{INLINE_GENERIC}
21876 @item @code{SUPPRESS_ALL}
21878 @item @code{PASSIVE}
21882 In GNAT, optimization is controlled strictly by command
21883 line parameters, as described in the corresponding section of this guide.
21884 The HP pragmas for control of optimization are
21885 recognized but ignored.
21887 Note that in GNAT, the default is optimization off, whereas in HP Ada
21888 the default is that optimization is turned on.
21890 @node Program Compilation and Library Management
21891 @section Program Compilation and Library Management
21894 HP Ada and GNAT provide a comparable set of commands to
21895 build programs. HP Ada also provides a program library,
21896 which is a concept that does not exist on GNAT. Instead,
21897 GNAT provides directories of sources that are compiled as
21900 The following table summarizes
21901 the HP Ada commands and provides
21902 equivalent GNAT commands. In this table, some GNAT
21903 equivalents reflect the fact that GNAT does not use the
21904 concept of a program library. Instead, it uses a model
21905 in which collections of source and object files are used
21906 in a manner consistent with other languages like C and
21907 Fortran. Therefore, standard system file commands are used
21908 to manipulate these elements. Those GNAT commands are marked with
21910 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
21913 @multitable @columnfractions .35 .65
21915 @item @emph{HP Ada Command}
21916 @tab @emph{GNAT Equivalent / Description}
21918 @item @command{ADA}
21919 @tab @command{GNAT COMPILE}@*
21920 Invokes the compiler to compile one or more Ada source files.
21922 @item @command{ACS ATTACH}@*
21923 @tab [No equivalent]@*
21924 Switches control of terminal from current process running the program
21927 @item @command{ACS CHECK}
21928 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
21929 Forms the execution closure of one
21930 or more compiled units and checks completeness and currency.
21932 @item @command{ACS COMPILE}
21933 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
21934 Forms the execution closure of one or
21935 more specified units, checks completeness and currency,
21936 identifies units that have revised source files, compiles same,
21937 and recompiles units that are or will become obsolete.
21938 Also completes incomplete generic instantiations.
21940 @item @command{ACS COPY FOREIGN}
21942 Copies a foreign object file into the program library as a
21945 @item @command{ACS COPY UNIT}
21947 Copies a compiled unit from one program library to another.
21949 @item @command{ACS CREATE LIBRARY}
21950 @tab Create /directory (*)@*
21951 Creates a program library.
21953 @item @command{ACS CREATE SUBLIBRARY}
21954 @tab Create /directory (*)@*
21955 Creates a program sublibrary.
21957 @item @command{ACS DELETE LIBRARY}
21959 Deletes a program library and its contents.
21961 @item @command{ACS DELETE SUBLIBRARY}
21963 Deletes a program sublibrary and its contents.
21965 @item @command{ACS DELETE UNIT}
21966 @tab Delete file (*)@*
21967 On OpenVMS systems, deletes one or more compiled units from
21968 the current program library.
21970 @item @command{ACS DIRECTORY}
21971 @tab Directory (*)@*
21972 On OpenVMS systems, lists units contained in the current
21975 @item @command{ACS ENTER FOREIGN}
21977 Allows the import of a foreign body as an Ada library
21978 specification and enters a reference to a pointer.
21980 @item @command{ACS ENTER UNIT}
21982 Enters a reference (pointer) from the current program library to
21983 a unit compiled into another program library.
21985 @item @command{ACS EXIT}
21986 @tab [No equivalent]@*
21987 Exits from the program library manager.
21989 @item @command{ACS EXPORT}
21991 Creates an object file that contains system-specific object code
21992 for one or more units. With GNAT, object files can simply be copied
21993 into the desired directory.
21995 @item @command{ACS EXTRACT SOURCE}
21997 Allows access to the copied source file for each Ada compilation unit
21999 @item @command{ACS HELP}
22000 @tab @command{HELP GNAT}@*
22001 Provides online help.
22003 @item @command{ACS LINK}
22004 @tab @command{GNAT LINK}@*
22005 Links an object file containing Ada units into an executable file.
22007 @item @command{ACS LOAD}
22009 Loads (partially compiles) Ada units into the program library.
22010 Allows loading a program from a collection of files into a library
22011 without knowing the relationship among units.
22013 @item @command{ACS MERGE}
22015 Merges into the current program library, one or more units from
22016 another library where they were modified.
22018 @item @command{ACS RECOMPILE}
22019 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
22020 Recompiles from external or copied source files any obsolete
22021 unit in the closure. Also, completes any incomplete generic
22024 @item @command{ACS REENTER}
22025 @tab @command{GNAT MAKE}@*
22026 Reenters current references to units compiled after last entered
22027 with the @command{ACS ENTER UNIT} command.
22029 @item @command{ACS SET LIBRARY}
22030 @tab Set default (*)@*
22031 Defines a program library to be the compilation context as well
22032 as the target library for compiler output and commands in general.
22034 @item @command{ACS SET PRAGMA}
22035 @tab Edit @file{gnat.adc} (*)@*
22036 Redefines specified values of the library characteristics
22037 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
22038 and @code{Float_Representation}.
22040 @item @command{ACS SET SOURCE}
22041 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
22042 Defines the source file search list for the @command{ACS COMPILE} command.
22044 @item @command{ACS SHOW LIBRARY}
22045 @tab Directory (*)@*
22046 Lists information about one or more program libraries.
22048 @item @command{ACS SHOW PROGRAM}
22049 @tab [No equivalent]@*
22050 Lists information about the execution closure of one or
22051 more units in the program library.
22053 @item @command{ACS SHOW SOURCE}
22054 @tab Show logical @code{ADA_INCLUDE_PATH}@*
22055 Shows the source file search used when compiling units.
22057 @item @command{ACS SHOW VERSION}
22058 @tab Compile with @option{VERBOSE} option
22059 Displays the version number of the compiler and program library
22062 @item @command{ACS SPAWN}
22063 @tab [No equivalent]@*
22064 Creates a subprocess of the current process (same as @command{DCL SPAWN}
22067 @item @command{ACS VERIFY}
22068 @tab [No equivalent]@*
22069 Performs a series of consistency checks on a program library to
22070 determine whether the library structure and library files are in
22077 @section Input-Output
22080 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
22081 Management Services (RMS) to perform operations on
22085 HP Ada and GNAT predefine an identical set of input-
22086 output packages. To make the use of the
22087 generic @code{TEXT_IO} operations more convenient, HP Ada
22088 provides predefined library packages that instantiate the
22089 integer and floating-point operations for the predefined
22090 integer and floating-point types as shown in the following table.
22092 @multitable @columnfractions .45 .55
22093 @item @emph{Package Name} @tab Instantiation
22095 @item @code{INTEGER_TEXT_IO}
22096 @tab @code{INTEGER_IO(INTEGER)}
22098 @item @code{SHORT_INTEGER_TEXT_IO}
22099 @tab @code{INTEGER_IO(SHORT_INTEGER)}
22101 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
22102 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
22104 @item @code{FLOAT_TEXT_IO}
22105 @tab @code{FLOAT_IO(FLOAT)}
22107 @item @code{LONG_FLOAT_TEXT_IO}
22108 @tab @code{FLOAT_IO(LONG_FLOAT)}
22112 The HP Ada predefined packages and their operations
22113 are implemented using OpenVMS Alpha files and input-output
22114 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
22115 Familiarity with the following is recommended:
22117 @item RMS file organizations and access methods
22119 @item OpenVMS file specifications and directories
22121 @item OpenVMS File Definition Language (FDL)
22125 GNAT provides I/O facilities that are completely
22126 compatible with HP Ada. The distribution includes the
22127 standard HP Ada versions of all I/O packages, operating
22128 in a manner compatible with HP Ada. In particular, the
22129 following packages are by default the HP Ada (Ada 83)
22130 versions of these packages rather than the renamings
22131 suggested in Annex J of the Ada 95 Reference Manual:
22133 @item @code{TEXT_IO}
22135 @item @code{SEQUENTIAL_IO}
22137 @item @code{DIRECT_IO}
22141 The use of the standard Ada 95 syntax for child packages (for
22142 example, @code{ADA.TEXT_IO}) retrieves the Ada 95 versions of these
22143 packages, as defined in the Ada 95 Reference Manual.
22144 GNAT provides HP-compatible predefined instantiations
22145 of the @code{TEXT_IO} packages, and also
22146 provides the standard predefined instantiations required
22147 by the Ada 95 Reference Manual.
22149 For further information on how GNAT interfaces to the file
22150 system or how I/O is implemented in programs written in
22151 mixed languages, see the chapter ``Implementation of the
22152 Standard I/O'' in the @cite{GNAT Reference Manual}.
22153 This chapter covers the following:
22155 @item Standard I/O packages
22157 @item @code{FORM} strings
22159 @item @code{ADA.DIRECT_IO}
22161 @item @code{ADA.SEQUENTIAL_IO}
22163 @item @code{ADA.TEXT_IO}
22165 @item Stream pointer positioning
22167 @item Reading and writing non-regular files
22169 @item @code{GET_IMMEDIATE}
22171 @item Treating @code{TEXT_IO} files as streams
22178 @node Implementation Limits
22179 @section Implementation Limits
22182 The following table lists implementation limits for HP Ada
22184 @multitable @columnfractions .60 .20 .20
22186 @item @emph{Compilation Parameter}
22191 @item In a subprogram or entry declaration, maximum number of
22192 formal parameters that are of an unconstrained record type
22197 @item Maximum identifier length (number of characters)
22202 @item Maximum number of characters in a source line
22207 @item Maximum collection size (number of bytes)
22212 @item Maximum number of discriminants for a record type
22217 @item Maximum number of formal parameters in an entry or
22218 subprogram declaration
22223 @item Maximum number of dimensions in an array type
22228 @item Maximum number of library units and subunits in a compilation.
22233 @item Maximum number of library units and subunits in an execution.
22238 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
22239 or @code{PSECT_OBJECT}
22244 @item Maximum number of enumeration literals in an enumeration type
22250 @item Maximum number of lines in a source file
22255 @item Maximum number of bits in any object
22260 @item Maximum size of the static portion of a stack frame (approximate)
22265 @node Tools and Utilities
22266 @section Tools and Utilities
22269 The following table lists some of the OpenVMS development tools
22270 available for HP Ada, and the corresponding tools for
22271 use with @value{EDITION} on Alpha and I64 platforms.
22272 Aside from the debugger, all the OpenVMS tools identified are part
22273 of the DECset package.
22277 @c Specify table in TeX since Texinfo does a poor job
22281 \settabs\+Language-Sensitive Editor\quad
22282 &Product with HP Ada\quad
22285 &\it Product with HP Ada
22286 & \it Product with GNAT Pro\cr
22288 \+Code Management System
22292 \+Language-Sensitive Editor
22294 & emacs or HP LSE (Alpha)\cr
22304 & OpenVMS Debug (I64)\cr
22306 \+Source Code Analyzer /
22323 \+Coverage Analyzer
22327 \+Module Management
22329 & Not applicable\cr
22339 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
22340 @c the TeX version above for the printed version
22342 @c @multitable @columnfractions .3 .4 .4
22343 @multitable {Source Code Analyzer /}{Product with HP Ada}{Product with GNAT Pro}
22345 @tab @i{Product with HP Ada}
22346 @tab @i{Product with @value{EDITION}}
22347 @item Code Management@*System
22350 @item Language-Sensitive@*Editor
22352 @tab emacs or HP LSE (Alpha)
22361 @tab OpenVMS Debug (I64)
22362 @item Source Code Analyzer /@*Cross Referencer
22366 @tab HP Digital Test@*Manager (DTM)
22368 @item Performance and@*Coverage Analyzer
22371 @item Module Management@*System
22373 @tab Not applicable
22381 @c **************************************
22382 @node Platform-Specific Information for the Run-Time Libraries
22383 @appendix Platform-Specific Information for the Run-Time Libraries
22384 @cindex Tasking and threads libraries
22385 @cindex Threads libraries and tasking
22386 @cindex Run-time libraries (platform-specific information)
22389 The GNAT run-time implementation may vary with respect to both the
22390 underlying threads library and the exception handling scheme.
22391 For threads support, one or more of the following are supplied:
22393 @item @b{native threads library}, a binding to the thread package from
22394 the underlying operating system
22396 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
22397 POSIX thread package
22401 For exception handling, either or both of two models are supplied:
22403 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
22404 Most programs should experience a substantial speed improvement by
22405 being compiled with a ZCX run-time.
22406 This is especially true for
22407 tasking applications or applications with many exception handlers.}
22408 @cindex Zero-Cost Exceptions
22409 @cindex ZCX (Zero-Cost Exceptions)
22410 which uses binder-generated tables that
22411 are interrogated at run time to locate a handler
22413 @item @b{setjmp / longjmp} (``SJLJ''),
22414 @cindex setjmp/longjmp Exception Model
22415 @cindex SJLJ (setjmp/longjmp Exception Model)
22416 which uses dynamically-set data to establish
22417 the set of handlers
22421 This appendix summarizes which combinations of threads and exception support
22422 are supplied on various GNAT platforms.
22423 It then shows how to select a particular library either
22424 permanently or temporarily,
22425 explains the properties of (and tradeoffs among) the various threads
22426 libraries, and provides some additional
22427 information about several specific platforms.
22430 * Summary of Run-Time Configurations::
22431 * Specifying a Run-Time Library::
22432 * Choosing the Scheduling Policy::
22433 * Solaris-Specific Considerations::
22434 * Linux-Specific Considerations::
22435 * AIX-Specific Considerations::
22438 @node Summary of Run-Time Configurations
22439 @section Summary of Run-Time Configurations
22441 @multitable @columnfractions .30 .70
22442 @item @b{alpha-openvms}
22443 @item @code{@ @ }@i{rts-native (default)}
22444 @item @code{@ @ @ @ }Tasking @tab native VMS threads
22445 @item @code{@ @ @ @ }Exceptions @tab ZCX
22447 @item @b{alpha-tru64}
22448 @item @code{@ @ }@i{rts-native (default)}
22449 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
22450 @item @code{@ @ @ @ }Exceptions @tab ZCX
22452 @item @code{@ @ }@i{rts-sjlj}
22453 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
22454 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22456 @item @b{ia64-hp_linux}
22457 @item @code{@ @ }@i{rts-native (default)}
22458 @item @code{@ @ @ @ }Tasking @tab pthread library
22459 @item @code{@ @ @ @ }Exceptions @tab ZCX
22461 @item @b{ia64-hpux}
22462 @item @code{@ @ }@i{rts-native (default)}
22463 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
22464 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22466 @item @b{ia64-openvms}
22467 @item @code{@ @ }@i{rts-native (default)}
22468 @item @code{@ @ @ @ }Tasking @tab native VMS threads
22469 @item @code{@ @ @ @ }Exceptions @tab ZCX
22471 @item @b{ia64-sgi_linux}
22472 @item @code{@ @ }@i{rts-native (default)}
22473 @item @code{@ @ @ @ }Tasking @tab pthread library
22474 @item @code{@ @ @ @ }Exceptions @tab ZCX
22476 @item @b{mips-irix}
22477 @item @code{@ @ }@i{rts-native (default)}
22478 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
22479 @item @code{@ @ @ @ }Exceptions @tab ZCX
22482 @item @code{@ @ }@i{rts-native (default)}
22483 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
22484 @item @code{@ @ @ @ }Exceptions @tab ZCX
22486 @item @code{@ @ }@i{rts-sjlj}
22487 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
22488 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22491 @item @code{@ @ }@i{rts-native (default)}
22492 @item @code{@ @ @ @ }Tasking @tab native AIX threads
22493 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22495 @item @b{ppc-darwin}
22496 @item @code{@ @ }@i{rts-native (default)}
22497 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
22498 @item @code{@ @ @ @ }Exceptions @tab ZCX
22500 @item @b{sparc-solaris} @tab
22501 @item @code{@ @ }@i{rts-native (default)}
22502 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
22503 @item @code{@ @ @ @ }Exceptions @tab ZCX
22505 @item @code{@ @ }@i{rts-m64}
22506 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
22507 @item @code{@ @ @ @ }Exceptions @tab ZCX
22508 @item @code{@ @ @ @ }Constraints @tab Use only when compiling in 64-bit mode;
22509 @item @tab Use only on Solaris 8 or later.
22510 @item @tab @xref{Building and Debugging 64-bit Applications}, for details.
22512 @item @code{@ @ }@i{rts-pthread}
22513 @item @code{@ @ @ @ }Tasking @tab pthread library
22514 @item @code{@ @ @ @ }Exceptions @tab ZCX
22516 @item @code{@ @ }@i{rts-sjlj}
22517 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
22518 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22520 @item @b{x86-linux}
22521 @item @code{@ @ }@i{rts-native (default)}
22522 @item @code{@ @ @ @ }Tasking @tab pthread library
22523 @item @code{@ @ @ @ }Exceptions @tab ZCX
22525 @item @code{@ @ }@i{rts-sjlj}
22526 @item @code{@ @ @ @ }Tasking @tab pthread library
22527 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22530 @item @code{@ @ }@i{rts-native (default)}
22531 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
22532 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22534 @item @b{x86-windows}
22535 @item @code{@ @ }@i{rts-native (default)}
22536 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
22537 @item @code{@ @ @ @ }Exceptions @tab ZCX
22539 @item @code{@ @ }@i{rts-sjlj (default)}
22540 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
22541 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22543 @item @b{x86_64-linux}
22544 @item @code{@ @ }@i{rts-native (default)}
22545 @item @code{@ @ @ @ }Tasking @tab pthread library
22546 @item @code{@ @ @ @ }Exceptions @tab ZCX
22548 @item @code{@ @ }@i{rts-sjlj}
22549 @item @code{@ @ @ @ }Tasking @tab pthread library
22550 @item @code{@ @ @ @ }Exceptions @tab SJLJ
22554 @node Specifying a Run-Time Library
22555 @section Specifying a Run-Time Library
22558 The @file{adainclude} subdirectory containing the sources of the GNAT
22559 run-time library, and the @file{adalib} subdirectory containing the
22560 @file{ALI} files and the static and/or shared GNAT library, are located
22561 in the gcc target-dependent area:
22564 target=$prefix/lib/gcc-lib/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
22568 As indicated above, on some platforms several run-time libraries are supplied.
22569 These libraries are installed in the target dependent area and
22570 contain a complete source and binary subdirectory. The detailed description
22571 below explains the differences between the different libraries in terms of
22572 their thread support.
22574 The default run-time library (when GNAT is installed) is @emph{rts-native}.
22575 This default run time is selected by the means of soft links.
22576 For example on x86-linux:
22582 +--- adainclude----------+
22584 +--- adalib-----------+ |
22586 +--- rts-native | |
22588 | +--- adainclude <---+
22590 | +--- adalib <----+
22601 If the @i{rts-sjlj} library is to be selected on a permanent basis,
22602 these soft links can be modified with the following commands:
22606 $ rm -f adainclude adalib
22607 $ ln -s rts-sjlj/adainclude adainclude
22608 $ ln -s rts-sjlj/adalib adalib
22612 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
22613 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
22614 @file{$target/ada_object_path}.
22616 Selecting another run-time library temporarily can be
22617 achieved by the regular mechanism for GNAT object or source path selection:
22621 Set the environment variables:
22624 $ ADA_INCLUDE_PATH=$target/rts-sjlj/adainclude:$ADA_INCLUDE_PATH
22625 $ ADA_OBJECTS_PATH=$target/rts-sjlj/adalib:$ADA_OBJECTS_PATH
22626 $ export ADA_INCLUDE_PATH ADA_OBJECTS_PATH
22630 Use @option{-aI$target/rts-sjlj/adainclude}
22631 and @option{-aO$target/rts-sjlj/adalib}
22632 on the @command{gnatmake} command line
22635 Use the switch @option{--RTS}; e.g., @option{--RTS=sjlj}
22636 @cindex @option{--RTS} option
22639 @node Choosing the Scheduling Policy
22640 @section Choosing the Scheduling Policy
22643 When using a POSIX threads implementation, you have a choice of several
22644 scheduling policies: @code{SCHED_FIFO},
22645 @cindex @code{SCHED_FIFO} scheduling policy
22647 @cindex @code{SCHED_RR} scheduling policy
22648 and @code{SCHED_OTHER}.
22649 @cindex @code{SCHED_OTHER} scheduling policy
22650 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
22651 or @code{SCHED_RR} requires special (e.g., root) privileges.
22653 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
22655 @cindex @code{SCHED_FIFO} scheduling policy
22656 you can use one of the following:
22660 @code{pragma Time_Slice (0.0)}
22661 @cindex pragma Time_Slice
22663 the corresponding binder option @option{-T0}
22664 @cindex @option{-T0} option
22666 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
22667 @cindex pragma Task_Dispatching_Policy
22671 To specify @code{SCHED_RR},
22672 @cindex @code{SCHED_RR} scheduling policy
22673 you should use @code{pragma Time_Slice} with a
22674 value greater than @code{0.0}, or else use the corresponding @option{-T}
22677 @node Solaris-Specific Considerations
22678 @section Solaris-Specific Considerations
22679 @cindex Solaris Sparc threads libraries
22682 This section addresses some topics related to the various threads libraries
22683 on Sparc Solaris and then provides some information on building and
22684 debugging 64-bit applications.
22687 * Solaris Threads Issues::
22688 * Building and Debugging 64-bit Applications::
22691 @node Solaris Threads Issues
22692 @subsection Solaris Threads Issues
22695 GNAT under Solaris comes with an alternate tasking run-time library
22696 based on POSIX threads --- @emph{rts-pthread}.
22697 @cindex rts-pthread threads library
22698 This run-time library has the advantage of being mostly shared across all
22699 POSIX-compliant thread implementations, and it also provides under
22700 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
22701 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
22702 and @code{PTHREAD_PRIO_PROTECT}
22703 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
22704 semantics that can be selected using the predefined pragma
22705 @code{Locking_Policy}
22706 @cindex pragma Locking_Policy (under rts-pthread)
22708 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
22709 @cindex @code{Inheritance_Locking} (under rts-pthread)
22710 @cindex @code{Ceiling_Locking} (under rts-pthread)
22712 As explained above, the native run-time library is based on the Solaris thread
22713 library (@code{libthread}) and is the default library.
22715 When the Solaris threads library is used (this is the default), programs
22716 compiled with GNAT can automatically take advantage of
22717 and can thus execute on multiple processors.
22718 The user can alternatively specify a processor on which the program should run
22719 to emulate a single-processor system. The multiprocessor / uniprocessor choice
22721 setting the environment variable @code{GNAT_PROCESSOR}
22722 @cindex @code{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
22723 to one of the following:
22727 Use the default configuration (run the program on all
22728 available processors) - this is the same as having
22729 @code{GNAT_PROCESSOR} unset
22732 Let the run-time implementation choose one processor and run the program on
22735 @item 0 .. Last_Proc
22736 Run the program on the specified processor.
22737 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
22738 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
22741 @node Building and Debugging 64-bit Applications
22742 @subsection Building and Debugging 64-bit Applications
22745 In a 64-bit application, all the sources involved must be compiled with the
22746 @option{-m64} command-line option, and a specific GNAT library (compiled with
22747 this option) is required.
22748 The easiest way to build a 64bit application is to add
22749 @option{-m64 --RTS=m64} to the @command{gnatmake} flags.
22751 To debug these applications, a special version of gdb called @command{gdb64}
22754 To summarize, building and debugging a ``Hello World'' program in 64-bit mode
22758 $ gnatmake -m64 -g --RTS=m64 hello.adb
22762 @node Linux-Specific Considerations
22763 @section Linux-Specific Considerations
22764 @cindex Linux threads libraries
22767 On GNU/Linux without NPTL support (usually system with GNU C Library
22768 older than 2.3), the signal model is not POSIX compliant, which means
22769 that to send a signal to the process, you need to send the signal to all
22770 threads, e.g. by using @code{killpg()}.
22772 @node AIX-Specific Considerations
22773 @section AIX-Specific Considerations
22774 @cindex AIX resolver library
22777 On AIX, the resolver library initializes some internal structure on
22778 the first call to @code{get*by*} functions, which are used to implement
22779 @code{GNAT.Sockets.Get_Host_By_Name} and
22780 @code{GNAT.Sockets.Get_Host_By_Addrss}.
22781 If such initialization occurs within an Ada task, and the stack size for
22782 the task is the default size, a stack overflow may occur.
22784 To avoid this overflow, the user should either ensure that the first call
22785 to @code{GNAT.Sockets.Get_Host_By_Name} or
22786 @code{GNAT.Sockets.Get_Host_By_Addrss}
22787 occurs in the environment task, or use @code{pragma Storage_Size} to
22788 specify a sufficiently large size for the stack of the task that contains
22791 @c *******************************
22792 @node Example of Binder Output File
22793 @appendix Example of Binder Output File
22796 This Appendix displays the source code for @command{gnatbind}'s output
22797 file generated for a simple ``Hello World'' program.
22798 Comments have been added for clarification purposes.
22800 @smallexample @c adanocomment
22804 -- The package is called Ada_Main unless this name is actually used
22805 -- as a unit name in the partition, in which case some other unique
22809 package ada_main is
22811 Elab_Final_Code : Integer;
22812 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
22814 -- The main program saves the parameters (argument count,
22815 -- argument values, environment pointer) in global variables
22816 -- for later access by other units including
22817 -- Ada.Command_Line.
22819 gnat_argc : Integer;
22820 gnat_argv : System.Address;
22821 gnat_envp : System.Address;
22823 -- The actual variables are stored in a library routine. This
22824 -- is useful for some shared library situations, where there
22825 -- are problems if variables are not in the library.
22827 pragma Import (C, gnat_argc);
22828 pragma Import (C, gnat_argv);
22829 pragma Import (C, gnat_envp);
22831 -- The exit status is similarly an external location
22833 gnat_exit_status : Integer;
22834 pragma Import (C, gnat_exit_status);
22836 GNAT_Version : constant String :=
22837 "GNAT Version: 3.15w (20010315)";
22838 pragma Export (C, GNAT_Version, "__gnat_version");
22840 -- This is the generated adafinal routine that performs
22841 -- finalization at the end of execution. In the case where
22842 -- Ada is the main program, this main program makes a call
22843 -- to adafinal at program termination.
22845 procedure adafinal;
22846 pragma Export (C, adafinal, "adafinal");
22848 -- This is the generated adainit routine that performs
22849 -- initialization at the start of execution. In the case
22850 -- where Ada is the main program, this main program makes
22851 -- a call to adainit at program startup.
22854 pragma Export (C, adainit, "adainit");
22856 -- This routine is called at the start of execution. It is
22857 -- a dummy routine that is used by the debugger to breakpoint
22858 -- at the start of execution.
22860 procedure Break_Start;
22861 pragma Import (C, Break_Start, "__gnat_break_start");
22863 -- This is the actual generated main program (it would be
22864 -- suppressed if the no main program switch were used). As
22865 -- required by standard system conventions, this program has
22866 -- the external name main.
22870 argv : System.Address;
22871 envp : System.Address)
22873 pragma Export (C, main, "main");
22875 -- The following set of constants give the version
22876 -- identification values for every unit in the bound
22877 -- partition. This identification is computed from all
22878 -- dependent semantic units, and corresponds to the
22879 -- string that would be returned by use of the
22880 -- Body_Version or Version attributes.
22882 type Version_32 is mod 2 ** 32;
22883 u00001 : constant Version_32 := 16#7880BEB3#;
22884 u00002 : constant Version_32 := 16#0D24CBD0#;
22885 u00003 : constant Version_32 := 16#3283DBEB#;
22886 u00004 : constant Version_32 := 16#2359F9ED#;
22887 u00005 : constant Version_32 := 16#664FB847#;
22888 u00006 : constant Version_32 := 16#68E803DF#;
22889 u00007 : constant Version_32 := 16#5572E604#;
22890 u00008 : constant Version_32 := 16#46B173D8#;
22891 u00009 : constant Version_32 := 16#156A40CF#;
22892 u00010 : constant Version_32 := 16#033DABE0#;
22893 u00011 : constant Version_32 := 16#6AB38FEA#;
22894 u00012 : constant Version_32 := 16#22B6217D#;
22895 u00013 : constant Version_32 := 16#68A22947#;
22896 u00014 : constant Version_32 := 16#18CC4A56#;
22897 u00015 : constant Version_32 := 16#08258E1B#;
22898 u00016 : constant Version_32 := 16#367D5222#;
22899 u00017 : constant Version_32 := 16#20C9ECA4#;
22900 u00018 : constant Version_32 := 16#50D32CB6#;
22901 u00019 : constant Version_32 := 16#39A8BB77#;
22902 u00020 : constant Version_32 := 16#5CF8FA2B#;
22903 u00021 : constant Version_32 := 16#2F1EB794#;
22904 u00022 : constant Version_32 := 16#31AB6444#;
22905 u00023 : constant Version_32 := 16#1574B6E9#;
22906 u00024 : constant Version_32 := 16#5109C189#;
22907 u00025 : constant Version_32 := 16#56D770CD#;
22908 u00026 : constant Version_32 := 16#02F9DE3D#;
22909 u00027 : constant Version_32 := 16#08AB6B2C#;
22910 u00028 : constant Version_32 := 16#3FA37670#;
22911 u00029 : constant Version_32 := 16#476457A0#;
22912 u00030 : constant Version_32 := 16#731E1B6E#;
22913 u00031 : constant Version_32 := 16#23C2E789#;
22914 u00032 : constant Version_32 := 16#0F1BD6A1#;
22915 u00033 : constant Version_32 := 16#7C25DE96#;
22916 u00034 : constant Version_32 := 16#39ADFFA2#;
22917 u00035 : constant Version_32 := 16#571DE3E7#;
22918 u00036 : constant Version_32 := 16#5EB646AB#;
22919 u00037 : constant Version_32 := 16#4249379B#;
22920 u00038 : constant Version_32 := 16#0357E00A#;
22921 u00039 : constant Version_32 := 16#3784FB72#;
22922 u00040 : constant Version_32 := 16#2E723019#;
22923 u00041 : constant Version_32 := 16#623358EA#;
22924 u00042 : constant Version_32 := 16#107F9465#;
22925 u00043 : constant Version_32 := 16#6843F68A#;
22926 u00044 : constant Version_32 := 16#63305874#;
22927 u00045 : constant Version_32 := 16#31E56CE1#;
22928 u00046 : constant Version_32 := 16#02917970#;
22929 u00047 : constant Version_32 := 16#6CCBA70E#;
22930 u00048 : constant Version_32 := 16#41CD4204#;
22931 u00049 : constant Version_32 := 16#572E3F58#;
22932 u00050 : constant Version_32 := 16#20729FF5#;
22933 u00051 : constant Version_32 := 16#1D4F93E8#;
22934 u00052 : constant Version_32 := 16#30B2EC3D#;
22935 u00053 : constant Version_32 := 16#34054F96#;
22936 u00054 : constant Version_32 := 16#5A199860#;
22937 u00055 : constant Version_32 := 16#0E7F912B#;
22938 u00056 : constant Version_32 := 16#5760634A#;
22939 u00057 : constant Version_32 := 16#5D851835#;
22941 -- The following Export pragmas export the version numbers
22942 -- with symbolic names ending in B (for body) or S
22943 -- (for spec) so that they can be located in a link. The
22944 -- information provided here is sufficient to track down
22945 -- the exact versions of units used in a given build.
22947 pragma Export (C, u00001, "helloB");
22948 pragma Export (C, u00002, "system__standard_libraryB");
22949 pragma Export (C, u00003, "system__standard_libraryS");
22950 pragma Export (C, u00004, "adaS");
22951 pragma Export (C, u00005, "ada__text_ioB");
22952 pragma Export (C, u00006, "ada__text_ioS");
22953 pragma Export (C, u00007, "ada__exceptionsB");
22954 pragma Export (C, u00008, "ada__exceptionsS");
22955 pragma Export (C, u00009, "gnatS");
22956 pragma Export (C, u00010, "gnat__heap_sort_aB");
22957 pragma Export (C, u00011, "gnat__heap_sort_aS");
22958 pragma Export (C, u00012, "systemS");
22959 pragma Export (C, u00013, "system__exception_tableB");
22960 pragma Export (C, u00014, "system__exception_tableS");
22961 pragma Export (C, u00015, "gnat__htableB");
22962 pragma Export (C, u00016, "gnat__htableS");
22963 pragma Export (C, u00017, "system__exceptionsS");
22964 pragma Export (C, u00018, "system__machine_state_operationsB");
22965 pragma Export (C, u00019, "system__machine_state_operationsS");
22966 pragma Export (C, u00020, "system__machine_codeS");
22967 pragma Export (C, u00021, "system__storage_elementsB");
22968 pragma Export (C, u00022, "system__storage_elementsS");
22969 pragma Export (C, u00023, "system__secondary_stackB");
22970 pragma Export (C, u00024, "system__secondary_stackS");
22971 pragma Export (C, u00025, "system__parametersB");
22972 pragma Export (C, u00026, "system__parametersS");
22973 pragma Export (C, u00027, "system__soft_linksB");
22974 pragma Export (C, u00028, "system__soft_linksS");
22975 pragma Export (C, u00029, "system__stack_checkingB");
22976 pragma Export (C, u00030, "system__stack_checkingS");
22977 pragma Export (C, u00031, "system__tracebackB");
22978 pragma Export (C, u00032, "system__tracebackS");
22979 pragma Export (C, u00033, "ada__streamsS");
22980 pragma Export (C, u00034, "ada__tagsB");
22981 pragma Export (C, u00035, "ada__tagsS");
22982 pragma Export (C, u00036, "system__string_opsB");
22983 pragma Export (C, u00037, "system__string_opsS");
22984 pragma Export (C, u00038, "interfacesS");
22985 pragma Export (C, u00039, "interfaces__c_streamsB");
22986 pragma Export (C, u00040, "interfaces__c_streamsS");
22987 pragma Export (C, u00041, "system__file_ioB");
22988 pragma Export (C, u00042, "system__file_ioS");
22989 pragma Export (C, u00043, "ada__finalizationB");
22990 pragma Export (C, u00044, "ada__finalizationS");
22991 pragma Export (C, u00045, "system__finalization_rootB");
22992 pragma Export (C, u00046, "system__finalization_rootS");
22993 pragma Export (C, u00047, "system__finalization_implementationB");
22994 pragma Export (C, u00048, "system__finalization_implementationS");
22995 pragma Export (C, u00049, "system__string_ops_concat_3B");
22996 pragma Export (C, u00050, "system__string_ops_concat_3S");
22997 pragma Export (C, u00051, "system__stream_attributesB");
22998 pragma Export (C, u00052, "system__stream_attributesS");
22999 pragma Export (C, u00053, "ada__io_exceptionsS");
23000 pragma Export (C, u00054, "system__unsigned_typesS");
23001 pragma Export (C, u00055, "system__file_control_blockS");
23002 pragma Export (C, u00056, "ada__finalization__list_controllerB");
23003 pragma Export (C, u00057, "ada__finalization__list_controllerS");
23005 -- BEGIN ELABORATION ORDER
23008 -- gnat.heap_sort_a (spec)
23009 -- gnat.heap_sort_a (body)
23010 -- gnat.htable (spec)
23011 -- gnat.htable (body)
23012 -- interfaces (spec)
23014 -- system.machine_code (spec)
23015 -- system.parameters (spec)
23016 -- system.parameters (body)
23017 -- interfaces.c_streams (spec)
23018 -- interfaces.c_streams (body)
23019 -- system.standard_library (spec)
23020 -- ada.exceptions (spec)
23021 -- system.exception_table (spec)
23022 -- system.exception_table (body)
23023 -- ada.io_exceptions (spec)
23024 -- system.exceptions (spec)
23025 -- system.storage_elements (spec)
23026 -- system.storage_elements (body)
23027 -- system.machine_state_operations (spec)
23028 -- system.machine_state_operations (body)
23029 -- system.secondary_stack (spec)
23030 -- system.stack_checking (spec)
23031 -- system.soft_links (spec)
23032 -- system.soft_links (body)
23033 -- system.stack_checking (body)
23034 -- system.secondary_stack (body)
23035 -- system.standard_library (body)
23036 -- system.string_ops (spec)
23037 -- system.string_ops (body)
23040 -- ada.streams (spec)
23041 -- system.finalization_root (spec)
23042 -- system.finalization_root (body)
23043 -- system.string_ops_concat_3 (spec)
23044 -- system.string_ops_concat_3 (body)
23045 -- system.traceback (spec)
23046 -- system.traceback (body)
23047 -- ada.exceptions (body)
23048 -- system.unsigned_types (spec)
23049 -- system.stream_attributes (spec)
23050 -- system.stream_attributes (body)
23051 -- system.finalization_implementation (spec)
23052 -- system.finalization_implementation (body)
23053 -- ada.finalization (spec)
23054 -- ada.finalization (body)
23055 -- ada.finalization.list_controller (spec)
23056 -- ada.finalization.list_controller (body)
23057 -- system.file_control_block (spec)
23058 -- system.file_io (spec)
23059 -- system.file_io (body)
23060 -- ada.text_io (spec)
23061 -- ada.text_io (body)
23063 -- END ELABORATION ORDER
23067 -- The following source file name pragmas allow the generated file
23068 -- names to be unique for different main programs. They are needed
23069 -- since the package name will always be Ada_Main.
23071 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
23072 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
23074 -- Generated package body for Ada_Main starts here
23076 package body ada_main is
23078 -- The actual finalization is performed by calling the
23079 -- library routine in System.Standard_Library.Adafinal
23081 procedure Do_Finalize;
23082 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
23089 procedure adainit is
23091 -- These booleans are set to True once the associated unit has
23092 -- been elaborated. It is also used to avoid elaborating the
23093 -- same unit twice.
23096 pragma Import (Ada, E040, "interfaces__c_streams_E");
23099 pragma Import (Ada, E008, "ada__exceptions_E");
23102 pragma Import (Ada, E014, "system__exception_table_E");
23105 pragma Import (Ada, E053, "ada__io_exceptions_E");
23108 pragma Import (Ada, E017, "system__exceptions_E");
23111 pragma Import (Ada, E024, "system__secondary_stack_E");
23114 pragma Import (Ada, E030, "system__stack_checking_E");
23117 pragma Import (Ada, E028, "system__soft_links_E");
23120 pragma Import (Ada, E035, "ada__tags_E");
23123 pragma Import (Ada, E033, "ada__streams_E");
23126 pragma Import (Ada, E046, "system__finalization_root_E");
23129 pragma Import (Ada, E048, "system__finalization_implementation_E");
23132 pragma Import (Ada, E044, "ada__finalization_E");
23135 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
23138 pragma Import (Ada, E055, "system__file_control_block_E");
23141 pragma Import (Ada, E042, "system__file_io_E");
23144 pragma Import (Ada, E006, "ada__text_io_E");
23146 -- Set_Globals is a library routine that stores away the
23147 -- value of the indicated set of global values in global
23148 -- variables within the library.
23150 procedure Set_Globals
23151 (Main_Priority : Integer;
23152 Time_Slice_Value : Integer;
23153 WC_Encoding : Character;
23154 Locking_Policy : Character;
23155 Queuing_Policy : Character;
23156 Task_Dispatching_Policy : Character;
23157 Adafinal : System.Address;
23158 Unreserve_All_Interrupts : Integer;
23159 Exception_Tracebacks : Integer);
23160 @findex __gnat_set_globals
23161 pragma Import (C, Set_Globals, "__gnat_set_globals");
23163 -- SDP_Table_Build is a library routine used to build the
23164 -- exception tables. See unit Ada.Exceptions in files
23165 -- a-except.ads/adb for full details of how zero cost
23166 -- exception handling works. This procedure, the call to
23167 -- it, and the two following tables are all omitted if the
23168 -- build is in longjmp/setjump exception mode.
23170 @findex SDP_Table_Build
23171 @findex Zero Cost Exceptions
23172 procedure SDP_Table_Build
23173 (SDP_Addresses : System.Address;
23174 SDP_Count : Natural;
23175 Elab_Addresses : System.Address;
23176 Elab_Addr_Count : Natural);
23177 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
23179 -- Table of Unit_Exception_Table addresses. Used for zero
23180 -- cost exception handling to build the top level table.
23182 ST : aliased constant array (1 .. 23) of System.Address := (
23184 Ada.Text_Io'UET_Address,
23185 Ada.Exceptions'UET_Address,
23186 Gnat.Heap_Sort_A'UET_Address,
23187 System.Exception_Table'UET_Address,
23188 System.Machine_State_Operations'UET_Address,
23189 System.Secondary_Stack'UET_Address,
23190 System.Parameters'UET_Address,
23191 System.Soft_Links'UET_Address,
23192 System.Stack_Checking'UET_Address,
23193 System.Traceback'UET_Address,
23194 Ada.Streams'UET_Address,
23195 Ada.Tags'UET_Address,
23196 System.String_Ops'UET_Address,
23197 Interfaces.C_Streams'UET_Address,
23198 System.File_Io'UET_Address,
23199 Ada.Finalization'UET_Address,
23200 System.Finalization_Root'UET_Address,
23201 System.Finalization_Implementation'UET_Address,
23202 System.String_Ops_Concat_3'UET_Address,
23203 System.Stream_Attributes'UET_Address,
23204 System.File_Control_Block'UET_Address,
23205 Ada.Finalization.List_Controller'UET_Address);
23207 -- Table of addresses of elaboration routines. Used for
23208 -- zero cost exception handling to make sure these
23209 -- addresses are included in the top level procedure
23212 EA : aliased constant array (1 .. 23) of System.Address := (
23213 adainit'Code_Address,
23214 Do_Finalize'Code_Address,
23215 Ada.Exceptions'Elab_Spec'Address,
23216 System.Exceptions'Elab_Spec'Address,
23217 Interfaces.C_Streams'Elab_Spec'Address,
23218 System.Exception_Table'Elab_Body'Address,
23219 Ada.Io_Exceptions'Elab_Spec'Address,
23220 System.Stack_Checking'Elab_Spec'Address,
23221 System.Soft_Links'Elab_Body'Address,
23222 System.Secondary_Stack'Elab_Body'Address,
23223 Ada.Tags'Elab_Spec'Address,
23224 Ada.Tags'Elab_Body'Address,
23225 Ada.Streams'Elab_Spec'Address,
23226 System.Finalization_Root'Elab_Spec'Address,
23227 Ada.Exceptions'Elab_Body'Address,
23228 System.Finalization_Implementation'Elab_Spec'Address,
23229 System.Finalization_Implementation'Elab_Body'Address,
23230 Ada.Finalization'Elab_Spec'Address,
23231 Ada.Finalization.List_Controller'Elab_Spec'Address,
23232 System.File_Control_Block'Elab_Spec'Address,
23233 System.File_Io'Elab_Body'Address,
23234 Ada.Text_Io'Elab_Spec'Address,
23235 Ada.Text_Io'Elab_Body'Address);
23237 -- Start of processing for adainit
23241 -- Call SDP_Table_Build to build the top level procedure
23242 -- table for zero cost exception handling (omitted in
23243 -- longjmp/setjump mode).
23245 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
23247 -- Call Set_Globals to record various information for
23248 -- this partition. The values are derived by the binder
23249 -- from information stored in the ali files by the compiler.
23251 @findex __gnat_set_globals
23253 (Main_Priority => -1,
23254 -- Priority of main program, -1 if no pragma Priority used
23256 Time_Slice_Value => -1,
23257 -- Time slice from Time_Slice pragma, -1 if none used
23259 WC_Encoding => 'b',
23260 -- Wide_Character encoding used, default is brackets
23262 Locking_Policy => ' ',
23263 -- Locking_Policy used, default of space means not
23264 -- specified, otherwise it is the first character of
23265 -- the policy name.
23267 Queuing_Policy => ' ',
23268 -- Queuing_Policy used, default of space means not
23269 -- specified, otherwise it is the first character of
23270 -- the policy name.
23272 Task_Dispatching_Policy => ' ',
23273 -- Task_Dispatching_Policy used, default of space means
23274 -- not specified, otherwise first character of the
23277 Adafinal => System.Null_Address,
23278 -- Address of Adafinal routine, not used anymore
23280 Unreserve_All_Interrupts => 0,
23281 -- Set true if pragma Unreserve_All_Interrupts was used
23283 Exception_Tracebacks => 0);
23284 -- Indicates if exception tracebacks are enabled
23286 Elab_Final_Code := 1;
23288 -- Now we have the elaboration calls for all units in the partition.
23289 -- The Elab_Spec and Elab_Body attributes generate references to the
23290 -- implicit elaboration procedures generated by the compiler for
23291 -- each unit that requires elaboration.
23294 Interfaces.C_Streams'Elab_Spec;
23298 Ada.Exceptions'Elab_Spec;
23301 System.Exception_Table'Elab_Body;
23305 Ada.Io_Exceptions'Elab_Spec;
23309 System.Exceptions'Elab_Spec;
23313 System.Stack_Checking'Elab_Spec;
23316 System.Soft_Links'Elab_Body;
23321 System.Secondary_Stack'Elab_Body;
23325 Ada.Tags'Elab_Spec;
23328 Ada.Tags'Elab_Body;
23332 Ada.Streams'Elab_Spec;
23336 System.Finalization_Root'Elab_Spec;
23340 Ada.Exceptions'Elab_Body;
23344 System.Finalization_Implementation'Elab_Spec;
23347 System.Finalization_Implementation'Elab_Body;
23351 Ada.Finalization'Elab_Spec;
23355 Ada.Finalization.List_Controller'Elab_Spec;
23359 System.File_Control_Block'Elab_Spec;
23363 System.File_Io'Elab_Body;
23367 Ada.Text_Io'Elab_Spec;
23370 Ada.Text_Io'Elab_Body;
23374 Elab_Final_Code := 0;
23382 procedure adafinal is
23391 -- main is actually a function, as in the ANSI C standard,
23392 -- defined to return the exit status. The three parameters
23393 -- are the argument count, argument values and environment
23396 @findex Main Program
23399 argv : System.Address;
23400 envp : System.Address)
23403 -- The initialize routine performs low level system
23404 -- initialization using a standard library routine which
23405 -- sets up signal handling and performs any other
23406 -- required setup. The routine can be found in file
23409 @findex __gnat_initialize
23410 procedure initialize;
23411 pragma Import (C, initialize, "__gnat_initialize");
23413 -- The finalize routine performs low level system
23414 -- finalization using a standard library routine. The
23415 -- routine is found in file a-final.c and in the standard
23416 -- distribution is a dummy routine that does nothing, so
23417 -- really this is a hook for special user finalization.
23419 @findex __gnat_finalize
23420 procedure finalize;
23421 pragma Import (C, finalize, "__gnat_finalize");
23423 -- We get to the main program of the partition by using
23424 -- pragma Import because if we try to with the unit and
23425 -- call it Ada style, then not only do we waste time
23426 -- recompiling it, but also, we don't really know the right
23427 -- switches (e.g. identifier character set) to be used
23430 procedure Ada_Main_Program;
23431 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
23433 -- Start of processing for main
23436 -- Save global variables
23442 -- Call low level system initialization
23446 -- Call our generated Ada initialization routine
23450 -- This is the point at which we want the debugger to get
23455 -- Now we call the main program of the partition
23459 -- Perform Ada finalization
23463 -- Perform low level system finalization
23467 -- Return the proper exit status
23468 return (gnat_exit_status);
23471 -- This section is entirely comments, so it has no effect on the
23472 -- compilation of the Ada_Main package. It provides the list of
23473 -- object files and linker options, as well as some standard
23474 -- libraries needed for the link. The gnatlink utility parses
23475 -- this b~hello.adb file to read these comment lines to generate
23476 -- the appropriate command line arguments for the call to the
23477 -- system linker. The BEGIN/END lines are used for sentinels for
23478 -- this parsing operation.
23480 -- The exact file names will of course depend on the environment,
23481 -- host/target and location of files on the host system.
23483 @findex Object file list
23484 -- BEGIN Object file/option list
23487 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
23488 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
23489 -- END Object file/option list
23495 The Ada code in the above example is exactly what is generated by the
23496 binder. We have added comments to more clearly indicate the function
23497 of each part of the generated @code{Ada_Main} package.
23499 The code is standard Ada in all respects, and can be processed by any
23500 tools that handle Ada. In particular, it is possible to use the debugger
23501 in Ada mode to debug the generated @code{Ada_Main} package. For example,
23502 suppose that for reasons that you do not understand, your program is crashing
23503 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
23504 you can place a breakpoint on the call:
23506 @smallexample @c ada
23507 Ada.Text_Io'Elab_Body;
23511 and trace the elaboration routine for this package to find out where
23512 the problem might be (more usually of course you would be debugging
23513 elaboration code in your own application).
23515 @node Elaboration Order Handling in GNAT
23516 @appendix Elaboration Order Handling in GNAT
23517 @cindex Order of elaboration
23518 @cindex Elaboration control
23521 * Elaboration Code in Ada 95::
23522 * Checking the Elaboration Order in Ada 95::
23523 * Controlling the Elaboration Order in Ada 95::
23524 * Controlling Elaboration in GNAT - Internal Calls::
23525 * Controlling Elaboration in GNAT - External Calls::
23526 * Default Behavior in GNAT - Ensuring Safety::
23527 * Treatment of Pragma Elaborate::
23528 * Elaboration Issues for Library Tasks::
23529 * Mixing Elaboration Models::
23530 * What to Do If the Default Elaboration Behavior Fails::
23531 * Elaboration for Access-to-Subprogram Values::
23532 * Summary of Procedures for Elaboration Control::
23533 * Other Elaboration Order Considerations::
23537 This chapter describes the handling of elaboration code in Ada 95 and
23538 in GNAT, and discusses how the order of elaboration of program units can
23539 be controlled in GNAT, either automatically or with explicit programming
23542 @node Elaboration Code in Ada 95
23543 @section Elaboration Code in Ada 95
23546 Ada 95 provides rather general mechanisms for executing code at elaboration
23547 time, that is to say before the main program starts executing. Such code arises
23551 @item Initializers for variables.
23552 Variables declared at the library level, in package specs or bodies, can
23553 require initialization that is performed at elaboration time, as in:
23554 @smallexample @c ada
23556 Sqrt_Half : Float := Sqrt (0.5);
23560 @item Package initialization code
23561 Code in a @code{BEGIN-END} section at the outer level of a package body is
23562 executed as part of the package body elaboration code.
23564 @item Library level task allocators
23565 Tasks that are declared using task allocators at the library level
23566 start executing immediately and hence can execute at elaboration time.
23570 Subprogram calls are possible in any of these contexts, which means that
23571 any arbitrary part of the program may be executed as part of the elaboration
23572 code. It is even possible to write a program which does all its work at
23573 elaboration time, with a null main program, although stylistically this
23574 would usually be considered an inappropriate way to structure
23577 An important concern arises in the context of elaboration code:
23578 we have to be sure that it is executed in an appropriate order. What we
23579 have is a series of elaboration code sections, potentially one section
23580 for each unit in the program. It is important that these execute
23581 in the correct order. Correctness here means that, taking the above
23582 example of the declaration of @code{Sqrt_Half},
23583 if some other piece of
23584 elaboration code references @code{Sqrt_Half},
23585 then it must run after the
23586 section of elaboration code that contains the declaration of
23589 There would never be any order of elaboration problem if we made a rule
23590 that whenever you @code{with} a unit, you must elaborate both the spec and body
23591 of that unit before elaborating the unit doing the @code{with}'ing:
23593 @smallexample @c ada
23597 package Unit_2 is ...
23603 would require that both the body and spec of @code{Unit_1} be elaborated
23604 before the spec of @code{Unit_2}. However, a rule like that would be far too
23605 restrictive. In particular, it would make it impossible to have routines
23606 in separate packages that were mutually recursive.
23608 You might think that a clever enough compiler could look at the actual
23609 elaboration code and determine an appropriate correct order of elaboration,
23610 but in the general case, this is not possible. Consider the following
23613 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
23615 the variable @code{Sqrt_1}, which is declared in the elaboration code
23616 of the body of @code{Unit_1}:
23618 @smallexample @c ada
23620 Sqrt_1 : Float := Sqrt (0.1);
23625 The elaboration code of the body of @code{Unit_1} also contains:
23627 @smallexample @c ada
23630 if expression_1 = 1 then
23631 Q := Unit_2.Func_2;
23638 @code{Unit_2} is exactly parallel,
23639 it has a procedure @code{Func_2} that references
23640 the variable @code{Sqrt_2}, which is declared in the elaboration code of
23641 the body @code{Unit_2}:
23643 @smallexample @c ada
23645 Sqrt_2 : Float := Sqrt (0.1);
23650 The elaboration code of the body of @code{Unit_2} also contains:
23652 @smallexample @c ada
23655 if expression_2 = 2 then
23656 Q := Unit_1.Func_1;
23663 Now the question is, which of the following orders of elaboration is
23688 If you carefully analyze the flow here, you will see that you cannot tell
23689 at compile time the answer to this question.
23690 If @code{expression_1} is not equal to 1,
23691 and @code{expression_2} is not equal to 2,
23692 then either order is acceptable, because neither of the function calls is
23693 executed. If both tests evaluate to true, then neither order is acceptable
23694 and in fact there is no correct order.
23696 If one of the two expressions is true, and the other is false, then one
23697 of the above orders is correct, and the other is incorrect. For example,
23698 if @code{expression_1} = 1 and @code{expression_2} /= 2,
23699 then the call to @code{Func_2}
23700 will occur, but not the call to @code{Func_1.}
23701 This means that it is essential
23702 to elaborate the body of @code{Unit_1} before
23703 the body of @code{Unit_2}, so the first
23704 order of elaboration is correct and the second is wrong.
23706 By making @code{expression_1} and @code{expression_2}
23707 depend on input data, or perhaps
23708 the time of day, we can make it impossible for the compiler or binder
23709 to figure out which of these expressions will be true, and hence it
23710 is impossible to guarantee a safe order of elaboration at run time.
23712 @node Checking the Elaboration Order in Ada 95
23713 @section Checking the Elaboration Order in Ada 95
23716 In some languages that involve the same kind of elaboration problems,
23717 e.g. Java and C++, the programmer is expected to worry about these
23718 ordering problems himself, and it is common to
23719 write a program in which an incorrect elaboration order gives
23720 surprising results, because it references variables before they
23722 Ada 95 is designed to be a safe language, and a programmer-beware approach is
23723 clearly not sufficient. Consequently, the language provides three lines
23727 @item Standard rules
23728 Some standard rules restrict the possible choice of elaboration
23729 order. In particular, if you @code{with} a unit, then its spec is always
23730 elaborated before the unit doing the @code{with}. Similarly, a parent
23731 spec is always elaborated before the child spec, and finally
23732 a spec is always elaborated before its corresponding body.
23734 @item Dynamic elaboration checks
23735 @cindex Elaboration checks
23736 @cindex Checks, elaboration
23737 Dynamic checks are made at run time, so that if some entity is accessed
23738 before it is elaborated (typically by means of a subprogram call)
23739 then the exception (@code{Program_Error}) is raised.
23741 @item Elaboration control
23742 Facilities are provided for the programmer to specify the desired order
23746 Let's look at these facilities in more detail. First, the rules for
23747 dynamic checking. One possible rule would be simply to say that the
23748 exception is raised if you access a variable which has not yet been
23749 elaborated. The trouble with this approach is that it could require
23750 expensive checks on every variable reference. Instead Ada 95 has two
23751 rules which are a little more restrictive, but easier to check, and
23755 @item Restrictions on calls
23756 A subprogram can only be called at elaboration time if its body
23757 has been elaborated. The rules for elaboration given above guarantee
23758 that the spec of the subprogram has been elaborated before the
23759 call, but not the body. If this rule is violated, then the
23760 exception @code{Program_Error} is raised.
23762 @item Restrictions on instantiations
23763 A generic unit can only be instantiated if the body of the generic
23764 unit has been elaborated. Again, the rules for elaboration given above
23765 guarantee that the spec of the generic unit has been elaborated
23766 before the instantiation, but not the body. If this rule is
23767 violated, then the exception @code{Program_Error} is raised.
23771 The idea is that if the body has been elaborated, then any variables
23772 it references must have been elaborated; by checking for the body being
23773 elaborated we guarantee that none of its references causes any
23774 trouble. As we noted above, this is a little too restrictive, because a
23775 subprogram that has no non-local references in its body may in fact be safe
23776 to call. However, it really would be unsafe to rely on this, because
23777 it would mean that the caller was aware of details of the implementation
23778 in the body. This goes against the basic tenets of Ada.
23780 A plausible implementation can be described as follows.
23781 A Boolean variable is associated with each subprogram
23782 and each generic unit. This variable is initialized to False, and is set to
23783 True at the point body is elaborated. Every call or instantiation checks the
23784 variable, and raises @code{Program_Error} if the variable is False.
23786 Note that one might think that it would be good enough to have one Boolean
23787 variable for each package, but that would not deal with cases of trying
23788 to call a body in the same package as the call
23789 that has not been elaborated yet.
23790 Of course a compiler may be able to do enough analysis to optimize away
23791 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
23792 does such optimizations, but still the easiest conceptual model is to
23793 think of there being one variable per subprogram.
23795 @node Controlling the Elaboration Order in Ada 95
23796 @section Controlling the Elaboration Order in Ada 95
23799 In the previous section we discussed the rules in Ada 95 which ensure
23800 that @code{Program_Error} is raised if an incorrect elaboration order is
23801 chosen. This prevents erroneous executions, but we need mechanisms to
23802 specify a correct execution and avoid the exception altogether.
23803 To achieve this, Ada 95 provides a number of features for controlling
23804 the order of elaboration. We discuss these features in this section.
23806 First, there are several ways of indicating to the compiler that a given
23807 unit has no elaboration problems:
23810 @item packages that do not require a body
23811 In Ada 95, a library package that does not require a body does not permit
23812 a body. This means that if we have a such a package, as in:
23814 @smallexample @c ada
23817 package Definitions is
23819 type m is new integer;
23821 type a is array (1 .. 10) of m;
23822 type b is array (1 .. 20) of m;
23830 A package that @code{with}'s @code{Definitions} may safely instantiate
23831 @code{Definitions.Subp} because the compiler can determine that there
23832 definitely is no package body to worry about in this case
23835 @cindex pragma Pure
23837 Places sufficient restrictions on a unit to guarantee that
23838 no call to any subprogram in the unit can result in an
23839 elaboration problem. This means that the compiler does not need
23840 to worry about the point of elaboration of such units, and in
23841 particular, does not need to check any calls to any subprograms
23844 @item pragma Preelaborate
23845 @findex Preelaborate
23846 @cindex pragma Preelaborate
23847 This pragma places slightly less stringent restrictions on a unit than
23849 but these restrictions are still sufficient to ensure that there
23850 are no elaboration problems with any calls to the unit.
23852 @item pragma Elaborate_Body
23853 @findex Elaborate_Body
23854 @cindex pragma Elaborate_Body
23855 This pragma requires that the body of a unit be elaborated immediately
23856 after its spec. Suppose a unit @code{A} has such a pragma,
23857 and unit @code{B} does
23858 a @code{with} of unit @code{A}. Recall that the standard rules require
23859 the spec of unit @code{A}
23860 to be elaborated before the @code{with}'ing unit; given the pragma in
23861 @code{A}, we also know that the body of @code{A}
23862 will be elaborated before @code{B}, so
23863 that calls to @code{A} are safe and do not need a check.
23868 unlike pragma @code{Pure} and pragma @code{Preelaborate},
23870 @code{Elaborate_Body} does not guarantee that the program is
23871 free of elaboration problems, because it may not be possible
23872 to satisfy the requested elaboration order.
23873 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
23875 marks @code{Unit_1} as @code{Elaborate_Body},
23876 and not @code{Unit_2,} then the order of
23877 elaboration will be:
23889 Now that means that the call to @code{Func_1} in @code{Unit_2}
23890 need not be checked,
23891 it must be safe. But the call to @code{Func_2} in
23892 @code{Unit_1} may still fail if
23893 @code{Expression_1} is equal to 1,
23894 and the programmer must still take
23895 responsibility for this not being the case.
23897 If all units carry a pragma @code{Elaborate_Body}, then all problems are
23898 eliminated, except for calls entirely within a body, which are
23899 in any case fully under programmer control. However, using the pragma
23900 everywhere is not always possible.
23901 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
23902 we marked both of them as having pragma @code{Elaborate_Body}, then
23903 clearly there would be no possible elaboration order.
23905 The above pragmas allow a server to guarantee safe use by clients, and
23906 clearly this is the preferable approach. Consequently a good rule in
23907 Ada 95 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
23908 and if this is not possible,
23909 mark them as @code{Elaborate_Body} if possible.
23910 As we have seen, there are situations where neither of these
23911 three pragmas can be used.
23912 So we also provide methods for clients to control the
23913 order of elaboration of the servers on which they depend:
23916 @item pragma Elaborate (unit)
23918 @cindex pragma Elaborate
23919 This pragma is placed in the context clause, after a @code{with} clause,
23920 and it requires that the body of the named unit be elaborated before
23921 the unit in which the pragma occurs. The idea is to use this pragma
23922 if the current unit calls at elaboration time, directly or indirectly,
23923 some subprogram in the named unit.
23925 @item pragma Elaborate_All (unit)
23926 @findex Elaborate_All
23927 @cindex pragma Elaborate_All
23928 This is a stronger version of the Elaborate pragma. Consider the
23932 Unit A @code{with}'s unit B and calls B.Func in elab code
23933 Unit B @code{with}'s unit C, and B.Func calls C.Func
23937 Now if we put a pragma @code{Elaborate (B)}
23938 in unit @code{A}, this ensures that the
23939 body of @code{B} is elaborated before the call, but not the
23940 body of @code{C}, so
23941 the call to @code{C.Func} could still cause @code{Program_Error} to
23944 The effect of a pragma @code{Elaborate_All} is stronger, it requires
23945 not only that the body of the named unit be elaborated before the
23946 unit doing the @code{with}, but also the bodies of all units that the
23947 named unit uses, following @code{with} links transitively. For example,
23948 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
23950 not only that the body of @code{B} be elaborated before @code{A},
23952 body of @code{C}, because @code{B} @code{with}'s @code{C}.
23956 We are now in a position to give a usage rule in Ada 95 for avoiding
23957 elaboration problems, at least if dynamic dispatching and access to
23958 subprogram values are not used. We will handle these cases separately
23961 The rule is simple. If a unit has elaboration code that can directly or
23962 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
23963 a generic package in a @code{with}'ed unit,
23964 then if the @code{with}'ed unit does not have
23965 pragma @code{Pure} or @code{Preelaborate}, then the client should have
23966 a pragma @code{Elaborate_All}
23967 for the @code{with}'ed unit. By following this rule a client is
23968 assured that calls can be made without risk of an exception.
23970 For generic subprogram instantiations, the rule can be relaxed to
23971 require only a pragma @code{Elaborate} since elaborating the body
23972 of a subprogram cannot cause any transitive elaboration (we are
23973 not calling the subprogram in this case, just elaborating its
23976 If this rule is not followed, then a program may be in one of four
23980 @item No order exists
23981 No order of elaboration exists which follows the rules, taking into
23982 account any @code{Elaborate}, @code{Elaborate_All},
23983 or @code{Elaborate_Body} pragmas. In
23984 this case, an Ada 95 compiler must diagnose the situation at bind
23985 time, and refuse to build an executable program.
23987 @item One or more orders exist, all incorrect
23988 One or more acceptable elaboration orders exists, and all of them
23989 generate an elaboration order problem. In this case, the binder
23990 can build an executable program, but @code{Program_Error} will be raised
23991 when the program is run.
23993 @item Several orders exist, some right, some incorrect
23994 One or more acceptable elaboration orders exists, and some of them
23995 work, and some do not. The programmer has not controlled
23996 the order of elaboration, so the binder may or may not pick one of
23997 the correct orders, and the program may or may not raise an
23998 exception when it is run. This is the worst case, because it means
23999 that the program may fail when moved to another compiler, or even
24000 another version of the same compiler.
24002 @item One or more orders exists, all correct
24003 One ore more acceptable elaboration orders exist, and all of them
24004 work. In this case the program runs successfully. This state of
24005 affairs can be guaranteed by following the rule we gave above, but
24006 may be true even if the rule is not followed.
24010 Note that one additional advantage of following our rules on the use
24011 of @code{Elaborate} and @code{Elaborate_All}
24012 is that the program continues to stay in the ideal (all orders OK) state
24013 even if maintenance
24014 changes some bodies of some units. Conversely, if a program that does
24015 not follow this rule happens to be safe at some point, this state of affairs
24016 may deteriorate silently as a result of maintenance changes.
24018 You may have noticed that the above discussion did not mention
24019 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
24020 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
24021 code in the body makes calls to some other unit, so it is still necessary
24022 to use @code{Elaborate_All} on such units.
24024 @node Controlling Elaboration in GNAT - Internal Calls
24025 @section Controlling Elaboration in GNAT - Internal Calls
24028 In the case of internal calls, i.e. calls within a single package, the
24029 programmer has full control over the order of elaboration, and it is up
24030 to the programmer to elaborate declarations in an appropriate order. For
24033 @smallexample @c ada
24036 function One return Float;
24040 function One return Float is
24049 will obviously raise @code{Program_Error} at run time, because function
24050 One will be called before its body is elaborated. In this case GNAT will
24051 generate a warning that the call will raise @code{Program_Error}:
24057 2. function One return Float;
24059 4. Q : Float := One;
24061 >>> warning: cannot call "One" before body is elaborated
24062 >>> warning: Program_Error will be raised at run time
24065 6. function One return Float is
24078 Note that in this particular case, it is likely that the call is safe, because
24079 the function @code{One} does not access any global variables.
24080 Nevertheless in Ada 95, we do not want the validity of the check to depend on
24081 the contents of the body (think about the separate compilation case), so this
24082 is still wrong, as we discussed in the previous sections.
24084 The error is easily corrected by rearranging the declarations so that the
24085 body of One appears before the declaration containing the call
24086 (note that in Ada 95,
24087 declarations can appear in any order, so there is no restriction that
24088 would prevent this reordering, and if we write:
24090 @smallexample @c ada
24093 function One return Float;
24095 function One return Float is
24106 then all is well, no warning is generated, and no
24107 @code{Program_Error} exception
24109 Things are more complicated when a chain of subprograms is executed:
24111 @smallexample @c ada
24114 function A return Integer;
24115 function B return Integer;
24116 function C return Integer;
24118 function B return Integer is begin return A; end;
24119 function C return Integer is begin return B; end;
24123 function A return Integer is begin return 1; end;
24129 Now the call to @code{C}
24130 at elaboration time in the declaration of @code{X} is correct, because
24131 the body of @code{C} is already elaborated,
24132 and the call to @code{B} within the body of
24133 @code{C} is correct, but the call
24134 to @code{A} within the body of @code{B} is incorrect, because the body
24135 of @code{A} has not been elaborated, so @code{Program_Error}
24136 will be raised on the call to @code{A}.
24137 In this case GNAT will generate a
24138 warning that @code{Program_Error} may be
24139 raised at the point of the call. Let's look at the warning:
24145 2. function A return Integer;
24146 3. function B return Integer;
24147 4. function C return Integer;
24149 6. function B return Integer is begin return A; end;
24151 >>> warning: call to "A" before body is elaborated may
24152 raise Program_Error
24153 >>> warning: "B" called at line 7
24154 >>> warning: "C" called at line 9
24156 7. function C return Integer is begin return B; end;
24158 9. X : Integer := C;
24160 11. function A return Integer is begin return 1; end;
24170 Note that the message here says ``may raise'', instead of the direct case,
24171 where the message says ``will be raised''. That's because whether
24173 actually called depends in general on run-time flow of control.
24174 For example, if the body of @code{B} said
24176 @smallexample @c ada
24179 function B return Integer is
24181 if some-condition-depending-on-input-data then
24192 then we could not know until run time whether the incorrect call to A would
24193 actually occur, so @code{Program_Error} might
24194 or might not be raised. It is possible for a compiler to
24195 do a better job of analyzing bodies, to
24196 determine whether or not @code{Program_Error}
24197 might be raised, but it certainly
24198 couldn't do a perfect job (that would require solving the halting problem
24199 and is provably impossible), and because this is a warning anyway, it does
24200 not seem worth the effort to do the analysis. Cases in which it
24201 would be relevant are rare.
24203 In practice, warnings of either of the forms given
24204 above will usually correspond to
24205 real errors, and should be examined carefully and eliminated.
24206 In the rare case where a warning is bogus, it can be suppressed by any of
24207 the following methods:
24211 Compile with the @option{-gnatws} switch set
24214 Suppress @code{Elaboration_Check} for the called subprogram
24217 Use pragma @code{Warnings_Off} to turn warnings off for the call
24221 For the internal elaboration check case,
24222 GNAT by default generates the
24223 necessary run-time checks to ensure
24224 that @code{Program_Error} is raised if any
24225 call fails an elaboration check. Of course this can only happen if a
24226 warning has been issued as described above. The use of pragma
24227 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
24228 some of these checks, meaning that it may be possible (but is not
24229 guaranteed) for a program to be able to call a subprogram whose body
24230 is not yet elaborated, without raising a @code{Program_Error} exception.
24232 @node Controlling Elaboration in GNAT - External Calls
24233 @section Controlling Elaboration in GNAT - External Calls
24236 The previous section discussed the case in which the execution of a
24237 particular thread of elaboration code occurred entirely within a
24238 single unit. This is the easy case to handle, because a programmer
24239 has direct and total control over the order of elaboration, and
24240 furthermore, checks need only be generated in cases which are rare
24241 and which the compiler can easily detect.
24242 The situation is more complex when separate compilation is taken into account.
24243 Consider the following:
24245 @smallexample @c ada
24249 function Sqrt (Arg : Float) return Float;
24252 package body Math is
24253 function Sqrt (Arg : Float) return Float is
24262 X : Float := Math.Sqrt (0.5);
24275 where @code{Main} is the main program. When this program is executed, the
24276 elaboration code must first be executed, and one of the jobs of the
24277 binder is to determine the order in which the units of a program are
24278 to be elaborated. In this case we have four units: the spec and body
24280 the spec of @code{Stuff} and the body of @code{Main}).
24281 In what order should the four separate sections of elaboration code
24284 There are some restrictions in the order of elaboration that the binder
24285 can choose. In particular, if unit U has a @code{with}
24286 for a package @code{X}, then you
24287 are assured that the spec of @code{X}
24288 is elaborated before U , but you are
24289 not assured that the body of @code{X}
24290 is elaborated before U.
24291 This means that in the above case, the binder is allowed to choose the
24302 but that's not good, because now the call to @code{Math.Sqrt}
24303 that happens during
24304 the elaboration of the @code{Stuff}
24305 spec happens before the body of @code{Math.Sqrt} is
24306 elaborated, and hence causes @code{Program_Error} exception to be raised.
24307 At first glance, one might say that the binder is misbehaving, because
24308 obviously you want to elaborate the body of something you @code{with}
24310 that is not a general rule that can be followed in all cases. Consider
24312 @smallexample @c ada
24320 package body Y is ...
24323 package body X is ...
24329 This is a common arrangement, and, apart from the order of elaboration
24330 problems that might arise in connection with elaboration code, this works fine.
24331 A rule that says that you must first elaborate the body of anything you
24332 @code{with} cannot work in this case:
24333 the body of @code{X} @code{with}'s @code{Y},
24334 which means you would have to
24335 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
24337 you have to elaborate the body of @code{X} first, but ... and we have a
24338 loop that cannot be broken.
24340 It is true that the binder can in many cases guess an order of elaboration
24341 that is unlikely to cause a @code{Program_Error}
24342 exception to be raised, and it tries to do so (in the
24343 above example of @code{Math/Stuff/Spec}, the GNAT binder will
24345 elaborate the body of @code{Math} right after its spec, so all will be well).
24347 However, a program that blindly relies on the binder to be helpful can
24348 get into trouble, as we discussed in the previous sections, so
24350 provides a number of facilities for assisting the programmer in
24351 developing programs that are robust with respect to elaboration order.
24353 @node Default Behavior in GNAT - Ensuring Safety
24354 @section Default Behavior in GNAT - Ensuring Safety
24357 The default behavior in GNAT ensures elaboration safety. In its
24358 default mode GNAT implements the
24359 rule we previously described as the right approach. Let's restate it:
24363 @emph{If a unit has elaboration code that can directly or indirectly make a
24364 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
24365 package in a @code{with}'ed unit, then if the @code{with}'ed unit
24366 does not have pragma @code{Pure} or
24367 @code{Preelaborate}, then the client should have an
24368 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
24370 @emph{In the case of instantiating a generic subprogram, it is always
24371 sufficient to have only an @code{Elaborate} pragma for the
24372 @code{with}'ed unit.}
24376 By following this rule a client is assured that calls and instantiations
24377 can be made without risk of an exception.
24379 In this mode GNAT traces all calls that are potentially made from
24380 elaboration code, and puts in any missing implicit @code{Elaborate}
24381 and @code{Elaborate_All} pragmas.
24382 The advantage of this approach is that no elaboration problems
24383 are possible if the binder can find an elaboration order that is
24384 consistent with these implicit @code{Elaborate} and
24385 @code{Elaborate_All} pragmas. The
24386 disadvantage of this approach is that no such order may exist.
24388 If the binder does not generate any diagnostics, then it means that it has
24389 found an elaboration order that is guaranteed to be safe. However, the binder
24390 may still be relying on implicitly generated @code{Elaborate} and
24391 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
24394 If it is important to guarantee portability, then the compilations should
24397 (warn on elaboration problems) switch. This will cause warning messages
24398 to be generated indicating the missing @code{Elaborate} and
24399 @code{Elaborate_All} pragmas.
24400 Consider the following source program:
24402 @smallexample @c ada
24407 m : integer := k.r;
24414 where it is clear that there
24415 should be a pragma @code{Elaborate_All}
24416 for unit @code{k}. An implicit pragma will be generated, and it is
24417 likely that the binder will be able to honor it. However, if you want
24418 to port this program to some other Ada compiler than GNAT.
24419 it is safer to include the pragma explicitly in the source. If this
24420 unit is compiled with the
24422 switch, then the compiler outputs a warning:
24429 3. m : integer := k.r;
24431 >>> warning: call to "r" may raise Program_Error
24432 >>> warning: missing pragma Elaborate_All for "k"
24440 and these warnings can be used as a guide for supplying manually
24441 the missing pragmas. It is usually a bad idea to use this warning
24442 option during development. That's because it will warn you when
24443 you need to put in a pragma, but cannot warn you when it is time
24444 to take it out. So the use of pragma @code{Elaborate_All} may lead to
24445 unnecessary dependencies and even false circularities.
24447 This default mode is more restrictive than the Ada Reference
24448 Manual, and it is possible to construct programs which will compile
24449 using the dynamic model described there, but will run into a
24450 circularity using the safer static model we have described.
24452 Of course any Ada compiler must be able to operate in a mode
24453 consistent with the requirements of the Ada Reference Manual,
24454 and in particular must have the capability of implementing the
24455 standard dynamic model of elaboration with run-time checks.
24457 In GNAT, this standard mode can be achieved either by the use of
24458 the @option{-gnatE} switch on the compiler (@command{gcc} or
24459 @command{gnatmake}) command, or by the use of the configuration pragma:
24461 @smallexample @c ada
24462 pragma Elaboration_Checks (RM);
24466 Either approach will cause the unit affected to be compiled using the
24467 standard dynamic run-time elaboration checks described in the Ada
24468 Reference Manual. The static model is generally preferable, since it
24469 is clearly safer to rely on compile and link time checks rather than
24470 run-time checks. However, in the case of legacy code, it may be
24471 difficult to meet the requirements of the static model. This
24472 issue is further discussed in
24473 @ref{What to Do If the Default Elaboration Behavior Fails}.
24475 Note that the static model provides a strict subset of the allowed
24476 behavior and programs of the Ada Reference Manual, so if you do
24477 adhere to the static model and no circularities exist,
24478 then you are assured that your program will
24479 work using the dynamic model, providing that you remove any
24480 pragma Elaborate statements from the source.
24482 @node Treatment of Pragma Elaborate
24483 @section Treatment of Pragma Elaborate
24484 @cindex Pragma Elaborate
24487 The use of @code{pragma Elaborate}
24488 should generally be avoided in Ada 95 programs.
24489 The reason for this is that there is no guarantee that transitive calls
24490 will be properly handled. Indeed at one point, this pragma was placed
24491 in Annex J (Obsolescent Features), on the grounds that it is never useful.
24493 Now that's a bit restrictive. In practice, the case in which
24494 @code{pragma Elaborate} is useful is when the caller knows that there
24495 are no transitive calls, or that the called unit contains all necessary
24496 transitive @code{pragma Elaborate} statements, and legacy code often
24497 contains such uses.
24499 Strictly speaking the static mode in GNAT should ignore such pragmas,
24500 since there is no assurance at compile time that the necessary safety
24501 conditions are met. In practice, this would cause GNAT to be incompatible
24502 with correctly written Ada 83 code that had all necessary
24503 @code{pragma Elaborate} statements in place. Consequently, we made the
24504 decision that GNAT in its default mode will believe that if it encounters
24505 a @code{pragma Elaborate} then the programmer knows what they are doing,
24506 and it will trust that no elaboration errors can occur.
24508 The result of this decision is two-fold. First to be safe using the
24509 static mode, you should remove all @code{pragma Elaborate} statements.
24510 Second, when fixing circularities in existing code, you can selectively
24511 use @code{pragma Elaborate} statements to convince the static mode of
24512 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
24515 When using the static mode with @option{-gnatwl}, any use of
24516 @code{pragma Elaborate} will generate a warning about possible
24519 @node Elaboration Issues for Library Tasks
24520 @section Elaboration Issues for Library Tasks
24521 @cindex Library tasks, elaboration issues
24522 @cindex Elaboration of library tasks
24525 In this section we examine special elaboration issues that arise for
24526 programs that declare library level tasks.
24528 Generally the model of execution of an Ada program is that all units are
24529 elaborated, and then execution of the program starts. However, the
24530 declaration of library tasks definitely does not fit this model. The
24531 reason for this is that library tasks start as soon as they are declared
24532 (more precisely, as soon as the statement part of the enclosing package
24533 body is reached), that is to say before elaboration
24534 of the program is complete. This means that if such a task calls a
24535 subprogram, or an entry in another task, the callee may or may not be
24536 elaborated yet, and in the standard
24537 Reference Manual model of dynamic elaboration checks, you can even
24538 get timing dependent Program_Error exceptions, since there can be
24539 a race between the elaboration code and the task code.
24541 The static model of elaboration in GNAT seeks to avoid all such
24542 dynamic behavior, by being conservative, and the conservative
24543 approach in this particular case is to assume that all the code
24544 in a task body is potentially executed at elaboration time if
24545 a task is declared at the library level.
24547 This can definitely result in unexpected circularities. Consider
24548 the following example
24550 @smallexample @c ada
24556 type My_Int is new Integer;
24558 function Ident (M : My_Int) return My_Int;
24562 package body Decls is
24563 task body Lib_Task is
24569 function Ident (M : My_Int) return My_Int is
24577 procedure Put_Val (Arg : Decls.My_Int);
24581 package body Utils is
24582 procedure Put_Val (Arg : Decls.My_Int) is
24584 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
24591 Decls.Lib_Task.Start;
24596 If the above example is compiled in the default static elaboration
24597 mode, then a circularity occurs. The circularity comes from the call
24598 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
24599 this call occurs in elaboration code, we need an implicit pragma
24600 @code{Elaborate_All} for @code{Utils}. This means that not only must
24601 the spec and body of @code{Utils} be elaborated before the body
24602 of @code{Decls}, but also the spec and body of any unit that is
24603 @code{with'ed} by the body of @code{Utils} must also be elaborated before
24604 the body of @code{Decls}. This is the transitive implication of
24605 pragma @code{Elaborate_All} and it makes sense, because in general
24606 the body of @code{Put_Val} might have a call to something in a
24607 @code{with'ed} unit.
24609 In this case, the body of Utils (actually its spec) @code{with's}
24610 @code{Decls}. Unfortunately this means that the body of @code{Decls}
24611 must be elaborated before itself, in case there is a call from the
24612 body of @code{Utils}.
24614 Here is the exact chain of events we are worrying about:
24618 In the body of @code{Decls} a call is made from within the body of a library
24619 task to a subprogram in the package @code{Utils}. Since this call may
24620 occur at elaboration time (given that the task is activated at elaboration
24621 time), we have to assume the worst, i.e. that the
24622 call does happen at elaboration time.
24625 This means that the body and spec of @code{Util} must be elaborated before
24626 the body of @code{Decls} so that this call does not cause an access before
24630 Within the body of @code{Util}, specifically within the body of
24631 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
24635 One such @code{with}'ed package is package @code{Decls}, so there
24636 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
24637 In fact there is such a call in this example, but we would have to
24638 assume that there was such a call even if it were not there, since
24639 we are not supposed to write the body of @code{Decls} knowing what
24640 is in the body of @code{Utils}; certainly in the case of the
24641 static elaboration model, the compiler does not know what is in
24642 other bodies and must assume the worst.
24645 This means that the spec and body of @code{Decls} must also be
24646 elaborated before we elaborate the unit containing the call, but
24647 that unit is @code{Decls}! This means that the body of @code{Decls}
24648 must be elaborated before itself, and that's a circularity.
24652 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
24653 the body of @code{Decls} you will get a true Ada Reference Manual
24654 circularity that makes the program illegal.
24656 In practice, we have found that problems with the static model of
24657 elaboration in existing code often arise from library tasks, so
24658 we must address this particular situation.
24660 Note that if we compile and run the program above, using the dynamic model of
24661 elaboration (that is to say use the @option{-gnatE} switch),
24662 then it compiles, binds,
24663 links, and runs, printing the expected result of 2. Therefore in some sense
24664 the circularity here is only apparent, and we need to capture
24665 the properties of this program that distinguish it from other library-level
24666 tasks that have real elaboration problems.
24668 We have four possible answers to this question:
24673 Use the dynamic model of elaboration.
24675 If we use the @option{-gnatE} switch, then as noted above, the program works.
24676 Why is this? If we examine the task body, it is apparent that the task cannot
24678 @code{accept} statement until after elaboration has been completed, because
24679 the corresponding entry call comes from the main program, not earlier.
24680 This is why the dynamic model works here. But that's really giving
24681 up on a precise analysis, and we prefer to take this approach only if we cannot
24683 problem in any other manner. So let us examine two ways to reorganize
24684 the program to avoid the potential elaboration problem.
24687 Split library tasks into separate packages.
24689 Write separate packages, so that library tasks are isolated from
24690 other declarations as much as possible. Let us look at a variation on
24693 @smallexample @c ada
24701 package body Decls1 is
24702 task body Lib_Task is
24710 type My_Int is new Integer;
24711 function Ident (M : My_Int) return My_Int;
24715 package body Decls2 is
24716 function Ident (M : My_Int) return My_Int is
24724 procedure Put_Val (Arg : Decls2.My_Int);
24728 package body Utils is
24729 procedure Put_Val (Arg : Decls2.My_Int) is
24731 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
24738 Decls1.Lib_Task.Start;
24743 All we have done is to split @code{Decls} into two packages, one
24744 containing the library task, and one containing everything else. Now
24745 there is no cycle, and the program compiles, binds, links and executes
24746 using the default static model of elaboration.
24749 Declare separate task types.
24751 A significant part of the problem arises because of the use of the
24752 single task declaration form. This means that the elaboration of
24753 the task type, and the elaboration of the task itself (i.e. the
24754 creation of the task) happen at the same time. A good rule
24755 of style in Ada 95 is to always create explicit task types. By
24756 following the additional step of placing task objects in separate
24757 packages from the task type declaration, many elaboration problems
24758 are avoided. Here is another modified example of the example program:
24760 @smallexample @c ada
24762 task type Lib_Task_Type is
24766 type My_Int is new Integer;
24768 function Ident (M : My_Int) return My_Int;
24772 package body Decls is
24773 task body Lib_Task_Type is
24779 function Ident (M : My_Int) return My_Int is
24787 procedure Put_Val (Arg : Decls.My_Int);
24791 package body Utils is
24792 procedure Put_Val (Arg : Decls.My_Int) is
24794 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
24800 Lib_Task : Decls.Lib_Task_Type;
24806 Declst.Lib_Task.Start;
24811 What we have done here is to replace the @code{task} declaration in
24812 package @code{Decls} with a @code{task type} declaration. Then we
24813 introduce a separate package @code{Declst} to contain the actual
24814 task object. This separates the elaboration issues for
24815 the @code{task type}
24816 declaration, which causes no trouble, from the elaboration issues
24817 of the task object, which is also unproblematic, since it is now independent
24818 of the elaboration of @code{Utils}.
24819 This separation of concerns also corresponds to
24820 a generally sound engineering principle of separating declarations
24821 from instances. This version of the program also compiles, binds, links,
24822 and executes, generating the expected output.
24825 Use No_Entry_Calls_In_Elaboration_Code restriction.
24826 @cindex No_Entry_Calls_In_Elaboration_Code
24828 The previous two approaches described how a program can be restructured
24829 to avoid the special problems caused by library task bodies. in practice,
24830 however, such restructuring may be difficult to apply to existing legacy code,
24831 so we must consider solutions that do not require massive rewriting.
24833 Let us consider more carefully why our original sample program works
24834 under the dynamic model of elaboration. The reason is that the code
24835 in the task body blocks immediately on the @code{accept}
24836 statement. Now of course there is nothing to prohibit elaboration
24837 code from making entry calls (for example from another library level task),
24838 so we cannot tell in isolation that
24839 the task will not execute the accept statement during elaboration.
24841 However, in practice it is very unusual to see elaboration code
24842 make any entry calls, and the pattern of tasks starting
24843 at elaboration time and then immediately blocking on @code{accept} or
24844 @code{select} statements is very common. What this means is that
24845 the compiler is being too pessimistic when it analyzes the
24846 whole package body as though it might be executed at elaboration
24849 If we know that the elaboration code contains no entry calls, (a very safe
24850 assumption most of the time, that could almost be made the default
24851 behavior), then we can compile all units of the program under control
24852 of the following configuration pragma:
24855 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
24859 This pragma can be placed in the @file{gnat.adc} file in the usual
24860 manner. If we take our original unmodified program and compile it
24861 in the presence of a @file{gnat.adc} containing the above pragma,
24862 then once again, we can compile, bind, link, and execute, obtaining
24863 the expected result. In the presence of this pragma, the compiler does
24864 not trace calls in a task body, that appear after the first @code{accept}
24865 or @code{select} statement, and therefore does not report a potential
24866 circularity in the original program.
24868 The compiler will check to the extent it can that the above
24869 restriction is not violated, but it is not always possible to do a
24870 complete check at compile time, so it is important to use this
24871 pragma only if the stated restriction is in fact met, that is to say
24872 no task receives an entry call before elaboration of all units is completed.
24876 @node Mixing Elaboration Models
24877 @section Mixing Elaboration Models
24879 So far, we have assumed that the entire program is either compiled
24880 using the dynamic model or static model, ensuring consistency. It
24881 is possible to mix the two models, but rules have to be followed
24882 if this mixing is done to ensure that elaboration checks are not
24885 The basic rule is that @emph{a unit compiled with the static model cannot
24886 be @code{with'ed} by a unit compiled with the dynamic model}. The
24887 reason for this is that in the static model, a unit assumes that
24888 its clients guarantee to use (the equivalent of) pragma
24889 @code{Elaborate_All} so that no elaboration checks are required
24890 in inner subprograms, and this assumption is violated if the
24891 client is compiled with dynamic checks.
24893 The precise rule is as follows. A unit that is compiled with dynamic
24894 checks can only @code{with} a unit that meets at least one of the
24895 following criteria:
24900 The @code{with'ed} unit is itself compiled with dynamic elaboration
24901 checks (that is with the @option{-gnatE} switch.
24904 The @code{with'ed} unit is an internal GNAT implementation unit from
24905 the System, Interfaces, Ada, or GNAT hierarchies.
24908 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
24911 The @code{with'ing} unit (that is the client) has an explicit pragma
24912 @code{Elaborate_All} for the @code{with'ed} unit.
24917 If this rule is violated, that is if a unit with dynamic elaboration
24918 checks @code{with's} a unit that does not meet one of the above four
24919 criteria, then the binder (@code{gnatbind}) will issue a warning
24920 similar to that in the following example:
24923 warning: "x.ads" has dynamic elaboration checks and with's
24924 warning: "y.ads" which has static elaboration checks
24928 These warnings indicate that the rule has been violated, and that as a result
24929 elaboration checks may be missed in the resulting executable file.
24930 This warning may be suppressed using the @option{-ws} binder switch
24931 in the usual manner.
24933 One useful application of this mixing rule is in the case of a subsystem
24934 which does not itself @code{with} units from the remainder of the
24935 application. In this case, the entire subsystem can be compiled with
24936 dynamic checks to resolve a circularity in the subsystem, while
24937 allowing the main application that uses this subsystem to be compiled
24938 using the more reliable default static model.
24940 @node What to Do If the Default Elaboration Behavior Fails
24941 @section What to Do If the Default Elaboration Behavior Fails
24944 If the binder cannot find an acceptable order, it outputs detailed
24945 diagnostics. For example:
24951 error: elaboration circularity detected
24952 info: "proc (body)" must be elaborated before "pack (body)"
24953 info: reason: Elaborate_All probably needed in unit "pack (body)"
24954 info: recompile "pack (body)" with -gnatwl
24955 info: for full details
24956 info: "proc (body)"
24957 info: is needed by its spec:
24958 info: "proc (spec)"
24959 info: which is withed by:
24960 info: "pack (body)"
24961 info: "pack (body)" must be elaborated before "proc (body)"
24962 info: reason: pragma Elaborate in unit "proc (body)"
24968 In this case we have a cycle that the binder cannot break. On the one
24969 hand, there is an explicit pragma Elaborate in @code{proc} for
24970 @code{pack}. This means that the body of @code{pack} must be elaborated
24971 before the body of @code{proc}. On the other hand, there is elaboration
24972 code in @code{pack} that calls a subprogram in @code{proc}. This means
24973 that for maximum safety, there should really be a pragma
24974 Elaborate_All in @code{pack} for @code{proc} which would require that
24975 the body of @code{proc} be elaborated before the body of
24976 @code{pack}. Clearly both requirements cannot be satisfied.
24977 Faced with a circularity of this kind, you have three different options.
24980 @item Fix the program
24981 The most desirable option from the point of view of long-term maintenance
24982 is to rearrange the program so that the elaboration problems are avoided.
24983 One useful technique is to place the elaboration code into separate
24984 child packages. Another is to move some of the initialization code to
24985 explicitly called subprograms, where the program controls the order
24986 of initialization explicitly. Although this is the most desirable option,
24987 it may be impractical and involve too much modification, especially in
24988 the case of complex legacy code.
24990 @item Perform dynamic checks
24991 If the compilations are done using the
24993 (dynamic elaboration check) switch, then GNAT behaves in a quite different
24994 manner. Dynamic checks are generated for all calls that could possibly result
24995 in raising an exception. With this switch, the compiler does not generate
24996 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
24997 exactly as specified in the Ada 95 Reference Manual. The binder will generate
24998 an executable program that may or may not raise @code{Program_Error}, and then
24999 it is the programmer's job to ensure that it does not raise an exception. Note
25000 that it is important to compile all units with the switch, it cannot be used
25003 @item Suppress checks
25004 The drawback of dynamic checks is that they generate a
25005 significant overhead at run time, both in space and time. If you
25006 are absolutely sure that your program cannot raise any elaboration
25007 exceptions, and you still want to use the dynamic elaboration model,
25008 then you can use the configuration pragma
25009 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
25010 example this pragma could be placed in the @file{gnat.adc} file.
25012 @item Suppress checks selectively
25013 When you know that certain calls or instantiations in elaboration code cannot
25014 possibly lead to an elaboration error, and the binder nevertheless complains
25015 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
25016 elaboration circularities, it is possible to remove those warnings locally and
25017 obtain a program that will bind. Clearly this can be unsafe, and it is the
25018 responsibility of the programmer to make sure that the resulting program has no
25019 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
25020 used with different granularity to suppress warnings and break elaboration
25025 Place the pragma that names the called subprogram in the declarative part
25026 that contains the call.
25029 Place the pragma in the declarative part, without naming an entity. This
25030 disables warnings on all calls in the corresponding declarative region.
25033 Place the pragma in the package spec that declares the called subprogram,
25034 and name the subprogram. This disables warnings on all elaboration calls to
25038 Place the pragma in the package spec that declares the called subprogram,
25039 without naming any entity. This disables warnings on all elaboration calls to
25040 all subprograms declared in this spec.
25042 @item Use Pragma Elaborate
25043 As previously described in section @xref{Treatment of Pragma Elaborate},
25044 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
25045 that no elaboration checks are required on calls to the designated unit.
25046 There may be cases in which the caller knows that no transitive calls
25047 can occur, so that a @code{pragma Elaborate} will be sufficient in a
25048 case where @code{pragma Elaborate_All} would cause a circularity.
25052 These five cases are listed in order of decreasing safety, and therefore
25053 require increasing programmer care in their application. Consider the
25056 @smallexample @c adanocomment
25058 function F1 return Integer;
25063 function F2 return Integer;
25064 function Pure (x : integer) return integer;
25065 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
25066 -- pragma Suppress (Elaboration_Check); -- (4)
25070 package body Pack1 is
25071 function F1 return Integer is
25075 Val : integer := Pack2.Pure (11); -- Elab. call (1)
25078 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
25079 -- pragma Suppress(Elaboration_Check); -- (2)
25081 X1 := Pack2.F2 + 1; -- Elab. call (2)
25086 package body Pack2 is
25087 function F2 return Integer is
25091 function Pure (x : integer) return integer is
25093 return x ** 3 - 3 * x;
25097 with Pack1, Ada.Text_IO;
25100 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
25103 In the absence of any pragmas, an attempt to bind this program produces
25104 the following diagnostics:
25110 error: elaboration circularity detected
25111 info: "pack1 (body)" must be elaborated before "pack1 (body)"
25112 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
25113 info: recompile "pack1 (body)" with -gnatwl for full details
25114 info: "pack1 (body)"
25115 info: must be elaborated along with its spec:
25116 info: "pack1 (spec)"
25117 info: which is withed by:
25118 info: "pack2 (body)"
25119 info: which must be elaborated along with its spec:
25120 info: "pack2 (spec)"
25121 info: which is withed by:
25122 info: "pack1 (body)"
25125 The sources of the circularity are the two calls to @code{Pack2.Pure} and
25126 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
25127 F2 is safe, even though F2 calls F1, because the call appears after the
25128 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
25129 remove the warning on the call. It is also possible to use pragma (2)
25130 because there are no other potentially unsafe calls in the block.
25133 The call to @code{Pure} is safe because this function does not depend on the
25134 state of @code{Pack2}. Therefore any call to this function is safe, and it
25135 is correct to place pragma (3) in the corresponding package spec.
25138 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
25139 warnings on all calls to functions declared therein. Note that this is not
25140 necessarily safe, and requires more detailed examination of the subprogram
25141 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
25142 be already elaborated.
25146 It is hard to generalize on which of these four approaches should be
25147 taken. Obviously if it is possible to fix the program so that the default
25148 treatment works, this is preferable, but this may not always be practical.
25149 It is certainly simple enough to use
25151 but the danger in this case is that, even if the GNAT binder
25152 finds a correct elaboration order, it may not always do so,
25153 and certainly a binder from another Ada compiler might not. A
25154 combination of testing and analysis (for which the warnings generated
25157 switch can be useful) must be used to ensure that the program is free
25158 of errors. One switch that is useful in this testing is the
25159 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
25162 Normally the binder tries to find an order that has the best chance of
25163 of avoiding elaboration problems. With this switch, the binder
25164 plays a devil's advocate role, and tries to choose the order that
25165 has the best chance of failing. If your program works even with this
25166 switch, then it has a better chance of being error free, but this is still
25169 For an example of this approach in action, consider the C-tests (executable
25170 tests) from the ACVC suite. If these are compiled and run with the default
25171 treatment, then all but one of them succeed without generating any error
25172 diagnostics from the binder. However, there is one test that fails, and
25173 this is not surprising, because the whole point of this test is to ensure
25174 that the compiler can handle cases where it is impossible to determine
25175 a correct order statically, and it checks that an exception is indeed
25176 raised at run time.
25178 This one test must be compiled and run using the
25180 switch, and then it passes. Alternatively, the entire suite can
25181 be run using this switch. It is never wrong to run with the dynamic
25182 elaboration switch if your code is correct, and we assume that the
25183 C-tests are indeed correct (it is less efficient, but efficiency is
25184 not a factor in running the ACVC tests.)
25186 @node Elaboration for Access-to-Subprogram Values
25187 @section Elaboration for Access-to-Subprogram Values
25188 @cindex Access-to-subprogram
25191 The introduction of access-to-subprogram types in Ada 95 complicates
25192 the handling of elaboration. The trouble is that it becomes
25193 impossible to tell at compile time which procedure
25194 is being called. This means that it is not possible for the binder
25195 to analyze the elaboration requirements in this case.
25197 If at the point at which the access value is created
25198 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
25199 the body of the subprogram is
25200 known to have been elaborated, then the access value is safe, and its use
25201 does not require a check. This may be achieved by appropriate arrangement
25202 of the order of declarations if the subprogram is in the current unit,
25203 or, if the subprogram is in another unit, by using pragma
25204 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
25205 on the referenced unit.
25207 If the referenced body is not known to have been elaborated at the point
25208 the access value is created, then any use of the access value must do a
25209 dynamic check, and this dynamic check will fail and raise a
25210 @code{Program_Error} exception if the body has not been elaborated yet.
25211 GNAT will generate the necessary checks, and in addition, if the
25213 switch is set, will generate warnings that such checks are required.
25215 The use of dynamic dispatching for tagged types similarly generates
25216 a requirement for dynamic checks, and premature calls to any primitive
25217 operation of a tagged type before the body of the operation has been
25218 elaborated, will result in the raising of @code{Program_Error}.
25220 @node Summary of Procedures for Elaboration Control
25221 @section Summary of Procedures for Elaboration Control
25222 @cindex Elaboration control
25225 First, compile your program with the default options, using none of
25226 the special elaboration control switches. If the binder successfully
25227 binds your program, then you can be confident that, apart from issues
25228 raised by the use of access-to-subprogram types and dynamic dispatching,
25229 the program is free of elaboration errors. If it is important that the
25230 program be portable, then use the
25232 switch to generate warnings about missing @code{Elaborate} or
25233 @code{Elaborate_All} pragmas, and supply the missing pragmas.
25235 If the program fails to bind using the default static elaboration
25236 handling, then you can fix the program to eliminate the binder
25237 message, or recompile the entire program with the
25238 @option{-gnatE} switch to generate dynamic elaboration checks,
25239 and, if you are sure there really are no elaboration problems,
25240 use a global pragma @code{Suppress (Elaboration_Check)}.
25242 @node Other Elaboration Order Considerations
25243 @section Other Elaboration Order Considerations
25245 This section has been entirely concerned with the issue of finding a valid
25246 elaboration order, as defined by the Ada Reference Manual. In a case
25247 where several elaboration orders are valid, the task is to find one
25248 of the possible valid elaboration orders (and the static model in GNAT
25249 will ensure that this is achieved).
25251 The purpose of the elaboration rules in the Ada Reference Manual is to
25252 make sure that no entity is accessed before it has been elaborated. For
25253 a subprogram, this means that the spec and body must have been elaborated
25254 before the subprogram is called. For an object, this means that the object
25255 must have been elaborated before its value is read or written. A violation
25256 of either of these two requirements is an access before elaboration order,
25257 and this section has been all about avoiding such errors.
25259 In the case where more than one order of elaboration is possible, in the
25260 sense that access before elaboration errors are avoided, then any one of
25261 the orders is ``correct'' in the sense that it meets the requirements of
25262 the Ada Reference Manual, and no such error occurs.
25264 However, it may be the case for a given program, that there are
25265 constraints on the order of elaboration that come not from consideration
25266 of avoiding elaboration errors, but rather from extra-lingual logic
25267 requirements. Consider this example:
25269 @smallexample @c ada
25270 with Init_Constants;
25271 package Constants is
25276 package Init_Constants is
25277 procedure P; -- require a body
25278 end Init_Constants;
25281 package body Init_Constants is
25282 procedure P is begin null; end;
25286 end Init_Constants;
25290 Z : Integer := Constants.X + Constants.Y;
25294 with Text_IO; use Text_IO;
25297 Put_Line (Calc.Z'Img);
25302 In this example, there is more than one valid order of elaboration. For
25303 example both the following are correct orders:
25306 Init_Constants spec
25309 Init_Constants body
25314 Init_Constants spec
25315 Init_Constants body
25322 There is no language rule to prefer one or the other, both are correct
25323 from an order of elaboration point of view. But the programmatic effects
25324 of the two orders are very different. In the first, the elaboration routine
25325 of @code{Calc} initializes @code{Z} to zero, and then the main program
25326 runs with this value of zero. But in the second order, the elaboration
25327 routine of @code{Calc} runs after the body of Init_Constants has set
25328 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
25331 One could perhaps by applying pretty clever non-artificial intelligence
25332 to the situation guess that it is more likely that the second order of
25333 elaboration is the one desired, but there is no formal linguistic reason
25334 to prefer one over the other. In fact in this particular case, GNAT will
25335 prefer the second order, because of the rule that bodies are elaborated
25336 as soon as possible, but it's just luck that this is what was wanted
25337 (if indeed the second order was preferred).
25339 If the program cares about the order of elaboration routines in a case like
25340 this, it is important to specify the order required. In this particular
25341 case, that could have been achieved by adding to the spec of Calc:
25343 @smallexample @c ada
25344 pragma Elaborate_All (Constants);
25348 which requires that the body (if any) and spec of @code{Constants},
25349 as well as the body and spec of any unit @code{with}'ed by
25350 @code{Constants} be elaborated before @code{Calc} is elaborated.
25352 Clearly no automatic method can always guess which alternative you require,
25353 and if you are working with legacy code that had constraints of this kind
25354 which were not properly specified by adding @code{Elaborate} or
25355 @code{Elaborate_All} pragmas, then indeed it is possible that two different
25356 compilers can choose different orders.
25358 The @code{gnatbind}
25359 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
25360 out problems. This switch causes bodies to be elaborated as late as possible
25361 instead of as early as possible. In the example above, it would have forced
25362 the choice of the first elaboration order. If you get different results
25363 when using this switch, and particularly if one set of results is right,
25364 and one is wrong as far as you are concerned, it shows that you have some
25365 missing @code{Elaborate} pragmas. For the example above, we have the
25369 gnatmake -f -q main
25372 gnatmake -f -q main -bargs -p
25378 It is of course quite unlikely that both these results are correct, so
25379 it is up to you in a case like this to investigate the source of the
25380 difference, by looking at the two elaboration orders that are chosen,
25381 and figuring out which is correct, and then adding the necessary
25382 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
25384 @node Inline Assembler
25385 @appendix Inline Assembler
25388 If you need to write low-level software that interacts directly
25389 with the hardware, Ada provides two ways to incorporate assembly
25390 language code into your program. First, you can import and invoke
25391 external routines written in assembly language, an Ada feature fully
25392 supported by GNAT. However, for small sections of code it may be simpler
25393 or more efficient to include assembly language statements directly
25394 in your Ada source program, using the facilities of the implementation-defined
25395 package @code{System.Machine_Code}, which incorporates the gcc
25396 Inline Assembler. The Inline Assembler approach offers a number of advantages,
25397 including the following:
25400 @item No need to use non-Ada tools
25401 @item Consistent interface over different targets
25402 @item Automatic usage of the proper calling conventions
25403 @item Access to Ada constants and variables
25404 @item Definition of intrinsic routines
25405 @item Possibility of inlining a subprogram comprising assembler code
25406 @item Code optimizer can take Inline Assembler code into account
25409 This chapter presents a series of examples to show you how to use
25410 the Inline Assembler. Although it focuses on the Intel x86,
25411 the general approach applies also to other processors.
25412 It is assumed that you are familiar with Ada
25413 and with assembly language programming.
25416 * Basic Assembler Syntax::
25417 * A Simple Example of Inline Assembler::
25418 * Output Variables in Inline Assembler::
25419 * Input Variables in Inline Assembler::
25420 * Inlining Inline Assembler Code::
25421 * Other Asm Functionality::
25424 @c ---------------------------------------------------------------------------
25425 @node Basic Assembler Syntax
25426 @section Basic Assembler Syntax
25429 The assembler used by GNAT and gcc is based not on the Intel assembly
25430 language, but rather on a language that descends from the AT&T Unix
25431 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
25432 The following table summarizes the main features of @emph{as} syntax
25433 and points out the differences from the Intel conventions.
25434 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
25435 pre-processor) documentation for further information.
25438 @item Register names
25439 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
25441 Intel: No extra punctuation; for example @code{eax}
25443 @item Immediate operand
25444 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
25446 Intel: No extra punctuation; for example @code{4}
25449 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
25451 Intel: No extra punctuation; for example @code{loc}
25453 @item Memory contents
25454 gcc / @emph{as}: No extra punctuation; for example @code{loc}
25456 Intel: Square brackets; for example @code{[loc]}
25458 @item Register contents
25459 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
25461 Intel: Square brackets; for example @code{[eax]}
25463 @item Hexadecimal numbers
25464 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
25466 Intel: Trailing ``h''; for example @code{A0h}
25469 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
25472 Intel: Implicit, deduced by assembler; for example @code{mov}
25474 @item Instruction repetition
25475 gcc / @emph{as}: Split into two lines; for example
25481 Intel: Keep on one line; for example @code{rep stosl}
25483 @item Order of operands
25484 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
25486 Intel: Destination first; for example @code{mov eax, 4}
25489 @c ---------------------------------------------------------------------------
25490 @node A Simple Example of Inline Assembler
25491 @section A Simple Example of Inline Assembler
25494 The following example will generate a single assembly language statement,
25495 @code{nop}, which does nothing. Despite its lack of run-time effect,
25496 the example will be useful in illustrating the basics of
25497 the Inline Assembler facility.
25499 @smallexample @c ada
25501 with System.Machine_Code; use System.Machine_Code;
25502 procedure Nothing is
25509 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
25510 here it takes one parameter, a @emph{template string} that must be a static
25511 expression and that will form the generated instruction.
25512 @code{Asm} may be regarded as a compile-time procedure that parses
25513 the template string and additional parameters (none here),
25514 from which it generates a sequence of assembly language instructions.
25516 The examples in this chapter will illustrate several of the forms
25517 for invoking @code{Asm}; a complete specification of the syntax
25518 is found in the @cite{GNAT Reference Manual}.
25520 Under the standard GNAT conventions, the @code{Nothing} procedure
25521 should be in a file named @file{nothing.adb}.
25522 You can build the executable in the usual way:
25526 However, the interesting aspect of this example is not its run-time behavior
25527 but rather the generated assembly code.
25528 To see this output, invoke the compiler as follows:
25530 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
25532 where the options are:
25536 compile only (no bind or link)
25538 generate assembler listing
25539 @item -fomit-frame-pointer
25540 do not set up separate stack frames
25542 do not add runtime checks
25545 This gives a human-readable assembler version of the code. The resulting
25546 file will have the same name as the Ada source file, but with a @code{.s}
25547 extension. In our example, the file @file{nothing.s} has the following
25552 .file "nothing.adb"
25554 ___gnu_compiled_ada:
25557 .globl __ada_nothing
25569 The assembly code you included is clearly indicated by
25570 the compiler, between the @code{#APP} and @code{#NO_APP}
25571 delimiters. The character before the 'APP' and 'NOAPP'
25572 can differ on different targets. For example, GNU/Linux uses '#APP' while
25573 on NT you will see '/APP'.
25575 If you make a mistake in your assembler code (such as using the
25576 wrong size modifier, or using a wrong operand for the instruction) GNAT
25577 will report this error in a temporary file, which will be deleted when
25578 the compilation is finished. Generating an assembler file will help
25579 in such cases, since you can assemble this file separately using the
25580 @emph{as} assembler that comes with gcc.
25582 Assembling the file using the command
25585 as @file{nothing.s}
25588 will give you error messages whose lines correspond to the assembler
25589 input file, so you can easily find and correct any mistakes you made.
25590 If there are no errors, @emph{as} will generate an object file
25591 @file{nothing.out}.
25593 @c ---------------------------------------------------------------------------
25594 @node Output Variables in Inline Assembler
25595 @section Output Variables in Inline Assembler
25598 The examples in this section, showing how to access the processor flags,
25599 illustrate how to specify the destination operands for assembly language
25602 @smallexample @c ada
25604 with Interfaces; use Interfaces;
25605 with Ada.Text_IO; use Ada.Text_IO;
25606 with System.Machine_Code; use System.Machine_Code;
25607 procedure Get_Flags is
25608 Flags : Unsigned_32;
25611 Asm ("pushfl" & LF & HT & -- push flags on stack
25612 "popl %%eax" & LF & HT & -- load eax with flags
25613 "movl %%eax, %0", -- store flags in variable
25614 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25615 Put_Line ("Flags register:" & Flags'Img);
25620 In order to have a nicely aligned assembly listing, we have separated
25621 multiple assembler statements in the Asm template string with linefeed
25622 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
25623 The resulting section of the assembly output file is:
25630 movl %eax, -40(%ebp)
25635 It would have been legal to write the Asm invocation as:
25638 Asm ("pushfl popl %%eax movl %%eax, %0")
25641 but in the generated assembler file, this would come out as:
25645 pushfl popl %eax movl %eax, -40(%ebp)
25649 which is not so convenient for the human reader.
25651 We use Ada comments
25652 at the end of each line to explain what the assembler instructions
25653 actually do. This is a useful convention.
25655 When writing Inline Assembler instructions, you need to precede each register
25656 and variable name with a percent sign. Since the assembler already requires
25657 a percent sign at the beginning of a register name, you need two consecutive
25658 percent signs for such names in the Asm template string, thus @code{%%eax}.
25659 In the generated assembly code, one of the percent signs will be stripped off.
25661 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
25662 variables: operands you later define using @code{Input} or @code{Output}
25663 parameters to @code{Asm}.
25664 An output variable is illustrated in
25665 the third statement in the Asm template string:
25669 The intent is to store the contents of the eax register in a variable that can
25670 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
25671 necessarily work, since the compiler might optimize by using a register
25672 to hold Flags, and the expansion of the @code{movl} instruction would not be
25673 aware of this optimization. The solution is not to store the result directly
25674 but rather to advise the compiler to choose the correct operand form;
25675 that is the purpose of the @code{%0} output variable.
25677 Information about the output variable is supplied in the @code{Outputs}
25678 parameter to @code{Asm}:
25680 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25683 The output is defined by the @code{Asm_Output} attribute of the target type;
25684 the general format is
25686 Type'Asm_Output (constraint_string, variable_name)
25689 The constraint string directs the compiler how
25690 to store/access the associated variable. In the example
25692 Unsigned_32'Asm_Output ("=m", Flags);
25694 the @code{"m"} (memory) constraint tells the compiler that the variable
25695 @code{Flags} should be stored in a memory variable, thus preventing
25696 the optimizer from keeping it in a register. In contrast,
25698 Unsigned_32'Asm_Output ("=r", Flags);
25700 uses the @code{"r"} (register) constraint, telling the compiler to
25701 store the variable in a register.
25703 If the constraint is preceded by the equal character (@strong{=}), it tells
25704 the compiler that the variable will be used to store data into it.
25706 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
25707 allowing the optimizer to choose whatever it deems best.
25709 There are a fairly large number of constraints, but the ones that are
25710 most useful (for the Intel x86 processor) are the following:
25716 global (i.e. can be stored anywhere)
25734 use one of eax, ebx, ecx or edx
25736 use one of eax, ebx, ecx, edx, esi or edi
25739 The full set of constraints is described in the gcc and @emph{as}
25740 documentation; note that it is possible to combine certain constraints
25741 in one constraint string.
25743 You specify the association of an output variable with an assembler operand
25744 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
25746 @smallexample @c ada
25748 Asm ("pushfl" & LF & HT & -- push flags on stack
25749 "popl %%eax" & LF & HT & -- load eax with flags
25750 "movl %%eax, %0", -- store flags in variable
25751 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25755 @code{%0} will be replaced in the expanded code by the appropriate operand,
25757 the compiler decided for the @code{Flags} variable.
25759 In general, you may have any number of output variables:
25762 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
25764 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
25765 of @code{Asm_Output} attributes
25769 @smallexample @c ada
25771 Asm ("movl %%eax, %0" & LF & HT &
25772 "movl %%ebx, %1" & LF & HT &
25774 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
25775 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
25776 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
25780 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
25781 in the Ada program.
25783 As a variation on the @code{Get_Flags} example, we can use the constraints
25784 string to direct the compiler to store the eax register into the @code{Flags}
25785 variable, instead of including the store instruction explicitly in the
25786 @code{Asm} template string:
25788 @smallexample @c ada
25790 with Interfaces; use Interfaces;
25791 with Ada.Text_IO; use Ada.Text_IO;
25792 with System.Machine_Code; use System.Machine_Code;
25793 procedure Get_Flags_2 is
25794 Flags : Unsigned_32;
25797 Asm ("pushfl" & LF & HT & -- push flags on stack
25798 "popl %%eax", -- save flags in eax
25799 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
25800 Put_Line ("Flags register:" & Flags'Img);
25806 The @code{"a"} constraint tells the compiler that the @code{Flags}
25807 variable will come from the eax register. Here is the resulting code:
25815 movl %eax,-40(%ebp)
25820 The compiler generated the store of eax into Flags after
25821 expanding the assembler code.
25823 Actually, there was no need to pop the flags into the eax register;
25824 more simply, we could just pop the flags directly into the program variable:
25826 @smallexample @c ada
25828 with Interfaces; use Interfaces;
25829 with Ada.Text_IO; use Ada.Text_IO;
25830 with System.Machine_Code; use System.Machine_Code;
25831 procedure Get_Flags_3 is
25832 Flags : Unsigned_32;
25835 Asm ("pushfl" & LF & HT & -- push flags on stack
25836 "pop %0", -- save flags in Flags
25837 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
25838 Put_Line ("Flags register:" & Flags'Img);
25843 @c ---------------------------------------------------------------------------
25844 @node Input Variables in Inline Assembler
25845 @section Input Variables in Inline Assembler
25848 The example in this section illustrates how to specify the source operands
25849 for assembly language statements.
25850 The program simply increments its input value by 1:
25852 @smallexample @c ada
25854 with Interfaces; use Interfaces;
25855 with Ada.Text_IO; use Ada.Text_IO;
25856 with System.Machine_Code; use System.Machine_Code;
25857 procedure Increment is
25859 function Incr (Value : Unsigned_32) return Unsigned_32 is
25860 Result : Unsigned_32;
25863 Inputs => Unsigned_32'Asm_Input ("a", Value),
25864 Outputs => Unsigned_32'Asm_Output ("=a", Result));
25868 Value : Unsigned_32;
25872 Put_Line ("Value before is" & Value'Img);
25873 Value := Incr (Value);
25874 Put_Line ("Value after is" & Value'Img);
25879 The @code{Outputs} parameter to @code{Asm} specifies
25880 that the result will be in the eax register and that it is to be stored
25881 in the @code{Result} variable.
25883 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
25884 but with an @code{Asm_Input} attribute.
25885 The @code{"="} constraint, indicating an output value, is not present.
25887 You can have multiple input variables, in the same way that you can have more
25888 than one output variable.
25890 The parameter count (%0, %1) etc, now starts at the first input
25891 statement, and continues with the output statements.
25892 When both parameters use the same variable, the
25893 compiler will treat them as the same %n operand, which is the case here.
25895 Just as the @code{Outputs} parameter causes the register to be stored into the
25896 target variable after execution of the assembler statements, so does the
25897 @code{Inputs} parameter cause its variable to be loaded into the register
25898 before execution of the assembler statements.
25900 Thus the effect of the @code{Asm} invocation is:
25902 @item load the 32-bit value of @code{Value} into eax
25903 @item execute the @code{incl %eax} instruction
25904 @item store the contents of eax into the @code{Result} variable
25907 The resulting assembler file (with @option{-O2} optimization) contains:
25910 _increment__incr.1:
25923 @c ---------------------------------------------------------------------------
25924 @node Inlining Inline Assembler Code
25925 @section Inlining Inline Assembler Code
25928 For a short subprogram such as the @code{Incr} function in the previous
25929 section, the overhead of the call and return (creating / deleting the stack
25930 frame) can be significant, compared to the amount of code in the subprogram
25931 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
25932 which directs the compiler to expand invocations of the subprogram at the
25933 point(s) of call, instead of setting up a stack frame for out-of-line calls.
25934 Here is the resulting program:
25936 @smallexample @c ada
25938 with Interfaces; use Interfaces;
25939 with Ada.Text_IO; use Ada.Text_IO;
25940 with System.Machine_Code; use System.Machine_Code;
25941 procedure Increment_2 is
25943 function Incr (Value : Unsigned_32) return Unsigned_32 is
25944 Result : Unsigned_32;
25947 Inputs => Unsigned_32'Asm_Input ("a", Value),
25948 Outputs => Unsigned_32'Asm_Output ("=a", Result));
25951 pragma Inline (Increment);
25953 Value : Unsigned_32;
25957 Put_Line ("Value before is" & Value'Img);
25958 Value := Increment (Value);
25959 Put_Line ("Value after is" & Value'Img);
25964 Compile the program with both optimization (@option{-O2}) and inlining
25965 enabled (@option{-gnatpn} instead of @option{-gnatp}).
25967 The @code{Incr} function is still compiled as usual, but at the
25968 point in @code{Increment} where our function used to be called:
25973 call _increment__incr.1
25978 the code for the function body directly appears:
25991 thus saving the overhead of stack frame setup and an out-of-line call.
25993 @c ---------------------------------------------------------------------------
25994 @node Other Asm Functionality
25995 @section Other @code{Asm} Functionality
25998 This section describes two important parameters to the @code{Asm}
25999 procedure: @code{Clobber}, which identifies register usage;
26000 and @code{Volatile}, which inhibits unwanted optimizations.
26003 * The Clobber Parameter::
26004 * The Volatile Parameter::
26007 @c ---------------------------------------------------------------------------
26008 @node The Clobber Parameter
26009 @subsection The @code{Clobber} Parameter
26012 One of the dangers of intermixing assembly language and a compiled language
26013 such as Ada is that the compiler needs to be aware of which registers are
26014 being used by the assembly code. In some cases, such as the earlier examples,
26015 the constraint string is sufficient to indicate register usage (e.g.,
26017 the eax register). But more generally, the compiler needs an explicit
26018 identification of the registers that are used by the Inline Assembly
26021 Using a register that the compiler doesn't know about
26022 could be a side effect of an instruction (like @code{mull}
26023 storing its result in both eax and edx).
26024 It can also arise from explicit register usage in your
26025 assembly code; for example:
26028 Asm ("movl %0, %%ebx" & LF & HT &
26030 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
26031 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
26035 where the compiler (since it does not analyze the @code{Asm} template string)
26036 does not know you are using the ebx register.
26038 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
26039 to identify the registers that will be used by your assembly code:
26043 Asm ("movl %0, %%ebx" & LF & HT &
26045 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
26046 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
26051 The Clobber parameter is a static string expression specifying the
26052 register(s) you are using. Note that register names are @emph{not} prefixed
26053 by a percent sign. Also, if more than one register is used then their names
26054 are separated by commas; e.g., @code{"eax, ebx"}
26056 The @code{Clobber} parameter has several additional uses:
26058 @item Use ``register'' name @code{cc} to indicate that flags might have changed
26059 @item Use ``register'' name @code{memory} if you changed a memory location
26062 @c ---------------------------------------------------------------------------
26063 @node The Volatile Parameter
26064 @subsection The @code{Volatile} Parameter
26065 @cindex Volatile parameter
26068 Compiler optimizations in the presence of Inline Assembler may sometimes have
26069 unwanted effects. For example, when an @code{Asm} invocation with an input
26070 variable is inside a loop, the compiler might move the loading of the input
26071 variable outside the loop, regarding it as a one-time initialization.
26073 If this effect is not desired, you can disable such optimizations by setting
26074 the @code{Volatile} parameter to @code{True}; for example:
26076 @smallexample @c ada
26078 Asm ("movl %0, %%ebx" & LF & HT &
26080 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
26081 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
26087 By default, @code{Volatile} is set to @code{False} unless there is no
26088 @code{Outputs} parameter.
26090 Although setting @code{Volatile} to @code{True} prevents unwanted
26091 optimizations, it will also disable other optimizations that might be
26092 important for efficiency. In general, you should set @code{Volatile}
26093 to @code{True} only if the compiler's optimizations have created
26095 @c END OF INLINE ASSEMBLER CHAPTER
26096 @c ===============================
26098 @c ***********************************
26099 @c * Compatibility and Porting Guide *
26100 @c ***********************************
26101 @node Compatibility and Porting Guide
26102 @appendix Compatibility and Porting Guide
26105 This chapter describes the compatibility issues that may arise between
26106 GNAT and other Ada 83 and Ada 95 compilation systems, and shows how GNAT
26107 can expedite porting
26108 applications developed in other Ada environments.
26111 * Compatibility with Ada 83::
26112 * Implementation-dependent characteristics::
26113 * Compatibility with Other Ada 95 Systems::
26114 * Representation Clauses::
26116 @c Brief section is only in non-VMS version
26117 @c Full chapter is in VMS version
26118 * Compatibility with HP Ada 83::
26121 * Transitioning from Alpha to I64 OpenVMS::
26125 @node Compatibility with Ada 83
26126 @section Compatibility with Ada 83
26127 @cindex Compatibility (between Ada 83 and Ada 95)
26130 Ada 95 is designed to be highly upwards compatible with Ada 83. In
26131 particular, the design intention is that the difficulties associated
26132 with moving from Ada 83 to Ada 95 should be no greater than those
26133 that occur when moving from one Ada 83 system to another.
26135 However, there are a number of points at which there are minor
26136 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
26137 full details of these issues,
26138 and should be consulted for a complete treatment.
26140 following subsections treat the most likely issues to be encountered.
26143 * Legal Ada 83 programs that are illegal in Ada 95::
26144 * More deterministic semantics::
26145 * Changed semantics::
26146 * Other language compatibility issues::
26149 @node Legal Ada 83 programs that are illegal in Ada 95
26150 @subsection Legal Ada 83 programs that are illegal in Ada 95
26153 @item Character literals
26154 Some uses of character literals are ambiguous. Since Ada 95 has introduced
26155 @code{Wide_Character} as a new predefined character type, some uses of
26156 character literals that were legal in Ada 83 are illegal in Ada 95.
26158 @smallexample @c ada
26159 for Char in 'A' .. 'Z' loop ... end loop;
26162 The problem is that @code{'A'} and @code{'Z'} could be from either
26163 @code{Character} or @code{Wide_Character}. The simplest correction
26164 is to make the type explicit; e.g.:
26165 @smallexample @c ada
26166 for Char in Character range 'A' .. 'Z' loop ... end loop;
26169 @item New reserved words
26170 The identifiers @code{abstract}, @code{aliased}, @code{protected},
26171 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
26172 Existing Ada 83 code using any of these identifiers must be edited to
26173 use some alternative name.
26175 @item Freezing rules
26176 The rules in Ada 95 are slightly different with regard to the point at
26177 which entities are frozen, and representation pragmas and clauses are
26178 not permitted past the freeze point. This shows up most typically in
26179 the form of an error message complaining that a representation item
26180 appears too late, and the appropriate corrective action is to move
26181 the item nearer to the declaration of the entity to which it refers.
26183 A particular case is that representation pragmas
26186 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
26188 cannot be applied to a subprogram body. If necessary, a separate subprogram
26189 declaration must be introduced to which the pragma can be applied.
26191 @item Optional bodies for library packages
26192 In Ada 83, a package that did not require a package body was nevertheless
26193 allowed to have one. This lead to certain surprises in compiling large
26194 systems (situations in which the body could be unexpectedly ignored by the
26195 binder). In Ada 95, if a package does not require a body then it is not
26196 permitted to have a body. To fix this problem, simply remove a redundant
26197 body if it is empty, or, if it is non-empty, introduce a dummy declaration
26198 into the spec that makes the body required. One approach is to add a private
26199 part to the package declaration (if necessary), and define a parameterless
26200 procedure called @code{Requires_Body}, which must then be given a dummy
26201 procedure body in the package body, which then becomes required.
26202 Another approach (assuming that this does not introduce elaboration
26203 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
26204 since one effect of this pragma is to require the presence of a package body.
26206 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
26207 In Ada 95, the exception @code{Numeric_Error} is a renaming of
26208 @code{Constraint_Error}.
26209 This means that it is illegal to have separate exception handlers for
26210 the two exceptions. The fix is simply to remove the handler for the
26211 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
26212 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
26214 @item Indefinite subtypes in generics
26215 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
26216 as the actual for a generic formal private type, but then the instantiation
26217 would be illegal if there were any instances of declarations of variables
26218 of this type in the generic body. In Ada 95, to avoid this clear violation
26219 of the methodological principle known as the ``contract model'',
26220 the generic declaration explicitly indicates whether
26221 or not such instantiations are permitted. If a generic formal parameter
26222 has explicit unknown discriminants, indicated by using @code{(<>)} after the
26223 type name, then it can be instantiated with indefinite types, but no
26224 stand-alone variables can be declared of this type. Any attempt to declare
26225 such a variable will result in an illegality at the time the generic is
26226 declared. If the @code{(<>)} notation is not used, then it is illegal
26227 to instantiate the generic with an indefinite type.
26228 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
26229 It will show up as a compile time error, and
26230 the fix is usually simply to add the @code{(<>)} to the generic declaration.
26233 @node More deterministic semantics
26234 @subsection More deterministic semantics
26238 Conversions from real types to integer types round away from 0. In Ada 83
26239 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
26240 implementation freedom was intended to support unbiased rounding in
26241 statistical applications, but in practice it interfered with portability.
26242 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
26243 is required. Numeric code may be affected by this change in semantics.
26244 Note, though, that this issue is no worse than already existed in Ada 83
26245 when porting code from one vendor to another.
26248 The Real-Time Annex introduces a set of policies that define the behavior of
26249 features that were implementation dependent in Ada 83, such as the order in
26250 which open select branches are executed.
26253 @node Changed semantics
26254 @subsection Changed semantics
26257 The worst kind of incompatibility is one where a program that is legal in
26258 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
26259 possible in Ada 83. Fortunately this is extremely rare, but the one
26260 situation that you should be alert to is the change in the predefined type
26261 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
26264 @item range of @code{Character}
26265 The range of @code{Standard.Character} is now the full 256 characters
26266 of Latin-1, whereas in most Ada 83 implementations it was restricted
26267 to 128 characters. Although some of the effects of
26268 this change will be manifest in compile-time rejection of legal
26269 Ada 83 programs it is possible for a working Ada 83 program to have
26270 a different effect in Ada 95, one that was not permitted in Ada 83.
26271 As an example, the expression
26272 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
26273 delivers @code{255} as its value.
26274 In general, you should look at the logic of any
26275 character-processing Ada 83 program and see whether it needs to be adapted
26276 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
26277 character handling package that may be relevant if code needs to be adapted
26278 to account for the additional Latin-1 elements.
26279 The desirable fix is to
26280 modify the program to accommodate the full character set, but in some cases
26281 it may be convenient to define a subtype or derived type of Character that
26282 covers only the restricted range.
26286 @node Other language compatibility issues
26287 @subsection Other language compatibility issues
26289 @item @option{-gnat83 switch}
26290 All implementations of GNAT provide a switch that causes GNAT to operate
26291 in Ada 83 mode. In this mode, some but not all compatibility problems
26292 of the type described above are handled automatically. For example, the
26293 new Ada 95 reserved words are treated simply as identifiers as in Ada 83.
26295 in practice, it is usually advisable to make the necessary modifications
26296 to the program to remove the need for using this switch.
26297 See @ref{Compiling Different Versions of Ada}.
26299 @item Support for removed Ada 83 pragmas and attributes
26300 A number of pragmas and attributes from Ada 83 have been removed from Ada 95,
26301 generally because they have been replaced by other mechanisms. Ada 95
26302 compilers are allowed, but not required, to implement these missing
26303 elements. In contrast with some other Ada 95 compilers, GNAT implements all
26304 such pragmas and attributes, eliminating this compatibility concern. These
26305 include @code{pragma Interface} and the floating point type attributes
26306 (@code{Emax}, @code{Mantissa}, etc.), among other items.
26309 @node Implementation-dependent characteristics
26310 @section Implementation-dependent characteristics
26312 Although the Ada language defines the semantics of each construct as
26313 precisely as practical, in some situations (for example for reasons of
26314 efficiency, or where the effect is heavily dependent on the host or target
26315 platform) the implementation is allowed some freedom. In porting Ada 83
26316 code to GNAT, you need to be aware of whether / how the existing code
26317 exercised such implementation dependencies. Such characteristics fall into
26318 several categories, and GNAT offers specific support in assisting the
26319 transition from certain Ada 83 compilers.
26322 * Implementation-defined pragmas::
26323 * Implementation-defined attributes::
26325 * Elaboration order::
26326 * Target-specific aspects::
26329 @node Implementation-defined pragmas
26330 @subsection Implementation-defined pragmas
26333 Ada compilers are allowed to supplement the language-defined pragmas, and
26334 these are a potential source of non-portability. All GNAT-defined pragmas
26335 are described in the GNAT Reference Manual, and these include several that
26336 are specifically intended to correspond to other vendors' Ada 83 pragmas.
26337 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
26339 compatibility with HP Ada 83, GNAT supplies the pragmas
26340 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
26341 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
26342 and @code{Volatile}.
26343 Other relevant pragmas include @code{External} and @code{Link_With}.
26344 Some vendor-specific
26345 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
26347 avoiding compiler rejection of units that contain such pragmas; they are not
26348 relevant in a GNAT context and hence are not otherwise implemented.
26350 @node Implementation-defined attributes
26351 @subsection Implementation-defined attributes
26353 Analogous to pragmas, the set of attributes may be extended by an
26354 implementation. All GNAT-defined attributes are described in the
26355 @cite{GNAT Reference Manual}, and these include several that are specifically
26357 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
26358 the attribute @code{VADS_Size} may be useful. For compatibility with HP
26359 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
26363 @subsection Libraries
26365 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
26366 code uses vendor-specific libraries then there are several ways to manage
26370 If the source code for the libraries (specifications and bodies) are
26371 available, then the libraries can be migrated in the same way as the
26374 If the source code for the specifications but not the bodies are
26375 available, then you can reimplement the bodies.
26377 Some new Ada 95 features obviate the need for library support. For
26378 example most Ada 83 vendors supplied a package for unsigned integers. The
26379 Ada 95 modular type feature is the preferred way to handle this need, so
26380 instead of migrating or reimplementing the unsigned integer package it may
26381 be preferable to retrofit the application using modular types.
26384 @node Elaboration order
26385 @subsection Elaboration order
26387 The implementation can choose any elaboration order consistent with the unit
26388 dependency relationship. This freedom means that some orders can result in
26389 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
26390 to invoke a subprogram its body has been elaborated, or to instantiate a
26391 generic before the generic body has been elaborated. By default GNAT
26392 attempts to choose a safe order (one that will not encounter access before
26393 elaboration problems) by implicitly inserting @code{Elaborate} or
26394 @code{Elaborate_All} pragmas where
26395 needed. However, this can lead to the creation of elaboration circularities
26396 and a resulting rejection of the program by gnatbind. This issue is
26397 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
26398 In brief, there are several
26399 ways to deal with this situation:
26403 Modify the program to eliminate the circularities, e.g. by moving
26404 elaboration-time code into explicitly-invoked procedures
26406 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
26407 @code{Elaborate} pragmas, and then inhibit the generation of implicit
26408 @code{Elaborate_All}
26409 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
26410 (by selectively suppressing elaboration checks via pragma
26411 @code{Suppress(Elaboration_Check)} when it is safe to do so).
26414 @node Target-specific aspects
26415 @subsection Target-specific aspects
26417 Low-level applications need to deal with machine addresses, data
26418 representations, interfacing with assembler code, and similar issues. If
26419 such an Ada 83 application is being ported to different target hardware (for
26420 example where the byte endianness has changed) then you will need to
26421 carefully examine the program logic; the porting effort will heavily depend
26422 on the robustness of the original design. Moreover, Ada 95 is sometimes
26423 incompatible with typical Ada 83 compiler practices regarding implicit
26424 packing, the meaning of the Size attribute, and the size of access values.
26425 GNAT's approach to these issues is described in @ref{Representation Clauses}.
26427 @node Compatibility with Other Ada 95 Systems
26428 @section Compatibility with Other Ada 95 Systems
26431 Providing that programs avoid the use of implementation dependent and
26432 implementation defined features of Ada 95, as documented in the Ada 95
26433 reference manual, there should be a high degree of portability between
26434 GNAT and other Ada 95 systems. The following are specific items which
26435 have proved troublesome in moving GNAT programs to other Ada 95
26436 compilers, but do not affect porting code to GNAT@.
26439 @item Ada 83 Pragmas and Attributes
26440 Ada 95 compilers are allowed, but not required, to implement the missing
26441 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
26442 GNAT implements all such pragmas and attributes, eliminating this as
26443 a compatibility concern, but some other Ada 95 compilers reject these
26444 pragmas and attributes.
26446 @item Special-needs Annexes
26447 GNAT implements the full set of special needs annexes. At the
26448 current time, it is the only Ada 95 compiler to do so. This means that
26449 programs making use of these features may not be portable to other Ada
26450 95 compilation systems.
26452 @item Representation Clauses
26453 Some other Ada 95 compilers implement only the minimal set of
26454 representation clauses required by the Ada 95 reference manual. GNAT goes
26455 far beyond this minimal set, as described in the next section.
26458 @node Representation Clauses
26459 @section Representation Clauses
26462 The Ada 83 reference manual was quite vague in describing both the minimal
26463 required implementation of representation clauses, and also their precise
26464 effects. The Ada 95 reference manual is much more explicit, but the minimal
26465 set of capabilities required in Ada 95 is quite limited.
26467 GNAT implements the full required set of capabilities described in the
26468 Ada 95 reference manual, but also goes much beyond this, and in particular
26469 an effort has been made to be compatible with existing Ada 83 usage to the
26470 greatest extent possible.
26472 A few cases exist in which Ada 83 compiler behavior is incompatible with
26473 requirements in the Ada 95 reference manual. These are instances of
26474 intentional or accidental dependence on specific implementation dependent
26475 characteristics of these Ada 83 compilers. The following is a list of
26476 the cases most likely to arise in existing legacy Ada 83 code.
26479 @item Implicit Packing
26480 Some Ada 83 compilers allowed a Size specification to cause implicit
26481 packing of an array or record. This could cause expensive implicit
26482 conversions for change of representation in the presence of derived
26483 types, and the Ada design intends to avoid this possibility.
26484 Subsequent AI's were issued to make it clear that such implicit
26485 change of representation in response to a Size clause is inadvisable,
26486 and this recommendation is represented explicitly in the Ada 95 RM
26487 as implementation advice that is followed by GNAT@.
26488 The problem will show up as an error
26489 message rejecting the size clause. The fix is simply to provide
26490 the explicit pragma @code{Pack}, or for more fine tuned control, provide
26491 a Component_Size clause.
26493 @item Meaning of Size Attribute
26494 The Size attribute in Ada 95 for discrete types is defined as being the
26495 minimal number of bits required to hold values of the type. For example,
26496 on a 32-bit machine, the size of Natural will typically be 31 and not
26497 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
26498 some 32 in this situation. This problem will usually show up as a compile
26499 time error, but not always. It is a good idea to check all uses of the
26500 'Size attribute when porting Ada 83 code. The GNAT specific attribute
26501 Object_Size can provide a useful way of duplicating the behavior of
26502 some Ada 83 compiler systems.
26504 @item Size of Access Types
26505 A common assumption in Ada 83 code is that an access type is in fact a pointer,
26506 and that therefore it will be the same size as a System.Address value. This
26507 assumption is true for GNAT in most cases with one exception. For the case of
26508 a pointer to an unconstrained array type (where the bounds may vary from one
26509 value of the access type to another), the default is to use a ``fat pointer'',
26510 which is represented as two separate pointers, one to the bounds, and one to
26511 the array. This representation has a number of advantages, including improved
26512 efficiency. However, it may cause some difficulties in porting existing Ada 83
26513 code which makes the assumption that, for example, pointers fit in 32 bits on
26514 a machine with 32-bit addressing.
26516 To get around this problem, GNAT also permits the use of ``thin pointers'' for
26517 access types in this case (where the designated type is an unconstrained array
26518 type). These thin pointers are indeed the same size as a System.Address value.
26519 To specify a thin pointer, use a size clause for the type, for example:
26521 @smallexample @c ada
26522 type X is access all String;
26523 for X'Size use Standard'Address_Size;
26527 which will cause the type X to be represented using a single pointer.
26528 When using this representation, the bounds are right behind the array.
26529 This representation is slightly less efficient, and does not allow quite
26530 such flexibility in the use of foreign pointers or in using the
26531 Unrestricted_Access attribute to create pointers to non-aliased objects.
26532 But for any standard portable use of the access type it will work in
26533 a functionally correct manner and allow porting of existing code.
26534 Note that another way of forcing a thin pointer representation
26535 is to use a component size clause for the element size in an array,
26536 or a record representation clause for an access field in a record.
26540 @c This brief section is only in the non-VMS version
26541 @c The complete chapter on HP Ada is in the VMS version
26542 @node Compatibility with HP Ada 83
26543 @section Compatibility with HP Ada 83
26546 The VMS version of GNAT fully implements all the pragmas and attributes
26547 provided by HP Ada 83, as well as providing the standard HP Ada 83
26548 libraries, including Starlet. In addition, data layouts and parameter
26549 passing conventions are highly compatible. This means that porting
26550 existing HP Ada 83 code to GNAT in VMS systems should be easier than
26551 most other porting efforts. The following are some of the most
26552 significant differences between GNAT and HP Ada 83.
26555 @item Default floating-point representation
26556 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
26557 it is VMS format. GNAT does implement the necessary pragmas
26558 (Long_Float, Float_Representation) for changing this default.
26561 The package System in GNAT exactly corresponds to the definition in the
26562 Ada 95 reference manual, which means that it excludes many of the
26563 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
26564 that contains the additional definitions, and a special pragma,
26565 Extend_System allows this package to be treated transparently as an
26566 extension of package System.
26569 The definitions provided by Aux_DEC are exactly compatible with those
26570 in the HP Ada 83 version of System, with one exception.
26571 HP Ada provides the following declarations:
26573 @smallexample @c ada
26574 TO_ADDRESS (INTEGER)
26575 TO_ADDRESS (UNSIGNED_LONGWORD)
26576 TO_ADDRESS (universal_integer)
26580 The version of TO_ADDRESS taking a universal integer argument is in fact
26581 an extension to Ada 83 not strictly compatible with the reference manual.
26582 In GNAT, we are constrained to be exactly compatible with the standard,
26583 and this means we cannot provide this capability. In HP Ada 83, the
26584 point of this definition is to deal with a call like:
26586 @smallexample @c ada
26587 TO_ADDRESS (16#12777#);
26591 Normally, according to the Ada 83 standard, one would expect this to be
26592 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
26593 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
26594 definition using universal_integer takes precedence.
26596 In GNAT, since the version with universal_integer cannot be supplied, it is
26597 not possible to be 100% compatible. Since there are many programs using
26598 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
26599 to change the name of the function in the UNSIGNED_LONGWORD case, so the
26600 declarations provided in the GNAT version of AUX_Dec are:
26602 @smallexample @c ada
26603 function To_Address (X : Integer) return Address;
26604 pragma Pure_Function (To_Address);
26606 function To_Address_Long (X : Unsigned_Longword)
26608 pragma Pure_Function (To_Address_Long);
26612 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
26613 change the name to TO_ADDRESS_LONG@.
26615 @item Task_Id values
26616 The Task_Id values assigned will be different in the two systems, and GNAT
26617 does not provide a specified value for the Task_Id of the environment task,
26618 which in GNAT is treated like any other declared task.
26621 For full details on these and other less significant compatibility issues,
26622 see appendix E of the HP publication entitled @cite{HP Ada, Technical
26623 Overview and Comparison on HP Platforms}.
26625 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
26626 attributes are recognized, although only a subset of them can sensibly
26627 be implemented. The description of pragmas in the
26628 @cite{GNAT Reference Manual}
26629 indicates whether or not they are applicable to non-VMS systems.
26633 @node Transitioning from Alpha to I64 OpenVMS
26634 @section Transitioning from Alpha to I64 OpenVMS
26637 * Introduction to transitioning::
26638 * Migration of 32 bit code::
26639 * Taking advantage of 64 bit addressing::
26640 * Technical details::
26643 @node Introduction to transitioning
26644 @subsection Introduction to transitioning
26647 This section is meant to assist users of @value{EDITION}
26648 for Alpha OpenVMS who are planning to transition to the I64 architecture.
26649 @value{EDITION} for Open VMS I64 has been designed to meet
26654 Providing a full conforming implementation of the Ada 95 language
26657 Allowing maximum backward compatibility, thus easing migration of existing
26661 Supplying a path for exploiting the full I64 address range
26665 Ada's strong typing semantics has made it
26666 impractical to have different 32-bit and 64-bit modes. As soon as
26667 one object could possibly be outside the 32-bit address space, this
26668 would make it necessary for the @code{System.Address} type to be 64 bits.
26669 In particular, this would cause inconsistencies if 32-bit code is
26670 called from 64-bit code that raises an exception.
26672 This issue has been resolved by always using 64-bit addressing
26673 at the system level, but allowing for automatic conversions between
26674 32-bit and 64-bit addresses where required. Thus users who
26675 do not currently require 64-bit addressing capabilities, can
26676 recompile their code with only minimal changes (and indeed
26677 if the code is written in portable Ada, with no assumptions about
26678 the size of the @code{Address} type, then no changes at all are necessary).
26680 this approach provides a simple, gradual upgrade path to future
26681 use of larger memories than available for 32-bit systems.
26682 Also, newly written applications or libraries will by default
26683 be fully compatible with future systems exploiting 64-bit
26684 addressing capabilities present in I64.
26686 @ref{Migration of 32 bit code}, will focus on porting applications
26687 that do not require more than 2 GB of
26688 addressable memory. This code will be referred to as
26689 @emph{32-bit code}.
26690 For applications intending to exploit the full I64 address space,
26691 @ref{Taking advantage of 64 bit addressing},
26692 will consider further changes that may be required.
26693 Such code is called @emph{64-bit code} in the
26694 remainder of this guide.
26697 @node Migration of 32 bit code
26698 @subsection Migration of 32-bit code
26703 * Unchecked conversions::
26704 * Predefined constants::
26705 * Single source compatibility::
26706 * Experience with source compatibility::
26709 @node Address types
26710 @subsubsection Address types
26713 To solve the problem of mixing 64-bit and 32-bit addressing,
26714 while maintaining maximum backward compatibility, the following
26715 approach has been taken:
26719 @code{System.Address} always has a size of 64 bits
26722 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
26727 Since @code{System.Short_Address} is a subtype of @code{System.Address},
26728 a @code{Short_Address}
26729 may be used where an @code{Address} is required, and vice versa, without
26730 needing explicit type conversions.
26731 By virtue of the Open VMS I64 parameter passing conventions,
26733 and exported subprograms that have 32-bit address parameters are
26734 compatible with those that have 64-bit address parameters.
26735 (See @ref{Making code 64 bit clean} for details.)
26737 The areas that may need attention are those where record types have
26738 been defined that contain components of the type @code{System.Address}, and
26739 where objects of this type are passed to code expecting a record layout with
26742 Different compilers on different platforms cannot be
26743 expected to represent the same type in the same way,
26744 since alignment constraints
26745 and other system-dependent properties affect the compiler's decision.
26746 For that reason, Ada code
26747 generally uses representation clauses to specify the expected
26748 layout where required.
26750 If such a representation clause uses 32 bits for a component having
26751 the type @code{System.Address}, GNAT Pro for OpenVMS I64 will detect
26752 that error and produce a specific diagnostic message.
26753 The developer should then determine whether the representation
26754 should be 64 bits or not and make either of two changes:
26755 change the size to 64 bits and leave the type as @code{System.Address}, or
26756 leave the size as 32 bits and change the type to @code{System.Short_Address}.
26757 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
26758 required in any code setting or accessing the field; the compiler will
26759 automatically perform any needed conversions between address
26763 @subsubsection Access types
26766 By default, objects designated by access values are always
26767 allocated in the 32-bit
26768 address space. Thus legacy code will never contain
26769 any objects that are not addressable with 32-bit addresses, and
26770 the compiler will never raise exceptions as result of mixing
26771 32-bit and 64-bit addresses.
26773 However, the access values themselves are represented in 64 bits, for optimum
26774 performance and future compatibility with 64-bit code. As was
26775 the case with @code{System.Address}, the compiler will give an error message
26776 if an object or record component has a representation clause that
26777 requires the access value to fit in 32 bits. In such a situation,
26778 an explicit size clause for the access type, specifying 32 bits,
26779 will have the desired effect.
26781 General access types (declared with @code{access all}) can never be
26782 32 bits, as values of such types must be able to refer to any object
26783 of the designated type,
26784 including objects residing outside the 32-bit address range.
26785 Existing Ada 83 code will not contain such type definitions,
26786 however, since general access types were introduced in Ada 95.
26788 @node Unchecked conversions
26789 @subsubsection Unchecked conversions
26792 In the case of an @code{Unchecked_Conversion} where the source type is a
26793 64-bit access type or the type @code{System.Address}, and the target
26794 type is a 32-bit type, the compiler will generate a warning.
26795 Even though the generated code will still perform the required
26796 conversions, it is highly recommended in these cases to use
26797 respectively a 32-bit access type or @code{System.Short_Address}
26798 as the source type.
26800 @node Predefined constants
26801 @subsubsection Predefined constants
26804 The following predefined constants have changed:
26806 @multitable {@code{System.Address_Size}} {2**32} {2**64}
26807 @item @b{Constant} @tab @b{Old} @tab @b{New}
26808 @item @code{System.Word_Size} @tab 32 @tab 64
26809 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
26810 @item @code{System.Address_Size} @tab 32 @tab 64
26814 If you need to refer to the specific
26815 memory size of a 32-bit implementation, instead of the
26816 actual memory size, use @code{System.Short_Memory_Size}
26817 rather than @code{System.Memory_Size}.
26818 Similarly, references to @code{System.Address_Size} may need
26819 to be replaced by @code{System.Short_Address'Size}.
26820 The program @command{gnatfind} may be useful for locating
26821 references to the above constants, so that you can verify that they
26824 @node Single source compatibility
26825 @subsubsection Single source compatibility
26828 In order to allow the same source code to be compiled on
26829 both Alpha and I64 platforms, GNAT Pro for Alpha OpenVMS
26830 defines @code{System.Short_Address} and System.Short_Memory_Size
26831 as aliases of respectively @code{System.Address} and
26832 @code{System.Memory_Size}.
26833 (These aliases also leave the door open for a possible
26834 future ``upgrade'' of OpenVMS Alpha to a 64-bit address space.)
26836 @node Experience with source compatibility
26837 @subsubsection Experience with source compatibility
26840 The Security Server and STARLET provide an interesting ``test case''
26841 for source compatibility issues, since it is in such system code
26842 where assumptions about @code{Address} size might be expected to occur.
26843 Indeed, there were a small number of occasions in the Security Server
26844 file @file{jibdef.ads}
26845 where a representation clause for a record type specified
26846 32 bits for a component of type @code{Address}.
26847 All of these errors were detected by the compiler.
26848 The repair was obvious and immediate; to simply replace @code{Address} by
26849 @code{Short_Address}.
26851 In the case of STARLET, there were several record types that should
26852 have had representation clauses but did not. In these record types
26853 there was an implicit assumption that an @code{Address} value occupied
26855 These compiled without error, but their usage resulted in run-time error
26856 returns from STARLET system calls.
26857 To assist in the compile-time detection of such situations, we
26858 plan to include a switch to generate a warning message when a
26859 record component is of type @code{Address}.
26862 @c ****************************************
26863 @node Taking advantage of 64 bit addressing
26864 @subsection Taking advantage of 64-bit addressing
26867 * Making code 64 bit clean::
26868 * Allocating memory from the 64 bit storage pool::
26869 * Restrictions on use of 64 bit objects::
26870 * Using 64 bit storage pools by default::
26871 * General access types::
26872 * STARLET and other predefined libraries::
26875 @node Making code 64 bit clean
26876 @subsubsection Making code 64-bit clean
26879 In order to prevent problems that may occur when (parts of) a
26880 system start using memory outside the 32-bit address range,
26881 we recommend some additional guidelines:
26885 For imported subprograms that take parameters of the
26886 type @code{System.Address}, ensure that these subprograms can
26887 indeed handle 64-bit addresses. If not, or when in doubt,
26888 change the subprogram declaration to specify
26889 @code{System.Short_Address} instead.
26892 Resolve all warnings related to size mismatches in
26893 unchecked conversions. Failing to do so causes
26894 erroneous execution if the source object is outside
26895 the 32-bit address space.
26898 (optional) Explicitly use the 32-bit storage pool
26899 for access types used in a 32-bit context, or use
26900 generic access types where possible
26901 (@pxref{Restrictions on use of 64 bit objects}).
26905 If these rules are followed, the compiler will automatically insert
26906 any necessary checks to ensure that no addresses or access values
26907 passed to 32-bit code ever refer to objects outside the 32-bit
26909 Any attempt to do this will raise @code{Constraint_Error}.
26911 @node Allocating memory from the 64 bit storage pool
26912 @subsubsection Allocating memory from the 64-bit storage pool
26915 For any access type @code{T} that potentially requires memory allocations
26916 beyond the 32-bit address space,
26917 use the following representation clause:
26919 @smallexample @c ada
26920 for T'Storage_Pool use System.Pool_64;
26924 @node Restrictions on use of 64 bit objects
26925 @subsubsection Restrictions on use of 64-bit objects
26928 Taking the address of an object allocated from a 64-bit storage pool,
26929 and then passing this address to a subprogram expecting
26930 @code{System.Short_Address},
26931 or assigning it to a variable of type @code{Short_Address}, will cause
26932 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
26933 (@pxref{Making code 64 bit clean}), or checks are suppressed,
26934 no exception is raised and execution
26935 will become erroneous.
26937 @node Using 64 bit storage pools by default
26938 @subsubsection Using 64-bit storage pools by default
26941 In some cases it may be desirable to have the compiler allocate
26942 from 64-bit storage pools by default. This may be the case for
26943 libraries that are 64-bit clean, but may be used in both 32-bit
26944 and 64-bit contexts. For these cases the following configuration
26945 pragma may be specified:
26947 @smallexample @c ada
26948 pragma Pool_64_Default;
26952 Any code compiled in the context of this pragma will by default
26953 use the @code{System.Pool_64} storage pool. This default may be overridden
26954 for a specific access type @code{T} by the representation clause:
26956 @smallexample @c ada
26957 for T'Storage_Pool use System.Pool_32;
26961 Any object whose address may be passed to a subprogram with a
26962 @code{Short_Address} argument, or assigned to a variable of type
26963 @code{Short_Address}, needs to be allocated from this pool.
26965 @node General access types
26966 @subsubsection General access types
26969 Objects designated by access values from a
26970 general access type (declared with @code{access all}) are never allocated
26971 from a 64-bit storage pool. Code that uses general access types will
26972 accept objects allocated in either 32-bit or 64-bit address spaces,
26973 but never allocate objects outside the 32-bit address space.
26974 Using general access types ensures maximum compatibility with both
26975 32-bit and 64-bit code.
26978 @node STARLET and other predefined libraries
26979 @subsubsection STARLET and other predefined libraries
26982 All code that comes as part of GNAT is 64-bit clean, but the
26983 restrictions given in @ref{Restrictions on use of 64 bit objects},
26984 still apply. Look at the package
26985 specifications to see in which contexts objects allocated
26986 in 64-bit address space are acceptable.
26988 @node Technical details
26989 @subsection Technical details
26992 GNAT Pro for Open VMS I64 takes advantage of the freedom given in the Ada
26993 standard with respect to the type of @code{System.Address}. Previous versions
26994 of GNAT Pro have defined this type as private and implemented it as
26997 In order to allow defining @code{System.Short_Address} as a proper subtype,
26998 and to match the implicit sign extension in parameter passing,
26999 in GNAT Pro for Open VMS I64, @code{System.Address} is defined as a
27000 visible (i.e., non-private) integer type.
27001 Standard operations on the type, such as the binary operators ``+'', ``-'',
27002 etc., that take @code{Address} operands and return an @code{Address} result,
27003 have been hidden by declaring these
27004 @code{abstract}, an Ada 95 feature that helps avoid the potential ambiguities
27005 that would otherwise result from overloading.
27006 (Note that, although @code{Address} is a visible integer type,
27007 good programming practice dictates against exploiting the type's
27008 integer properties such as literals, since this will compromise
27011 Defining @code{Address} as a visible integer type helps achieve
27012 maximum compatibility for existing Ada code,
27013 without sacrificing the capabilities of the I64 architecture.
27017 @c ************************************************
27019 @node Microsoft Windows Topics
27020 @appendix Microsoft Windows Topics
27026 This chapter describes topics that are specific to the Microsoft Windows
27027 platforms (NT, 2000, and XP Professional).
27030 * Using GNAT on Windows::
27031 * Using a network installation of GNAT::
27032 * CONSOLE and WINDOWS subsystems::
27033 * Temporary Files::
27034 * Mixed-Language Programming on Windows::
27035 * Windows Calling Conventions::
27036 * Introduction to Dynamic Link Libraries (DLLs)::
27037 * Using DLLs with GNAT::
27038 * Building DLLs with GNAT::
27039 * Building DLLs with GNAT Project files::
27040 * Building DLLs with gnatdll::
27041 * GNAT and Windows Resources::
27042 * Debugging a DLL::
27043 * Setting Stack Size from gnatlink::
27044 * Setting Heap Size from gnatlink::
27047 @node Using GNAT on Windows
27048 @section Using GNAT on Windows
27051 One of the strengths of the GNAT technology is that its tool set
27052 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
27053 @code{gdb} debugger, etc.) is used in the same way regardless of the
27056 On Windows this tool set is complemented by a number of Microsoft-specific
27057 tools that have been provided to facilitate interoperability with Windows
27058 when this is required. With these tools:
27063 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
27067 You can use any Dynamically Linked Library (DLL) in your Ada code (both
27068 relocatable and non-relocatable DLLs are supported).
27071 You can build Ada DLLs for use in other applications. These applications
27072 can be written in a language other than Ada (e.g., C, C++, etc). Again both
27073 relocatable and non-relocatable Ada DLLs are supported.
27076 You can include Windows resources in your Ada application.
27079 You can use or create COM/DCOM objects.
27083 Immediately below are listed all known general GNAT-for-Windows restrictions.
27084 Other restrictions about specific features like Windows Resources and DLLs
27085 are listed in separate sections below.
27090 It is not possible to use @code{GetLastError} and @code{SetLastError}
27091 when tasking, protected records, or exceptions are used. In these
27092 cases, in order to implement Ada semantics, the GNAT run-time system
27093 calls certain Win32 routines that set the last error variable to 0 upon
27094 success. It should be possible to use @code{GetLastError} and
27095 @code{SetLastError} when tasking, protected record, and exception
27096 features are not used, but it is not guaranteed to work.
27099 It is not possible to link against Microsoft libraries except for
27100 import libraries. The library must be built to be compatible with
27101 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
27102 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
27103 not be compatible with the GNAT runtime. Even if the library is
27104 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
27107 When the compilation environment is located on FAT32 drives, users may
27108 experience recompilations of the source files that have not changed if
27109 Daylight Saving Time (DST) state has changed since the last time files
27110 were compiled. NTFS drives do not have this problem.
27113 No components of the GNAT toolset use any entries in the Windows
27114 registry. The only entries that can be created are file associations and
27115 PATH settings, provided the user has chosen to create them at installation
27116 time, as well as some minimal book-keeping information needed to correctly
27117 uninstall or integrate different GNAT products.
27120 @node Using a network installation of GNAT
27121 @section Using a network installation of GNAT
27124 Make sure the system on which GNAT is installed is accessible from the
27125 current machine, i.e. the install location is shared over the network.
27126 Shared resources are accessed on Windows by means of UNC paths, which
27127 have the format @code{\\server\sharename\path}
27129 In order to use such a network installation, simply add the UNC path of the
27130 @file{bin} directory of your GNAT installation in front of your PATH. For
27131 example, if GNAT is installed in @file{\GNAT} directory of a share location
27132 called @file{c-drive} on a machine @file{LOKI}, the following command will
27135 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
27137 Be aware that every compilation using the network installation results in the
27138 transfer of large amounts of data across the network and will likely cause
27139 serious performance penalty.
27141 @node CONSOLE and WINDOWS subsystems
27142 @section CONSOLE and WINDOWS subsystems
27143 @cindex CONSOLE Subsystem
27144 @cindex WINDOWS Subsystem
27148 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
27149 (which is the default subsystem) will always create a console when
27150 launching the application. This is not something desirable when the
27151 application has a Windows GUI. To get rid of this console the
27152 application must be using the @code{WINDOWS} subsystem. To do so
27153 the @option{-mwindows} linker option must be specified.
27156 $ gnatmake winprog -largs -mwindows
27159 @node Temporary Files
27160 @section Temporary Files
27161 @cindex Temporary files
27164 It is possible to control where temporary files gets created by setting
27165 the TMP environment variable. The file will be created:
27168 @item Under the directory pointed to by the TMP environment variable if
27169 this directory exists.
27171 @item Under c:\temp, if the TMP environment variable is not set (or not
27172 pointing to a directory) and if this directory exists.
27174 @item Under the current working directory otherwise.
27178 This allows you to determine exactly where the temporary
27179 file will be created. This is particularly useful in networked
27180 environments where you may not have write access to some
27183 @node Mixed-Language Programming on Windows
27184 @section Mixed-Language Programming on Windows
27187 Developing pure Ada applications on Windows is no different than on
27188 other GNAT-supported platforms. However, when developing or porting an
27189 application that contains a mix of Ada and C/C++, the choice of your
27190 Windows C/C++ development environment conditions your overall
27191 interoperability strategy.
27193 If you use @command{gcc} to compile the non-Ada part of your application,
27194 there are no Windows-specific restrictions that affect the overall
27195 interoperability with your Ada code. If you plan to use
27196 Microsoft tools (e.g. Microsoft Visual C/C++), you should be aware of
27197 the following limitations:
27201 You cannot link your Ada code with an object or library generated with
27202 Microsoft tools if these use the @code{.tls} section (Thread Local
27203 Storage section) since the GNAT linker does not yet support this section.
27206 You cannot link your Ada code with an object or library generated with
27207 Microsoft tools if these use I/O routines other than those provided in
27208 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
27209 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
27210 libraries can cause a conflict with @code{msvcrt.dll} services. For
27211 instance Visual C++ I/O stream routines conflict with those in
27216 If you do want to use the Microsoft tools for your non-Ada code and hit one
27217 of the above limitations, you have two choices:
27221 Encapsulate your non Ada code in a DLL to be linked with your Ada
27222 application. In this case, use the Microsoft or whatever environment to
27223 build the DLL and use GNAT to build your executable
27224 (@pxref{Using DLLs with GNAT}).
27227 Or you can encapsulate your Ada code in a DLL to be linked with the
27228 other part of your application. In this case, use GNAT to build the DLL
27229 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
27230 environment to build your executable.
27233 @node Windows Calling Conventions
27234 @section Windows Calling Conventions
27239 * C Calling Convention::
27240 * Stdcall Calling Convention::
27241 * Win32 Calling Convention::
27242 * DLL Calling Convention::
27246 When a subprogram @code{F} (caller) calls a subprogram @code{G}
27247 (callee), there are several ways to push @code{G}'s parameters on the
27248 stack and there are several possible scenarios to clean up the stack
27249 upon @code{G}'s return. A calling convention is an agreed upon software
27250 protocol whereby the responsibilities between the caller (@code{F}) and
27251 the callee (@code{G}) are clearly defined. Several calling conventions
27252 are available for Windows:
27256 @code{C} (Microsoft defined)
27259 @code{Stdcall} (Microsoft defined)
27262 @code{Win32} (GNAT specific)
27265 @code{DLL} (GNAT specific)
27268 @node C Calling Convention
27269 @subsection @code{C} Calling Convention
27272 This is the default calling convention used when interfacing to C/C++
27273 routines compiled with either @command{gcc} or Microsoft Visual C++.
27275 In the @code{C} calling convention subprogram parameters are pushed on the
27276 stack by the caller from right to left. The caller itself is in charge of
27277 cleaning up the stack after the call. In addition, the name of a routine
27278 with @code{C} calling convention is mangled by adding a leading underscore.
27280 The name to use on the Ada side when importing (or exporting) a routine
27281 with @code{C} calling convention is the name of the routine. For
27282 instance the C function:
27285 int get_val (long);
27289 should be imported from Ada as follows:
27291 @smallexample @c ada
27293 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
27294 pragma Import (C, Get_Val, External_Name => "get_val");
27299 Note that in this particular case the @code{External_Name} parameter could
27300 have been omitted since, when missing, this parameter is taken to be the
27301 name of the Ada entity in lower case. When the @code{Link_Name} parameter
27302 is missing, as in the above example, this parameter is set to be the
27303 @code{External_Name} with a leading underscore.
27305 When importing a variable defined in C, you should always use the @code{C}
27306 calling convention unless the object containing the variable is part of a
27307 DLL (in which case you should use the @code{Stdcall} calling
27308 convention, @pxref{Stdcall Calling Convention}).
27310 @node Stdcall Calling Convention
27311 @subsection @code{Stdcall} Calling Convention
27314 This convention, which was the calling convention used for Pascal
27315 programs, is used by Microsoft for all the routines in the Win32 API for
27316 efficiency reasons. It must be used to import any routine for which this
27317 convention was specified.
27319 In the @code{Stdcall} calling convention subprogram parameters are pushed
27320 on the stack by the caller from right to left. The callee (and not the
27321 caller) is in charge of cleaning the stack on routine exit. In addition,
27322 the name of a routine with @code{Stdcall} calling convention is mangled by
27323 adding a leading underscore (as for the @code{C} calling convention) and a
27324 trailing @code{@@}@code{@i{nn}}, where @i{nn} is the overall size (in
27325 bytes) of the parameters passed to the routine.
27327 The name to use on the Ada side when importing a C routine with a
27328 @code{Stdcall} calling convention is the name of the C routine. The leading
27329 underscore and trailing @code{@@}@code{@i{nn}} are added automatically by
27330 the compiler. For instance the Win32 function:
27333 @b{APIENTRY} int get_val (long);
27337 should be imported from Ada as follows:
27339 @smallexample @c ada
27341 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
27342 pragma Import (Stdcall, Get_Val);
27343 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
27348 As for the @code{C} calling convention, when the @code{External_Name}
27349 parameter is missing, it is taken to be the name of the Ada entity in lower
27350 case. If instead of writing the above import pragma you write:
27352 @smallexample @c ada
27354 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
27355 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
27360 then the imported routine is @code{_retrieve_val@@4}. However, if instead
27361 of specifying the @code{External_Name} parameter you specify the
27362 @code{Link_Name} as in the following example:
27364 @smallexample @c ada
27366 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
27367 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
27372 then the imported routine is @code{retrieve_val@@4}, that is, there is no
27373 trailing underscore but the appropriate @code{@@}@code{@i{nn}} is always
27374 added at the end of the @code{Link_Name} by the compiler.
27377 Note, that in some special cases a DLL's entry point name lacks a trailing
27378 @code{@@}@code{@i{nn}} while the exported name generated for a call has it.
27379 The @code{gnatdll} tool, which creates the import library for the DLL, is able
27380 to handle those cases (@pxref{Using gnatdll} for the description of
27384 It is also possible to import variables defined in a DLL by using an
27385 import pragma for a variable. As an example, if a DLL contains a
27386 variable defined as:
27393 then, to access this variable from Ada you should write:
27395 @smallexample @c ada
27397 My_Var : Interfaces.C.int;
27398 pragma Import (Stdcall, My_Var);
27403 Note that to ease building cross-platform bindings this convention
27404 will be handled as a @code{C} calling convention on non Windows platforms.
27406 @node Win32 Calling Convention
27407 @subsection @code{Win32} Calling Convention
27410 This convention, which is GNAT-specific is fully equivalent to the
27411 @code{Stdcall} calling convention described above.
27413 @node DLL Calling Convention
27414 @subsection @code{DLL} Calling Convention
27417 This convention, which is GNAT-specific is fully equivalent to the
27418 @code{Stdcall} calling convention described above.
27420 @node Introduction to Dynamic Link Libraries (DLLs)
27421 @section Introduction to Dynamic Link Libraries (DLLs)
27425 A Dynamically Linked Library (DLL) is a library that can be shared by
27426 several applications running under Windows. A DLL can contain any number of
27427 routines and variables.
27429 One advantage of DLLs is that you can change and enhance them without
27430 forcing all the applications that depend on them to be relinked or
27431 recompiled. However, you should be aware than all calls to DLL routines are
27432 slower since, as you will understand below, such calls are indirect.
27434 To illustrate the remainder of this section, suppose that an application
27435 wants to use the services of a DLL @file{API.dll}. To use the services
27436 provided by @file{API.dll} you must statically link against the DLL or
27437 an import library which contains a jump table with an entry for each
27438 routine and variable exported by the DLL. In the Microsoft world this
27439 import library is called @file{API.lib}. When using GNAT this import
27440 library is called either @file{libAPI.a} or @file{libapi.a} (names are
27443 After you have linked your application with the DLL or the import library
27444 and you run your application, here is what happens:
27448 Your application is loaded into memory.
27451 The DLL @file{API.dll} is mapped into the address space of your
27452 application. This means that:
27456 The DLL will use the stack of the calling thread.
27459 The DLL will use the virtual address space of the calling process.
27462 The DLL will allocate memory from the virtual address space of the calling
27466 Handles (pointers) can be safely exchanged between routines in the DLL
27467 routines and routines in the application using the DLL.
27471 The entries in the jump table (from the import library @file{libAPI.a}
27472 or @file{API.lib} or automatically created when linking against a DLL)
27473 which is part of your application are initialized with the addresses
27474 of the routines and variables in @file{API.dll}.
27477 If present in @file{API.dll}, routines @code{DllMain} or
27478 @code{DllMainCRTStartup} are invoked. These routines typically contain
27479 the initialization code needed for the well-being of the routines and
27480 variables exported by the DLL.
27484 There is an additional point which is worth mentioning. In the Windows
27485 world there are two kind of DLLs: relocatable and non-relocatable
27486 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
27487 in the target application address space. If the addresses of two
27488 non-relocatable DLLs overlap and these happen to be used by the same
27489 application, a conflict will occur and the application will run
27490 incorrectly. Hence, when possible, it is always preferable to use and
27491 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
27492 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
27493 User's Guide) removes the debugging symbols from the DLL but the DLL can
27494 still be relocated.
27496 As a side note, an interesting difference between Microsoft DLLs and
27497 Unix shared libraries, is the fact that on most Unix systems all public
27498 routines are exported by default in a Unix shared library, while under
27499 Windows it is possible (but not required) to list exported routines in
27500 a definition file (@pxref{The Definition File}).
27502 @node Using DLLs with GNAT
27503 @section Using DLLs with GNAT
27506 * Creating an Ada Spec for the DLL Services::
27507 * Creating an Import Library::
27511 To use the services of a DLL, say @file{API.dll}, in your Ada application
27516 The Ada spec for the routines and/or variables you want to access in
27517 @file{API.dll}. If not available this Ada spec must be built from the C/C++
27518 header files provided with the DLL.
27521 The import library (@file{libAPI.a} or @file{API.lib}). As previously
27522 mentioned an import library is a statically linked library containing the
27523 import table which will be filled at load time to point to the actual
27524 @file{API.dll} routines. Sometimes you don't have an import library for the
27525 DLL you want to use. The following sections will explain how to build
27526 one. Note that this is optional.
27529 The actual DLL, @file{API.dll}.
27533 Once you have all the above, to compile an Ada application that uses the
27534 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
27535 you simply issue the command
27538 $ gnatmake my_ada_app -largs -lAPI
27542 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
27543 tells the GNAT linker to look first for a library named @file{API.lib}
27544 (Microsoft-style name) and if not found for a library named @file{libAPI.a}
27545 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
27546 contains the following pragma
27548 @smallexample @c ada
27549 pragma Linker_Options ("-lAPI");
27553 you do not have to add @option{-largs -lAPI} at the end of the
27554 @command{gnatmake} command.
27556 If any one of the items above is missing you will have to create it
27557 yourself. The following sections explain how to do so using as an
27558 example a fictitious DLL called @file{API.dll}.
27560 @node Creating an Ada Spec for the DLL Services
27561 @subsection Creating an Ada Spec for the DLL Services
27564 A DLL typically comes with a C/C++ header file which provides the
27565 definitions of the routines and variables exported by the DLL. The Ada
27566 equivalent of this header file is a package spec that contains definitions
27567 for the imported entities. If the DLL you intend to use does not come with
27568 an Ada spec you have to generate one such spec yourself. For example if
27569 the header file of @file{API.dll} is a file @file{api.h} containing the
27570 following two definitions:
27582 then the equivalent Ada spec could be:
27584 @smallexample @c ada
27587 with Interfaces.C.Strings;
27592 function Get (Str : C.Strings.Chars_Ptr) return C.int;
27595 pragma Import (C, Get);
27596 pragma Import (DLL, Some_Var);
27603 Note that a variable is
27604 @strong{always imported with a Stdcall convention}. A function
27605 can have @code{C} or @code{Stdcall} convention.
27606 (@pxref{Windows Calling Conventions}).
27608 @node Creating an Import Library
27609 @subsection Creating an Import Library
27610 @cindex Import library
27613 * The Definition File::
27614 * GNAT-Style Import Library::
27615 * Microsoft-Style Import Library::
27619 If a Microsoft-style import library @file{API.lib} or a GNAT-style
27620 import library @file{libAPI.a} is available with @file{API.dll} you
27621 can skip this section. You can also skip this section if
27622 @file{API.dll} is built with GNU tools as in this case it is possible
27623 to link directly against the DLL. Otherwise read on.
27625 @node The Definition File
27626 @subsubsection The Definition File
27627 @cindex Definition file
27631 As previously mentioned, and unlike Unix systems, the list of symbols
27632 that are exported from a DLL must be provided explicitly in Windows.
27633 The main goal of a definition file is precisely that: list the symbols
27634 exported by a DLL. A definition file (usually a file with a @code{.def}
27635 suffix) has the following structure:
27641 [DESCRIPTION @i{string}]
27651 @item LIBRARY @i{name}
27652 This section, which is optional, gives the name of the DLL.
27654 @item DESCRIPTION @i{string}
27655 This section, which is optional, gives a description string that will be
27656 embedded in the import library.
27659 This section gives the list of exported symbols (procedures, functions or
27660 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
27661 section of @file{API.def} looks like:
27675 Note that you must specify the correct suffix (@code{@@}@code{@i{nn}})
27676 (@pxref{Windows Calling Conventions}) for a Stdcall
27677 calling convention function in the exported symbols list.
27680 There can actually be other sections in a definition file, but these
27681 sections are not relevant to the discussion at hand.
27683 @node GNAT-Style Import Library
27684 @subsubsection GNAT-Style Import Library
27687 To create a static import library from @file{API.dll} with the GNAT tools
27688 you should proceed as follows:
27692 Create the definition file @file{API.def} (@pxref{The Definition File}).
27693 For that use the @code{dll2def} tool as follows:
27696 $ dll2def API.dll > API.def
27700 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
27701 to standard output the list of entry points in the DLL. Note that if
27702 some routines in the DLL have the @code{Stdcall} convention
27703 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@i{nn}
27704 suffix then you'll have to edit @file{api.def} to add it, and specify
27705 @code{-k} to @code{gnatdll} when creating the import library.
27708 Here are some hints to find the right @code{@@}@i{nn} suffix.
27712 If you have the Microsoft import library (.lib), it is possible to get
27713 the right symbols by using Microsoft @code{dumpbin} tool (see the
27714 corresponding Microsoft documentation for further details).
27717 $ dumpbin /exports api.lib
27721 If you have a message about a missing symbol at link time the compiler
27722 tells you what symbol is expected. You just have to go back to the
27723 definition file and add the right suffix.
27727 Build the import library @code{libAPI.a}, using @code{gnatdll}
27728 (@pxref{Using gnatdll}) as follows:
27731 $ gnatdll -e API.def -d API.dll
27735 @code{gnatdll} takes as input a definition file @file{API.def} and the
27736 name of the DLL containing the services listed in the definition file
27737 @file{API.dll}. The name of the static import library generated is
27738 computed from the name of the definition file as follows: if the
27739 definition file name is @i{xyz}@code{.def}, the import library name will
27740 be @code{lib}@i{xyz}@code{.a}. Note that in the previous example option
27741 @option{-e} could have been removed because the name of the definition
27742 file (before the ``@code{.def}'' suffix) is the same as the name of the
27743 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
27746 @node Microsoft-Style Import Library
27747 @subsubsection Microsoft-Style Import Library
27750 With GNAT you can either use a GNAT-style or Microsoft-style import
27751 library. A Microsoft import library is needed only if you plan to make an
27752 Ada DLL available to applications developed with Microsoft
27753 tools (@pxref{Mixed-Language Programming on Windows}).
27755 To create a Microsoft-style import library for @file{API.dll} you
27756 should proceed as follows:
27760 Create the definition file @file{API.def} from the DLL. For this use either
27761 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
27762 tool (see the corresponding Microsoft documentation for further details).
27765 Build the actual import library using Microsoft's @code{lib} utility:
27768 $ lib -machine:IX86 -def:API.def -out:API.lib
27772 If you use the above command the definition file @file{API.def} must
27773 contain a line giving the name of the DLL:
27780 See the Microsoft documentation for further details about the usage of
27784 @node Building DLLs with GNAT
27785 @section Building DLLs with GNAT
27786 @cindex DLLs, building
27789 This section explain how to build DLLs using the GNAT built-in DLL
27790 support. With the following procedure it is straight forward to build
27791 and use DLLs with GNAT.
27795 @item building object files
27797 The first step is to build all objects files that are to be included
27798 into the DLL. This is done by using the standard @command{gnatmake} tool.
27800 @item building the DLL
27802 To build the DLL you must use @command{gcc}'s @code{-shared}
27803 option. It is quite simple to use this method:
27806 $ gcc -shared -o api.dll obj1.o obj2.o ...
27809 It is important to note that in this case all symbols found in the
27810 object files are automatically exported. It is possible to restrict
27811 the set of symbols to export by passing to @command{gcc} a definition
27812 file, @pxref{The Definition File}. For example:
27815 $ gcc -shared -o api.dll api.def obj1.o obj2.o ...
27818 If you use a definition file you must export the elaboration procedures
27819 for every package that required one. Elaboration procedures are named
27820 using the package name followed by "_E".
27822 @item preparing DLL to be used
27824 For the DLL to be used by client programs the bodies must be hidden
27825 from it and the .ali set with read-only attribute. This is very important
27826 otherwise GNAT will recompile all packages and will not actually use
27827 the code in the DLL. For example:
27831 $ copy *.ads *.ali api.dll apilib
27832 $ attrib +R apilib\*.ali
27837 At this point it is possible to use the DLL by directly linking
27838 against it. Note that you must use the GNAT shared runtime when using
27839 GNAT shared libraries. This is achieved by using @code{-shared} binder's
27843 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
27846 @node Building DLLs with GNAT Project files
27847 @section Building DLLs with GNAT Project files
27848 @cindex DLLs, building
27851 There is nothing specific to Windows in this area. @pxref{Library Projects}.
27853 @node Building DLLs with gnatdll
27854 @section Building DLLs with gnatdll
27855 @cindex DLLs, building
27858 * Limitations When Using Ada DLLs from Ada::
27859 * Exporting Ada Entities::
27860 * Ada DLLs and Elaboration::
27861 * Ada DLLs and Finalization::
27862 * Creating a Spec for Ada DLLs::
27863 * Creating the Definition File::
27868 Note that it is preferred to use the built-in GNAT DLL support
27869 (@pxref{Building DLLs with GNAT}) or GNAT Project files
27870 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
27872 This section explains how to build DLLs containing Ada code using
27873 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
27874 remainder of this section.
27876 The steps required to build an Ada DLL that is to be used by Ada as well as
27877 non-Ada applications are as follows:
27881 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
27882 @code{Stdcall} calling convention to avoid any Ada name mangling for the
27883 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
27884 skip this step if you plan to use the Ada DLL only from Ada applications.
27887 Your Ada code must export an initialization routine which calls the routine
27888 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
27889 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
27890 routine exported by the Ada DLL must be invoked by the clients of the DLL
27891 to initialize the DLL.
27894 When useful, the DLL should also export a finalization routine which calls
27895 routine @code{adafinal} generated by @command{gnatbind} to perform the
27896 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
27897 The finalization routine exported by the Ada DLL must be invoked by the
27898 clients of the DLL when the DLL services are no further needed.
27901 You must provide a spec for the services exported by the Ada DLL in each
27902 of the programming languages to which you plan to make the DLL available.
27905 You must provide a definition file listing the exported entities
27906 (@pxref{The Definition File}).
27909 Finally you must use @code{gnatdll} to produce the DLL and the import
27910 library (@pxref{Using gnatdll}).
27914 Note that a relocatable DLL stripped using the @code{strip}
27915 binutils tool will not be relocatable anymore. To build a DLL without
27916 debug information pass @code{-largs -s} to @code{gnatdll}. This
27917 restriction does not apply to a DLL built using a Library Project.
27918 @pxref{Library Projects}.
27920 @node Limitations When Using Ada DLLs from Ada
27921 @subsection Limitations When Using Ada DLLs from Ada
27924 When using Ada DLLs from Ada applications there is a limitation users
27925 should be aware of. Because on Windows the GNAT run time is not in a DLL of
27926 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
27927 each Ada DLL includes the services of the GNAT run time that are necessary
27928 to the Ada code inside the DLL. As a result, when an Ada program uses an
27929 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
27930 one in the main program.
27932 It is therefore not possible to exchange GNAT run-time objects between the
27933 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
27934 handles (e.g. @code{Text_IO.File_Type}), tasks types, protected objects
27937 It is completely safe to exchange plain elementary, array or record types,
27938 Windows object handles, etc.
27940 @node Exporting Ada Entities
27941 @subsection Exporting Ada Entities
27942 @cindex Export table
27945 Building a DLL is a way to encapsulate a set of services usable from any
27946 application. As a result, the Ada entities exported by a DLL should be
27947 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
27948 any Ada name mangling. As an example here is an Ada package
27949 @code{API}, spec and body, exporting two procedures, a function, and a
27952 @smallexample @c ada
27955 with Interfaces.C; use Interfaces;
27957 Count : C.int := 0;
27958 function Factorial (Val : C.int) return C.int;
27960 procedure Initialize_API;
27961 procedure Finalize_API;
27962 -- Initialization & Finalization routines. More in the next section.
27964 pragma Export (C, Initialize_API);
27965 pragma Export (C, Finalize_API);
27966 pragma Export (C, Count);
27967 pragma Export (C, Factorial);
27973 @smallexample @c ada
27976 package body API is
27977 function Factorial (Val : C.int) return C.int is
27980 Count := Count + 1;
27981 for K in 1 .. Val loop
27987 procedure Initialize_API is
27989 pragma Import (C, Adainit);
27992 end Initialize_API;
27994 procedure Finalize_API is
27995 procedure Adafinal;
27996 pragma Import (C, Adafinal);
28006 If the Ada DLL you are building will only be used by Ada applications
28007 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
28008 convention. As an example, the previous package could be written as
28011 @smallexample @c ada
28015 Count : Integer := 0;
28016 function Factorial (Val : Integer) return Integer;
28018 procedure Initialize_API;
28019 procedure Finalize_API;
28020 -- Initialization and Finalization routines.
28026 @smallexample @c ada
28029 package body API is
28030 function Factorial (Val : Integer) return Integer is
28031 Fact : Integer := 1;
28033 Count := Count + 1;
28034 for K in 1 .. Val loop
28041 -- The remainder of this package body is unchanged.
28048 Note that if you do not export the Ada entities with a @code{C} or
28049 @code{Stdcall} convention you will have to provide the mangled Ada names
28050 in the definition file of the Ada DLL
28051 (@pxref{Creating the Definition File}).
28053 @node Ada DLLs and Elaboration
28054 @subsection Ada DLLs and Elaboration
28055 @cindex DLLs and elaboration
28058 The DLL that you are building contains your Ada code as well as all the
28059 routines in the Ada library that are needed by it. The first thing a
28060 user of your DLL must do is elaborate the Ada code
28061 (@pxref{Elaboration Order Handling in GNAT}).
28063 To achieve this you must export an initialization routine
28064 (@code{Initialize_API} in the previous example), which must be invoked
28065 before using any of the DLL services. This elaboration routine must call
28066 the Ada elaboration routine @code{adainit} generated by the GNAT binder
28067 (@pxref{Binding with Non-Ada Main Programs}). See the body of
28068 @code{Initialize_Api} for an example. Note that the GNAT binder is
28069 automatically invoked during the DLL build process by the @code{gnatdll}
28070 tool (@pxref{Using gnatdll}).
28072 When a DLL is loaded, Windows systematically invokes a routine called
28073 @code{DllMain}. It would therefore be possible to call @code{adainit}
28074 directly from @code{DllMain} without having to provide an explicit
28075 initialization routine. Unfortunately, it is not possible to call
28076 @code{adainit} from the @code{DllMain} if your program has library level
28077 tasks because access to the @code{DllMain} entry point is serialized by
28078 the system (that is, only a single thread can execute ``through'' it at a
28079 time), which means that the GNAT run time will deadlock waiting for the
28080 newly created task to complete its initialization.
28082 @node Ada DLLs and Finalization
28083 @subsection Ada DLLs and Finalization
28084 @cindex DLLs and finalization
28087 When the services of an Ada DLL are no longer needed, the client code should
28088 invoke the DLL finalization routine, if available. The DLL finalization
28089 routine is in charge of releasing all resources acquired by the DLL. In the
28090 case of the Ada code contained in the DLL, this is achieved by calling
28091 routine @code{adafinal} generated by the GNAT binder
28092 (@pxref{Binding with Non-Ada Main Programs}).
28093 See the body of @code{Finalize_Api} for an
28094 example. As already pointed out the GNAT binder is automatically invoked
28095 during the DLL build process by the @code{gnatdll} tool
28096 (@pxref{Using gnatdll}).
28098 @node Creating a Spec for Ada DLLs
28099 @subsection Creating a Spec for Ada DLLs
28102 To use the services exported by the Ada DLL from another programming
28103 language (e.g. C), you have to translate the specs of the exported Ada
28104 entities in that language. For instance in the case of @code{API.dll},
28105 the corresponding C header file could look like:
28110 extern int *_imp__count;
28111 #define count (*_imp__count)
28112 int factorial (int);
28118 It is important to understand that when building an Ada DLL to be used by
28119 other Ada applications, you need two different specs for the packages
28120 contained in the DLL: one for building the DLL and the other for using
28121 the DLL. This is because the @code{DLL} calling convention is needed to
28122 use a variable defined in a DLL, but when building the DLL, the variable
28123 must have either the @code{Ada} or @code{C} calling convention. As an
28124 example consider a DLL comprising the following package @code{API}:
28126 @smallexample @c ada
28130 Count : Integer := 0;
28132 -- Remainder of the package omitted.
28139 After producing a DLL containing package @code{API}, the spec that
28140 must be used to import @code{API.Count} from Ada code outside of the
28143 @smallexample @c ada
28148 pragma Import (DLL, Count);
28154 @node Creating the Definition File
28155 @subsection Creating the Definition File
28158 The definition file is the last file needed to build the DLL. It lists
28159 the exported symbols. As an example, the definition file for a DLL
28160 containing only package @code{API} (where all the entities are exported
28161 with a @code{C} calling convention) is:
28176 If the @code{C} calling convention is missing from package @code{API},
28177 then the definition file contains the mangled Ada names of the above
28178 entities, which in this case are:
28187 api__initialize_api
28192 @node Using gnatdll
28193 @subsection Using @code{gnatdll}
28197 * gnatdll Example::
28198 * gnatdll behind the Scenes::
28203 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
28204 and non-Ada sources that make up your DLL have been compiled.
28205 @code{gnatdll} is actually in charge of two distinct tasks: build the
28206 static import library for the DLL and the actual DLL. The form of the
28207 @code{gnatdll} command is
28211 $ gnatdll [@var{switches}] @var{list-of-files} [-largs @var{opts}]
28216 where @i{list-of-files} is a list of ALI and object files. The object
28217 file list must be the exact list of objects corresponding to the non-Ada
28218 sources whose services are to be included in the DLL. The ALI file list
28219 must be the exact list of ALI files for the corresponding Ada sources
28220 whose services are to be included in the DLL. If @i{list-of-files} is
28221 missing, only the static import library is generated.
28224 You may specify any of the following switches to @code{gnatdll}:
28227 @item -a[@var{address}]
28228 @cindex @option{-a} (@code{gnatdll})
28229 Build a non-relocatable DLL at @var{address}. If @var{address} is not
28230 specified the default address @var{0x11000000} will be used. By default,
28231 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
28232 advise the reader to build relocatable DLL.
28234 @item -b @var{address}
28235 @cindex @option{-b} (@code{gnatdll})
28236 Set the relocatable DLL base address. By default the address is
28239 @item -bargs @var{opts}
28240 @cindex @option{-bargs} (@code{gnatdll})
28241 Binder options. Pass @var{opts} to the binder.
28243 @item -d @var{dllfile}
28244 @cindex @option{-d} (@code{gnatdll})
28245 @var{dllfile} is the name of the DLL. This switch must be present for
28246 @code{gnatdll} to do anything. The name of the generated import library is
28247 obtained algorithmically from @var{dllfile} as shown in the following
28248 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
28249 @code{libxyz.a}. The name of the definition file to use (if not specified
28250 by option @option{-e}) is obtained algorithmically from @var{dllfile}
28251 as shown in the following example:
28252 if @var{dllfile} is @code{xyz.dll}, the definition
28253 file used is @code{xyz.def}.
28255 @item -e @var{deffile}
28256 @cindex @option{-e} (@code{gnatdll})
28257 @var{deffile} is the name of the definition file.
28260 @cindex @option{-g} (@code{gnatdll})
28261 Generate debugging information. This information is stored in the object
28262 file and copied from there to the final DLL file by the linker,
28263 where it can be read by the debugger. You must use the
28264 @option{-g} switch if you plan on using the debugger or the symbolic
28268 @cindex @option{-h} (@code{gnatdll})
28269 Help mode. Displays @code{gnatdll} switch usage information.
28272 @cindex @option{-I} (@code{gnatdll})
28273 Direct @code{gnatdll} to search the @var{dir} directory for source and
28274 object files needed to build the DLL.
28275 (@pxref{Search Paths and the Run-Time Library (RTL)}).
28278 @cindex @option{-k} (@code{gnatdll})
28279 Removes the @code{@@}@i{nn} suffix from the import library's exported
28280 names, but keeps them for the link names. You must specify this
28281 option if you want to use a @code{Stdcall} function in a DLL for which
28282 the @code{@@}@i{nn} suffix has been removed. This is the case for most
28283 of the Windows NT DLL for example. This option has no effect when
28284 @option{-n} option is specified.
28286 @item -l @var{file}
28287 @cindex @option{-l} (@code{gnatdll})
28288 The list of ALI and object files used to build the DLL are listed in
28289 @var{file}, instead of being given in the command line. Each line in
28290 @var{file} contains the name of an ALI or object file.
28293 @cindex @option{-n} (@code{gnatdll})
28294 No Import. Do not create the import library.
28297 @cindex @option{-q} (@code{gnatdll})
28298 Quiet mode. Do not display unnecessary messages.
28301 @cindex @option{-v} (@code{gnatdll})
28302 Verbose mode. Display extra information.
28304 @item -largs @var{opts}
28305 @cindex @option{-largs} (@code{gnatdll})
28306 Linker options. Pass @var{opts} to the linker.
28309 @node gnatdll Example
28310 @subsubsection @code{gnatdll} Example
28313 As an example the command to build a relocatable DLL from @file{api.adb}
28314 once @file{api.adb} has been compiled and @file{api.def} created is
28317 $ gnatdll -d api.dll api.ali
28321 The above command creates two files: @file{libapi.a} (the import
28322 library) and @file{api.dll} (the actual DLL). If you want to create
28323 only the DLL, just type:
28326 $ gnatdll -d api.dll -n api.ali
28330 Alternatively if you want to create just the import library, type:
28333 $ gnatdll -d api.dll
28336 @node gnatdll behind the Scenes
28337 @subsubsection @code{gnatdll} behind the Scenes
28340 This section details the steps involved in creating a DLL. @code{gnatdll}
28341 does these steps for you. Unless you are interested in understanding what
28342 goes on behind the scenes, you should skip this section.
28344 We use the previous example of a DLL containing the Ada package @code{API},
28345 to illustrate the steps necessary to build a DLL. The starting point is a
28346 set of objects that will make up the DLL and the corresponding ALI
28347 files. In the case of this example this means that @file{api.o} and
28348 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
28353 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
28354 the information necessary to generate relocation information for the
28360 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
28365 In addition to the base file, the @command{gnatlink} command generates an
28366 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
28367 asks @command{gnatlink} to generate the routines @code{DllMain} and
28368 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
28369 is loaded into memory.
28372 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
28373 export table (@file{api.exp}). The export table contains the relocation
28374 information in a form which can be used during the final link to ensure
28375 that the Windows loader is able to place the DLL anywhere in memory.
28379 $ dlltool --dllname api.dll --def api.def --base-file api.base \
28380 --output-exp api.exp
28385 @code{gnatdll} builds the base file using the new export table. Note that
28386 @command{gnatbind} must be called once again since the binder generated file
28387 has been deleted during the previous call to @command{gnatlink}.
28392 $ gnatlink api -o api.jnk api.exp -mdll
28393 -Wl,--base-file,api.base
28398 @code{gnatdll} builds the new export table using the new base file and
28399 generates the DLL import library @file{libAPI.a}.
28403 $ dlltool --dllname api.dll --def api.def --base-file api.base \
28404 --output-exp api.exp --output-lib libAPI.a
28409 Finally @code{gnatdll} builds the relocatable DLL using the final export
28415 $ gnatlink api api.exp -o api.dll -mdll
28420 @node Using dlltool
28421 @subsubsection Using @code{dlltool}
28424 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
28425 DLLs and static import libraries. This section summarizes the most
28426 common @code{dlltool} switches. The form of the @code{dlltool} command
28430 $ dlltool [@var{switches}]
28434 @code{dlltool} switches include:
28437 @item --base-file @var{basefile}
28438 @cindex @option{--base-file} (@command{dlltool})
28439 Read the base file @var{basefile} generated by the linker. This switch
28440 is used to create a relocatable DLL.
28442 @item --def @var{deffile}
28443 @cindex @option{--def} (@command{dlltool})
28444 Read the definition file.
28446 @item --dllname @var{name}
28447 @cindex @option{--dllname} (@command{dlltool})
28448 Gives the name of the DLL. This switch is used to embed the name of the
28449 DLL in the static import library generated by @code{dlltool} with switch
28450 @option{--output-lib}.
28453 @cindex @option{-k} (@command{dlltool})
28454 Kill @code{@@}@i{nn} from exported names
28455 (@pxref{Windows Calling Conventions}
28456 for a discussion about @code{Stdcall}-style symbols.
28459 @cindex @option{--help} (@command{dlltool})
28460 Prints the @code{dlltool} switches with a concise description.
28462 @item --output-exp @var{exportfile}
28463 @cindex @option{--output-exp} (@command{dlltool})
28464 Generate an export file @var{exportfile}. The export file contains the
28465 export table (list of symbols in the DLL) and is used to create the DLL.
28467 @item --output-lib @i{libfile}
28468 @cindex @option{--output-lib} (@command{dlltool})
28469 Generate a static import library @var{libfile}.
28472 @cindex @option{-v} (@command{dlltool})
28475 @item --as @i{assembler-name}
28476 @cindex @option{--as} (@command{dlltool})
28477 Use @i{assembler-name} as the assembler. The default is @code{as}.
28480 @node GNAT and Windows Resources
28481 @section GNAT and Windows Resources
28482 @cindex Resources, windows
28485 * Building Resources::
28486 * Compiling Resources::
28487 * Using Resources::
28491 Resources are an easy way to add Windows specific objects to your
28492 application. The objects that can be added as resources include:
28521 This section explains how to build, compile and use resources.
28523 @node Building Resources
28524 @subsection Building Resources
28525 @cindex Resources, building
28528 A resource file is an ASCII file. By convention resource files have an
28529 @file{.rc} extension.
28530 The easiest way to build a resource file is to use Microsoft tools
28531 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
28532 @code{dlgedit.exe} to build dialogs.
28533 It is always possible to build an @file{.rc} file yourself by writing a
28536 It is not our objective to explain how to write a resource file. A
28537 complete description of the resource script language can be found in the
28538 Microsoft documentation.
28540 @node Compiling Resources
28541 @subsection Compiling Resources
28544 @cindex Resources, compiling
28547 This section describes how to build a GNAT-compatible (COFF) object file
28548 containing the resources. This is done using the Resource Compiler
28549 @code{windres} as follows:
28552 $ windres -i myres.rc -o myres.o
28556 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
28557 file. You can specify an alternate preprocessor (usually named
28558 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
28559 parameter. A list of all possible options may be obtained by entering
28560 the command @code{windres} @option{--help}.
28562 It is also possible to use the Microsoft resource compiler @code{rc.exe}
28563 to produce a @file{.res} file (binary resource file). See the
28564 corresponding Microsoft documentation for further details. In this case
28565 you need to use @code{windres} to translate the @file{.res} file to a
28566 GNAT-compatible object file as follows:
28569 $ windres -i myres.res -o myres.o
28572 @node Using Resources
28573 @subsection Using Resources
28574 @cindex Resources, using
28577 To include the resource file in your program just add the
28578 GNAT-compatible object file for the resource(s) to the linker
28579 arguments. With @command{gnatmake} this is done by using the @option{-largs}
28583 $ gnatmake myprog -largs myres.o
28586 @node Debugging a DLL
28587 @section Debugging a DLL
28588 @cindex DLL debugging
28591 * Program and DLL Both Built with GCC/GNAT::
28592 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
28596 Debugging a DLL is similar to debugging a standard program. But
28597 we have to deal with two different executable parts: the DLL and the
28598 program that uses it. We have the following four possibilities:
28602 The program and the DLL are built with @code{GCC/GNAT}.
28604 The program is built with foreign tools and the DLL is built with
28607 The program is built with @code{GCC/GNAT} and the DLL is built with
28613 In this section we address only cases one and two above.
28614 There is no point in trying to debug
28615 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
28616 information in it. To do so you must use a debugger compatible with the
28617 tools suite used to build the DLL.
28619 @node Program and DLL Both Built with GCC/GNAT
28620 @subsection Program and DLL Both Built with GCC/GNAT
28623 This is the simplest case. Both the DLL and the program have @code{GDB}
28624 compatible debugging information. It is then possible to break anywhere in
28625 the process. Let's suppose here that the main procedure is named
28626 @code{ada_main} and that in the DLL there is an entry point named
28630 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
28631 program must have been built with the debugging information (see GNAT -g
28632 switch). Here are the step-by-step instructions for debugging it:
28635 @item Launch @code{GDB} on the main program.
28641 @item Start the program and stop at the beginning of the main procedure
28648 This step is required to be able to set a breakpoint inside the DLL. As long
28649 as the program is not run, the DLL is not loaded. This has the
28650 consequence that the DLL debugging information is also not loaded, so it is not
28651 possible to set a breakpoint in the DLL.
28653 @item Set a breakpoint inside the DLL
28656 (gdb) break ada_dll
28663 At this stage a breakpoint is set inside the DLL. From there on
28664 you can use the standard approach to debug the whole program
28665 (@pxref{Running and Debugging Ada Programs}).
28668 @c This used to work, probably because the DLLs were non-relocatable
28669 @c keep this section around until the problem is sorted out.
28671 To break on the @code{DllMain} routine it is not possible to follow
28672 the procedure above. At the time the program stop on @code{ada_main}
28673 the @code{DllMain} routine as already been called. Either you can use
28674 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
28677 @item Launch @code{GDB} on the main program.
28683 @item Load DLL symbols
28686 (gdb) add-sym api.dll
28689 @item Set a breakpoint inside the DLL
28692 (gdb) break ada_dll.adb:45
28695 Note that at this point it is not possible to break using the routine symbol
28696 directly as the program is not yet running. The solution is to break
28697 on the proper line (break in @file{ada_dll.adb} line 45).
28699 @item Start the program
28708 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
28709 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
28712 * Debugging the DLL Directly::
28713 * Attaching to a Running Process::
28717 In this case things are slightly more complex because it is not possible to
28718 start the main program and then break at the beginning to load the DLL and the
28719 associated DLL debugging information. It is not possible to break at the
28720 beginning of the program because there is no @code{GDB} debugging information,
28721 and therefore there is no direct way of getting initial control. This
28722 section addresses this issue by describing some methods that can be used
28723 to break somewhere in the DLL to debug it.
28726 First suppose that the main procedure is named @code{main} (this is for
28727 example some C code built with Microsoft Visual C) and that there is a
28728 DLL named @code{test.dll} containing an Ada entry point named
28732 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
28733 been built with debugging information (see GNAT -g option).
28735 @node Debugging the DLL Directly
28736 @subsubsection Debugging the DLL Directly
28740 Find out the executable starting address
28743 $ objdump --file-header main.exe
28746 The starting address is reported on the last line. For example:
28749 main.exe: file format pei-i386
28750 architecture: i386, flags 0x0000010a:
28751 EXEC_P, HAS_DEBUG, D_PAGED
28752 start address 0x00401010
28756 Launch the debugger on the executable.
28763 Set a breakpoint at the starting address, and launch the program.
28766 $ (gdb) break *0x00401010
28770 The program will stop at the given address.
28773 Set a breakpoint on a DLL subroutine.
28776 (gdb) break ada_dll.adb:45
28779 Or if you want to break using a symbol on the DLL, you need first to
28780 select the Ada language (language used by the DLL).
28783 (gdb) set language ada
28784 (gdb) break ada_dll
28788 Continue the program.
28795 This will run the program until it reaches the breakpoint that has been
28796 set. From that point you can use the standard way to debug a program
28797 as described in (@pxref{Running and Debugging Ada Programs}).
28802 It is also possible to debug the DLL by attaching to a running process.
28804 @node Attaching to a Running Process
28805 @subsubsection Attaching to a Running Process
28806 @cindex DLL debugging, attach to process
28809 With @code{GDB} it is always possible to debug a running process by
28810 attaching to it. It is possible to debug a DLL this way. The limitation
28811 of this approach is that the DLL must run long enough to perform the
28812 attach operation. It may be useful for instance to insert a time wasting
28813 loop in the code of the DLL to meet this criterion.
28817 @item Launch the main program @file{main.exe}.
28823 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
28824 that the process PID for @file{main.exe} is 208.
28832 @item Attach to the running process to be debugged.
28838 @item Load the process debugging information.
28841 (gdb) symbol-file main.exe
28844 @item Break somewhere in the DLL.
28847 (gdb) break ada_dll
28850 @item Continue process execution.
28859 This last step will resume the process execution, and stop at
28860 the breakpoint we have set. From there you can use the standard
28861 approach to debug a program as described in
28862 (@pxref{Running and Debugging Ada Programs}).
28864 @node Setting Stack Size from gnatlink
28865 @section Setting Stack Size from @command{gnatlink}
28868 It is possible to specify the program stack size at link time. On modern
28869 versions of Windows, starting with XP, this is mostly useful to set the size of
28870 the main stack (environment task). The other task stacks are set with pragma
28871 Linker_Options or with gnatbind -d. On older versions of Windows (2000, NT4,
28872 etc.), it is not possible to set the reserve size of individual tasks and thus
28873 the link-time stack size applies to all tasks.
28875 This setting can be done with
28876 @command{gnatlink} using either:
28880 @item using @option{-Xlinker} linker option
28883 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
28886 This sets the stack reserve size to 0x10000 bytes and the stack commit
28887 size to 0x1000 bytes.
28889 @item using @option{-Wl} linker option
28892 $ gnatlink hello -Wl,--stack=0x1000000
28895 This sets the stack reserve size to 0x1000000 bytes. Note that with
28896 @option{-Wl} option it is not possible to set the stack commit size
28897 because the coma is a separator for this option.
28901 @node Setting Heap Size from gnatlink
28902 @section Setting Heap Size from @command{gnatlink}
28905 Under Windows systems, it is possible to specify the program heap size from
28906 @command{gnatlink} using either:
28910 @item using @option{-Xlinker} linker option
28913 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
28916 This sets the heap reserve size to 0x10000 bytes and the heap commit
28917 size to 0x1000 bytes.
28919 @item using @option{-Wl} linker option
28922 $ gnatlink hello -Wl,--heap=0x1000000
28925 This sets the heap reserve size to 0x1000000 bytes. Note that with
28926 @option{-Wl} option it is not possible to set the heap commit size
28927 because the coma is a separator for this option.
28934 @c **********************************
28935 @c * GNU Free Documentation License *
28936 @c **********************************
28938 @c GNU Free Documentation License
28940 @node Index,,GNU Free Documentation License, Top
28946 @c Put table of contents at end, otherwise it precedes the "title page" in
28947 @c the .txt version
28948 @c Edit the pdf file to move the contents to the beginning, after the title