1 \input texinfo @c -*-texinfo-*-
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
6 @c GNAT DOCUMENTATION o
10 @c Copyright (C) 1992-2004 Ada Core Technologies, Inc. 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, 59 Temple Place - Suite 330, Boston, o
21 @c MA 02111-1307, 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
89 @settitle GNAT User's Guide for Native Platforms / OpenVMS Alpha
90 @dircategory GNU Ada tools
92 * GNAT User's Guide (gnat_ugn_vms) for Native Platforms / OpenVMS Alpha
97 @settitle GNAT User's Guide for Native Platforms / Unix and Windows
99 * GNAT User's Guide (gnat_ugn_unw) for Native Platforms / Unix and Windows
103 @include gcc-common.texi
105 @setchapternewpage odd
110 Copyright @copyright{} 1995-2004, Free Software Foundation
112 Permission is granted to copy, distribute and/or modify this document
113 under the terms of the GNU Free Documentation License, Version 1.2
114 or any later version published by the Free Software Foundation;
115 with the Invariant Sections being ``GNU Free Documentation License'', with the
116 Front-Cover Texts being
118 ``GNAT User's Guide for Native Platforms / OpenVMS Alpha'',
121 ``GNAT User's Guide for Native Platforms / Unix and Windows'',
123 and with no Back-Cover Texts.
124 A copy of the license is included in the section entitled
125 ``GNU Free Documentation License''.
130 @title GNAT User's Guide
131 @center @titlefont{for Native Platforms}
136 @titlefont{@i{Unix and Windows}}
139 @titlefont{@i{OpenVMS Alpha}}
144 @subtitle GNAT, The GNU Ada 95 Compiler
145 @subtitle GCC version @value{version-GCC}
147 @author Ada Core Technologies, Inc.
150 @vskip 0pt plus 1filll
158 @node Top, About This Guide, (dir), (dir)
159 @top GNAT User's Guide
163 GNAT User's Guide for Native Platforms / OpenVMS Alpha
168 GNAT User's Guide for Native Platforms / Unix and Windows
172 GNAT, The GNU Ada 95 Compiler@*
173 GCC version @value{version-GCC}@*
176 Ada Core Technologies, Inc.@*
180 * Getting Started with GNAT::
181 * The GNAT Compilation Model::
182 * Compiling Using gcc::
183 * Binding Using gnatbind::
184 * Linking Using gnatlink::
185 * The GNAT Make Program gnatmake::
186 * Improving Performance::
187 * Renaming Files Using gnatchop::
188 * Configuration Pragmas::
189 * Handling Arbitrary File Naming Conventions Using gnatname::
190 * GNAT Project Manager::
191 * The Cross-Referencing Tools gnatxref and gnatfind::
192 * The GNAT Pretty-Printer gnatpp::
193 * File Name Krunching Using gnatkr::
194 * Preprocessing Using gnatprep::
196 * The GNAT Run-Time Library Builder gnatlbr::
198 * The GNAT Library Browser gnatls::
199 * Cleaning Up Using gnatclean::
201 * GNAT and Libraries::
202 * Using the GNU make Utility::
204 * Finding Memory Problems::
205 * Creating Sample Bodies Using gnatstub::
206 * Other Utility Programs::
207 * Running and Debugging Ada Programs::
209 * Compatibility with DEC Ada::
211 * Platform-Specific Information for the Run-Time Libraries::
212 * Example of Binder Output File::
213 * Elaboration Order Handling in GNAT::
215 * Compatibility and Porting Guide::
217 * Microsoft Windows Topics::
219 * GNU Free Documentation License::
222 --- The Detailed Node Listing ---
226 * What This Guide Contains::
227 * What You Should Know before Reading This Guide::
228 * Related Information::
231 Getting Started with GNAT
234 * Running a Simple Ada Program::
235 * Running a Program with Multiple Units::
236 * Using the gnatmake Utility::
238 * Editing with Emacs::
241 * Introduction to GPS::
242 * Introduction to Glide and GVD::
245 The GNAT Compilation Model
247 * Source Representation::
248 * Foreign Language Representation::
249 * File Naming Rules::
250 * Using Other File Names::
251 * Alternative File Naming Schemes::
252 * Generating Object Files::
253 * Source Dependencies::
254 * The Ada Library Information Files::
255 * Binding an Ada Program::
256 * Mixed Language Programming::
257 * Building Mixed Ada & C++ Programs::
258 * Comparison between GNAT and C/C++ Compilation Models::
259 * Comparison between GNAT and Conventional Ada Library Models::
261 * Placement of temporary files::
264 Foreign Language Representation
267 * Other 8-Bit Codes::
268 * Wide Character Encodings::
270 Compiling Ada Programs With gcc
272 * Compiling Programs::
274 * Search Paths and the Run-Time Library (RTL)::
275 * Order of Compilation Issues::
280 * Output and Error Message Control::
281 * Warning Message Control::
282 * Debugging and Assertion Control::
284 * Stack Overflow Checking::
285 * Validity Checking::
287 * Using gcc for Syntax Checking::
288 * Using gcc for Semantic Checking::
289 * Compiling Ada 83 Programs::
290 * Character Set Control::
291 * File Naming Control::
292 * Subprogram Inlining Control::
293 * Auxiliary Output Control::
294 * Debugging Control::
295 * Exception Handling Control::
296 * Units to Sources Mapping Files::
297 * Integrated Preprocessing::
302 Binding Ada Programs With gnatbind
305 * Switches for gnatbind::
306 * Command-Line Access::
307 * Search Paths for gnatbind::
308 * Examples of gnatbind Usage::
310 Switches for gnatbind
312 * Consistency-Checking Modes::
313 * Binder Error Message Control::
314 * Elaboration Control::
316 * Binding with Non-Ada Main Programs::
317 * Binding Programs with No Main Subprogram::
319 Linking Using gnatlink
322 * Switches for gnatlink::
323 * Setting Stack Size from gnatlink::
324 * Setting Heap Size from gnatlink::
326 The GNAT Make Program gnatmake
329 * Switches for gnatmake::
330 * Mode Switches for gnatmake::
331 * Notes on the Command Line::
332 * How gnatmake Works::
333 * Examples of gnatmake Usage::
336 Improving Performance
337 * Performance Considerations::
338 * Reducing the Size of Ada Executables with gnatelim::
340 Performance Considerations
341 * Controlling Run-Time Checks::
342 * Use of Restrictions::
343 * Optimization Levels::
344 * Debugging Optimized Code::
345 * Inlining of Subprograms::
346 * Optimization and Strict Aliasing::
348 * Coverage Analysis::
351 Reducing the Size of Ada Executables with gnatelim
354 * Correcting the List of Eliminate Pragmas::
355 * Making Your Executables Smaller::
356 * Summary of the gnatelim Usage Cycle::
358 Renaming Files Using gnatchop
360 * Handling Files with Multiple Units::
361 * Operating gnatchop in Compilation Mode::
362 * Command Line for gnatchop::
363 * Switches for gnatchop::
364 * Examples of gnatchop Usage::
366 Configuration Pragmas
368 * Handling of Configuration Pragmas::
369 * The Configuration Pragmas Files::
371 Handling Arbitrary File Naming Conventions Using gnatname
373 * Arbitrary File Naming Conventions::
375 * Switches for gnatname::
376 * Examples of gnatname Usage::
381 * Examples of Project Files::
382 * Project File Syntax::
383 * Objects and Sources in Project Files::
384 * Importing Projects::
385 * Project Extension::
386 * External References in Project Files::
387 * Packages in Project Files::
388 * Variables from Imported Projects::
391 * Using Third-Party Libraries through Projects::
392 * Stand-alone Library Projects::
393 * Switches Related to Project Files::
394 * Tools Supporting Project Files::
395 * An Extended Example::
396 * Project File Complete Syntax::
399 The Cross-Referencing Tools gnatxref and gnatfind
401 * gnatxref Switches::
402 * gnatfind Switches::
403 * Project Files for gnatxref and gnatfind::
404 * Regular Expressions in gnatfind and gnatxref::
405 * Examples of gnatxref Usage::
406 * Examples of gnatfind Usage::
409 The GNAT Pretty-Printer gnatpp
411 * Switches for gnatpp::
415 File Name Krunching Using gnatkr
420 * Examples of gnatkr Usage::
422 Preprocessing Using gnatprep
425 * Switches for gnatprep::
426 * Form of Definitions File::
427 * Form of Input Text for gnatprep::
430 The GNAT Run-Time Library Builder gnatlbr
433 * Switches for gnatlbr::
434 * Examples of gnatlbr Usage::
437 The GNAT Library Browser gnatls
440 * Switches for gnatls::
441 * Examples of gnatls Usage::
443 Cleaning Up Using gnatclean
445 * Running gnatclean::
446 * Switches for gnatclean::
447 * Examples of gnatclean Usage::
453 * Creating an Ada Library::
454 * Installing an Ada Library::
455 * Using an Ada Library::
456 * Creating an Ada Library to be Used in a Non-Ada Context::
457 * Rebuilding the GNAT Run-Time Library::
459 Using the GNU make Utility
461 * Using gnatmake in a Makefile::
462 * Automatically Creating a List of Directories::
463 * Generating the Command Line Switches::
464 * Overcoming Command Line Length Limits::
467 Finding Memory Problems
472 * The GNAT Debug Pool Facility::
478 * Switches for gnatmem::
479 * Example of gnatmem Usage::
482 The GNAT Debug Pool Facility
484 Creating Sample Bodies Using gnatstub
487 * Switches for gnatstub::
489 Other Utility Programs
491 * Using Other Utility Programs with GNAT::
492 * The External Symbol Naming Scheme of GNAT::
494 * Ada Mode for Glide::
496 * Converting Ada Files to html with gnathtml::
498 Running and Debugging Ada Programs
500 * The GNAT Debugger GDB::
502 * Introduction to GDB Commands::
503 * Using Ada Expressions::
504 * Calling User-Defined Subprograms::
505 * Using the Next Command in a Function::
508 * Debugging Generic Units::
509 * GNAT Abnormal Termination or Failure to Terminate::
510 * Naming Conventions for GNAT Source Files::
511 * Getting Internal Debugging Information::
519 Compatibility with DEC Ada
521 * Ada 95 Compatibility::
522 * Differences in the Definition of Package System::
523 * Language-Related Features::
524 * The Package STANDARD::
525 * The Package SYSTEM::
526 * Tasking and Task-Related Features::
527 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
528 * Pragmas and Pragma-Related Features::
529 * Library of Predefined Units::
531 * Main Program Definition::
532 * Implementation-Defined Attributes::
533 * Compiler and Run-Time Interfacing::
534 * Program Compilation and Library Management::
536 * Implementation Limits::
539 Language-Related Features
541 * Integer Types and Representations::
542 * Floating-Point Types and Representations::
543 * Pragmas Float_Representation and Long_Float::
544 * Fixed-Point Types and Representations::
545 * Record and Array Component Alignment::
547 * Other Representation Clauses::
549 Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
551 * Assigning Task IDs::
552 * Task IDs and Delays::
553 * Task-Related Pragmas::
554 * Scheduling and Task Priority::
556 * External Interrupts::
558 Pragmas and Pragma-Related Features
560 * Restrictions on the Pragma INLINE::
561 * Restrictions on the Pragma INTERFACE::
562 * Restrictions on the Pragma SYSTEM_NAME::
564 Library of Predefined Units
566 * Changes to DECLIB::
570 * Shared Libraries and Options Files::
574 Platform-Specific Information for the Run-Time Libraries
576 * Summary of Run-Time Configurations::
577 * Specifying a Run-Time Library::
578 * Choosing between Native and FSU Threads Libraries::
579 * Choosing the Scheduling Policy::
580 * Solaris-Specific Considerations::
581 * IRIX-Specific Considerations::
582 * Linux-Specific Considerations::
584 Example of Binder Output File
586 Elaboration Order Handling in GNAT
588 * Elaboration Code in Ada 95::
589 * Checking the Elaboration Order in Ada 95::
590 * Controlling the Elaboration Order in Ada 95::
591 * Controlling Elaboration in GNAT - Internal Calls::
592 * Controlling Elaboration in GNAT - External Calls::
593 * Default Behavior in GNAT - Ensuring Safety::
594 * Treatment of Pragma Elaborate::
595 * Elaboration Issues for Library Tasks::
596 * Mixing Elaboration Models::
597 * What to Do If the Default Elaboration Behavior Fails::
598 * Elaboration for Access-to-Subprogram Values::
599 * Summary of Procedures for Elaboration Control::
600 * Other Elaboration Order Considerations::
604 * Basic Assembler Syntax::
605 * A Simple Example of Inline Assembler::
606 * Output Variables in Inline Assembler::
607 * Input Variables in Inline Assembler::
608 * Inlining Inline Assembler Code::
609 * Other Asm Functionality::
610 * A Complete Example::
612 Compatibility and Porting Guide
614 * Compatibility with Ada 83::
615 * Implementation-dependent characteristics::
616 * Compatibility with DEC Ada 83::
617 * Compatibility with Other Ada 95 Systems::
618 * Representation Clauses::
621 Microsoft Windows Topics
623 * Using GNAT on Windows::
624 * CONSOLE and WINDOWS subsystems::
626 * Mixed-Language Programming on Windows::
627 * Windows Calling Conventions::
628 * Introduction to Dynamic Link Libraries (DLLs)::
629 * Using DLLs with GNAT::
630 * Building DLLs with GNAT::
631 * GNAT and Windows Resources::
633 * GNAT and COM/DCOM Objects::
641 @node About This Guide
642 @unnumbered About This Guide
646 This guide describes the use of of GNAT, a full language compiler for the Ada
647 95 programming language, implemented on HP OpenVMS Alpha platforms.
650 This guide describes the use of GNAT, a compiler and software development
651 toolset for the full Ada 95 programming language.
653 It describes the features of the compiler and tools, and details
654 how to use them to build Ada 95 applications.
657 * What This Guide Contains::
658 * What You Should Know before Reading This Guide::
659 * Related Information::
663 @node What This Guide Contains
664 @unnumberedsec What This Guide Contains
667 This guide contains the following chapters:
671 @ref{Getting Started with GNAT}, describes how to get started compiling
672 and running Ada programs with the GNAT Ada programming environment.
674 @ref{The GNAT Compilation Model}, describes the compilation model used
678 @ref{Compiling Using gcc}, describes how to compile
679 Ada programs with @code{gcc}, the Ada compiler.
682 @ref{Binding Using gnatbind}, describes how to
683 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
687 @ref{Linking Using gnatlink},
688 describes @code{gnatlink}, a
689 program that provides for linking using the GNAT run-time library to
690 construct a program. @code{gnatlink} can also incorporate foreign language
691 object units into the executable.
694 @ref{The GNAT Make Program gnatmake}, describes @code{gnatmake}, a
695 utility that automatically determines the set of sources
696 needed by an Ada compilation unit, and executes the necessary compilations
700 @ref{Improving Performance}, shows various techniques for making your
701 Ada program run faster or take less space.
702 It discusses the effect of the compiler's optimization switch and
703 also describes the @command{gnatelim} tool.
706 @ref{Renaming Files Using gnatchop}, describes
707 @code{gnatchop}, a utility that allows you to preprocess a file that
708 contains Ada source code, and split it into one or more new files, one
709 for each compilation unit.
712 @ref{Configuration Pragmas}, describes the configuration pragmas
716 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
717 shows how to override the default GNAT file naming conventions,
718 either for an individual unit or globally.
721 @ref{GNAT Project Manager}, describes how to use project files
722 to organize large projects.
725 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
726 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
727 way to navigate through sources.
730 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
731 version of an Ada source file with control over casing, indentation,
732 comment placement, and other elements of program presentation style.
736 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
737 file name krunching utility, used to handle shortened
738 file names on operating systems with a limit on the length of names.
741 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
742 preprocessor utility that allows a single source file to be used to
743 generate multiple or parameterized source files, by means of macro
748 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
749 a tool for rebuilding the GNAT run time with user-supplied
750 configuration pragmas.
754 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
755 utility that displays information about compiled units, including dependences
756 on the corresponding sources files, and consistency of compilations.
759 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
760 to delete files that are produced by the compiler, binder and linker.
764 @ref{GNAT and Libraries}, describes the process of creating and using
765 Libraries with GNAT. It also describes how to recompile the GNAT run-time
769 @ref{Using the GNU make Utility}, describes some techniques for using
770 the GNAT toolset in Makefiles.
774 @ref{Finding Memory Problems}, describes
776 @command{gnatmem}, a utility that monitors dynamic allocation and deallocation
777 and helps detect ``memory leaks'', and
779 the GNAT Debug Pool facility, which helps detect incorrect memory references.
782 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
783 a utility that generates empty but compilable bodies for library units.
786 @ref{Other Utility Programs}, discusses several other GNAT utilities,
787 including @code{gnathtml}.
790 @ref{Running and Debugging Ada Programs}, describes how to run and debug
795 @ref{Compatibility with DEC Ada}, details the compatibility of GNAT with
796 DEC Ada 83 @footnote{``DEC Ada'' refers to the legacy product originally
797 developed by Digital Equipment Corporation and currently supported by HP.}
802 @ref{Platform-Specific Information for the Run-Time Libraries},
803 describes the various run-time
804 libraries supported by GNAT on various platforms and explains how to
805 choose a particular library.
808 @ref{Example of Binder Output File}, shows the source code for the binder
809 output file for a sample program.
812 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
813 you deal with elaboration order issues.
816 @ref{Inline Assembler}, shows how to use the inline assembly facility
820 @ref{Compatibility and Porting Guide}, includes sections on compatibility
821 of GNAT with other Ada 83 and Ada 95 compilation systems, to assist
822 in porting code from other environments.
826 @ref{Microsoft Windows Topics}, presents information relevant to the
827 Microsoft Windows platform.
832 @c *************************************************
833 @node What You Should Know before Reading This Guide
834 @c *************************************************
835 @unnumberedsec What You Should Know before Reading This Guide
837 @cindex Ada 95 Language Reference Manual
839 This user's guide assumes that you are familiar with Ada 95 language, as
840 described in the International Standard ANSI/ISO/IEC-8652:1995, January
843 @node Related Information
844 @unnumberedsec Related Information
847 For further information about related tools, refer to the following
852 @cite{GNAT Reference Manual}, which contains all reference
853 material for the GNAT implementation of Ada 95.
857 @cite{Using the GNAT Programming System}, which describes the GPS
858 integrated development environment.
861 @cite{GNAT Programming System Tutorial}, which introduces the
862 main GPS features through examples.
866 @cite{Ada 95 Language Reference Manual}, which contains all reference
867 material for the Ada 95 programming language.
870 @cite{Debugging with GDB}
872 , located in the GNU:[DOCS] directory,
874 contains all details on the use of the GNU source-level debugger.
877 @cite{GNU Emacs Manual}
879 , located in the GNU:[DOCS] directory if the EMACS kit is installed,
881 contains full information on the extensible editor and programming
888 @unnumberedsec Conventions
890 @cindex Typographical conventions
893 Following are examples of the typographical and graphic conventions used
898 @code{Functions}, @code{utility program names}, @code{standard names},
905 @file{File Names}, @file{button names}, and @file{field names}.
914 [optional information or parameters]
917 Examples are described by text
919 and then shown this way.
924 Commands that are entered by the user are preceded in this manual by the
925 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
926 uses this sequence as a prompt, then the commands will appear exactly as
927 you see them in the manual. If your system uses some other prompt, then
928 the command will appear with the @code{$} replaced by whatever prompt
929 character you are using.
932 Full file names are shown with the ``@code{/}'' character
933 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
934 If you are using GNAT on a Windows platform, please note that
935 the ``@code{\}'' character should be used instead.
940 @c ****************************
941 @node Getting Started with GNAT
942 @chapter Getting Started with GNAT
945 This chapter describes some simple ways of using GNAT to build
946 executable Ada programs.
948 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
949 show how to use the command line environment.
950 @ref{Introduction to Glide and GVD}, provides a brief
951 introduction to the visually-oriented IDE for GNAT.
952 Supplementing Glide on some platforms is GPS, the
953 GNAT Programming System, which offers a richer graphical
954 ``look and feel'', enhanced configurability, support for
955 development in other programming language, comprehensive
956 browsing features, and many other capabilities.
957 For information on GPS please refer to
958 @cite{Using the GNAT Programming System}.
963 * Running a Simple Ada Program::
964 * Running a Program with Multiple Units::
965 * Using the gnatmake Utility::
967 * Editing with Emacs::
970 * Introduction to GPS::
971 * Introduction to Glide and GVD::
976 @section Running GNAT
979 Three steps are needed to create an executable file from an Ada source
984 The source file(s) must be compiled.
986 The file(s) must be bound using the GNAT binder.
988 All appropriate object files must be linked to produce an executable.
992 All three steps are most commonly handled by using the @code{gnatmake}
993 utility program that, given the name of the main program, automatically
994 performs the necessary compilation, binding and linking steps.
997 @node Running a Simple Ada Program
998 @section Running a Simple Ada Program
1001 Any text editor may be used to prepare an Ada program.
1004 used, the optional Ada mode may be helpful in laying out the program.
1007 program text is a normal text file. We will suppose in our initial
1008 example that you have used your editor to prepare the following
1009 standard format text file:
1011 @smallexample @c ada
1013 with Ada.Text_IO; use Ada.Text_IO;
1016 Put_Line ("Hello WORLD!");
1022 This file should be named @file{hello.adb}.
1023 With the normal default file naming conventions, GNAT requires
1025 contain a single compilation unit whose file name is the
1027 with periods replaced by hyphens; the
1028 extension is @file{ads} for a
1029 spec and @file{adb} for a body.
1030 You can override this default file naming convention by use of the
1031 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1032 Alternatively, if you want to rename your files according to this default
1033 convention, which is probably more convenient if you will be using GNAT
1034 for all your compilations, then the @code{gnatchop} utility
1035 can be used to generate correctly-named source files
1036 (@pxref{Renaming Files Using gnatchop}).
1038 You can compile the program using the following command (@code{$} is used
1039 as the command prompt in the examples in this document):
1046 @code{gcc} is the command used to run the compiler. This compiler is
1047 capable of compiling programs in several languages, including Ada 95 and
1048 C. It assumes that you have given it an Ada program if the file extension is
1049 either @file{.ads} or @file{.adb}, and it will then call
1050 the GNAT compiler to compile the specified file.
1053 The @option{-c} switch is required. It tells @command{gcc} to only do a
1054 compilation. (For C programs, @command{gcc} can also do linking, but this
1055 capability is not used directly for Ada programs, so the @option{-c}
1056 switch must always be present.)
1059 This compile command generates a file
1060 @file{hello.o}, which is the object
1061 file corresponding to your Ada program. It also generates
1062 an ``Ada Library Information'' file @file{hello.ali},
1063 which contains additional information used to check
1064 that an Ada program is consistent.
1065 To build an executable file,
1066 use @code{gnatbind} to bind the program
1067 and @code{gnatlink} to link it. The
1068 argument to both @code{gnatbind} and @code{gnatlink} is the name of the
1069 @file{ALI} file, but the default extension of @file{.ali} can
1070 be omitted. This means that in the most common case, the argument
1071 is simply the name of the main program:
1079 A simpler method of carrying out these steps is to use
1081 a master program that invokes all the required
1082 compilation, binding and linking tools in the correct order. In particular,
1083 @command{gnatmake} automatically recompiles any sources that have been
1084 modified since they were last compiled, or sources that depend
1085 on such modified sources, so that ``version skew'' is avoided.
1086 @cindex Version skew (avoided by @command{gnatmake})
1089 $ gnatmake hello.adb
1093 The result is an executable program called @file{hello}, which can be
1096 @c The following should be removed (BMB 2001-01-23)
1098 @c $ ^./hello^$ RUN HELLO^
1099 @c @end smallexample
1106 assuming that the current directory is on the search path
1107 for executable programs.
1110 and, if all has gone well, you will see
1117 appear in response to this command.
1120 @c ****************************************
1121 @node Running a Program with Multiple Units
1122 @section Running a Program with Multiple Units
1125 Consider a slightly more complicated example that has three files: a
1126 main program, and the spec and body of a package:
1128 @smallexample @c ada
1131 package Greetings is
1136 with Ada.Text_IO; use Ada.Text_IO;
1137 package body Greetings is
1140 Put_Line ("Hello WORLD!");
1143 procedure Goodbye is
1145 Put_Line ("Goodbye WORLD!");
1162 Following the one-unit-per-file rule, place this program in the
1163 following three separate files:
1167 spec of package @code{Greetings}
1170 body of package @code{Greetings}
1173 body of main program
1177 To build an executable version of
1178 this program, we could use four separate steps to compile, bind, and link
1179 the program, as follows:
1183 $ gcc -c greetings.adb
1189 Note that there is no required order of compilation when using GNAT.
1190 In particular it is perfectly fine to compile the main program first.
1191 Also, it is not necessary to compile package specs in the case where
1192 there is an accompanying body; you only need to compile the body. If you want
1193 to submit these files to the compiler for semantic checking and not code
1194 generation, then use the
1195 @option{-gnatc} switch:
1198 $ gcc -c greetings.ads -gnatc
1202 Although the compilation can be done in separate steps as in the
1203 above example, in practice it is almost always more convenient
1204 to use the @code{gnatmake} tool. All you need to know in this case
1205 is the name of the main program's source file. The effect of the above four
1206 commands can be achieved with a single one:
1209 $ gnatmake gmain.adb
1213 In the next section we discuss the advantages of using @code{gnatmake} in
1216 @c *****************************
1217 @node Using the gnatmake Utility
1218 @section Using the @command{gnatmake} Utility
1221 If you work on a program by compiling single components at a time using
1222 @code{gcc}, you typically keep track of the units you modify. In order to
1223 build a consistent system, you compile not only these units, but also any
1224 units that depend on the units you have modified.
1225 For example, in the preceding case,
1226 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1227 you edit @file{greetings.ads}, you must recompile both
1228 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1229 units that depend on @file{greetings.ads}.
1231 @code{gnatbind} will warn you if you forget one of these compilation
1232 steps, so that it is impossible to generate an inconsistent program as a
1233 result of forgetting to do a compilation. Nevertheless it is tedious and
1234 error-prone to keep track of dependencies among units.
1235 One approach to handle the dependency-bookkeeping is to use a
1236 makefile. However, makefiles present maintenance problems of their own:
1237 if the dependencies change as you change the program, you must make
1238 sure that the makefile is kept up-to-date manually, which is also an
1239 error-prone process.
1241 The @code{gnatmake} utility takes care of these details automatically.
1242 Invoke it using either one of the following forms:
1245 $ gnatmake gmain.adb
1246 $ gnatmake ^gmain^GMAIN^
1250 The argument is the name of the file containing the main program;
1251 you may omit the extension. @code{gnatmake}
1252 examines the environment, automatically recompiles any files that need
1253 recompiling, and binds and links the resulting set of object files,
1254 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1255 In a large program, it
1256 can be extremely helpful to use @code{gnatmake}, because working out by hand
1257 what needs to be recompiled can be difficult.
1259 Note that @code{gnatmake}
1260 takes into account all the Ada 95 rules that
1261 establish dependencies among units. These include dependencies that result
1262 from inlining subprogram bodies, and from
1263 generic instantiation. Unlike some other
1264 Ada make tools, @code{gnatmake} does not rely on the dependencies that were
1265 found by the compiler on a previous compilation, which may possibly
1266 be wrong when sources change. @code{gnatmake} determines the exact set of
1267 dependencies from scratch each time it is run.
1270 @node Editing with Emacs
1271 @section Editing with Emacs
1275 Emacs is an extensible self-documenting text editor that is available in a
1276 separate VMSINSTAL kit.
1278 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1279 click on the Emacs Help menu and run the Emacs Tutorial.
1280 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1281 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1283 Documentation on Emacs and other tools is available in Emacs under the
1284 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1285 use the middle mouse button to select a topic (e.g. Emacs).
1287 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1288 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1289 get to the Emacs manual.
1290 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1293 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1294 which is sufficiently extensible to provide for a complete programming
1295 environment and shell for the sophisticated user.
1299 @node Introduction to GPS
1300 @section Introduction to GPS
1301 @cindex GPS (GNAT Programming System)
1302 @cindex GNAT Programming System (GPS)
1304 Although the command line interface (@command{gnatmake}, etc.) alone
1305 is sufficient, a graphical Interactive Development
1306 Environment can make it easier for you to compose, navigate, and debug
1307 programs. This section describes the main features of GPS
1308 (``GNAT Programming System''), the GNAT graphical IDE.
1309 You will see how to use GPS to build and debug an executable, and
1310 you will also learn some of the basics of the GNAT ``project'' facility.
1312 GPS enables you to do much more than is presented here;
1313 e.g., you can produce a call graph, interface to a third-party
1314 Version Control System, and inspect the generated assembly language
1316 Indeed, GPS also supports languages other than Ada.
1317 Such additional information, and an explanation of all of the GPS menu
1318 items. may be found in the on-line help, which includes
1319 a user's guide and a tutorial (these are also accessible from the GNAT
1323 * Building a New Program with GPS::
1324 * Simple Debugging with GPS::
1328 @node Building a New Program with GPS
1329 @subsection Building a New Program with GPS
1331 GPS invokes the GNAT compilation tools using information
1332 contained in a @emph{project} (also known as a @emph{project file}):
1333 a collection of properties such
1334 as source directories, identities of main subprograms, tool switches, etc.,
1335 and their associated values.
1336 (See @ref{GNAT Project Manager}, for details.)
1337 In order to run GPS, you will need to either create a new project
1338 or else open an existing one.
1340 This section will explain how you can use GPS to create a project,
1341 to associate Ada source files with a project, and to build and run
1345 @item @emph{Creating a project}
1347 Invoke GPS, either from the command line or the platform's IDE.
1348 After it starts, GPS will display a ``Welcome'' screen with three
1353 @code{Start with default project in directory}
1356 @code{Create new project with wizard}
1359 @code{Open existing project}
1363 Select @code{Create new project with wizard} and press @code{OK}.
1364 A new window will appear. In the text box labeled with
1365 @code{Enter the name of the project to create}, type @file{sample}
1366 as the project name.
1367 In the next box, browse to choose the directory in which you
1368 would like to create the project file.
1369 After selecting an appropriate directory, press @code{Forward}.
1371 A window will appear with the title
1372 @code{Version Control System Configuration}.
1373 Simply press @code{Forward}.
1375 A window will appear with the title
1376 @code{Please select the source directories for this project}.
1377 The directory that you specified for the project file will be selected
1378 by default as the one to use for sources; simply press @code{Forward}.
1380 A window will appear with the title
1381 @code{Please select the build directory for this project}.
1382 The directory that you specified for the project file will be selected
1383 by default for object files and executables;
1384 simply press @code{Forward}.
1386 A window will appear with the title
1387 @code{Please select the main units for this project}.
1388 You will supply this information later, after creating the source file.
1389 Simply press @code{Forward} for now.
1391 A window will appear with the title
1392 @code{Please select the switches to build the project}.
1393 Press @code{Apply}. This will create a project file named
1394 @file{sample.prj} in the directory that you had specified.
1396 @item @emph{Creating and saving the source file}
1398 After you create the new project, a GPS window will appear, which is
1399 partitioned into two main sections:
1403 A @emph{Workspace area}, initially greyed out, which you will use for
1404 creating and editing source files
1407 Directly below, a @emph{Messages area}, which initially displays a
1408 ``Welcome'' message.
1409 (If the Messages area is not visible, drag its border upward to expand it.)
1413 Select @code{File} on the menu bar, and then the @code{New} command.
1414 The Workspace area will become white, and you can now
1415 enter the source program explicitly.
1416 Type the following text
1418 @smallexample @c ada
1420 with Ada.Text_IO; use Ada.Text_IO;
1423 Put_Line("Hello from GPS!");
1429 Select @code{File}, then @code{Save As}, and enter the source file name
1431 The file will be saved in the same directory you specified as the
1432 location of the default project file.
1435 @item @emph{Updating the project file}
1437 You need to add the new source file to the project.
1439 the @code{Project} menu and then @code{Edit project properties}.
1440 Click the @code{Main files} tab on the left, and then the
1442 Choose @file{hello.adb} from the list, and press @code{Open}.
1443 The project settings window will reflect this action.
1446 @item @emph{Building and running the program}
1448 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1449 and select @file{hello.adb}.
1450 The Messages window will display the resulting invocations of @command{gcc},
1451 @command{gnatbind}, and @command{gnatlink}
1452 (reflecting the default switch settings from the
1453 project file that you created) and then a ``successful compilation/build''
1456 To run the program, choose the @code{Build} menu, then @code{Run}, and
1457 select @command{hello}.
1458 An @emph{Arguments Selection} window will appear.
1459 There are no command line arguments, so just click @code{OK}.
1461 The Messages window will now display the program's output (the string
1462 @code{Hello from GPS}), and at the bottom of the GPS window a status
1463 update is displayed (@code{Run: hello}).
1464 Close the GPS window (or select @code{File}, then @code{Exit}) to
1465 terminate this GPS session.
1470 @node Simple Debugging with GPS
1471 @subsection Simple Debugging with GPS
1473 This section illustrates basic debugging techniques (setting breakpoints,
1474 examining/modifying variables, single stepping).
1477 @item @emph{Opening a project}
1479 Start GPS and select @code{Open existing project}; browse to
1480 specify the project file @file{sample.prj} that you had created in the
1483 @item @emph{Creating a source file}
1485 Select @code{File}, then @code{New}, and type in the following program:
1487 @smallexample @c ada
1489 with Ada.Text_IO; use Ada.Text_IO;
1490 procedure Example is
1491 Line : String (1..80);
1494 Put_Line("Type a line of text at each prompt; an empty line to exit");
1498 Put_Line (Line (1..N) );
1506 Select @code{File}, then @code{Save as}, and enter the file name
1509 @item @emph{Updating the project file}
1511 Add @code{Example} as a new main unit for the project:
1514 Select @code{Project}, then @code{Edit Project Properties}.
1517 Select the @code{Main files} tab, click @code{Add}, then
1518 select the file @file{example.adb} from the list, and
1520 You will see the file name appear in the list of main units
1526 @item @emph{Building/running the executable}
1528 To build the executable
1529 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1531 Run the program to see its effect (in the Messages area).
1532 Each line that you enter is displayed; an empty line will
1533 cause the loop to exit and the program to terminate.
1535 @item @emph{Debugging the program}
1537 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1538 which are required for debugging, are on by default when you create
1540 Thus unless you intentionally remove these settings, you will be able
1541 to debug any program that you develop using GPS.
1544 @item @emph{Initializing}
1546 Select @code{Debug}, then @code{Initialize}, then @file{example}
1548 @item @emph{Setting a breakpoint}
1550 After performing the initialization step, you will observe a small
1551 icon to the right of each line number.
1552 This serves as a toggle for breakpoints; clicking the icon will
1553 set a breakpoint at the corresponding line (the icon will change to
1554 a red circle with an ``x''), and clicking it again
1555 will remove the breakpoint / reset the icon.
1557 For purposes of this example, set a breakpoint at line 10 (the
1558 statement @code{Put_Line@ (Line@ (1..N));}
1560 @item @emph{Starting program execution}
1562 Select @code{Debug}, then @code{Run}. When the
1563 @code{Program Arguments} window appears, click @code{OK}.
1564 A console window will appear; enter some line of text,
1565 e.g. @code{abcde}, at the prompt.
1566 The program will pause execution when it gets to the
1567 breakpoint, and the corresponding line is highlighted.
1569 @item @emph{Examining a variable}
1571 Move the mouse over one of the occurrences of the variable @code{N}.
1572 You will see the value (5) displayed, in ``tool tip'' fashion.
1573 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1574 You will see information about @code{N} appear in the @code{Debugger Data}
1575 pane, showing the value as 5.
1578 @item @emph{Assigning a new value to a variable}
1580 Right click on the @code{N} in the @code{Debugger Data} pane, and
1581 select @code{Set value of N}.
1582 When the input window appears, enter the value @code{4} and click
1584 This value does not automatically appear in the @code{Debugger Data}
1585 pane; to see it, right click again on the @code{N} in the
1586 @code{Debugger Data} pane and select @code{Update value}.
1587 The new value, 4, will appear in red.
1589 @item @emph{Single stepping}
1591 Select @code{Debug}, then @code{Next}.
1592 This will cause the next statement to be executed, in this case the
1593 call of @code{Put_Line} with the string slice.
1594 Notice in the console window that the displayed string is simply
1595 @code{abcd} and not @code{abcde} which you had entered.
1596 This is because the upper bound of the slice is now 4 rather than 5.
1598 @item @emph{Removing a breakpoint}
1600 Toggle the breakpoint icon at line 10.
1602 @item @emph{Resuming execution from a breakpoint}
1604 Select @code{Debug}, then @code{Continue}.
1605 The program will reach the next iteration of the loop, and
1606 wait for input after displaying the prompt.
1607 This time, just hit the @kbd{Enter} key.
1608 The value of @code{N} will be 0, and the program will terminate.
1609 The console window will disappear.
1614 @node Introduction to Glide and GVD
1615 @section Introduction to Glide and GVD
1619 This section describes the main features of Glide,
1620 a GNAT graphical IDE, and also shows how to use the basic commands in GVD,
1621 the GNU Visual Debugger.
1622 These tools may be present in addition to, or in place of, GPS on some
1624 Additional information on Glide and GVD may be found
1625 in the on-line help for these tools.
1628 * Building a New Program with Glide::
1629 * Simple Debugging with GVD::
1630 * Other Glide Features::
1633 @node Building a New Program with Glide
1634 @subsection Building a New Program with Glide
1636 The simplest way to invoke Glide is to enter @command{glide}
1637 at the command prompt. It will generally be useful to issue this
1638 as a background command, thus allowing you to continue using
1639 your command window for other purposes while Glide is running:
1646 Glide will start up with an initial screen displaying the top-level menu items
1647 as well as some other information. The menu selections are as follows
1649 @item @code{Buffers}
1660 For this introductory example, you will need to create a new Ada source file.
1661 First, select the @code{Files} menu. This will pop open a menu with around
1662 a dozen or so items. To create a file, select the @code{Open file...} choice.
1663 Depending on the platform, you may see a pop-up window where you can browse
1664 to an appropriate directory and then enter the file name, or else simply
1665 see a line at the bottom of the Glide window where you can likewise enter
1666 the file name. Note that in Glide, when you attempt to open a non-existent
1667 file, the effect is to create a file with that name. For this example enter
1668 @file{hello.adb} as the name of the file.
1670 A new buffer will now appear, occupying the entire Glide window,
1671 with the file name at the top. The menu selections are slightly different
1672 from the ones you saw on the opening screen; there is an @code{Entities} item,
1673 and in place of @code{Glide} there is now an @code{Ada} item. Glide uses
1674 the file extension to identify the source language, so @file{adb} indicates
1677 You will enter some of the source program lines explicitly,
1678 and use the syntax-oriented template mechanism to enter other lines.
1679 First, type the following text:
1681 with Ada.Text_IO; use Ada.Text_IO;
1687 Observe that Glide uses different colors to distinguish reserved words from
1688 identifiers. Also, after the @code{procedure Hello is} line, the cursor is
1689 automatically indented in anticipation of declarations. When you enter
1690 @code{begin}, Glide recognizes that there are no declarations and thus places
1691 @code{begin} flush left. But after the @code{begin} line the cursor is again
1692 indented, where the statement(s) will be placed.
1694 The main part of the program will be a @code{for} loop. Instead of entering
1695 the text explicitly, however, use a statement template. Select the @code{Ada}
1696 item on the top menu bar, move the mouse to the @code{Statements} item,
1697 and you will see a large selection of alternatives. Choose @code{for loop}.
1698 You will be prompted (at the bottom of the buffer) for a loop name;
1699 simply press the @key{Enter} key since a loop name is not needed.
1700 You should see the beginning of a @code{for} loop appear in the source
1701 program window. You will now be prompted for the name of the loop variable;
1702 enter a line with the identifier @code{ind} (lower case). Note that,
1703 by default, Glide capitalizes the name (you can override such behavior
1704 if you wish, although this is outside the scope of this introduction).
1705 Next, Glide prompts you for the loop range; enter a line containing
1706 @code{1..5} and you will see this also appear in the source program,
1707 together with the remaining elements of the @code{for} loop syntax.
1709 Next enter the statement (with an intentional error, a missing semicolon)
1710 that will form the body of the loop:
1712 Put_Line("Hello, World" & Integer'Image(I))
1716 Finally, type @code{end Hello;} as the last line in the program.
1717 Now save the file: choose the @code{File} menu item, and then the
1718 @code{Save buffer} selection. You will see a message at the bottom
1719 of the buffer confirming that the file has been saved.
1721 You are now ready to attempt to build the program. Select the @code{Ada}
1722 item from the top menu bar. Although we could choose simply to compile
1723 the file, we will instead attempt to do a build (which invokes
1724 @command{gnatmake}) since, if the compile is successful, we want to build
1725 an executable. Thus select @code{Ada build}. This will fail because of the
1726 compilation error, and you will notice that the Glide window has been split:
1727 the top window contains the source file, and the bottom window contains the
1728 output from the GNAT tools. Glide allows you to navigate from a compilation
1729 error to the source file position corresponding to the error: click the
1730 middle mouse button (or simultaneously press the left and right buttons,
1731 on a two-button mouse) on the diagnostic line in the tool window. The
1732 focus will shift to the source window, and the cursor will be positioned
1733 on the character at which the error was detected.
1735 Correct the error: type in a semicolon to terminate the statement.
1736 Although you can again save the file explicitly, you can also simply invoke
1737 @code{Ada} @result{} @code{Build} and you will be prompted to save the file.
1738 This time the build will succeed; the tool output window shows you the
1739 options that are supplied by default. The GNAT tools' output (e.g.
1740 object and ALI files, executable) will go in the directory from which
1743 To execute the program, choose @code{Ada} and then @code{Run}.
1744 You should see the program's output displayed in the bottom window:
1754 @node Simple Debugging with GVD
1755 @subsection Simple Debugging with GVD
1758 This section describes how to set breakpoints, examine/modify variables,
1759 and step through execution.
1761 In order to enable debugging, you need to pass the @option{-g} switch
1762 to both the compiler and to @command{gnatlink}. If you are using
1763 the command line, passing @option{-g} to @command{gnatmake} will have
1764 this effect. You can then launch GVD, e.g. on the @code{hello} program,
1765 by issuing the command:
1772 If you are using Glide, then @option{-g} is passed to the relevant tools
1773 by default when you do a build. Start the debugger by selecting the
1774 @code{Ada} menu item, and then @code{Debug}.
1776 GVD comes up in a multi-part window. One pane shows the names of files
1777 comprising your executable; another pane shows the source code of the current
1778 unit (initially your main subprogram), another pane shows the debugger output
1779 and user interactions, and the fourth pane (the data canvas at the top
1780 of the window) displays data objects that you have selected.
1782 To the left of the source file pane, you will notice green dots adjacent
1783 to some lines. These are lines for which object code exists and where
1784 breakpoints can thus be set. You set/reset a breakpoint by clicking
1785 the green dot. When a breakpoint is set, the dot is replaced by an @code{X}
1786 in a red circle. Clicking the circle toggles the breakpoint off,
1787 and the red circle is replaced by the green dot.
1789 For this example, set a breakpoint at the statement where @code{Put_Line}
1792 Start program execution by selecting the @code{Run} button on the top menu bar.
1793 (The @code{Start} button will also start your program, but it will
1794 cause program execution to break at the entry to your main subprogram.)
1795 Evidence of reaching the breakpoint will appear: the source file line will be
1796 highlighted, and the debugger interactions pane will display
1799 You can examine the values of variables in several ways. Move the mouse
1800 over an occurrence of @code{Ind} in the @code{for} loop, and you will see
1801 the value (now @code{1}) displayed. Alternatively, right-click on @code{Ind}
1802 and select @code{Display Ind}; a box showing the variable's name and value
1803 will appear in the data canvas.
1805 Although a loop index is a constant with respect to Ada semantics,
1806 you can change its value in the debugger. Right-click in the box
1807 for @code{Ind}, and select the @code{Set Value of Ind} item.
1808 Enter @code{2} as the new value, and press @command{OK}.
1809 The box for @code{Ind} shows the update.
1811 Press the @code{Step} button on the top menu bar; this will step through
1812 one line of program text (the invocation of @code{Put_Line}), and you can
1813 observe the effect of having modified @code{Ind} since the value displayed
1816 Remove the breakpoint, and resume execution by selecting the @code{Cont}
1817 button. You will see the remaining output lines displayed in the debugger
1818 interaction window, along with a message confirming normal program
1821 @node Other Glide Features
1822 @subsection Other Glide Features
1825 You may have observed that some of the menu selections contain abbreviations;
1826 e.g., @code{(C-x C-f)} for @code{Open file...} in the @code{Files} menu.
1827 These are @emph{shortcut keys} that you can use instead of selecting
1828 menu items. The @key{C} stands for @key{Ctrl}; thus @code{(C-x C-f)} means
1829 @key{Ctrl-x} followed by @key{Ctrl-f}, and this sequence can be used instead
1830 of selecting @code{Files} and then @code{Open file...}.
1832 To abort a Glide command, type @key{Ctrl-g}.
1834 If you want Glide to start with an existing source file, you can either
1835 launch Glide as above and then open the file via @code{Files} @result{}
1836 @code{Open file...}, or else simply pass the name of the source file
1837 on the command line:
1844 While you are using Glide, a number of @emph{buffers} exist.
1845 You create some explicitly; e.g., when you open/create a file.
1846 Others arise as an effect of the commands that you issue; e.g., the buffer
1847 containing the output of the tools invoked during a build. If a buffer
1848 is hidden, you can bring it into a visible window by first opening
1849 the @code{Buffers} menu and then selecting the desired entry.
1851 If a buffer occupies only part of the Glide screen and you want to expand it
1852 to fill the entire screen, then click in the buffer and then select
1853 @code{Files} @result{} @code{One Window}.
1855 If a window is occupied by one buffer and you want to split the window
1856 to bring up a second buffer, perform the following steps:
1858 @item Select @code{Files} @result{} @code{Split Window};
1859 this will produce two windows each of which holds the original buffer
1860 (these are not copies, but rather different views of the same buffer contents)
1862 @item With the focus in one of the windows,
1863 select the desired buffer from the @code{Buffers} menu
1867 To exit from Glide, choose @code{Files} @result{} @code{Exit}.
1870 @node The GNAT Compilation Model
1871 @chapter The GNAT Compilation Model
1872 @cindex GNAT compilation model
1873 @cindex Compilation model
1876 * Source Representation::
1877 * Foreign Language Representation::
1878 * File Naming Rules::
1879 * Using Other File Names::
1880 * Alternative File Naming Schemes::
1881 * Generating Object Files::
1882 * Source Dependencies::
1883 * The Ada Library Information Files::
1884 * Binding an Ada Program::
1885 * Mixed Language Programming::
1886 * Building Mixed Ada & C++ Programs::
1887 * Comparison between GNAT and C/C++ Compilation Models::
1888 * Comparison between GNAT and Conventional Ada Library Models::
1890 * Placement of temporary files::
1895 This chapter describes the compilation model used by GNAT. Although
1896 similar to that used by other languages, such as C and C++, this model
1897 is substantially different from the traditional Ada compilation models,
1898 which are based on a library. The model is initially described without
1899 reference to the library-based model. If you have not previously used an
1900 Ada compiler, you need only read the first part of this chapter. The
1901 last section describes and discusses the differences between the GNAT
1902 model and the traditional Ada compiler models. If you have used other
1903 Ada compilers, this section will help you to understand those
1904 differences, and the advantages of the GNAT model.
1906 @node Source Representation
1907 @section Source Representation
1911 Ada source programs are represented in standard text files, using
1912 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1913 7-bit ASCII set, plus additional characters used for
1914 representing foreign languages (@pxref{Foreign Language Representation}
1915 for support of non-USA character sets). The format effector characters
1916 are represented using their standard ASCII encodings, as follows:
1921 Vertical tab, @code{16#0B#}
1925 Horizontal tab, @code{16#09#}
1929 Carriage return, @code{16#0D#}
1933 Line feed, @code{16#0A#}
1937 Form feed, @code{16#0C#}
1941 Source files are in standard text file format. In addition, GNAT will
1942 recognize a wide variety of stream formats, in which the end of physical
1943 physical lines is marked by any of the following sequences:
1944 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1945 in accommodating files that are imported from other operating systems.
1947 @cindex End of source file
1948 @cindex Source file, end
1950 The end of a source file is normally represented by the physical end of
1951 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1952 recognized as signalling the end of the source file. Again, this is
1953 provided for compatibility with other operating systems where this
1954 code is used to represent the end of file.
1956 Each file contains a single Ada compilation unit, including any pragmas
1957 associated with the unit. For example, this means you must place a
1958 package declaration (a package @dfn{spec}) and the corresponding body in
1959 separate files. An Ada @dfn{compilation} (which is a sequence of
1960 compilation units) is represented using a sequence of files. Similarly,
1961 you will place each subunit or child unit in a separate file.
1963 @node Foreign Language Representation
1964 @section Foreign Language Representation
1967 GNAT supports the standard character sets defined in Ada 95 as well as
1968 several other non-standard character sets for use in localized versions
1969 of the compiler (@pxref{Character Set Control}).
1972 * Other 8-Bit Codes::
1973 * Wide Character Encodings::
1981 The basic character set is Latin-1. This character set is defined by ISO
1982 standard 8859, part 1. The lower half (character codes @code{16#00#}
1983 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper half
1984 is used to represent additional characters. These include extended letters
1985 used by European languages, such as French accents, the vowels with umlauts
1986 used in German, and the extra letter A-ring used in Swedish.
1988 @findex Ada.Characters.Latin_1
1989 For a complete list of Latin-1 codes and their encodings, see the source
1990 file of library unit @code{Ada.Characters.Latin_1} in file
1991 @file{a-chlat1.ads}.
1992 You may use any of these extended characters freely in character or
1993 string literals. In addition, the extended characters that represent
1994 letters can be used in identifiers.
1996 @node Other 8-Bit Codes
1997 @subsection Other 8-Bit Codes
2000 GNAT also supports several other 8-bit coding schemes:
2003 @item ISO 8859-2 (Latin-2)
2006 Latin-2 letters allowed in identifiers, with uppercase and lowercase
2009 @item ISO 8859-3 (Latin-3)
2012 Latin-3 letters allowed in identifiers, with uppercase and lowercase
2015 @item ISO 8859-4 (Latin-4)
2018 Latin-4 letters allowed in identifiers, with uppercase and lowercase
2021 @item ISO 8859-5 (Cyrillic)
2024 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
2025 lowercase equivalence.
2027 @item ISO 8859-15 (Latin-9)
2030 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
2031 lowercase equivalence
2033 @item IBM PC (code page 437)
2034 @cindex code page 437
2035 This code page is the normal default for PCs in the U.S. It corresponds
2036 to the original IBM PC character set. This set has some, but not all, of
2037 the extended Latin-1 letters, but these letters do not have the same
2038 encoding as Latin-1. In this mode, these letters are allowed in
2039 identifiers with uppercase and lowercase equivalence.
2041 @item IBM PC (code page 850)
2042 @cindex code page 850
2043 This code page is a modification of 437 extended to include all the
2044 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
2045 mode, all these letters are allowed in identifiers with uppercase and
2046 lowercase equivalence.
2048 @item Full Upper 8-bit
2049 Any character in the range 80-FF allowed in identifiers, and all are
2050 considered distinct. In other words, there are no uppercase and lowercase
2051 equivalences in this range. This is useful in conjunction with
2052 certain encoding schemes used for some foreign character sets (e.g.
2053 the typical method of representing Chinese characters on the PC).
2056 No upper-half characters in the range 80-FF are allowed in identifiers.
2057 This gives Ada 83 compatibility for identifier names.
2061 For precise data on the encodings permitted, and the uppercase and lowercase
2062 equivalences that are recognized, see the file @file{csets.adb} in
2063 the GNAT compiler sources. You will need to obtain a full source release
2064 of GNAT to obtain this file.
2066 @node Wide Character Encodings
2067 @subsection Wide Character Encodings
2070 GNAT allows wide character codes to appear in character and string
2071 literals, and also optionally in identifiers, by means of the following
2072 possible encoding schemes:
2077 In this encoding, a wide character is represented by the following five
2085 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2086 characters (using uppercase letters) of the wide character code. For
2087 example, ESC A345 is used to represent the wide character with code
2089 This scheme is compatible with use of the full Wide_Character set.
2091 @item Upper-Half Coding
2092 @cindex Upper-Half Coding
2093 The wide character with encoding @code{16#abcd#} where the upper bit is on
2094 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
2095 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
2096 character, but is not required to be in the upper half. This method can
2097 be also used for shift-JIS or EUC, where the internal coding matches the
2100 @item Shift JIS Coding
2101 @cindex Shift JIS Coding
2102 A wide character is represented by a two-character sequence,
2104 @code{16#cd#}, with the restrictions described for upper-half encoding as
2105 described above. The internal character code is the corresponding JIS
2106 character according to the standard algorithm for Shift-JIS
2107 conversion. Only characters defined in the JIS code set table can be
2108 used with this encoding method.
2112 A wide character is represented by a two-character sequence
2114 @code{16#cd#}, with both characters being in the upper half. The internal
2115 character code is the corresponding JIS character according to the EUC
2116 encoding algorithm. Only characters defined in the JIS code set table
2117 can be used with this encoding method.
2120 A wide character is represented using
2121 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
2122 10646-1/Am.2. Depending on the character value, the representation
2123 is a one, two, or three byte sequence:
2128 16#0000#-16#007f#: 2#0xxxxxxx#
2129 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
2130 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
2135 where the xxx bits correspond to the left-padded bits of the
2136 16-bit character value. Note that all lower half ASCII characters
2137 are represented as ASCII bytes and all upper half characters and
2138 other wide characters are represented as sequences of upper-half
2139 (The full UTF-8 scheme allows for encoding 31-bit characters as
2140 6-byte sequences, but in this implementation, all UTF-8 sequences
2141 of four or more bytes length will be treated as illegal).
2142 @item Brackets Coding
2143 In this encoding, a wide character is represented by the following eight
2151 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
2152 characters (using uppercase letters) of the wide character code. For
2153 example, [``A345''] is used to represent the wide character with code
2154 @code{16#A345#}. It is also possible (though not required) to use the
2155 Brackets coding for upper half characters. For example, the code
2156 @code{16#A3#} can be represented as @code{[``A3'']}.
2158 This scheme is compatible with use of the full Wide_Character set,
2159 and is also the method used for wide character encoding in the standard
2160 ACVC (Ada Compiler Validation Capability) test suite distributions.
2165 Note: Some of these coding schemes do not permit the full use of the
2166 Ada 95 character set. For example, neither Shift JIS, nor EUC allow the
2167 use of the upper half of the Latin-1 set.
2169 @node File Naming Rules
2170 @section File Naming Rules
2173 The default file name is determined by the name of the unit that the
2174 file contains. The name is formed by taking the full expanded name of
2175 the unit and replacing the separating dots with hyphens and using
2176 ^lowercase^uppercase^ for all letters.
2178 An exception arises if the file name generated by the above rules starts
2179 with one of the characters
2186 and the second character is a
2187 minus. In this case, the character ^tilde^dollar sign^ is used in place
2188 of the minus. The reason for this special rule is to avoid clashes with
2189 the standard names for child units of the packages System, Ada,
2190 Interfaces, and GNAT, which use the prefixes
2199 The file extension is @file{.ads} for a spec and
2200 @file{.adb} for a body. The following list shows some
2201 examples of these rules.
2208 @item arith_functions.ads
2209 Arith_Functions (package spec)
2210 @item arith_functions.adb
2211 Arith_Functions (package body)
2213 Func.Spec (child package spec)
2215 Func.Spec (child package body)
2217 Sub (subunit of Main)
2218 @item ^a~bad.adb^A$BAD.ADB^
2219 A.Bad (child package body)
2223 Following these rules can result in excessively long
2224 file names if corresponding
2225 unit names are long (for example, if child units or subunits are
2226 heavily nested). An option is available to shorten such long file names
2227 (called file name ``krunching''). This may be particularly useful when
2228 programs being developed with GNAT are to be used on operating systems
2229 with limited file name lengths. @xref{Using gnatkr}.
2231 Of course, no file shortening algorithm can guarantee uniqueness over
2232 all possible unit names; if file name krunching is used, it is your
2233 responsibility to ensure no name clashes occur. Alternatively you
2234 can specify the exact file names that you want used, as described
2235 in the next section. Finally, if your Ada programs are migrating from a
2236 compiler with a different naming convention, you can use the gnatchop
2237 utility to produce source files that follow the GNAT naming conventions.
2238 (For details @pxref{Renaming Files Using gnatchop}.)
2240 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2241 systems, case is not significant. So for example on @code{Windows XP}
2242 if the canonical name is @code{main-sub.adb}, you can use the file name
2243 @code{Main-Sub.adb} instead. However, case is significant for other
2244 operating systems, so for example, if you want to use other than
2245 canonically cased file names on a Unix system, you need to follow
2246 the procedures described in the next section.
2248 @node Using Other File Names
2249 @section Using Other File Names
2253 In the previous section, we have described the default rules used by
2254 GNAT to determine the file name in which a given unit resides. It is
2255 often convenient to follow these default rules, and if you follow them,
2256 the compiler knows without being explicitly told where to find all
2259 However, in some cases, particularly when a program is imported from
2260 another Ada compiler environment, it may be more convenient for the
2261 programmer to specify which file names contain which units. GNAT allows
2262 arbitrary file names to be used by means of the Source_File_Name pragma.
2263 The form of this pragma is as shown in the following examples:
2264 @cindex Source_File_Name pragma
2266 @smallexample @c ada
2268 pragma Source_File_Name (My_Utilities.Stacks,
2269 Spec_File_Name => "myutilst_a.ada");
2270 pragma Source_File_name (My_Utilities.Stacks,
2271 Body_File_Name => "myutilst.ada");
2276 As shown in this example, the first argument for the pragma is the unit
2277 name (in this example a child unit). The second argument has the form
2278 of a named association. The identifier
2279 indicates whether the file name is for a spec or a body;
2280 the file name itself is given by a string literal.
2282 The source file name pragma is a configuration pragma, which means that
2283 normally it will be placed in the @file{gnat.adc}
2284 file used to hold configuration
2285 pragmas that apply to a complete compilation environment.
2286 For more details on how the @file{gnat.adc} file is created and used
2287 @pxref{Handling of Configuration Pragmas}
2288 @cindex @file{gnat.adc}
2291 GNAT allows completely arbitrary file names to be specified using the
2292 source file name pragma. However, if the file name specified has an
2293 extension other than @file{.ads} or @file{.adb} it is necessary to use
2294 a special syntax when compiling the file. The name in this case must be
2295 preceded by the special sequence @code{-x} followed by a space and the name
2296 of the language, here @code{ada}, as in:
2299 $ gcc -c -x ada peculiar_file_name.sim
2304 @code{gnatmake} handles non-standard file names in the usual manner (the
2305 non-standard file name for the main program is simply used as the
2306 argument to gnatmake). Note that if the extension is also non-standard,
2307 then it must be included in the gnatmake command, it may not be omitted.
2309 @node Alternative File Naming Schemes
2310 @section Alternative File Naming Schemes
2311 @cindex File naming schemes, alternative
2314 In the previous section, we described the use of the @code{Source_File_Name}
2315 pragma to allow arbitrary names to be assigned to individual source files.
2316 However, this approach requires one pragma for each file, and especially in
2317 large systems can result in very long @file{gnat.adc} files, and also create
2318 a maintenance problem.
2320 GNAT also provides a facility for specifying systematic file naming schemes
2321 other than the standard default naming scheme previously described. An
2322 alternative scheme for naming is specified by the use of
2323 @code{Source_File_Name} pragmas having the following format:
2324 @cindex Source_File_Name pragma
2326 @smallexample @c ada
2327 pragma Source_File_Name (
2328 Spec_File_Name => FILE_NAME_PATTERN
2329 [,Casing => CASING_SPEC]
2330 [,Dot_Replacement => STRING_LITERAL]);
2332 pragma Source_File_Name (
2333 Body_File_Name => FILE_NAME_PATTERN
2334 [,Casing => CASING_SPEC]
2335 [,Dot_Replacement => STRING_LITERAL]);
2337 pragma Source_File_Name (
2338 Subunit_File_Name => FILE_NAME_PATTERN
2339 [,Casing => CASING_SPEC]
2340 [,Dot_Replacement => STRING_LITERAL]);
2342 FILE_NAME_PATTERN ::= STRING_LITERAL
2343 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2347 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2348 It contains a single asterisk character, and the unit name is substituted
2349 systematically for this asterisk. The optional parameter
2350 @code{Casing} indicates
2351 whether the unit name is to be all upper-case letters, all lower-case letters,
2352 or mixed-case. If no
2353 @code{Casing} parameter is used, then the default is all
2354 ^lower-case^upper-case^.
2356 The optional @code{Dot_Replacement} string is used to replace any periods
2357 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2358 argument is used then separating dots appear unchanged in the resulting
2360 Although the above syntax indicates that the
2361 @code{Casing} argument must appear
2362 before the @code{Dot_Replacement} argument, but it
2363 is also permissible to write these arguments in the opposite order.
2365 As indicated, it is possible to specify different naming schemes for
2366 bodies, specs, and subunits. Quite often the rule for subunits is the
2367 same as the rule for bodies, in which case, there is no need to give
2368 a separate @code{Subunit_File_Name} rule, and in this case the
2369 @code{Body_File_name} rule is used for subunits as well.
2371 The separate rule for subunits can also be used to implement the rather
2372 unusual case of a compilation environment (e.g. a single directory) which
2373 contains a subunit and a child unit with the same unit name. Although
2374 both units cannot appear in the same partition, the Ada Reference Manual
2375 allows (but does not require) the possibility of the two units coexisting
2376 in the same environment.
2378 The file name translation works in the following steps:
2383 If there is a specific @code{Source_File_Name} pragma for the given unit,
2384 then this is always used, and any general pattern rules are ignored.
2387 If there is a pattern type @code{Source_File_Name} pragma that applies to
2388 the unit, then the resulting file name will be used if the file exists. If
2389 more than one pattern matches, the latest one will be tried first, and the
2390 first attempt resulting in a reference to a file that exists will be used.
2393 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2394 for which the corresponding file exists, then the standard GNAT default
2395 naming rules are used.
2400 As an example of the use of this mechanism, consider a commonly used scheme
2401 in which file names are all lower case, with separating periods copied
2402 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2403 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2406 @smallexample @c ada
2407 pragma Source_File_Name
2408 (Spec_File_Name => "*.1.ada");
2409 pragma Source_File_Name
2410 (Body_File_Name => "*.2.ada");
2414 The default GNAT scheme is actually implemented by providing the following
2415 default pragmas internally:
2417 @smallexample @c ada
2418 pragma Source_File_Name
2419 (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2420 pragma Source_File_Name
2421 (Body_File_Name => "*.adb", Dot_Replacement => "-");
2425 Our final example implements a scheme typically used with one of the
2426 Ada 83 compilers, where the separator character for subunits was ``__''
2427 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2428 by adding @file{.ADA}, and subunits by
2429 adding @file{.SEP}. All file names were
2430 upper case. Child units were not present of course since this was an
2431 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2432 the same double underscore separator for child units.
2434 @smallexample @c ada
2435 pragma Source_File_Name
2436 (Spec_File_Name => "*_.ADA",
2437 Dot_Replacement => "__",
2438 Casing = Uppercase);
2439 pragma Source_File_Name
2440 (Body_File_Name => "*.ADA",
2441 Dot_Replacement => "__",
2442 Casing = Uppercase);
2443 pragma Source_File_Name
2444 (Subunit_File_Name => "*.SEP",
2445 Dot_Replacement => "__",
2446 Casing = Uppercase);
2449 @node Generating Object Files
2450 @section Generating Object Files
2453 An Ada program consists of a set of source files, and the first step in
2454 compiling the program is to generate the corresponding object files.
2455 These are generated by compiling a subset of these source files.
2456 The files you need to compile are the following:
2460 If a package spec has no body, compile the package spec to produce the
2461 object file for the package.
2464 If a package has both a spec and a body, compile the body to produce the
2465 object file for the package. The source file for the package spec need
2466 not be compiled in this case because there is only one object file, which
2467 contains the code for both the spec and body of the package.
2470 For a subprogram, compile the subprogram body to produce the object file
2471 for the subprogram. The spec, if one is present, is as usual in a
2472 separate file, and need not be compiled.
2476 In the case of subunits, only compile the parent unit. A single object
2477 file is generated for the entire subunit tree, which includes all the
2481 Compile child units independently of their parent units
2482 (though, of course, the spec of all the ancestor unit must be present in order
2483 to compile a child unit).
2487 Compile generic units in the same manner as any other units. The object
2488 files in this case are small dummy files that contain at most the
2489 flag used for elaboration checking. This is because GNAT always handles generic
2490 instantiation by means of macro expansion. However, it is still necessary to
2491 compile generic units, for dependency checking and elaboration purposes.
2495 The preceding rules describe the set of files that must be compiled to
2496 generate the object files for a program. Each object file has the same
2497 name as the corresponding source file, except that the extension is
2500 You may wish to compile other files for the purpose of checking their
2501 syntactic and semantic correctness. For example, in the case where a
2502 package has a separate spec and body, you would not normally compile the
2503 spec. However, it is convenient in practice to compile the spec to make
2504 sure it is error-free before compiling clients of this spec, because such
2505 compilations will fail if there is an error in the spec.
2507 GNAT provides an option for compiling such files purely for the
2508 purposes of checking correctness; such compilations are not required as
2509 part of the process of building a program. To compile a file in this
2510 checking mode, use the @option{-gnatc} switch.
2512 @node Source Dependencies
2513 @section Source Dependencies
2516 A given object file clearly depends on the source file which is compiled
2517 to produce it. Here we are using @dfn{depends} in the sense of a typical
2518 @code{make} utility; in other words, an object file depends on a source
2519 file if changes to the source file require the object file to be
2521 In addition to this basic dependency, a given object may depend on
2522 additional source files as follows:
2526 If a file being compiled @code{with}'s a unit @var{X}, the object file
2527 depends on the file containing the spec of unit @var{X}. This includes
2528 files that are @code{with}'ed implicitly either because they are parents
2529 of @code{with}'ed child units or they are run-time units required by the
2530 language constructs used in a particular unit.
2533 If a file being compiled instantiates a library level generic unit, the
2534 object file depends on both the spec and body files for this generic
2538 If a file being compiled instantiates a generic unit defined within a
2539 package, the object file depends on the body file for the package as
2540 well as the spec file.
2544 @cindex @option{-gnatn} switch
2545 If a file being compiled contains a call to a subprogram for which
2546 pragma @code{Inline} applies and inlining is activated with the
2547 @option{-gnatn} switch, the object file depends on the file containing the
2548 body of this subprogram as well as on the file containing the spec. Note
2549 that for inlining to actually occur as a result of the use of this switch,
2550 it is necessary to compile in optimizing mode.
2552 @cindex @option{-gnatN} switch
2553 The use of @option{-gnatN} activates a more extensive inlining optimization
2554 that is performed by the front end of the compiler. This inlining does
2555 not require that the code generation be optimized. Like @option{-gnatn},
2556 the use of this switch generates additional dependencies.
2558 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
2559 to specify both options.
2562 If an object file O depends on the proper body of a subunit through inlining
2563 or instantiation, it depends on the parent unit of the subunit. This means that
2564 any modification of the parent unit or one of its subunits affects the
2568 The object file for a parent unit depends on all its subunit body files.
2571 The previous two rules meant that for purposes of computing dependencies and
2572 recompilation, a body and all its subunits are treated as an indivisible whole.
2575 These rules are applied transitively: if unit @code{A} @code{with}'s
2576 unit @code{B}, whose elaboration calls an inlined procedure in package
2577 @code{C}, the object file for unit @code{A} will depend on the body of
2578 @code{C}, in file @file{c.adb}.
2580 The set of dependent files described by these rules includes all the
2581 files on which the unit is semantically dependent, as described in the
2582 Ada 95 Language Reference Manual. However, it is a superset of what the
2583 ARM describes, because it includes generic, inline, and subunit dependencies.
2585 An object file must be recreated by recompiling the corresponding source
2586 file if any of the source files on which it depends are modified. For
2587 example, if the @code{make} utility is used to control compilation,
2588 the rule for an Ada object file must mention all the source files on
2589 which the object file depends, according to the above definition.
2590 The determination of the necessary
2591 recompilations is done automatically when one uses @code{gnatmake}.
2594 @node The Ada Library Information Files
2595 @section The Ada Library Information Files
2596 @cindex Ada Library Information files
2597 @cindex @file{ALI} files
2600 Each compilation actually generates two output files. The first of these
2601 is the normal object file that has a @file{.o} extension. The second is a
2602 text file containing full dependency information. It has the same
2603 name as the source file, but an @file{.ali} extension.
2604 This file is known as the Ada Library Information (@file{ALI}) file.
2605 The following information is contained in the @file{ALI} file.
2609 Version information (indicates which version of GNAT was used to compile
2610 the unit(s) in question)
2613 Main program information (including priority and time slice settings,
2614 as well as the wide character encoding used during compilation).
2617 List of arguments used in the @code{gcc} command for the compilation
2620 Attributes of the unit, including configuration pragmas used, an indication
2621 of whether the compilation was successful, exception model used etc.
2624 A list of relevant restrictions applying to the unit (used for consistency)
2628 Categorization information (e.g. use of pragma @code{Pure}).
2631 Information on all @code{with}'ed units, including presence of
2632 @code{Elaborate} or @code{Elaborate_All} pragmas.
2635 Information from any @code{Linker_Options} pragmas used in the unit
2638 Information on the use of @code{Body_Version} or @code{Version}
2639 attributes in the unit.
2642 Dependency information. This is a list of files, together with
2643 time stamp and checksum information. These are files on which
2644 the unit depends in the sense that recompilation is required
2645 if any of these units are modified.
2648 Cross-reference data. Contains information on all entities referenced
2649 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2650 provide cross-reference information.
2655 For a full detailed description of the format of the @file{ALI} file,
2656 see the source of the body of unit @code{Lib.Writ}, contained in file
2657 @file{lib-writ.adb} in the GNAT compiler sources.
2659 @node Binding an Ada Program
2660 @section Binding an Ada Program
2663 When using languages such as C and C++, once the source files have been
2664 compiled the only remaining step in building an executable program
2665 is linking the object modules together. This means that it is possible to
2666 link an inconsistent version of a program, in which two units have
2667 included different versions of the same header.
2669 The rules of Ada do not permit such an inconsistent program to be built.
2670 For example, if two clients have different versions of the same package,
2671 it is illegal to build a program containing these two clients.
2672 These rules are enforced by the GNAT binder, which also determines an
2673 elaboration order consistent with the Ada rules.
2675 The GNAT binder is run after all the object files for a program have
2676 been created. It is given the name of the main program unit, and from
2677 this it determines the set of units required by the program, by reading the
2678 corresponding ALI files. It generates error messages if the program is
2679 inconsistent or if no valid order of elaboration exists.
2681 If no errors are detected, the binder produces a main program, in Ada by
2682 default, that contains calls to the elaboration procedures of those
2683 compilation unit that require them, followed by
2684 a call to the main program. This Ada program is compiled to generate the
2685 object file for the main program. The name of
2686 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2687 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2690 Finally, the linker is used to build the resulting executable program,
2691 using the object from the main program from the bind step as well as the
2692 object files for the Ada units of the program.
2694 @node Mixed Language Programming
2695 @section Mixed Language Programming
2696 @cindex Mixed Language Programming
2699 This section describes how to develop a mixed-language program,
2700 specifically one that comprises units in both Ada and C.
2703 * Interfacing to C::
2704 * Calling Conventions::
2707 @node Interfacing to C
2708 @subsection Interfacing to C
2710 Interfacing Ada with a foreign language such as C involves using
2711 compiler directives to import and/or export entity definitions in each
2712 language---using @code{extern} statements in C, for instance, and the
2713 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada. For
2714 a full treatment of these topics, read Appendix B, section 1 of the Ada
2715 95 Language Reference Manual.
2717 There are two ways to build a program using GNAT that contains some Ada
2718 sources and some foreign language sources, depending on whether or not
2719 the main subprogram is written in Ada. Here is a source example with
2720 the main subprogram in Ada:
2726 void print_num (int num)
2728 printf ("num is %d.\n", num);
2734 /* num_from_Ada is declared in my_main.adb */
2735 extern int num_from_Ada;
2739 return num_from_Ada;
2743 @smallexample @c ada
2745 procedure My_Main is
2747 -- Declare then export an Integer entity called num_from_Ada
2748 My_Num : Integer := 10;
2749 pragma Export (C, My_Num, "num_from_Ada");
2751 -- Declare an Ada function spec for Get_Num, then use
2752 -- C function get_num for the implementation.
2753 function Get_Num return Integer;
2754 pragma Import (C, Get_Num, "get_num");
2756 -- Declare an Ada procedure spec for Print_Num, then use
2757 -- C function print_num for the implementation.
2758 procedure Print_Num (Num : Integer);
2759 pragma Import (C, Print_Num, "print_num");
2762 Print_Num (Get_Num);
2768 To build this example, first compile the foreign language files to
2769 generate object files:
2776 Then, compile the Ada units to produce a set of object files and ALI
2779 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2783 Run the Ada binder on the Ada main program:
2785 gnatbind my_main.ali
2789 Link the Ada main program, the Ada objects and the other language
2792 gnatlink my_main.ali file1.o file2.o
2796 The last three steps can be grouped in a single command:
2798 gnatmake my_main.adb -largs file1.o file2.o
2801 @cindex Binder output file
2803 If the main program is in a language other than Ada, then you may have
2804 more than one entry point into the Ada subsystem. You must use a special
2805 binder option to generate callable routines that initialize and
2806 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2807 Calls to the initialization and finalization routines must be inserted
2808 in the main program, or some other appropriate point in the code. The
2809 call to initialize the Ada units must occur before the first Ada
2810 subprogram is called, and the call to finalize the Ada units must occur
2811 after the last Ada subprogram returns. The binder will place the
2812 initialization and finalization subprograms into the
2813 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2814 sources. To illustrate, we have the following example:
2818 extern void adainit (void);
2819 extern void adafinal (void);
2820 extern int add (int, int);
2821 extern int sub (int, int);
2823 int main (int argc, char *argv[])
2829 /* Should print "21 + 7 = 28" */
2830 printf ("%d + %d = %d\n", a, b, add (a, b));
2831 /* Should print "21 - 7 = 14" */
2832 printf ("%d - %d = %d\n", a, b, sub (a, b));
2838 @smallexample @c ada
2841 function Add (A, B : Integer) return Integer;
2842 pragma Export (C, Add, "add");
2846 package body Unit1 is
2847 function Add (A, B : Integer) return Integer is
2855 function Sub (A, B : Integer) return Integer;
2856 pragma Export (C, Sub, "sub");
2860 package body Unit2 is
2861 function Sub (A, B : Integer) return Integer is
2870 The build procedure for this application is similar to the last
2871 example's. First, compile the foreign language files to generate object
2878 Next, compile the Ada units to produce a set of object files and ALI
2881 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2882 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2886 Run the Ada binder on every generated ALI file. Make sure to use the
2887 @option{-n} option to specify a foreign main program:
2889 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2893 Link the Ada main program, the Ada objects and the foreign language
2894 objects. You need only list the last ALI file here:
2896 gnatlink unit2.ali main.o -o exec_file
2899 This procedure yields a binary executable called @file{exec_file}.
2902 @node Calling Conventions
2903 @subsection Calling Conventions
2904 @cindex Foreign Languages
2905 @cindex Calling Conventions
2906 GNAT follows standard calling sequence conventions and will thus interface
2907 to any other language that also follows these conventions. The following
2908 Convention identifiers are recognized by GNAT:
2911 @cindex Interfacing to Ada
2912 @cindex Other Ada compilers
2913 @cindex Convention Ada
2915 This indicates that the standard Ada calling sequence will be
2916 used and all Ada data items may be passed without any limitations in the
2917 case where GNAT is used to generate both the caller and callee. It is also
2918 possible to mix GNAT generated code and code generated by another Ada
2919 compiler. In this case, the data types should be restricted to simple
2920 cases, including primitive types. Whether complex data types can be passed
2921 depends on the situation. Probably it is safe to pass simple arrays, such
2922 as arrays of integers or floats. Records may or may not work, depending
2923 on whether both compilers lay them out identically. Complex structures
2924 involving variant records, access parameters, tasks, or protected types,
2925 are unlikely to be able to be passed.
2927 Note that in the case of GNAT running
2928 on a platform that supports DEC Ada 83, a higher degree of compatibility
2929 can be guaranteed, and in particular records are layed out in an identical
2930 manner in the two compilers. Note also that if output from two different
2931 compilers is mixed, the program is responsible for dealing with elaboration
2932 issues. Probably the safest approach is to write the main program in the
2933 version of Ada other than GNAT, so that it takes care of its own elaboration
2934 requirements, and then call the GNAT-generated adainit procedure to ensure
2935 elaboration of the GNAT components. Consult the documentation of the other
2936 Ada compiler for further details on elaboration.
2938 However, it is not possible to mix the tasking run time of GNAT and
2939 DEC Ada 83, All the tasking operations must either be entirely within
2940 GNAT compiled sections of the program, or entirely within DEC Ada 83
2941 compiled sections of the program.
2943 @cindex Interfacing to Assembly
2944 @cindex Convention Assembler
2946 Specifies assembler as the convention. In practice this has the
2947 same effect as convention Ada (but is not equivalent in the sense of being
2948 considered the same convention).
2950 @cindex Convention Asm
2953 Equivalent to Assembler.
2955 @cindex Interfacing to COBOL
2956 @cindex Convention COBOL
2959 Data will be passed according to the conventions described
2960 in section B.4 of the Ada 95 Reference Manual.
2963 @cindex Interfacing to C
2964 @cindex Convention C
2966 Data will be passed according to the conventions described
2967 in section B.3 of the Ada 95 Reference Manual.
2969 @findex C varargs function
2970 @cindex Intefacing to C varargs function
2971 @cindex varargs function intefacs
2972 @item C varargs function
2973 In C, @code{varargs} allows a function to take a variable number of
2974 arguments. There is no direct equivalent in this to Ada. One
2975 approach that can be used is to create a C wrapper for each
2976 different profile and then interface to this C wrapper. For
2977 example, to print an @code{int} value using @code{printf},
2978 create a C function @code{printfi} that takes two arguments, a
2979 pointer to a string and an int, and calls @code{printf}.
2980 Then in the Ada program, use pragma @code{Import} to
2981 interface to printfi.
2983 It may work on some platforms to directly interface to
2984 a @code{varargs} function by providing a specific Ada profile
2985 for a a particular call. However, this does not work on
2986 all platforms, since there is no guarantee that the
2987 calling sequence for a two argument normal C function
2988 is the same as for calling a @code{varargs} C function with
2989 the same two arguments.
2991 @cindex Convention Default
2996 @cindex Convention External
3002 @cindex Interfacing to C++
3003 @cindex Convention C++
3005 This stands for C++. For most purposes this is identical to C.
3006 See the separate description of the specialized GNAT pragmas relating to
3007 C++ interfacing for further details.
3010 @cindex Interfacing to Fortran
3011 @cindex Convention Fortran
3013 Data will be passed according to the conventions described
3014 in section B.5 of the Ada 95 Reference Manual.
3017 This applies to an intrinsic operation, as defined in the Ada 95
3018 Reference Manual. If a a pragma Import (Intrinsic) applies to a subprogram,
3019 this means that the body of the subprogram is provided by the compiler itself,
3020 usually by means of an efficient code sequence, and that the user does not
3021 supply an explicit body for it. In an application program, the pragma can
3022 only be applied to the following two sets of names, which the GNAT compiler
3027 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_-
3028 Arithmetic. The corresponding subprogram declaration must have
3029 two formal parameters. The
3030 first one must be a signed integer type or a modular type with a binary
3031 modulus, and the second parameter must be of type Natural.
3032 The return type must be the same as the type of the first argument. The size
3033 of this type can only be 8, 16, 32, or 64.
3034 @item binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
3035 The corresponding operator declaration must have parameters and result type
3036 that have the same root numeric type (for example, all three are long_float
3037 types). This simplifies the definition of operations that use type checking
3038 to perform dimensional checks:
3040 @smallexample @c ada
3041 type Distance is new Long_Float;
3042 type Time is new Long_Float;
3043 type Velocity is new Long_Float;
3044 function "/" (D : Distance; T : Time)
3046 pragma Import (Intrinsic, "/");
3050 This common idiom is often programmed with a generic definition and an
3051 explicit body. The pragma makes it simpler to introduce such declarations.
3052 It incurs no overhead in compilation time or code size, because it is
3053 implemented as a single machine instruction.
3059 @cindex Convention Stdcall
3061 This is relevant only to NT/Win95 implementations of GNAT,
3062 and specifies that the Stdcall calling sequence will be used, as defined
3066 @cindex Convention DLL
3068 This is equivalent to Stdcall.
3071 @cindex Convention Win32
3073 This is equivalent to Stdcall.
3077 @cindex Convention Stubbed
3079 This is a special convention that indicates that the compiler
3080 should provide a stub body that raises @code{Program_Error}.
3084 GNAT additionally provides a useful pragma @code{Convention_Identifier}
3085 that can be used to parametrize conventions and allow additional synonyms
3086 to be specified. For example if you have legacy code in which the convention
3087 identifier Fortran77 was used for Fortran, you can use the configuration
3090 @smallexample @c ada
3091 pragma Convention_Identifier (Fortran77, Fortran);
3095 And from now on the identifier Fortran77 may be used as a convention
3096 identifier (for example in an @code{Import} pragma) with the same
3099 @node Building Mixed Ada & C++ Programs
3100 @section Building Mixed Ada & C++ Programs
3103 A programmer inexperienced with mixed-language development may find that
3104 building an application containing both Ada and C++ code can be a
3105 challenge. As a matter of fact, interfacing with C++ has not been
3106 standardized in the Ada 95 Reference Manual due to the immaturity of --
3107 and lack of standards for -- C++ at the time. This section gives a few
3108 hints that should make this task easier. The first section addresses
3109 the differences regarding interfacing with C. The second section
3110 looks into the delicate problem of linking the complete application from
3111 its Ada and C++ parts. The last section gives some hints on how the GNAT
3112 run time can be adapted in order to allow inter-language dispatching
3113 with a new C++ compiler.
3116 * Interfacing to C++::
3117 * Linking a Mixed C++ & Ada Program::
3118 * A Simple Example::
3119 * Adapting the Run Time to a New C++ Compiler::
3122 @node Interfacing to C++
3123 @subsection Interfacing to C++
3126 GNAT supports interfacing with C++ compilers generating code that is
3127 compatible with the standard Application Binary Interface of the given
3131 Interfacing can be done at 3 levels: simple data, subprograms, and
3132 classes. In the first two cases, GNAT offers a specific @var{Convention
3133 CPP} that behaves exactly like @var{Convention C}. Usually, C++ mangles
3134 the names of subprograms, and currently, GNAT does not provide any help
3135 to solve the demangling problem. This problem can be addressed in two
3139 by modifying the C++ code in order to force a C convention using
3140 the @code{extern "C"} syntax.
3143 by figuring out the mangled name and use it as the Link_Name argument of
3148 Interfacing at the class level can be achieved by using the GNAT specific
3149 pragmas such as @code{CPP_Class} and @code{CPP_Virtual}. See the GNAT
3150 Reference Manual for additional information.
3152 @node Linking a Mixed C++ & Ada Program
3153 @subsection Linking a Mixed C++ & Ada Program
3156 Usually the linker of the C++ development system must be used to link
3157 mixed applications because most C++ systems will resolve elaboration
3158 issues (such as calling constructors on global class instances)
3159 transparently during the link phase. GNAT has been adapted to ease the
3160 use of a foreign linker for the last phase. Three cases can be
3165 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3166 The C++ linker can simply be called by using the C++ specific driver
3167 called @code{c++}. Note that this setup is not very common because it
3168 may involve recompiling the whole GCC tree from sources, which makes it
3169 harder to upgrade the compilation system for one language without
3170 destabilizing the other.
3175 $ gnatmake ada_unit -largs file1.o file2.o --LINK=c++
3179 Using GNAT and G++ from two different GCC installations: If both
3180 compilers are on the PATH, the previous method may be used. It is
3181 important to note that environment variables such as C_INCLUDE_PATH,
3182 GCC_EXEC_PREFIX, BINUTILS_ROOT, and GCC_ROOT will affect both compilers
3183 at the same time and may make one of the two compilers operate
3184 improperly if set during invocation of the wrong compiler. It is also
3185 very important that the linker uses the proper @file{libgcc.a} GCC
3186 library -- that is, the one from the C++ compiler installation. The
3187 implicit link command as suggested in the gnatmake command from the
3188 former example can be replaced by an explicit link command with the
3189 full-verbosity option in order to verify which library is used:
3192 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3194 If there is a problem due to interfering environment variables, it can
3195 be worked around by using an intermediate script. The following example
3196 shows the proper script to use when GNAT has not been installed at its
3197 default location and g++ has been installed at its default location:
3205 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3209 Using a non-GNU C++ compiler: The commands previously described can be
3210 used to insure that the C++ linker is used. Nonetheless, you need to add
3211 the path to libgcc explicitly, since some libraries needed by GNAT are
3212 located in this directory:
3217 CC $* `gcc -print-libgcc-file-name`
3218 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3221 Where CC is the name of the non-GNU C++ compiler.
3225 @node A Simple Example
3226 @subsection A Simple Example
3228 The following example, provided as part of the GNAT examples, shows how
3229 to achieve procedural interfacing between Ada and C++ in both
3230 directions. The C++ class A has two methods. The first method is exported
3231 to Ada by the means of an extern C wrapper function. The second method
3232 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3233 a limited record with a layout comparable to the C++ class. The Ada
3234 subprogram, in turn, calls the C++ method. So, starting from the C++
3235 main program, the process passes back and forth between the two
3239 Here are the compilation commands:
3241 $ gnatmake -c simple_cpp_interface
3244 $ gnatbind -n simple_cpp_interface
3245 $ gnatlink simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
3246 -lstdc++ ex7.o cpp_main.o
3250 Here are the corresponding sources:
3258 void adainit (void);
3259 void adafinal (void);
3260 void method1 (A *t);
3282 class A : public Origin @{
3284 void method1 (void);
3285 virtual void method2 (int v);
3295 extern "C" @{ void ada_method2 (A *t, int v);@}
3297 void A::method1 (void)
3300 printf ("in A::method1, a_value = %d \n",a_value);
3304 void A::method2 (int v)
3306 ada_method2 (this, v);
3307 printf ("in A::method2, a_value = %d \n",a_value);
3314 printf ("in A::A, a_value = %d \n",a_value);
3318 @b{package} @b{body} Simple_Cpp_Interface @b{is}
3320 @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer) @b{is}
3324 @b{end} Ada_Method2;
3326 @b{end} Simple_Cpp_Interface;
3328 @b{package} Simple_Cpp_Interface @b{is}
3329 @b{type} A @b{is} @b{limited}
3334 @b{pragma} Convention (C, A);
3336 @b{procedure} Method1 (This : @b{in} @b{out} A);
3337 @b{pragma} Import (C, Method1);
3339 @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer);
3340 @b{pragma} Export (C, Ada_Method2);
3342 @b{end} Simple_Cpp_Interface;
3345 @node Adapting the Run Time to a New C++ Compiler
3346 @subsection Adapting the Run Time to a New C++ Compiler
3348 GNAT offers the capability to derive Ada 95 tagged types directly from
3349 preexisting C++ classes and . See ``Interfacing with C++'' in the
3350 @cite{GNAT Reference Manual}. The mechanism used by GNAT for achieving
3352 has been made user configurable through a GNAT library unit
3353 @code{Interfaces.CPP}. The default version of this file is adapted to
3354 the GNU C++ compiler. Internal knowledge of the virtual
3355 table layout used by the new C++ compiler is needed to configure
3356 properly this unit. The Interface of this unit is known by the compiler
3357 and cannot be changed except for the value of the constants defining the
3358 characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size,
3359 CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source
3360 of this unit for more details.
3362 @node Comparison between GNAT and C/C++ Compilation Models
3363 @section Comparison between GNAT and C/C++ Compilation Models
3366 The GNAT model of compilation is close to the C and C++ models. You can
3367 think of Ada specs as corresponding to header files in C. As in C, you
3368 don't need to compile specs; they are compiled when they are used. The
3369 Ada @code{with} is similar in effect to the @code{#include} of a C
3372 One notable difference is that, in Ada, you may compile specs separately
3373 to check them for semantic and syntactic accuracy. This is not always
3374 possible with C headers because they are fragments of programs that have
3375 less specific syntactic or semantic rules.
3377 The other major difference is the requirement for running the binder,
3378 which performs two important functions. First, it checks for
3379 consistency. In C or C++, the only defense against assembling
3380 inconsistent programs lies outside the compiler, in a makefile, for
3381 example. The binder satisfies the Ada requirement that it be impossible
3382 to construct an inconsistent program when the compiler is used in normal
3385 @cindex Elaboration order control
3386 The other important function of the binder is to deal with elaboration
3387 issues. There are also elaboration issues in C++ that are handled
3388 automatically. This automatic handling has the advantage of being
3389 simpler to use, but the C++ programmer has no control over elaboration.
3390 Where @code{gnatbind} might complain there was no valid order of
3391 elaboration, a C++ compiler would simply construct a program that
3392 malfunctioned at run time.
3394 @node Comparison between GNAT and Conventional Ada Library Models
3395 @section Comparison between GNAT and Conventional Ada Library Models
3398 This section is intended to be useful to Ada programmers who have
3399 previously used an Ada compiler implementing the traditional Ada library
3400 model, as described in the Ada 95 Language Reference Manual. If you
3401 have not used such a system, please go on to the next section.
3403 @cindex GNAT library
3404 In GNAT, there is no @dfn{library} in the normal sense. Instead, the set of
3405 source files themselves acts as the library. Compiling Ada programs does
3406 not generate any centralized information, but rather an object file and
3407 a ALI file, which are of interest only to the binder and linker.
3408 In a traditional system, the compiler reads information not only from
3409 the source file being compiled, but also from the centralized library.
3410 This means that the effect of a compilation depends on what has been
3411 previously compiled. In particular:
3415 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3416 to the version of the unit most recently compiled into the library.
3419 Inlining is effective only if the necessary body has already been
3420 compiled into the library.
3423 Compiling a unit may obsolete other units in the library.
3427 In GNAT, compiling one unit never affects the compilation of any other
3428 units because the compiler reads only source files. Only changes to source
3429 files can affect the results of a compilation. In particular:
3433 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3434 to the source version of the unit that is currently accessible to the
3439 Inlining requires the appropriate source files for the package or
3440 subprogram bodies to be available to the compiler. Inlining is always
3441 effective, independent of the order in which units are complied.
3444 Compiling a unit never affects any other compilations. The editing of
3445 sources may cause previous compilations to be out of date if they
3446 depended on the source file being modified.
3450 The most important result of these differences is that order of compilation
3451 is never significant in GNAT. There is no situation in which one is
3452 required to do one compilation before another. What shows up as order of
3453 compilation requirements in the traditional Ada library becomes, in
3454 GNAT, simple source dependencies; in other words, there is only a set
3455 of rules saying what source files must be present when a file is
3459 @node Placement of temporary files
3460 @section Placement of temporary files
3461 @cindex Temporary files (user control over placement)
3464 GNAT creates temporary files in the directory designated by the environment
3465 variable @env{TMPDIR}.
3466 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3467 for detailed information on how environment variables are resolved.
3468 For most users the easiest way to make use of this feature is to simply
3469 define @env{TMPDIR} as a job level logical name).
3470 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3471 for compiler temporary files, then you can include something like the
3472 following command in your @file{LOGIN.COM} file:
3475 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3479 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3480 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3481 designated by @env{TEMP}.
3482 If none of these environment variables are defined then GNAT uses the
3483 directory designated by the logical name @code{SYS$SCRATCH:}
3484 (by default the user's home directory). If all else fails
3485 GNAT uses the current directory for temporary files.
3489 @c *************************
3490 @node Compiling Using gcc
3491 @chapter Compiling Using @code{gcc}
3494 This chapter discusses how to compile Ada programs using the @code{gcc}
3495 command. It also describes the set of switches
3496 that can be used to control the behavior of the compiler.
3498 * Compiling Programs::
3499 * Switches for gcc::
3500 * Search Paths and the Run-Time Library (RTL)::
3501 * Order of Compilation Issues::
3505 @node Compiling Programs
3506 @section Compiling Programs
3509 The first step in creating an executable program is to compile the units
3510 of the program using the @code{gcc} command. You must compile the
3515 the body file (@file{.adb}) for a library level subprogram or generic
3519 the spec file (@file{.ads}) for a library level package or generic
3520 package that has no body
3523 the body file (@file{.adb}) for a library level package
3524 or generic package that has a body
3529 You need @emph{not} compile the following files
3534 the spec of a library unit which has a body
3541 because they are compiled as part of compiling related units. GNAT
3543 when the corresponding body is compiled, and subunits when the parent is
3546 @cindex cannot generate code
3547 If you attempt to compile any of these files, you will get one of the
3548 following error messages (where fff is the name of the file you compiled):
3551 cannot generate code for file @var{fff} (package spec)
3552 to check package spec, use -gnatc
3554 cannot generate code for file @var{fff} (missing subunits)
3555 to check parent unit, use -gnatc
3557 cannot generate code for file @var{fff} (subprogram spec)
3558 to check subprogram spec, use -gnatc
3560 cannot generate code for file @var{fff} (subunit)
3561 to check subunit, use -gnatc
3565 As indicated by the above error messages, if you want to submit
3566 one of these files to the compiler to check for correct semantics
3567 without generating code, then use the @option{-gnatc} switch.
3569 The basic command for compiling a file containing an Ada unit is
3572 $ gcc -c [@var{switches}] @file{file name}
3576 where @var{file name} is the name of the Ada file (usually
3578 @file{.ads} for a spec or @file{.adb} for a body).
3581 @option{-c} switch to tell @code{gcc} to compile, but not link, the file.
3583 The result of a successful compilation is an object file, which has the
3584 same name as the source file but an extension of @file{.o} and an Ada
3585 Library Information (ALI) file, which also has the same name as the
3586 source file, but with @file{.ali} as the extension. GNAT creates these
3587 two output files in the current directory, but you may specify a source
3588 file in any directory using an absolute or relative path specification
3589 containing the directory information.
3592 @code{gcc} is actually a driver program that looks at the extensions of
3593 the file arguments and loads the appropriate compiler. For example, the
3594 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3595 These programs are in directories known to the driver program (in some
3596 configurations via environment variables you set), but need not be in
3597 your path. The @code{gcc} driver also calls the assembler and any other
3598 utilities needed to complete the generation of the required object
3601 It is possible to supply several file names on the same @code{gcc}
3602 command. This causes @code{gcc} to call the appropriate compiler for
3603 each file. For example, the following command lists three separate
3604 files to be compiled:
3607 $ gcc -c x.adb y.adb z.c
3611 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3612 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3613 The compiler generates three object files @file{x.o}, @file{y.o} and
3614 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3615 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3618 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3621 @node Switches for gcc
3622 @section Switches for @code{gcc}
3625 The @code{gcc} command accepts switches that control the
3626 compilation process. These switches are fully described in this section.
3627 First we briefly list all the switches, in alphabetical order, then we
3628 describe the switches in more detail in functionally grouped sections.
3631 * Output and Error Message Control::
3632 * Warning Message Control::
3633 * Debugging and Assertion Control::
3635 * Stack Overflow Checking::
3636 * Validity Checking::
3638 * Using gcc for Syntax Checking::
3639 * Using gcc for Semantic Checking::
3640 * Compiling Ada 83 Programs::
3641 * Character Set Control::
3642 * File Naming Control::
3643 * Subprogram Inlining Control::
3644 * Auxiliary Output Control::
3645 * Debugging Control::
3646 * Exception Handling Control::
3647 * Units to Sources Mapping Files::
3648 * Integrated Preprocessing::
3657 @cindex @option{-b} (@code{gcc})
3658 @item -b @var{target}
3659 Compile your program to run on @var{target}, which is the name of a
3660 system configuration. You must have a GNAT cross-compiler built if
3661 @var{target} is not the same as your host system.
3664 @cindex @option{-B} (@code{gcc})
3665 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3666 from @var{dir} instead of the default location. Only use this switch
3667 when multiple versions of the GNAT compiler are available. See the
3668 @code{gcc} manual page for further details. You would normally use the
3669 @option{-b} or @option{-V} switch instead.
3672 @cindex @option{-c} (@code{gcc})
3673 Compile. Always use this switch when compiling Ada programs.
3675 Note: for some other languages when using @code{gcc}, notably in
3676 the case of C and C++, it is possible to use
3677 use @code{gcc} without a @option{-c} switch to
3678 compile and link in one step. In the case of GNAT, you
3679 cannot use this approach, because the binder must be run
3680 and @code{gcc} cannot be used to run the GNAT binder.
3684 @cindex @option{-fno-inline} (@code{gcc})
3685 Suppresses all back-end inlining, even if other optimization or inlining
3687 This includes suppression of inlining that results
3688 from the use of the pragma @code{Inline_Always}.
3689 See also @option{-gnatn} and @option{-gnatN}.
3691 @item -fno-strict-aliasing
3692 @cindex @option{-fno-strict-aliasing} (@code{gcc})
3693 Causes the compiler to avoid assumptions regarding non-aliasing
3694 of objects of different types. See section
3695 @pxref{Optimization and Strict Aliasing} for details.
3698 @cindex @option{-fstack-check} (@code{gcc})
3699 Activates stack checking.
3700 See @ref{Stack Overflow Checking} for details of the use of this option.
3703 @cindex @option{^-g^/DEBUG^} (@code{gcc})
3704 Generate debugging information. This information is stored in the object
3705 file and copied from there to the final executable file by the linker,
3706 where it can be read by the debugger. You must use the
3707 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
3710 @cindex @option{-gnat83} (@code{gcc})
3711 Enforce Ada 83 restrictions.
3714 @cindex @option{-gnata} (@code{gcc})
3715 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
3719 @cindex @option{-gnatA} (@code{gcc})
3720 Avoid processing @file{gnat.adc}. If a gnat.adc file is present,
3724 @cindex @option{-gnatb} (@code{gcc})
3725 Generate brief messages to @file{stderr} even if verbose mode set.
3728 @cindex @option{-gnatc} (@code{gcc})
3729 Check syntax and semantics only (no code generation attempted).
3732 @cindex @option{-gnatd} (@code{gcc})
3733 Specify debug options for the compiler. The string of characters after
3734 the @option{-gnatd} specify the specific debug options. The possible
3735 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
3736 compiler source file @file{debug.adb} for details of the implemented
3737 debug options. Certain debug options are relevant to applications
3738 programmers, and these are documented at appropriate points in this
3742 @cindex @option{-gnatD} (@code{gcc})
3743 Create expanded source files for source level debugging. This switch
3744 also suppress generation of cross-reference information
3745 (see @option{-gnatx}).
3747 @item -gnatec=@var{path}
3748 @cindex @option{-gnatec} (@code{gcc})
3749 Specify a configuration pragma file
3751 (the equal sign is optional)
3753 (see @ref{The Configuration Pragmas Files}).
3755 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
3756 @cindex @option{-gnateD} (@code{gcc})
3757 Defines a symbol, associated with value, for preprocessing.
3758 (see @ref{Integrated Preprocessing})
3761 @cindex @option{-gnatef} (@code{gcc})
3762 Display full source path name in brief error messages.
3764 @item -gnatem=@var{path}
3765 @cindex @option{-gnatem} (@code{gcc})
3766 Specify a mapping file
3768 (the equal sign is optional)
3770 (see @ref{Units to Sources Mapping Files}).
3772 @item -gnatep=@var{file}
3773 @cindex @option{-gnatep} (@code{gcc})
3774 Specify a preprocessing data file
3776 (the equal sign is optional)
3778 (see @ref{Integrated Preprocessing}).
3781 @cindex @option{-gnatE} (@code{gcc})
3782 Full dynamic elaboration checks.
3785 @cindex @option{-gnatf} (@code{gcc})
3786 Full errors. Multiple errors per line, all undefined references, do not
3787 attempt to suppress cascaded errors.
3790 @cindex @option{-gnatF} (@code{gcc})
3791 Externals names are folded to all uppercase.
3794 @cindex @option{-gnatg} (@code{gcc})
3795 Internal GNAT implementation mode. This should not be used for
3796 applications programs, it is intended only for use by the compiler
3797 and its run-time library. For documentation, see the GNAT sources.
3798 Note that @option{-gnatg} implies @option{-gnatwu} so that warnings
3799 are generated on unreferenced entities, and all warnings are treated
3803 @cindex @option{-gnatG} (@code{gcc})
3804 List generated expanded code in source form.
3806 @item ^-gnath^/HELP^
3807 @cindex @option{^-gnath^/HELP^} (@code{gcc})
3808 Output usage information. The output is written to @file{stdout}.
3810 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
3811 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@code{gcc})
3812 Identifier character set
3814 (@var{c}=1/2/3/4/8/9/p/f/n/w).
3817 For details of the possible selections for @var{c},
3818 see @xref{Character Set Control}.
3821 @item -gnatk=@var{n}
3822 @cindex @option{-gnatk} (@code{gcc})
3823 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
3826 @cindex @option{-gnatl} (@code{gcc})
3827 Output full source listing with embedded error messages.
3830 @cindex @option{-gnatL} (@code{gcc})
3831 Use the longjmp/setjmp method for exception handling
3833 @item -gnatm=@var{n}
3834 @cindex @option{-gnatm} (@code{gcc})
3835 Limit number of detected error or warning messages to @var{n}
3836 where @var{n} is in the range 1..999_999. The default setting if
3837 no switch is given is 9999. Compilation is terminated if this
3841 @cindex @option{-gnatn} (@code{gcc})
3842 Activate inlining for subprograms for which
3843 pragma @code{inline} is specified. This inlining is performed
3844 by the GCC back-end.
3847 @cindex @option{-gnatN} (@code{gcc})
3848 Activate front end inlining for subprograms for which
3849 pragma @code{Inline} is specified. This inlining is performed
3850 by the front end and will be visible in the
3851 @option{-gnatG} output.
3852 In some cases, this has proved more effective than the back end
3853 inlining resulting from the use of
3856 @option{-gnatN} automatically implies
3857 @option{-gnatn} so it is not necessary
3858 to specify both options. There are a few cases that the back-end inlining
3859 catches that cannot be dealt with in the front-end.
3862 @cindex @option{-gnato} (@code{gcc})
3863 Enable numeric overflow checking (which is not normally enabled by
3864 default). Not that division by zero is a separate check that is not
3865 controlled by this switch (division by zero checking is on by default).
3868 @cindex @option{-gnatp} (@code{gcc})
3869 Suppress all checks.
3872 @cindex @option{-gnatP} (@code{gcc})
3873 Enable polling. This is required on some systems (notably Windows NT) to
3874 obtain asynchronous abort and asynchronous transfer of control capability.
3875 See the description of pragma Polling in the GNAT Reference Manual for
3879 @cindex @option{-gnatq} (@code{gcc})
3880 Don't quit; try semantics, even if parse errors.
3883 @cindex @option{-gnatQ} (@code{gcc})
3884 Don't quit; generate @file{ALI} and tree files even if illegalities.
3886 @item ^-gnatR[0/1/2/3[s]]^/REPRESENTATION_INFO^
3887 @cindex @option{-gnatR} (@code{gcc})
3888 Output representation information for declared types and objects.
3891 @cindex @option{-gnats} (@code{gcc})
3895 @cindex @option{-gnatS} (@code{gcc})
3896 Print package Standard.
3899 @cindex @option{-gnatt} (@code{gcc})
3900 Generate tree output file.
3902 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
3903 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@code{gcc})
3904 All compiler tables start at @var{nnn} times usual starting size.
3907 @cindex @option{-gnatu} (@code{gcc})
3908 List units for this compilation.
3911 @cindex @option{-gnatU} (@code{gcc})
3912 Tag all error messages with the unique string ``error:''
3915 @cindex @option{-gnatv} (@code{gcc})
3916 Verbose mode. Full error output with source lines to @file{stdout}.
3919 @cindex @option{-gnatV} (@code{gcc})
3920 Control level of validity checking. See separate section describing
3923 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}[,...])^
3924 @cindex @option{^-gnatw^/WARNINGS^} (@code{gcc})
3926 ^@var{xxx} is a string of option letters that^the list of options^ denotes
3927 the exact warnings that
3928 are enabled or disabled. (see @ref{Warning Message Control})
3930 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
3931 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@code{gcc})
3932 Wide character encoding method
3934 (@var{e}=n/h/u/s/e/8).
3937 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
3941 @cindex @option{-gnatx} (@code{gcc})
3942 Suppress generation of cross-reference information.
3944 @item ^-gnaty^/STYLE_CHECKS=(option,option..)^
3945 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@code{gcc})
3946 Enable built-in style checks. (see @ref{Style Checking})
3948 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
3949 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@code{gcc})
3950 Distribution stub generation and compilation
3952 (@var{m}=r/c for receiver/caller stubs).
3955 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
3956 to be generated and compiled).
3960 Use the zero cost method for exception handling
3962 @item ^-I^/SEARCH=^@var{dir}
3963 @cindex @option{^-I^/SEARCH^} (@code{gcc})
3965 Direct GNAT to search the @var{dir} directory for source files needed by
3966 the current compilation
3967 (@pxref{Search Paths and the Run-Time Library (RTL)}).
3969 @item ^-I-^/NOCURRENT_DIRECTORY^
3970 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gcc})
3972 Except for the source file named in the command line, do not look for source
3973 files in the directory containing the source file named in the command line
3974 (@pxref{Search Paths and the Run-Time Library (RTL)}).
3978 @cindex @option{-mbig-switch} (@command{gcc})
3979 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
3980 This standard gcc switch causes the compiler to use larger offsets in its
3981 jump table representation for @code{case} statements.
3982 This may result in less efficient code, but is sometimes necessary
3983 (for example on HP-UX targets)
3984 @cindex HP-UX and @option{-mbig-switch} option
3985 in order to compile large and/or nested @code{case} statements.
3988 @cindex @option{-o} (@code{gcc})
3989 This switch is used in @code{gcc} to redirect the generated object file
3990 and its associated ALI file. Beware of this switch with GNAT, because it may
3991 cause the object file and ALI file to have different names which in turn
3992 may confuse the binder and the linker.
3996 @cindex @option{-nostdinc} (@command{gcc})
3997 Inhibit the search of the default location for the GNAT Run Time
3998 Library (RTL) source files.
4001 @cindex @option{-nostdlib} (@command{gcc})
4002 Inhibit the search of the default location for the GNAT Run Time
4003 Library (RTL) ALI files.
4007 @cindex @option{-O} (@code{gcc})
4008 @var{n} controls the optimization level.
4012 No optimization, the default setting if no @option{-O} appears
4015 Normal optimization, the default if you specify @option{-O} without
4019 Extensive optimization
4022 Extensive optimization with automatic inlining of subprograms not
4023 specified by pragma @code{Inline}. This applies only to
4024 inlining within a unit. For details on control of inlining
4025 see @xref{Subprogram Inlining Control}.
4031 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4032 Equivalent to @option{/OPTIMIZE=NONE}.
4033 This is the default behavior in the absence of an @option{/OPTMIZE}
4036 @item /OPTIMIZE[=(keyword[,...])]
4037 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4038 Selects the level of optimization for your program. The supported
4039 keywords are as follows:
4042 Perform most optimizations, including those that
4044 This is the default if the @option{/OPTMIZE} qualifier is supplied
4045 without keyword options.
4048 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4051 Perform some optimizations, but omit ones that are costly.
4054 Same as @code{SOME}.
4057 Full optimization, and also attempt automatic inlining of small
4058 subprograms within a unit even when pragma @code{Inline}
4059 is not specified (@pxref{Inlining of Subprograms}).
4062 Try to unroll loops. This keyword may be specified together with
4063 any keyword above other than @code{NONE}. Loop unrolling
4064 usually, but not always, improves the performance of programs.
4069 @item -pass-exit-codes
4070 @cindex @option{-pass-exit-codes} (@code{gcc})
4071 Catch exit codes from the compiler and use the most meaningful as
4075 @item --RTS=@var{rts-path}
4076 @cindex @option{--RTS} (@code{gcc})
4077 Specifies the default location of the runtime library. Same meaning as the
4078 equivalent @code{gnatmake} flag (see @ref{Switches for gnatmake}).
4081 @cindex @option{^-S^/ASM^} (@code{gcc})
4082 ^Used in place of @option{-c} to^Used to^
4083 cause the assembler source file to be
4084 generated, using @file{^.s^.S^} as the extension,
4085 instead of the object file.
4086 This may be useful if you need to examine the generated assembly code.
4089 @cindex @option{^-v^/VERBOSE^} (@code{gcc})
4090 Show commands generated by the @code{gcc} driver. Normally used only for
4091 debugging purposes or if you need to be sure what version of the
4092 compiler you are executing.
4096 @cindex @option{-V} (@code{gcc})
4097 Execute @var{ver} version of the compiler. This is the @code{gcc}
4098 version, not the GNAT version.
4104 You may combine a sequence of GNAT switches into a single switch. For
4105 example, the combined switch
4107 @cindex Combining GNAT switches
4113 is equivalent to specifying the following sequence of switches:
4116 -gnato -gnatf -gnati3
4121 @c NEED TO CHECK THIS FOR VMS
4124 The following restrictions apply to the combination of switches
4129 The switch @option{-gnatc} if combined with other switches must come
4130 first in the string.
4133 The switch @option{-gnats} if combined with other switches must come
4134 first in the string.
4138 @option{^-gnatz^/DISTRIBUTION_STUBS^}, @option{-gnatzc}, and @option{-gnatzr}
4139 may not be combined with any other switches.
4143 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4144 switch), then all further characters in the switch are interpreted
4145 as style modifiers (see description of @option{-gnaty}).
4148 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4149 switch), then all further characters in the switch are interpreted
4150 as debug flags (see description of @option{-gnatd}).
4153 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4154 switch), then all further characters in the switch are interpreted
4155 as warning mode modifiers (see description of @option{-gnatw}).
4158 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4159 switch), then all further characters in the switch are interpreted
4160 as validity checking options (see description of @option{-gnatV}).
4165 @node Output and Error Message Control
4166 @subsection Output and Error Message Control
4170 The standard default format for error messages is called ``brief format''.
4171 Brief format messages are written to @file{stderr} (the standard error
4172 file) and have the following form:
4175 e.adb:3:04: Incorrect spelling of keyword "function"
4176 e.adb:4:20: ";" should be "is"
4180 The first integer after the file name is the line number in the file,
4181 and the second integer is the column number within the line.
4182 @code{glide} can parse the error messages
4183 and point to the referenced character.
4184 The following switches provide control over the error message
4190 @cindex @option{-gnatv} (@code{gcc})
4193 The v stands for verbose.
4195 The effect of this setting is to write long-format error
4196 messages to @file{stdout} (the standard output file.
4197 The same program compiled with the
4198 @option{-gnatv} switch would generate:
4202 3. funcion X (Q : Integer)
4204 >>> Incorrect spelling of keyword "function"
4207 >>> ";" should be "is"
4212 The vertical bar indicates the location of the error, and the @samp{>>>}
4213 prefix can be used to search for error messages. When this switch is
4214 used the only source lines output are those with errors.
4217 @cindex @option{-gnatl} (@code{gcc})
4219 The @code{l} stands for list.
4221 This switch causes a full listing of
4222 the file to be generated. The output might look as follows:
4228 3. funcion X (Q : Integer)
4230 >>> Incorrect spelling of keyword "function"
4233 >>> ";" should be "is"
4245 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4246 standard output is redirected, a brief summary is written to
4247 @file{stderr} (standard error) giving the number of error messages and
4248 warning messages generated.
4251 @cindex @option{-gnatU} (@code{gcc})
4252 This switch forces all error messages to be preceded by the unique
4253 string ``error:''. This means that error messages take a few more
4254 characters in space, but allows easy searching for and identification
4258 @cindex @option{-gnatb} (@code{gcc})
4260 The @code{b} stands for brief.
4262 This switch causes GNAT to generate the
4263 brief format error messages to @file{stderr} (the standard error
4264 file) as well as the verbose
4265 format message or full listing (which as usual is written to
4266 @file{stdout} (the standard output file).
4268 @item -gnatm^^=^@var{n}
4269 @cindex @option{-gnatm} (@code{gcc})
4271 The @code{m} stands for maximum.
4273 @var{n} is a decimal integer in the
4274 range of 1 to 999 and limits the number of error messages to be
4275 generated. For example, using @option{-gnatm2} might yield
4278 e.adb:3:04: Incorrect spelling of keyword "function"
4279 e.adb:5:35: missing ".."
4280 fatal error: maximum errors reached
4281 compilation abandoned
4285 @cindex @option{-gnatf} (@code{gcc})
4286 @cindex Error messages, suppressing
4288 The @code{f} stands for full.
4290 Normally, the compiler suppresses error messages that are likely to be
4291 redundant. This switch causes all error
4292 messages to be generated. In particular, in the case of
4293 references to undefined variables. If a given variable is referenced
4294 several times, the normal format of messages is
4296 e.adb:7:07: "V" is undefined (more references follow)
4300 where the parenthetical comment warns that there are additional
4301 references to the variable @code{V}. Compiling the same program with the
4302 @option{-gnatf} switch yields
4305 e.adb:7:07: "V" is undefined
4306 e.adb:8:07: "V" is undefined
4307 e.adb:8:12: "V" is undefined
4308 e.adb:8:16: "V" is undefined
4309 e.adb:9:07: "V" is undefined
4310 e.adb:9:12: "V" is undefined
4314 The @option{-gnatf} switch also generates additional information for
4315 some error messages. Some examples are:
4319 Full details on entities not available in high integrity mode
4321 Details on possibly non-portable unchecked conversion
4323 List possible interpretations for ambiguous calls
4325 Additional details on incorrect parameters
4330 @cindex @option{-gnatq} (@code{gcc})
4332 The @code{q} stands for quit (really ``don't quit'').
4334 In normal operation mode, the compiler first parses the program and
4335 determines if there are any syntax errors. If there are, appropriate
4336 error messages are generated and compilation is immediately terminated.
4338 GNAT to continue with semantic analysis even if syntax errors have been
4339 found. This may enable the detection of more errors in a single run. On
4340 the other hand, the semantic analyzer is more likely to encounter some
4341 internal fatal error when given a syntactically invalid tree.
4344 @cindex @option{-gnatQ} (@code{gcc})
4345 In normal operation mode, the @file{ALI} file is not generated if any
4346 illegalities are detected in the program. The use of @option{-gnatQ} forces
4347 generation of the @file{ALI} file. This file is marked as being in
4348 error, so it cannot be used for binding purposes, but it does contain
4349 reasonably complete cross-reference information, and thus may be useful
4350 for use by tools (e.g. semantic browsing tools or integrated development
4351 environments) that are driven from the @file{ALI} file. This switch
4352 implies @option{-gnatq}, since the semantic phase must be run to get a
4353 meaningful ALI file.
4355 In addition, if @option{-gnatt} is also specified, then the tree file is
4356 generated even if there are illegalities. It may be useful in this case
4357 to also specify @option{-gnatq} to ensure that full semantic processing
4358 occurs. The resulting tree file can be processed by ASIS, for the purpose
4359 of providing partial information about illegal units, but if the error
4360 causes the tree to be badly malformed, then ASIS may crash during the
4363 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4364 being in error, @code{gnatmake} will attempt to recompile the source when it
4365 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4367 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4368 since ALI files are never generated if @option{-gnats} is set.
4373 @node Warning Message Control
4374 @subsection Warning Message Control
4375 @cindex Warning messages
4377 In addition to error messages, which correspond to illegalities as defined
4378 in the Ada 95 Reference Manual, the compiler detects two kinds of warning
4381 First, the compiler considers some constructs suspicious and generates a
4382 warning message to alert you to a possible error. Second, if the
4383 compiler detects a situation that is sure to raise an exception at
4384 run time, it generates a warning message. The following shows an example
4385 of warning messages:
4387 e.adb:4:24: warning: creation of object may raise Storage_Error
4388 e.adb:10:17: warning: static value out of range
4389 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4393 GNAT considers a large number of situations as appropriate
4394 for the generation of warning messages. As always, warnings are not
4395 definite indications of errors. For example, if you do an out-of-range
4396 assignment with the deliberate intention of raising a
4397 @code{Constraint_Error} exception, then the warning that may be
4398 issued does not indicate an error. Some of the situations for which GNAT
4399 issues warnings (at least some of the time) are given in the following
4400 list. This list is not complete, and new warnings are often added to
4401 subsequent versions of GNAT. The list is intended to give a general idea
4402 of the kinds of warnings that are generated.
4406 Possible infinitely recursive calls
4409 Out-of-range values being assigned
4412 Possible order of elaboration problems
4418 Fixed-point type declarations with a null range
4421 Variables that are never assigned a value
4424 Variables that are referenced before being initialized
4427 Task entries with no corresponding @code{accept} statement
4430 Duplicate accepts for the same task entry in a @code{select}
4433 Objects that take too much storage
4436 Unchecked conversion between types of differing sizes
4439 Missing @code{return} statement along some execution path in a function
4442 Incorrect (unrecognized) pragmas
4445 Incorrect external names
4448 Allocation from empty storage pool
4451 Potentially blocking operation in protected type
4454 Suspicious parenthesization of expressions
4457 Mismatching bounds in an aggregate
4460 Attempt to return local value by reference
4464 Premature instantiation of a generic body
4467 Attempt to pack aliased components
4470 Out of bounds array subscripts
4473 Wrong length on string assignment
4476 Violations of style rules if style checking is enabled
4479 Unused @code{with} clauses
4482 @code{Bit_Order} usage that does not have any effect
4485 @code{Standard.Duration} used to resolve universal fixed expression
4488 Dereference of possibly null value
4491 Declaration that is likely to cause storage error
4494 Internal GNAT unit @code{with}'ed by application unit
4497 Values known to be out of range at compile time
4500 Unreferenced labels and variables
4503 Address overlays that could clobber memory
4506 Unexpected initialization when address clause present
4509 Bad alignment for address clause
4512 Useless type conversions
4515 Redundant assignment statements and other redundant constructs
4518 Useless exception handlers
4521 Accidental hiding of name by child unit
4525 Access before elaboration detected at compile time
4528 A range in a @code{for} loop that is known to be null or might be null
4533 The following switches are available to control the handling of
4539 @emph{Activate all optional errors.}
4540 @cindex @option{-gnatwa} (@code{gcc})
4541 This switch activates most optional warning messages, see remaining list
4542 in this section for details on optional warning messages that can be
4543 individually controlled. The warnings that are not turned on by this
4545 @option{-gnatwd} (implicit dereferencing),
4546 @option{-gnatwh} (hiding),
4547 and @option{-gnatwl} (elaboration warnings).
4548 All other optional warnings are turned on.
4551 @emph{Suppress all optional errors.}
4552 @cindex @option{-gnatwA} (@code{gcc})
4553 This switch suppresses all optional warning messages, see remaining list
4554 in this section for details on optional warning messages that can be
4555 individually controlled.
4558 @emph{Activate warnings on conditionals.}
4559 @cindex @option{-gnatwc} (@code{gcc})
4560 @cindex Conditionals, constant
4561 This switch activates warnings for conditional expressions used in
4562 tests that are known to be True or False at compile time. The default
4563 is that such warnings are not generated.
4564 Note that this warning does
4565 not get issued for the use of boolean variables or constants whose
4566 values are known at compile time, since this is a standard technique
4567 for conditional compilation in Ada, and this would generate too many
4568 ``false positive'' warnings.
4569 This warning can also be turned on using @option{-gnatwa}.
4572 @emph{Suppress warnings on conditionals.}
4573 @cindex @option{-gnatwC} (@code{gcc})
4574 This switch suppresses warnings for conditional expressions used in
4575 tests that are known to be True or False at compile time.
4578 @emph{Activate warnings on implicit dereferencing.}
4579 @cindex @option{-gnatwd} (@code{gcc})
4580 If this switch is set, then the use of a prefix of an access type
4581 in an indexed component, slice, or selected component without an
4582 explicit @code{.all} will generate a warning. With this warning
4583 enabled, access checks occur only at points where an explicit
4584 @code{.all} appears in the source code (assuming no warnings are
4585 generated as a result of this switch). The default is that such
4586 warnings are not generated.
4587 Note that @option{-gnatwa} does not affect the setting of
4588 this warning option.
4591 @emph{Suppress warnings on implicit dereferencing.}
4592 @cindex @option{-gnatwD} (@code{gcc})
4593 @cindex Implicit dereferencing
4594 @cindex Dereferencing, implicit
4595 This switch suppresses warnings for implicit dereferences in
4596 indexed components, slices, and selected components.
4599 @emph{Treat warnings as errors.}
4600 @cindex @option{-gnatwe} (@code{gcc})
4601 @cindex Warnings, treat as error
4602 This switch causes warning messages to be treated as errors.
4603 The warning string still appears, but the warning messages are counted
4604 as errors, and prevent the generation of an object file.
4607 @emph{Activate warnings on unreferenced formals.}
4608 @cindex @option{-gnatwf} (@code{gcc})
4609 @cindex Formals, unreferenced
4610 This switch causes a warning to be generated if a formal parameter
4611 is not referenced in the body of the subprogram. This warning can
4612 also be turned on using @option{-gnatwa} or @option{-gnatwu}.
4615 @emph{Suppress warnings on unreferenced formals.}
4616 @cindex @option{-gnatwF} (@code{gcc})
4617 This switch suppresses warnings for unreferenced formal
4618 parameters. Note that the
4619 combination @option{-gnatwu} followed by @option{-gnatwF} has the
4620 effect of warning on unreferenced entities other than subprogram
4624 @emph{Activate warnings on unrecognized pragmas.}
4625 @cindex @option{-gnatwg} (@code{gcc})
4626 @cindex Pragmas, unrecognized
4627 This switch causes a warning to be generated if an unrecognized
4628 pragma is encountered. Apart from issuing this warning, the
4629 pragma is ignored and has no effect. This warning can
4630 also be turned on using @option{-gnatwa}. The default
4631 is that such warnings are issued (satisfying the Ada Reference
4632 Manual requirement that such warnings appear).
4635 @emph{Suppress warnings on unrecognized pragmas.}
4636 @cindex @option{-gnatwG} (@code{gcc})
4637 This switch suppresses warnings for unrecognized pragmas.
4640 @emph{Activate warnings on hiding.}
4641 @cindex @option{-gnatwh} (@code{gcc})
4642 @cindex Hiding of Declarations
4643 This switch activates warnings on hiding declarations.
4644 A declaration is considered hiding
4645 if it is for a non-overloadable entity, and it declares an entity with the
4646 same name as some other entity that is directly or use-visible. The default
4647 is that such warnings are not generated.
4648 Note that @option{-gnatwa} does not affect the setting of this warning option.
4651 @emph{Suppress warnings on hiding.}
4652 @cindex @option{-gnatwH} (@code{gcc})
4653 This switch suppresses warnings on hiding declarations.
4656 @emph{Activate warnings on implementation units.}
4657 @cindex @option{-gnatwi} (@code{gcc})
4658 This switch activates warnings for a @code{with} of an internal GNAT
4659 implementation unit, defined as any unit from the @code{Ada},
4660 @code{Interfaces}, @code{GNAT},
4661 ^^@code{DEC},^ or @code{System}
4662 hierarchies that is not
4663 documented in either the Ada Reference Manual or the GNAT
4664 Programmer's Reference Manual. Such units are intended only
4665 for internal implementation purposes and should not be @code{with}'ed
4666 by user programs. The default is that such warnings are generated
4667 This warning can also be turned on using @option{-gnatwa}.
4670 @emph{Disable warnings on implementation units.}
4671 @cindex @option{-gnatwI} (@code{gcc})
4672 This switch disables warnings for a @code{with} of an internal GNAT
4673 implementation unit.
4676 @emph{Activate warnings on obsolescent features (Annex J).}
4677 @cindex @option{-gnatwj} (@code{gcc})
4678 @cindex Features, obsolescent
4679 @cindex Obsolescent features
4680 If this warning option is activated, then warnings are generated for
4681 calls to subprograms marked with @code{pragma Obsolescent} and
4682 for use of features in Annex J of the Ada Reference Manual. In the
4683 case of Annex J, not all features are flagged. In particular use
4684 of the renamed packages (like @code{Text_IO}) and use of package
4685 @code{ASCII} are not flagged, since these are very common and
4686 would generate many annoying positive warnings. The default is that
4687 such warnings are not generated.
4690 @emph{Suppress warnings on obsolescent features (Annex J).}
4691 @cindex @option{-gnatwJ} (@code{gcc})
4692 This switch disables warnings on use of obsolescent features.
4695 @emph{Activate warnings on variables that could be constants.}
4696 @cindex @option{-gnatwk} (@code{gcc})
4697 This switch activates warnings for variables that are initialized but
4698 never modified, and then could be declared constants.
4701 @emph{Suppress warnings on variables that could be constants.}
4702 @cindex @option{-gnatwK} (@code{gcc})
4703 This switch disables warnings on variables that could be declared constants.
4706 @emph{Activate warnings for missing elaboration pragmas.}
4707 @cindex @option{-gnatwl} (@code{gcc})
4708 @cindex Elaboration, warnings
4709 This switch activates warnings on missing
4710 @code{pragma Elaborate_All} statements.
4711 See the section in this guide on elaboration checking for details on
4712 when such pragma should be used. Warnings are also generated if you
4713 are using the static mode of elaboration, and a @code{pragma Elaborate}
4714 is encountered. The default is that such warnings
4716 This warning is not automatically turned on by the use of @option{-gnatwa}.
4719 @emph{Suppress warnings for missing elaboration pragmas.}
4720 @cindex @option{-gnatwL} (@code{gcc})
4721 This switch suppresses warnings on missing pragma Elaborate_All statements.
4722 See the section in this guide on elaboration checking for details on
4723 when such pragma should be used.
4726 @emph{Activate warnings on modified but unreferenced variables.}
4727 @cindex @option{-gnatwm} (@code{gcc})
4728 This switch activates warnings for variables that are assigned (using
4729 an initialization value or with one or more assignment statements) but
4730 whose value is never read. The warning is suppressed for volatile
4731 variables and also for variables that are renamings of other variables
4732 or for which an address clause is given.
4733 This warning can also be turned on using @option{-gnatwa}.
4736 @emph{Disable warnings on modified but unreferenced variables.}
4737 @cindex @option{-gnatwM} (@code{gcc})
4738 This switch disables warnings for variables that are assigned or
4739 initialized, but never read.
4742 @emph{Set normal warnings mode.}
4743 @cindex @option{-gnatwn} (@code{gcc})
4744 This switch sets normal warning mode, in which enabled warnings are
4745 issued and treated as warnings rather than errors. This is the default
4746 mode. the switch @option{-gnatwn} can be used to cancel the effect of
4747 an explicit @option{-gnatws} or
4748 @option{-gnatwe}. It also cancels the effect of the
4749 implicit @option{-gnatwe} that is activated by the
4750 use of @option{-gnatg}.
4753 @emph{Activate warnings on address clause overlays.}
4754 @cindex @option{-gnatwo} (@code{gcc})
4755 @cindex Address Clauses, warnings
4756 This switch activates warnings for possibly unintended initialization
4757 effects of defining address clauses that cause one variable to overlap
4758 another. The default is that such warnings are generated.
4759 This warning can also be turned on using @option{-gnatwa}.
4762 @emph{Suppress warnings on address clause overlays.}
4763 @cindex @option{-gnatwO} (@code{gcc})
4764 This switch suppresses warnings on possibly unintended initialization
4765 effects of defining address clauses that cause one variable to overlap
4769 @emph{Activate warnings on ineffective pragma Inlines.}
4770 @cindex @option{-gnatwp} (@code{gcc})
4771 @cindex Inlining, warnings
4772 This switch activates warnings for failure of front end inlining
4773 (activated by @option{-gnatN}) to inline a particular call. There are
4774 many reasons for not being able to inline a call, including most
4775 commonly that the call is too complex to inline.
4776 This warning can also be turned on using @option{-gnatwa}.
4779 @emph{Suppress warnings on ineffective pragma Inlines.}
4780 @cindex @option{-gnatwP} (@code{gcc})
4781 This switch suppresses warnings on ineffective pragma Inlines. If the
4782 inlining mechanism cannot inline a call, it will simply ignore the
4786 @emph{Activate warnings on redundant constructs.}
4787 @cindex @option{-gnatwr} (@code{gcc})
4788 This switch activates warnings for redundant constructs. The following
4789 is the current list of constructs regarded as redundant:
4790 This warning can also be turned on using @option{-gnatwa}.
4794 Assignment of an item to itself.
4796 Type conversion that converts an expression to its own type.
4798 Use of the attribute @code{Base} where @code{typ'Base} is the same
4801 Use of pragma @code{Pack} when all components are placed by a record
4802 representation clause.
4804 Exception handler containing only a reraise statement (raise with no
4805 operand) which has no effect.
4807 Use of the operator abs on an operand that is known at compile time
4810 Use of an unnecessary extra level of parentheses (C-style) around conditions
4811 in @code{if} statements, @code{while} statements and @code{exit} statements.
4813 Comparison of boolean expressions to an explicit True value.
4817 @emph{Suppress warnings on redundant constructs.}
4818 @cindex @option{-gnatwR} (@code{gcc})
4819 This switch suppresses warnings for redundant constructs.
4822 @emph{Suppress all warnings.}
4823 @cindex @option{-gnatws} (@code{gcc})
4824 This switch completely suppresses the
4825 output of all warning messages from the GNAT front end.
4826 Note that it does not suppress warnings from the @code{gcc} back end.
4827 To suppress these back end warnings as well, use the switch @option{-w}
4828 in addition to @option{-gnatws}.
4831 @emph{Activate warnings on unused entities.}
4832 @cindex @option{-gnatwu} (@code{gcc})
4833 This switch activates warnings to be generated for entities that
4834 are declared but not referenced, and for units that are @code{with}'ed
4836 referenced. In the case of packages, a warning is also generated if
4837 no entities in the package are referenced. This means that if the package
4838 is referenced but the only references are in @code{use}
4839 clauses or @code{renames}
4840 declarations, a warning is still generated. A warning is also generated
4841 for a generic package that is @code{with}'ed but never instantiated.
4842 In the case where a package or subprogram body is compiled, and there
4843 is a @code{with} on the corresponding spec
4844 that is only referenced in the body,
4845 a warning is also generated, noting that the
4846 @code{with} can be moved to the body. The default is that
4847 such warnings are not generated.
4848 This switch also activates warnings on unreferenced formals
4849 (it is includes the effect of @option{-gnatwf}).
4850 This warning can also be turned on using @option{-gnatwa}.
4853 @emph{Suppress warnings on unused entities.}
4854 @cindex @option{-gnatwU} (@code{gcc})
4855 This switch suppresses warnings for unused entities and packages.
4856 It also turns off warnings on unreferenced formals (and thus includes
4857 the effect of @option{-gnatwF}).
4860 @emph{Activate warnings on unassigned variables.}
4861 @cindex @option{-gnatwv} (@code{gcc})
4862 @cindex Unassigned variable warnings
4863 This switch activates warnings for access to variables which
4864 may not be properly initialized. The default is that
4865 such warnings are generated.
4868 @emph{Suppress warnings on unassigned variables.}
4869 @cindex @option{-gnatwV} (@code{gcc})
4870 This switch suppresses warnings for access to variables which
4871 may not be properly initialized.
4874 @emph{Activate warnings on Export/Import pragmas.}
4875 @cindex @option{-gnatwx} (@code{gcc})
4876 @cindex Export/Import pragma warnings
4877 This switch activates warnings on Export/Import pragmas when
4878 the compiler detects a possible conflict between the Ada and
4879 foreign language calling sequences. For example, the use of
4880 default parameters in a convention C procedure is dubious
4881 because the C compiler cannot supply the proper default, so
4882 a warning is issued. The default is that such warnings are
4886 @emph{Suppress warnings on Export/Import pragmas.}
4887 @cindex @option{-gnatwX} (@code{gcc})
4888 This switch suppresses warnings on Export/Import pragmas.
4889 The sense of this is that you are telling the compiler that
4890 you know what you are doing in writing the pragma, and it
4891 should not complain at you.
4894 @emph{Activate warnings on unchecked conversions.}
4895 @cindex @option{-gnatwz} (@code{gcc})
4896 @cindex Unchecked_Conversion warnings
4897 This switch activates warnings for unchecked conversions
4898 where the types are known at compile time to have different
4900 is that such warnings are generated.
4903 @emph{Suppress warnings on unchecked conversions.}
4904 @cindex @option{-gnatwZ} (@code{gcc})
4905 This switch suppresses warnings for unchecked conversions
4906 where the types are known at compile time to have different
4909 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
4910 @cindex @option{-Wuninitialized}
4911 The warnings controlled by the @option{-gnatw} switch are generated by the
4912 front end of the compiler. In some cases, the @option{^gcc^GCC^} back end
4913 can provide additional warnings. One such useful warning is provided by
4914 @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^}. This must be used in
4915 conjunction with tunrning on optimization mode. This causes the flow
4916 analysis circuits of the back end optimizer to output additional
4917 warnings about uninitialized variables.
4919 @item ^-w^/NO_BACK_END_WARNINGS^
4921 This switch suppresses warnings from the @option{^gcc^GCC^} back end. It may
4922 be used in conjunction with @option{-gnatws} to ensure that all warnings
4923 are suppressed during the entire compilation process.
4929 A string of warning parameters can be used in the same parameter. For example:
4936 will turn on all optional warnings except for elaboration pragma warnings,
4937 and also specify that warnings should be treated as errors.
4939 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
4965 @node Debugging and Assertion Control
4966 @subsection Debugging and Assertion Control
4970 @cindex @option{-gnata} (@code{gcc})
4976 The pragmas @code{Assert} and @code{Debug} normally have no effect and
4977 are ignored. This switch, where @samp{a} stands for assert, causes
4978 @code{Assert} and @code{Debug} pragmas to be activated.
4980 The pragmas have the form:
4984 @b{pragma} Assert (@var{Boolean-expression} [,
4985 @var{static-string-expression}])
4986 @b{pragma} Debug (@var{procedure call})
4991 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
4992 If the result is @code{True}, the pragma has no effect (other than
4993 possible side effects from evaluating the expression). If the result is
4994 @code{False}, the exception @code{Assert_Failure} declared in the package
4995 @code{System.Assertions} is
4996 raised (passing @var{static-string-expression}, if present, as the
4997 message associated with the exception). If no string expression is
4998 given the default is a string giving the file name and line number
5001 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5002 @code{pragma Debug} may appear within a declaration sequence, allowing
5003 debugging procedures to be called between declarations.
5006 @item /DEBUG[=debug-level]
5008 Specifies how much debugging information is to be included in
5009 the resulting object file where 'debug-level' is one of the following:
5012 Include both debugger symbol records and traceback
5014 This is the default setting.
5016 Include both debugger symbol records and traceback in
5019 Excludes both debugger symbol records and traceback
5020 the object file. Same as /NODEBUG.
5022 Includes only debugger symbol records in the object
5023 file. Note that this doesn't include traceback information.
5028 @node Validity Checking
5029 @subsection Validity Checking
5030 @findex Validity Checking
5033 The Ada 95 Reference Manual has specific requirements for checking
5034 for invalid values. In particular, RM 13.9.1 requires that the
5035 evaluation of invalid values (for example from unchecked conversions),
5036 not result in erroneous execution. In GNAT, the result of such an
5037 evaluation in normal default mode is to either use the value
5038 unmodified, or to raise Constraint_Error in those cases where use
5039 of the unmodified value would cause erroneous execution. The cases
5040 where unmodified values might lead to erroneous execution are case
5041 statements (where a wild jump might result from an invalid value),
5042 and subscripts on the left hand side (where memory corruption could
5043 occur as a result of an invalid value).
5045 The @option{-gnatV^@var{x}^^} switch allows more control over the validity
5048 The @code{x} argument is a string of letters that
5049 indicate validity checks that are performed or not performed in addition
5050 to the default checks described above.
5053 The options allowed for this qualifier
5054 indicate validity checks that are performed or not performed in addition
5055 to the default checks described above.
5062 @emph{All validity checks.}
5063 @cindex @option{-gnatVa} (@code{gcc})
5064 All validity checks are turned on.
5066 That is, @option{-gnatVa} is
5067 equivalent to @option{gnatVcdfimorst}.
5071 @emph{Validity checks for copies.}
5072 @cindex @option{-gnatVc} (@code{gcc})
5073 The right hand side of assignments, and the initializing values of
5074 object declarations are validity checked.
5077 @emph{Default (RM) validity checks.}
5078 @cindex @option{-gnatVd} (@code{gcc})
5079 Some validity checks are done by default following normal Ada semantics
5081 A check is done in case statements that the expression is within the range
5082 of the subtype. If it is not, Constraint_Error is raised.
5083 For assignments to array components, a check is done that the expression used
5084 as index is within the range. If it is not, Constraint_Error is raised.
5085 Both these validity checks may be turned off using switch @option{-gnatVD}.
5086 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5087 switch @option{-gnatVd} will leave the checks turned on.
5088 Switch @option{-gnatVD} should be used only if you are sure that all such
5089 expressions have valid values. If you use this switch and invalid values
5090 are present, then the program is erroneous, and wild jumps or memory
5091 overwriting may occur.
5094 @emph{Validity checks for floating-point values.}
5095 @cindex @option{-gnatVf} (@code{gcc})
5096 In the absence of this switch, validity checking occurs only for discrete
5097 values. If @option{-gnatVf} is specified, then validity checking also applies
5098 for floating-point values, and NaN's and infinities are considered invalid,
5099 as well as out of range values for constrained types. Note that this means
5100 that standard @code{IEEE} infinity mode is not allowed. The exact contexts
5101 in which floating-point values are checked depends on the setting of other
5102 options. For example,
5103 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
5104 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
5105 (the order does not matter) specifies that floating-point parameters of mode
5106 @code{in} should be validity checked.
5109 @emph{Validity checks for @code{in} mode parameters}
5110 @cindex @option{-gnatVi} (@code{gcc})
5111 Arguments for parameters of mode @code{in} are validity checked in function
5112 and procedure calls at the point of call.
5115 @emph{Validity checks for @code{in out} mode parameters.}
5116 @cindex @option{-gnatVm} (@code{gcc})
5117 Arguments for parameters of mode @code{in out} are validity checked in
5118 procedure calls at the point of call. The @code{'m'} here stands for
5119 modify, since this concerns parameters that can be modified by the call.
5120 Note that there is no specific option to test @code{out} parameters,
5121 but any reference within the subprogram will be tested in the usual
5122 manner, and if an invalid value is copied back, any reference to it
5123 will be subject to validity checking.
5126 @emph{No validity checks.}
5127 @cindex @option{-gnatVn} (@code{gcc})
5128 This switch turns off all validity checking, including the default checking
5129 for case statements and left hand side subscripts. Note that the use of
5130 the switch @option{-gnatp} suppresses all run-time checks, including
5131 validity checks, and thus implies @option{-gnatVn}. When this switch
5132 is used, it cancels any other @option{-gnatV} previously issued.
5135 @emph{Validity checks for operator and attribute operands.}
5136 @cindex @option{-gnatVo} (@code{gcc})
5137 Arguments for predefined operators and attributes are validity checked.
5138 This includes all operators in package @code{Standard},
5139 the shift operators defined as intrinsic in package @code{Interfaces}
5140 and operands for attributes such as @code{Pos}. Checks are also made
5141 on individual component values for composite comparisons.
5144 @emph{Validity checks for parameters.}
5145 @cindex @option{-gnatVp} (@code{gcc})
5146 This controls the treatment of parameters within a subprogram (as opposed
5147 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
5148 of parameters on a call. If either of these call options is used, then
5149 normally an assumption is made within a subprogram that the input arguments
5150 have been validity checking at the point of call, and do not need checking
5151 again within a subprogram). If @option{-gnatVp} is set, then this assumption
5152 is not made, and parameters are not assumed to be valid, so their validity
5153 will be checked (or rechecked) within the subprogram.
5156 @emph{Validity checks for function returns.}
5157 @cindex @option{-gnatVr} (@code{gcc})
5158 The expression in @code{return} statements in functions is validity
5162 @emph{Validity checks for subscripts.}
5163 @cindex @option{-gnatVs} (@code{gcc})
5164 All subscripts expressions are checked for validity, whether they appear
5165 on the right side or left side (in default mode only left side subscripts
5166 are validity checked).
5169 @emph{Validity checks for tests.}
5170 @cindex @option{-gnatVt} (@code{gcc})
5171 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
5172 statements are checked, as well as guard expressions in entry calls.
5177 The @option{-gnatV} switch may be followed by
5178 ^a string of letters^a list of options^
5179 to turn on a series of validity checking options.
5181 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
5182 specifies that in addition to the default validity checking, copies and
5183 function return expressions are to be validity checked.
5184 In order to make it easier
5185 to specify the desired combination of effects,
5187 the upper case letters @code{CDFIMORST} may
5188 be used to turn off the corresponding lower case option.
5191 the prefix @code{NO} on an option turns off the corresponding validity
5194 @item @code{NOCOPIES}
5195 @item @code{NODEFAULT}
5196 @item @code{NOFLOATS}
5197 @item @code{NOIN_PARAMS}
5198 @item @code{NOMOD_PARAMS}
5199 @item @code{NOOPERANDS}
5200 @item @code{NORETURNS}
5201 @item @code{NOSUBSCRIPTS}
5202 @item @code{NOTESTS}
5206 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
5207 turns on all validity checking options except for
5208 checking of @code{@b{in out}} procedure arguments.
5210 The specification of additional validity checking generates extra code (and
5211 in the case of @option{-gnatVa} the code expansion can be substantial.
5212 However, these additional checks can be very useful in detecting
5213 uninitialized variables, incorrect use of unchecked conversion, and other
5214 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
5215 is useful in conjunction with the extra validity checking, since this
5216 ensures that wherever possible uninitialized variables have invalid values.
5218 See also the pragma @code{Validity_Checks} which allows modification of
5219 the validity checking mode at the program source level, and also allows for
5220 temporary disabling of validity checks.
5223 @node Style Checking
5224 @subsection Style Checking
5225 @findex Style checking
5228 The @option{-gnaty^x^(option,option,...)^} switch
5229 @cindex @option{-gnaty} (@code{gcc})
5230 causes the compiler to
5231 enforce specified style rules. A limited set of style rules has been used
5232 in writing the GNAT sources themselves. This switch allows user programs
5233 to activate all or some of these checks. If the source program fails a
5234 specified style check, an appropriate warning message is given, preceded by
5235 the character sequence ``(style)''.
5237 @code{(option,option,...)} is a sequence of keywords
5240 The string @var{x} is a sequence of letters or digits
5242 indicating the particular style
5243 checks to be performed. The following checks are defined:
5248 @emph{Specify indentation level.}
5249 If a digit from 1-9 appears
5250 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
5251 then proper indentation is checked, with the digit indicating the
5252 indentation level required.
5253 The general style of required indentation is as specified by
5254 the examples in the Ada Reference Manual. Full line comments must be
5255 aligned with the @code{--} starting on a column that is a multiple of
5256 the alignment level.
5259 @emph{Check attribute casing.}
5260 If the ^letter a^word ATTRIBUTE^ appears in the string after @option{-gnaty}
5261 then attribute names, including the case of keywords such as @code{digits}
5262 used as attributes names, must be written in mixed case, that is, the
5263 initial letter and any letter following an underscore must be uppercase.
5264 All other letters must be lowercase.
5267 @emph{Blanks not allowed at statement end.}
5268 If the ^letter b^word BLANKS^ appears in the string after @option{-gnaty} then
5269 trailing blanks are not allowed at the end of statements. The purpose of this
5270 rule, together with h (no horizontal tabs), is to enforce a canonical format
5271 for the use of blanks to separate source tokens.
5274 @emph{Check comments.}
5275 If the ^letter c^word COMMENTS^ appears in the string after @option{-gnaty}
5276 then comments must meet the following set of rules:
5281 The ``@code{--}'' that starts the column must either start in column one,
5282 or else at least one blank must precede this sequence.
5285 Comments that follow other tokens on a line must have at least one blank
5286 following the ``@code{--}'' at the start of the comment.
5289 Full line comments must have two blanks following the ``@code{--}'' that
5290 starts the comment, with the following exceptions.
5293 A line consisting only of the ``@code{--}'' characters, possibly preceded
5294 by blanks is permitted.
5297 A comment starting with ``@code{--x}'' where @code{x} is a special character
5299 This allows proper processing of the output generated by specialized tools
5300 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
5302 language (where ``@code{--#}'' is used). For the purposes of this rule, a
5303 special character is defined as being in one of the ASCII ranges
5304 @code{16#21#..16#2F#} or @code{16#3A#..16#3F#}.
5305 Note that this usage is not permitted
5306 in GNAT implementation units (i.e. when @option{-gnatg} is used).
5309 A line consisting entirely of minus signs, possibly preceded by blanks, is
5310 permitted. This allows the construction of box comments where lines of minus
5311 signs are used to form the top and bottom of the box.
5314 If a comment starts and ends with ``@code{--}'' is permitted as long as at
5315 least one blank follows the initial ``@code{--}''. Together with the preceding
5316 rule, this allows the construction of box comments, as shown in the following
5319 ---------------------------
5320 -- This is a box comment --
5321 -- with two text lines. --
5322 ---------------------------
5327 @emph{Check end/exit labels.}
5328 If the ^letter e^word END^ appears in the string after @option{-gnaty} then
5329 optional labels on @code{end} statements ending subprograms and on
5330 @code{exit} statements exiting named loops, are required to be present.
5333 @emph{No form feeds or vertical tabs.}
5334 If the ^letter f^word VTABS^ appears in the string after @option{-gnaty} then
5335 neither form feeds nor vertical tab characters are not permitted
5339 @emph{No horizontal tabs.}
5340 If the ^letter h^word HTABS^ appears in the string after @option{-gnaty} then
5341 horizontal tab characters are not permitted in the source text.
5342 Together with the b (no blanks at end of line) check, this
5343 enforces a canonical form for the use of blanks to separate
5347 @emph{Check if-then layout.}
5348 If the ^letter i^word IF_THEN^ appears in the string after @option{-gnaty},
5349 then the keyword @code{then} must appear either on the same
5350 line as corresponding @code{if}, or on a line on its own, lined
5351 up under the @code{if} with at least one non-blank line in between
5352 containing all or part of the condition to be tested.
5355 @emph{Check keyword casing.}
5356 If the ^letter k^word KEYWORD^ appears in the string after @option{-gnaty} then
5357 all keywords must be in lower case (with the exception of keywords
5358 such as @code{digits} used as attribute names to which this check
5362 @emph{Check layout.}
5363 If the ^letter l^word LAYOUT^ appears in the string after @option{-gnaty} then
5364 layout of statement and declaration constructs must follow the
5365 recommendations in the Ada Reference Manual, as indicated by the
5366 form of the syntax rules. For example an @code{else} keyword must
5367 be lined up with the corresponding @code{if} keyword.
5369 There are two respects in which the style rule enforced by this check
5370 option are more liberal than those in the Ada Reference Manual. First
5371 in the case of record declarations, it is permissible to put the
5372 @code{record} keyword on the same line as the @code{type} keyword, and
5373 then the @code{end} in @code{end record} must line up under @code{type}.
5374 For example, either of the following two layouts is acceptable:
5376 @smallexample @c ada
5392 Second, in the case of a block statement, a permitted alternative
5393 is to put the block label on the same line as the @code{declare} or
5394 @code{begin} keyword, and then line the @code{end} keyword up under
5395 the block label. For example both the following are permitted:
5397 @smallexample @c ada
5415 The same alternative format is allowed for loops. For example, both of
5416 the following are permitted:
5418 @smallexample @c ada
5420 Clear : while J < 10 loop
5431 @item ^m^LINE_LENGTH^
5432 @emph{Check maximum line length.}
5433 If the ^letter m^word LINE_LENGTH^ appears in the string after @option{-gnaty}
5434 then the length of source lines must not exceed 79 characters, including
5435 any trailing blanks. The value of 79 allows convenient display on an
5436 80 character wide device or window, allowing for possible special
5437 treatment of 80 character lines. Note that this count is of raw
5438 characters in the source text. This means that a tab character counts
5439 as one character in this count and a wide character sequence counts as
5440 several characters (however many are needed in the encoding).
5442 @item ^Mnnn^MAX_LENGTH=nnn^
5443 @emph{Set maximum line length.}
5444 If the sequence ^M^MAX_LENGTH=^nnn, where nnn is a decimal number, appears in
5445 the string after @option{-gnaty} then the length of lines must not exceed the
5448 @item ^n^STANDARD_CASING^
5449 @emph{Check casing of entities in Standard.}
5450 If the ^letter n^word STANDARD_CASING^ appears in the string
5451 after @option{-gnaty} then any identifier from Standard must be cased
5452 to match the presentation in the Ada Reference Manual (for example,
5453 @code{Integer} and @code{ASCII.NUL}).
5455 @item ^o^ORDERED_SUBPROGRAMS^
5456 @emph{Check order of subprogram bodies.}
5457 If the ^letter o^word ORDERED_SUBPROGRAMS^ appears in the string
5458 after @option{-gnaty} then all subprogram bodies in a given scope
5459 (e.g. a package body) must be in alphabetical order. The ordering
5460 rule uses normal Ada rules for comparing strings, ignoring casing
5461 of letters, except that if there is a trailing numeric suffix, then
5462 the value of this suffix is used in the ordering (e.g. Junk2 comes
5466 @emph{Check pragma casing.}
5467 If the ^letter p^word PRAGMA^ appears in the string after @option{-gnaty} then
5468 pragma names must be written in mixed case, that is, the
5469 initial letter and any letter following an underscore must be uppercase.
5470 All other letters must be lowercase.
5472 @item ^r^REFERENCES^
5473 @emph{Check references.}
5474 If the ^letter r^word REFERENCES^ appears in the string after @option{-gnaty}
5475 then all identifier references must be cased in the same way as the
5476 corresponding declaration. No specific casing style is imposed on
5477 identifiers. The only requirement is for consistency of references
5481 @emph{Check separate specs.}
5482 If the ^letter s^word SPECS^ appears in the string after @option{-gnaty} then
5483 separate declarations (``specs'') are required for subprograms (a
5484 body is not allowed to serve as its own declaration). The only
5485 exception is that parameterless library level procedures are
5486 not required to have a separate declaration. This exception covers
5487 the most frequent form of main program procedures.
5490 @emph{Check token spacing.}
5491 If the ^letter t^word TOKEN^ appears in the string after @option{-gnaty} then
5492 the following token spacing rules are enforced:
5497 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
5500 The token @code{=>} must be surrounded by spaces.
5503 The token @code{<>} must be preceded by a space or a left parenthesis.
5506 Binary operators other than @code{**} must be surrounded by spaces.
5507 There is no restriction on the layout of the @code{**} binary operator.
5510 Colon must be surrounded by spaces.
5513 Colon-equal (assignment, initialization) must be surrounded by spaces.
5516 Comma must be the first non-blank character on the line, or be
5517 immediately preceded by a non-blank character, and must be followed
5521 If the token preceding a left parenthesis ends with a letter or digit, then
5522 a space must separate the two tokens.
5525 A right parenthesis must either be the first non-blank character on
5526 a line, or it must be preceded by a non-blank character.
5529 A semicolon must not be preceded by a space, and must not be followed by
5530 a non-blank character.
5533 A unary plus or minus may not be followed by a space.
5536 A vertical bar must be surrounded by spaces.
5540 In the above rules, appearing in column one is always permitted, that is,
5541 counts as meeting either a requirement for a required preceding space,
5542 or as meeting a requirement for no preceding space.
5544 Appearing at the end of a line is also always permitted, that is, counts
5545 as meeting either a requirement for a following space, or as meeting
5546 a requirement for no following space.
5551 If any of these style rules is violated, a message is generated giving
5552 details on the violation. The initial characters of such messages are
5553 always ``@code{(style)}''. Note that these messages are treated as warning
5554 messages, so they normally do not prevent the generation of an object
5555 file. The @option{-gnatwe} switch can be used to treat warning messages,
5556 including style messages, as fatal errors.
5560 @option{-gnaty} on its own (that is not
5561 followed by any letters or digits),
5562 is equivalent to @code{gnaty3abcefhiklmprst}, that is all checking
5563 options enabled with the exception of -gnatyo,
5566 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
5567 the exception of ORDERED_SUBPROGRAMS,
5569 with an indentation level of 3. This is the standard
5570 checking option that is used for the GNAT sources.
5579 clears any previously set style checks.
5581 @node Run-Time Checks
5582 @subsection Run-Time Checks
5583 @cindex Division by zero
5584 @cindex Access before elaboration
5585 @cindex Checks, division by zero
5586 @cindex Checks, access before elaboration
5589 If you compile with the default options, GNAT will insert many run-time
5590 checks into the compiled code, including code that performs range
5591 checking against constraints, but not arithmetic overflow checking for
5592 integer operations (including division by zero) or checks for access
5593 before elaboration on subprogram calls. All other run-time checks, as
5594 required by the Ada 95 Reference Manual, are generated by default.
5595 The following @code{gcc} switches refine this default behavior:
5600 @cindex @option{-gnatp} (@code{gcc})
5601 @cindex Suppressing checks
5602 @cindex Checks, suppressing
5604 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
5605 had been present in the source. Validity checks are also suppressed (in
5606 other words @option{-gnatp} also implies @option{-gnatVn}.
5607 Use this switch to improve the performance
5608 of the code at the expense of safety in the presence of invalid data or
5612 @cindex @option{-gnato} (@code{gcc})
5613 @cindex Overflow checks
5614 @cindex Check, overflow
5615 Enables overflow checking for integer operations.
5616 This causes GNAT to generate slower and larger executable
5617 programs by adding code to check for overflow (resulting in raising
5618 @code{Constraint_Error} as required by standard Ada
5619 semantics). These overflow checks correspond to situations in which
5620 the true value of the result of an operation may be outside the base
5621 range of the result type. The following example shows the distinction:
5623 @smallexample @c ada
5624 X1 : Integer := Integer'Last;
5625 X2 : Integer range 1 .. 5 := 5;
5626 X3 : Integer := Integer'Last;
5627 X4 : Integer range 1 .. 5 := 5;
5628 F : Float := 2.0E+20;
5637 Here the first addition results in a value that is outside the base range
5638 of Integer, and hence requires an overflow check for detection of the
5639 constraint error. Thus the first assignment to @code{X1} raises a
5640 @code{Constraint_Error} exception only if @option{-gnato} is set.
5642 The second increment operation results in a violation
5643 of the explicit range constraint, and such range checks are always
5644 performed (unless specifically suppressed with a pragma @code{suppress}
5645 or the use of @option{-gnatp}).
5647 The two conversions of @code{F} both result in values that are outside
5648 the base range of type @code{Integer} and thus will raise
5649 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
5650 The fact that the result of the second conversion is assigned to
5651 variable @code{X4} with a restricted range is irrelevant, since the problem
5652 is in the conversion, not the assignment.
5654 Basically the rule is that in the default mode (@option{-gnato} not
5655 used), the generated code assures that all integer variables stay
5656 within their declared ranges, or within the base range if there is
5657 no declared range. This prevents any serious problems like indexes
5658 out of range for array operations.
5660 What is not checked in default mode is an overflow that results in
5661 an in-range, but incorrect value. In the above example, the assignments
5662 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
5663 range of the target variable, but the result is wrong in the sense that
5664 it is too large to be represented correctly. Typically the assignment
5665 to @code{X1} will result in wrap around to the largest negative number.
5666 The conversions of @code{F} will result in some @code{Integer} value
5667 and if that integer value is out of the @code{X4} range then the
5668 subsequent assignment would generate an exception.
5670 @findex Machine_Overflows
5671 Note that the @option{-gnato} switch does not affect the code generated
5672 for any floating-point operations; it applies only to integer
5674 For floating-point, GNAT has the @code{Machine_Overflows}
5675 attribute set to @code{False} and the normal mode of operation is to
5676 generate IEEE NaN and infinite values on overflow or invalid operations
5677 (such as dividing 0.0 by 0.0).
5679 The reason that we distinguish overflow checking from other kinds of
5680 range constraint checking is that a failure of an overflow check can
5681 generate an incorrect value, but cannot cause erroneous behavior. This
5682 is unlike the situation with a constraint check on an array subscript,
5683 where failure to perform the check can result in random memory description,
5684 or the range check on a case statement, where failure to perform the check
5685 can cause a wild jump.
5687 Note again that @option{-gnato} is off by default, so overflow checking is
5688 not performed in default mode. This means that out of the box, with the
5689 default settings, GNAT does not do all the checks expected from the
5690 language description in the Ada Reference Manual. If you want all constraint
5691 checks to be performed, as described in this Manual, then you must
5692 explicitly use the -gnato switch either on the @code{gnatmake} or
5696 @cindex @option{-gnatE} (@code{gcc})
5697 @cindex Elaboration checks
5698 @cindex Check, elaboration
5699 Enables dynamic checks for access-before-elaboration
5700 on subprogram calls and generic instantiations.
5701 For full details of the effect and use of this switch,
5702 @xref{Compiling Using gcc}.
5707 The setting of these switches only controls the default setting of the
5708 checks. You may modify them using either @code{Suppress} (to remove
5709 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
5712 @node Stack Overflow Checking
5713 @subsection Stack Overflow Checking
5714 @cindex Stack Overflow Checking
5715 @cindex -fstack-check
5718 For most operating systems, @code{gcc} does not perform stack overflow
5719 checking by default. This means that if the main environment task or
5720 some other task exceeds the available stack space, then unpredictable
5721 behavior will occur.
5723 To activate stack checking, compile all units with the gcc option
5724 @option{-fstack-check}. For example:
5727 gcc -c -fstack-check package1.adb
5731 Units compiled with this option will generate extra instructions to check
5732 that any use of the stack (for procedure calls or for declaring local
5733 variables in declare blocks) do not exceed the available stack space.
5734 If the space is exceeded, then a @code{Storage_Error} exception is raised.
5736 For declared tasks, the stack size is always controlled by the size
5737 given in an applicable @code{Storage_Size} pragma (or is set to
5738 the default size if no pragma is used.
5740 For the environment task, the stack size depends on
5741 system defaults and is unknown to the compiler. The stack
5742 may even dynamically grow on some systems, precluding the
5743 normal Ada semantics for stack overflow. In the worst case,
5744 unbounded stack usage, causes unbounded stack expansion
5745 resulting in the system running out of virtual memory.
5747 The stack checking may still work correctly if a fixed
5748 size stack is allocated, but this cannot be guaranteed.
5749 To ensure that a clean exception is signalled for stack
5750 overflow, set the environment variable
5751 @code{GNAT_STACK_LIMIT} to indicate the maximum
5752 stack area that can be used, as in:
5753 @cindex GNAT_STACK_LIMIT
5756 SET GNAT_STACK_LIMIT 1600
5760 The limit is given in kilobytes, so the above declaration would
5761 set the stack limit of the environment task to 1.6 megabytes.
5762 Note that the only purpose of this usage is to limit the amount
5763 of stack used by the environment task. If it is necessary to
5764 increase the amount of stack for the environment task, then this
5765 is an operating systems issue, and must be addressed with the
5766 appropriate operating systems commands.
5769 @node Using gcc for Syntax Checking
5770 @subsection Using @code{gcc} for Syntax Checking
5773 @cindex @option{-gnats} (@code{gcc})
5777 The @code{s} stands for ``syntax''.
5780 Run GNAT in syntax checking only mode. For
5781 example, the command
5784 $ gcc -c -gnats x.adb
5788 compiles file @file{x.adb} in syntax-check-only mode. You can check a
5789 series of files in a single command
5791 , and can use wild cards to specify such a group of files.
5792 Note that you must specify the @option{-c} (compile
5793 only) flag in addition to the @option{-gnats} flag.
5796 You may use other switches in conjunction with @option{-gnats}. In
5797 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
5798 format of any generated error messages.
5800 When the source file is empty or contains only empty lines and/or comments,
5801 the output is a warning:
5804 $ gcc -c -gnats -x ada toto.txt
5805 toto.txt:1:01: warning: empty file, contains no compilation units
5809 Otherwise, the output is simply the error messages, if any. No object file or
5810 ALI file is generated by a syntax-only compilation. Also, no units other
5811 than the one specified are accessed. For example, if a unit @code{X}
5812 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
5813 check only mode does not access the source file containing unit
5816 @cindex Multiple units, syntax checking
5817 Normally, GNAT allows only a single unit in a source file. However, this
5818 restriction does not apply in syntax-check-only mode, and it is possible
5819 to check a file containing multiple compilation units concatenated
5820 together. This is primarily used by the @code{gnatchop} utility
5821 (@pxref{Renaming Files Using gnatchop}).
5825 @node Using gcc for Semantic Checking
5826 @subsection Using @code{gcc} for Semantic Checking
5829 @cindex @option{-gnatc} (@code{gcc})
5833 The @code{c} stands for ``check''.
5835 Causes the compiler to operate in semantic check mode,
5836 with full checking for all illegalities specified in the
5837 Ada 95 Reference Manual, but without generation of any object code
5838 (no object file is generated).
5840 Because dependent files must be accessed, you must follow the GNAT
5841 semantic restrictions on file structuring to operate in this mode:
5845 The needed source files must be accessible
5846 (@pxref{Search Paths and the Run-Time Library (RTL)}).
5849 Each file must contain only one compilation unit.
5852 The file name and unit name must match (@pxref{File Naming Rules}).
5855 The output consists of error messages as appropriate. No object file is
5856 generated. An @file{ALI} file is generated for use in the context of
5857 cross-reference tools, but this file is marked as not being suitable
5858 for binding (since no object file is generated).
5859 The checking corresponds exactly to the notion of
5860 legality in the Ada 95 Reference Manual.
5862 Any unit can be compiled in semantics-checking-only mode, including
5863 units that would not normally be compiled (subunits,
5864 and specifications where a separate body is present).
5867 @node Compiling Ada 83 Programs
5868 @subsection Compiling Ada 83 Programs
5870 @cindex Ada 83 compatibility
5872 @cindex @option{-gnat83} (@code{gcc})
5873 @cindex ACVC, Ada 83 tests
5876 Although GNAT is primarily an Ada 95 compiler, it accepts this switch to
5877 specify that an Ada 83 program is to be compiled in Ada 83 mode. If you specify
5878 this switch, GNAT rejects most Ada 95 extensions and applies Ada 83 semantics
5879 where this can be done easily.
5880 It is not possible to guarantee this switch does a perfect
5881 job; for example, some subtle tests, such as are
5882 found in earlier ACVC tests (and that have been removed from the ACATS suite
5883 for Ada 95), might not compile correctly.
5884 Nevertheless, this switch may be useful in some circumstances, for example
5885 where, due to contractual reasons, legacy code needs to be maintained
5886 using only Ada 83 features.
5888 With few exceptions (most notably the need to use @code{<>} on
5889 @cindex Generic formal parameters
5890 unconstrained generic formal parameters, the use of the new Ada 95
5891 reserved words, and the use of packages
5892 with optional bodies), it is not necessary to use the
5893 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
5894 exceptions, Ada 95 is upwardly compatible with Ada 83. This
5895 means that a correct Ada 83 program is usually also a correct Ada 95
5897 For further information, please refer to @ref{Compatibility and Porting Guide}.
5901 @node Character Set Control
5902 @subsection Character Set Control
5904 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
5905 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@code{gcc})
5908 Normally GNAT recognizes the Latin-1 character set in source program
5909 identifiers, as described in the Ada 95 Reference Manual.
5911 GNAT to recognize alternate character sets in identifiers. @var{c} is a
5912 single character ^^or word^ indicating the character set, as follows:
5916 ISO 8859-1 (Latin-1) identifiers
5919 ISO 8859-2 (Latin-2) letters allowed in identifiers
5922 ISO 8859-3 (Latin-3) letters allowed in identifiers
5925 ISO 8859-4 (Latin-4) letters allowed in identifiers
5928 ISO 8859-5 (Cyrillic) letters allowed in identifiers
5931 ISO 8859-15 (Latin-9) letters allowed in identifiers
5934 IBM PC letters (code page 437) allowed in identifiers
5937 IBM PC letters (code page 850) allowed in identifiers
5939 @item ^f^FULL_UPPER^
5940 Full upper-half codes allowed in identifiers
5943 No upper-half codes allowed in identifiers
5946 Wide-character codes (that is, codes greater than 255)
5947 allowed in identifiers
5950 @xref{Foreign Language Representation}, for full details on the
5951 implementation of these character sets.
5953 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
5954 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@code{gcc})
5955 Specify the method of encoding for wide characters.
5956 @var{e} is one of the following:
5961 Hex encoding (brackets coding also recognized)
5964 Upper half encoding (brackets encoding also recognized)
5967 Shift/JIS encoding (brackets encoding also recognized)
5970 EUC encoding (brackets encoding also recognized)
5973 UTF-8 encoding (brackets encoding also recognized)
5976 Brackets encoding only (default value)
5978 For full details on the these encoding
5979 methods see @xref{Wide Character Encodings}.
5980 Note that brackets coding is always accepted, even if one of the other
5981 options is specified, so for example @option{-gnatW8} specifies that both
5982 brackets and @code{UTF-8} encodings will be recognized. The units that are
5983 with'ed directly or indirectly will be scanned using the specified
5984 representation scheme, and so if one of the non-brackets scheme is
5985 used, it must be used consistently throughout the program. However,
5986 since brackets encoding is always recognized, it may be conveniently
5987 used in standard libraries, allowing these libraries to be used with
5988 any of the available coding schemes.
5989 scheme. If no @option{-gnatW?} parameter is present, then the default
5990 representation is Brackets encoding only.
5992 Note that the wide character representation that is specified (explicitly
5993 or by default) for the main program also acts as the default encoding used
5994 for Wide_Text_IO files if not specifically overridden by a WCEM form
5998 @node File Naming Control
5999 @subsection File Naming Control
6002 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
6003 @cindex @option{-gnatk} (@code{gcc})
6004 Activates file name ``krunching''. @var{n}, a decimal integer in the range
6005 1-999, indicates the maximum allowable length of a file name (not
6006 including the @file{.ads} or @file{.adb} extension). The default is not
6007 to enable file name krunching.
6009 For the source file naming rules, @xref{File Naming Rules}.
6013 @node Subprogram Inlining Control
6014 @subsection Subprogram Inlining Control
6019 @cindex @option{-gnatn} (@code{gcc})
6021 The @code{n} here is intended to suggest the first syllable of the
6024 GNAT recognizes and processes @code{Inline} pragmas. However, for the
6025 inlining to actually occur, optimization must be enabled. To enable
6026 inlining of subprograms specified by pragma @code{Inline},
6027 you must also specify this switch.
6028 In the absence of this switch, GNAT does not attempt
6029 inlining and does not need to access the bodies of
6030 subprograms for which @code{pragma Inline} is specified if they are not
6031 in the current unit.
6033 If you specify this switch the compiler will access these bodies,
6034 creating an extra source dependency for the resulting object file, and
6035 where possible, the call will be inlined.
6036 For further details on when inlining is possible
6037 see @xref{Inlining of Subprograms}.
6040 @cindex @option{-gnatN} (@code{gcc})
6041 The front end inlining activated by this switch is generally more extensive,
6042 and quite often more effective than the standard @option{-gnatn} inlining mode.
6043 It will also generate additional dependencies.
6045 @option{-gnatN} automatically implies @option{-gnatn} so it is not necessary
6046 to specify both options.
6049 @node Auxiliary Output Control
6050 @subsection Auxiliary Output Control
6054 @cindex @option{-gnatt} (@code{gcc})
6055 @cindex Writing internal trees
6056 @cindex Internal trees, writing to file
6057 Causes GNAT to write the internal tree for a unit to a file (with the
6058 extension @file{.adt}.
6059 This not normally required, but is used by separate analysis tools.
6061 these tools do the necessary compilations automatically, so you should
6062 not have to specify this switch in normal operation.
6065 @cindex @option{-gnatu} (@code{gcc})
6066 Print a list of units required by this compilation on @file{stdout}.
6067 The listing includes all units on which the unit being compiled depends
6068 either directly or indirectly.
6071 @item -pass-exit-codes
6072 @cindex @option{-pass-exit-codes} (@code{gcc})
6073 If this switch is not used, the exit code returned by @code{gcc} when
6074 compiling multiple files indicates whether all source files have
6075 been successfully used to generate object files or not.
6077 When @option{-pass-exit-codes} is used, @code{gcc} exits with an extended
6078 exit status and allows an integrated development environment to better
6079 react to a compilation failure. Those exit status are:
6083 There was an error in at least one source file.
6085 At least one source file did not generate an object file.
6087 The compiler died unexpectedly (internal error for example).
6089 An object file has been generated for every source file.
6094 @node Debugging Control
6095 @subsection Debugging Control
6099 @cindex Debugging options
6102 @cindex @option{-gnatd} (@code{gcc})
6103 Activate internal debugging switches. @var{x} is a letter or digit, or
6104 string of letters or digits, which specifies the type of debugging
6105 outputs desired. Normally these are used only for internal development
6106 or system debugging purposes. You can find full documentation for these
6107 switches in the body of the @code{Debug} unit in the compiler source
6108 file @file{debug.adb}.
6112 @cindex @option{-gnatG} (@code{gcc})
6113 This switch causes the compiler to generate auxiliary output containing
6114 a pseudo-source listing of the generated expanded code. Like most Ada
6115 compilers, GNAT works by first transforming the high level Ada code into
6116 lower level constructs. For example, tasking operations are transformed
6117 into calls to the tasking run-time routines. A unique capability of GNAT
6118 is to list this expanded code in a form very close to normal Ada source.
6119 This is very useful in understanding the implications of various Ada
6120 usage on the efficiency of the generated code. There are many cases in
6121 Ada (e.g. the use of controlled types), where simple Ada statements can
6122 generate a lot of run-time code. By using @option{-gnatG} you can identify
6123 these cases, and consider whether it may be desirable to modify the coding
6124 approach to improve efficiency.
6126 The format of the output is very similar to standard Ada source, and is
6127 easily understood by an Ada programmer. The following special syntactic
6128 additions correspond to low level features used in the generated code that
6129 do not have any exact analogies in pure Ada source form. The following
6130 is a partial list of these special constructions. See the specification
6131 of package @code{Sprint} in file @file{sprint.ads} for a full list.
6134 @item new @var{xxx} [storage_pool = @var{yyy}]
6135 Shows the storage pool being used for an allocator.
6137 @item at end @var{procedure-name};
6138 Shows the finalization (cleanup) procedure for a scope.
6140 @item (if @var{expr} then @var{expr} else @var{expr})
6141 Conditional expression equivalent to the @code{x?y:z} construction in C.
6143 @item @var{target}^^^(@var{source})
6144 A conversion with floating-point truncation instead of rounding.
6146 @item @var{target}?(@var{source})
6147 A conversion that bypasses normal Ada semantic checking. In particular
6148 enumeration types and fixed-point types are treated simply as integers.
6150 @item @var{target}?^^^(@var{source})
6151 Combines the above two cases.
6153 @item @var{x} #/ @var{y}
6154 @itemx @var{x} #mod @var{y}
6155 @itemx @var{x} #* @var{y}
6156 @itemx @var{x} #rem @var{y}
6157 A division or multiplication of fixed-point values which are treated as
6158 integers without any kind of scaling.
6160 @item free @var{expr} [storage_pool = @var{xxx}]
6161 Shows the storage pool associated with a @code{free} statement.
6163 @item freeze @var{typename} [@var{actions}]
6164 Shows the point at which @var{typename} is frozen, with possible
6165 associated actions to be performed at the freeze point.
6167 @item reference @var{itype}
6168 Reference (and hence definition) to internal type @var{itype}.
6170 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
6171 Intrinsic function call.
6173 @item @var{labelname} : label
6174 Declaration of label @var{labelname}.
6176 @item @var{expr} && @var{expr} && @var{expr} ... && @var{expr}
6177 A multiple concatenation (same effect as @var{expr} & @var{expr} &
6178 @var{expr}, but handled more efficiently).
6180 @item [constraint_error]
6181 Raise the @code{Constraint_Error} exception.
6183 @item @var{expression}'reference
6184 A pointer to the result of evaluating @var{expression}.
6186 @item @var{target-type}!(@var{source-expression})
6187 An unchecked conversion of @var{source-expression} to @var{target-type}.
6189 @item [@var{numerator}/@var{denominator}]
6190 Used to represent internal real literals (that) have no exact
6191 representation in base 2-16 (for example, the result of compile time
6192 evaluation of the expression 1.0/27.0).
6196 @cindex @option{-gnatD} (@code{gcc})
6197 When used in conjunction with @option{-gnatG}, this switch causes
6198 the expanded source, as described above for
6199 @option{-gnatG} to be written to files with names
6200 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
6201 instead of to the standard ooutput file. For
6202 example, if the source file name is @file{hello.adb}, then a file
6203 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
6204 information generated by the @code{gcc} @option{^-g^/DEBUG^} switch
6205 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
6206 you to do source level debugging using the generated code which is
6207 sometimes useful for complex code, for example to find out exactly
6208 which part of a complex construction raised an exception. This switch
6209 also suppress generation of cross-reference information (see
6210 @option{-gnatx}) since otherwise the cross-reference information
6211 would refer to the @file{^.dg^.DG^} file, which would cause
6212 confusion since this is not the original source file.
6214 Note that @option{-gnatD} actually implies @option{-gnatG}
6215 automatically, so it is not necessary to give both options.
6216 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
6219 @item -gnatR[0|1|2|3[s]]
6220 @cindex @option{-gnatR} (@code{gcc})
6221 This switch controls output from the compiler of a listing showing
6222 representation information for declared types and objects. For
6223 @option{-gnatR0}, no information is output (equivalent to omitting
6224 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
6225 so @option{-gnatR} with no parameter has the same effect), size and alignment
6226 information is listed for declared array and record types. For
6227 @option{-gnatR2}, size and alignment information is listed for all
6228 expression information for values that are computed at run time for
6229 variant records. These symbolic expressions have a mostly obvious
6230 format with #n being used to represent the value of the n'th
6231 discriminant. See source files @file{repinfo.ads/adb} in the
6232 @code{GNAT} sources for full details on the format of @option{-gnatR3}
6233 output. If the switch is followed by an s (e.g. @option{-gnatR2s}), then
6234 the output is to a file with the name @file{^file.rep^file_REP^} where
6235 file is the name of the corresponding source file.
6238 @item /REPRESENTATION_INFO
6239 @cindex @option{/REPRESENTATION_INFO} (@code{gcc})
6240 This qualifier controls output from the compiler of a listing showing
6241 representation information for declared types and objects. For
6242 @option{/REPRESENTATION_INFO=NONE}, no information is output
6243 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
6244 @option{/REPRESENTATION_INFO} without option is equivalent to
6245 @option{/REPRESENTATION_INFO=ARRAYS}.
6246 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
6247 information is listed for declared array and record types. For
6248 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
6249 is listed for all expression information for values that are computed
6250 at run time for variant records. These symbolic expressions have a mostly
6251 obvious format with #n being used to represent the value of the n'th
6252 discriminant. See source files @file{REPINFO.ADS/ADB} in the
6253 @code{GNAT} sources for full details on the format of
6254 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
6255 If _FILE is added at the end of an option
6256 (e.g. @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
6257 then the output is to a file with the name @file{file_REP} where
6258 file is the name of the corresponding source file.
6262 @cindex @option{-gnatS} (@code{gcc})
6263 The use of the switch @option{-gnatS} for an
6264 Ada compilation will cause the compiler to output a
6265 representation of package Standard in a form very
6266 close to standard Ada. It is not quite possible to
6267 do this and remain entirely Standard (since new
6268 numeric base types cannot be created in standard
6269 Ada), but the output is easily
6270 readable to any Ada programmer, and is useful to
6271 determine the characteristics of target dependent
6272 types in package Standard.
6275 @cindex @option{-gnatx} (@code{gcc})
6276 Normally the compiler generates full cross-referencing information in
6277 the @file{ALI} file. This information is used by a number of tools,
6278 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
6279 suppresses this information. This saves some space and may slightly
6280 speed up compilation, but means that these tools cannot be used.
6283 @node Exception Handling Control
6284 @subsection Exception Handling Control
6287 GNAT uses two methods for handling exceptions at run-time. The
6288 @code{longjmp/setjmp} method saves the context when entering
6289 a frame with an exception handler. Then when an exception is
6290 raised, the context can be restored immediately, without the
6291 need for tracing stack frames. This method provides very fast
6292 exception propagation, but introduces significant overhead for
6293 the use of exception handlers, even if no exception is raised.
6295 The other approach is called ``zero cost'' exception handling.
6296 With this method, the compiler builds static tables to describe
6297 the exception ranges. No dynamic code is required when entering
6298 a frame containing an exception handler. When an exception is
6299 raised, the tables are used to control a back trace of the
6300 subprogram invocation stack to locate the required exception
6301 handler. This method has considerably poorer performance for
6302 the propagation of exceptions, but there is no overhead for
6303 exception handlers if no exception is raised.
6305 The following switches can be used to control which of the
6306 two exception handling methods is used.
6312 @cindex @option{-gnatL} (@code{gcc})
6313 This switch causes the longjmp/setjmp approach to be used
6314 for exception handling. If this is the default mechanism for the
6315 target (see below), then this has no effect. If the default
6316 mechanism for the target is zero cost exceptions, then
6317 this switch can be used to modify this default, but it must be
6318 used for all units in the partition, including all run-time
6319 library units. One way to achieve this is to use the
6320 @option{-a} and @option{-f} switches for @code{gnatmake}.
6321 This option is rarely used. One case in which it may be
6322 advantageous is if you have an application where exception
6323 raising is common and the overall performance of the
6324 application is improved by favoring exception propagation.
6327 @cindex @option{-gnatZ} (@code{gcc})
6328 @cindex Zero Cost Exceptions
6329 This switch causes the zero cost approach to be sed
6330 for exception handling. If this is the default mechanism for the
6331 target (see below), then this has no effect. If the default
6332 mechanism for the target is longjmp/setjmp exceptions, then
6333 this switch can be used to modify this default, but it must be
6334 used for all units in the partition, including all run-time
6335 library units. One way to achieve this is to use the
6336 @option{-a} and @option{-f} switches for @code{gnatmake}.
6337 This option can only be used if the zero cost approach
6338 is available for the target in use (see below).
6342 The @code{longjmp/setjmp} approach is available on all targets, but
6343 the @code{zero cost} approach is only available on selected targets.
6344 To determine whether zero cost exceptions can be used for a
6345 particular target, look at the private part of the file system.ads.
6346 Either @code{GCC_ZCX_Support} or @code{Front_End_ZCX_Support} must
6347 be True to use the zero cost approach. If both of these switches
6348 are set to False, this means that zero cost exception handling
6349 is not yet available for that target. The switch
6350 @code{ZCX_By_Default} indicates the default approach. If this
6351 switch is set to True, then the @code{zero cost} approach is
6354 @node Units to Sources Mapping Files
6355 @subsection Units to Sources Mapping Files
6359 @item -gnatem^^=^@var{path}
6360 @cindex @option{-gnatem} (@code{gcc})
6361 A mapping file is a way to communicate to the compiler two mappings:
6362 from unit names to file names (without any directory information) and from
6363 file names to path names (with full directory information). These mappings
6364 are used by the compiler to short-circuit the path search.
6366 The use of mapping files is not required for correct operation of the
6367 compiler, but mapping files can improve efficiency, particularly when
6368 sources are read over a slow network connection. In normal operation,
6369 you need not be concerned with the format or use of mapping files,
6370 and the @option{-gnatem} switch is not a switch that you would use
6371 explicitly. it is intended only for use by automatic tools such as
6372 @code{gnatmake} running under the project file facility. The
6373 description here of the format of mapping files is provided
6374 for completeness and for possible use by other tools.
6376 A mapping file is a sequence of sets of three lines. In each set,
6377 the first line is the unit name, in lower case, with ``@code{%s}''
6379 specifications and ``@code{%b}'' appended for bodies; the second line is the
6380 file name; and the third line is the path name.
6386 /gnat/project1/sources/main.2.ada
6389 When the switch @option{-gnatem} is specified, the compiler will create
6390 in memory the two mappings from the specified file. If there is any problem
6391 (non existent file, truncated file or duplicate entries), no mapping
6394 Several @option{-gnatem} switches may be specified; however, only the last
6395 one on the command line will be taken into account.
6397 When using a project file, @code{gnatmake} create a temporary mapping file
6398 and communicates it to the compiler using this switch.
6403 @node Integrated Preprocessing
6404 @subsection Integrated Preprocessing
6407 GNAT sources may be preprocessed immediately before compilation; the actual
6408 text of the source is not the text of the source file, but is derived from it
6409 through a process called preprocessing. Integrated preprocessing is specified
6410 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
6411 indicates, through a text file, the preprocessing data to be used.
6412 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
6415 It is recommended that @code{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
6416 used when Integrated Preprocessing is used. The reason is that preprocessing
6417 with another Preprocessing Data file without changing the sources will
6418 not trigger recompilation without this switch.
6421 Note that @code{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
6422 always trigger recompilation for sources that are preprocessed,
6423 because @code{gnatmake} cannot compute the checksum of the source after
6427 The actual preprocessing function is described in details in section
6428 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
6429 preprocessing is triggered and parameterized.
6433 @item -gnatep=@var{file}
6434 @cindex @option{-gnatep} (@code{gcc})
6435 This switch indicates to the compiler the file name (without directory
6436 information) of the preprocessor data file to use. The preprocessor data file
6437 should be found in the source directories.
6440 A preprocessing data file is a text file with significant lines indicating
6441 how should be preprocessed either a specific source or all sources not
6442 mentioned in other lines. A significant line is a non empty, non comment line.
6443 Comments are similar to Ada comments.
6446 Each significant line starts with either a literal string or the character '*'.
6447 A literal string is the file name (without directory information) of the source
6448 to preprocess. A character '*' indicates the preprocessing for all the sources
6449 that are not specified explicitly on other lines (order of the lines is not
6450 significant). It is an error to have two lines with the same file name or two
6451 lines starting with the character '*'.
6454 After the file name or the character '*', another optional literal string
6455 indicating the file name of the definition file to be used for preprocessing.
6456 (see @ref{Form of Definitions File}. The definition files are found by the
6457 compiler in one of the source directories. In some cases, when compiling
6458 a source in a directory other than the current directory, if the definition
6459 file is in the current directory, it may be necessary to add the current
6460 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
6461 the compiler would not find the definition file.
6464 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
6465 be found. Those ^switches^switches^ are:
6470 Causes both preprocessor lines and the lines deleted by
6471 preprocessing to be replaced by blank lines, preserving the line number.
6472 This ^switch^switch^ is always implied; however, if specified after @option{-c}
6473 it cancels the effect of @option{-c}.
6476 Causes both preprocessor lines and the lines deleted
6477 by preprocessing to be retained as comments marked
6478 with the special string ``@code{--! }''.
6480 @item -Dsymbol=value
6481 Define or redefine a symbol, associated with value. A symbol is an Ada
6482 identifier, or an Ada reserved word, with the exception of @code{if},
6483 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6484 @code{value} is either a literal string, an Ada identifier or any Ada reserved
6485 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
6486 same name defined in a definition file.
6489 Causes a sorted list of symbol names and values to be
6490 listed on the standard output file.
6493 Causes undefined symbols to be treated as having the value @code{FALSE}
6495 of a preprocessor test. In the absence of this option, an undefined symbol in
6496 a @code{#if} or @code{#elsif} test will be treated as an error.
6501 Examples of valid lines in a preprocessor data file:
6504 "toto.adb" "prep.def" -u
6505 -- preprocess "toto.adb", using definition file "prep.def",
6506 -- undefined symbol are False.
6509 -- preprocess all other sources without a definition file;
6510 -- suppressed lined are commented; symbol VERSION has the value V101.
6512 "titi.adb" "prep2.def" -s
6513 -- preprocess "titi.adb", using definition file "prep2.def";
6514 -- list all symbols with their values.
6517 @item ^-gnateD^/DATA_PREPROCESSING=^symbol[=value]
6518 @cindex @option{-gnateD} (@code{gcc})
6519 Define or redefine a preprocessing symbol, associated with value. If no value
6520 is given on the command line, then the value of the symbol is @code{True}.
6521 A symbol is an identifier, following normal Ada (case-insensitive)
6522 rules for its syntax, and value is any sequence (including an empty sequence)
6523 of characters from the set (letters, digits, period, underline).
6524 Ada reserved words may be used as symbols, with the exceptions of @code{if},
6525 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
6528 A symbol declared with this ^switch^switch^ on the command line replaces a
6529 symbol with the same name either in a definition file or specified with a
6530 ^switch^switch^ -D in the preprocessor data file.
6533 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
6539 @subsection Return Codes
6540 @cindex Return Codes
6541 @cindex @option{/RETURN_CODES=VMS}
6544 On VMS, GNAT compiled programs return POSIX-style codes by default,
6545 e.g. @option{/RETURN_CODES=POSIX}.
6547 To enable VMS style return codes, GNAT LINK with the option
6548 @option{/RETURN_CODES=VMS}. For example:
6551 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
6555 Programs built with /RETURN_CODES=VMS are suitable to be called in
6556 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
6557 are suitable for spawning with appropriate GNAT RTL routines.
6562 @node Search Paths and the Run-Time Library (RTL)
6563 @section Search Paths and the Run-Time Library (RTL)
6566 With the GNAT source-based library system, the compiler must be able to
6567 find source files for units that are needed by the unit being compiled.
6568 Search paths are used to guide this process.
6570 The compiler compiles one source file whose name must be given
6571 explicitly on the command line. In other words, no searching is done
6572 for this file. To find all other source files that are needed (the most
6573 common being the specs of units), the compiler examines the following
6574 directories, in the following order:
6578 The directory containing the source file of the main unit being compiled
6579 (the file name on the command line).
6582 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
6583 @code{gcc} command line, in the order given.
6586 @findex ADA_INCLUDE_PATH
6587 Each of the directories listed in the value of the
6588 @code{ADA_INCLUDE_PATH} ^environment variable^logical name^.
6590 Construct this value
6591 exactly as the @code{PATH} environment variable: a list of directory
6592 names separated by colons (semicolons when working with the NT version).
6595 Normally, define this value as a logical name containing a comma separated
6596 list of directory names.
6598 This variable can also be defined by means of an environment string
6599 (an argument to the DEC C exec* set of functions).
6603 DEFINE ANOTHER_PATH FOO:[BAG]
6604 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
6607 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
6608 first, followed by the standard Ada 95
6609 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
6610 If this is not redefined, the user will obtain the DEC Ada 83 IO packages
6611 (Text_IO, Sequential_IO, etc)
6612 instead of the Ada95 packages. Thus, in order to get the Ada 95
6613 packages by default, ADA_INCLUDE_PATH must be redefined.
6617 @findex ADA_PRJ_INCLUDE_FILE
6618 Each of the directories listed in the text file whose name is given
6619 by the @code{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
6622 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
6623 driver when project files are used. It should not normally be set
6627 The content of the @file{ada_source_path} file which is part of the GNAT
6628 installation tree and is used to store standard libraries such as the
6629 GNAT Run Time Library (RTL) source files.
6631 @ref{Installing an Ada Library}
6636 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
6637 inhibits the use of the directory
6638 containing the source file named in the command line. You can still
6639 have this directory on your search path, but in this case it must be
6640 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
6642 Specifying the switch @option{-nostdinc}
6643 inhibits the search of the default location for the GNAT Run Time
6644 Library (RTL) source files.
6646 The compiler outputs its object files and ALI files in the current
6649 Caution: The object file can be redirected with the @option{-o} switch;
6650 however, @code{gcc} and @code{gnat1} have not been coordinated on this
6651 so the @file{ALI} file will not go to the right place. Therefore, you should
6652 avoid using the @option{-o} switch.
6656 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
6657 children make up the GNAT RTL, together with the simple @code{System.IO}
6658 package used in the @code{"Hello World"} example. The sources for these units
6659 are needed by the compiler and are kept together in one directory. Not
6660 all of the bodies are needed, but all of the sources are kept together
6661 anyway. In a normal installation, you need not specify these directory
6662 names when compiling or binding. Either the environment variables or
6663 the built-in defaults cause these files to be found.
6665 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
6666 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
6667 consisting of child units of @code{GNAT}. This is a collection of generally
6668 useful types, subprograms, etc. See the @cite{GNAT Reference Manual} for
6671 Besides simplifying access to the RTL, a major use of search paths is
6672 in compiling sources from multiple directories. This can make
6673 development environments much more flexible.
6676 @node Order of Compilation Issues
6677 @section Order of Compilation Issues
6680 If, in our earlier example, there was a spec for the @code{hello}
6681 procedure, it would be contained in the file @file{hello.ads}; yet this
6682 file would not have to be explicitly compiled. This is the result of the
6683 model we chose to implement library management. Some of the consequences
6684 of this model are as follows:
6688 There is no point in compiling specs (except for package
6689 specs with no bodies) because these are compiled as needed by clients. If
6690 you attempt a useless compilation, you will receive an error message.
6691 It is also useless to compile subunits because they are compiled as needed
6695 There are no order of compilation requirements: performing a
6696 compilation never obsoletes anything. The only way you can obsolete
6697 something and require recompilations is to modify one of the
6698 source files on which it depends.
6701 There is no library as such, apart from the ALI files
6702 (@pxref{The Ada Library Information Files}, for information on the format
6703 of these files). For now we find it convenient to create separate ALI files,
6704 but eventually the information therein may be incorporated into the object
6708 When you compile a unit, the source files for the specs of all units
6709 that it @code{with}'s, all its subunits, and the bodies of any generics it
6710 instantiates must be available (reachable by the search-paths mechanism
6711 described above), or you will receive a fatal error message.
6718 The following are some typical Ada compilation command line examples:
6721 @item $ gcc -c xyz.adb
6722 Compile body in file @file{xyz.adb} with all default options.
6725 @item $ gcc -c -O2 -gnata xyz-def.adb
6728 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
6731 Compile the child unit package in file @file{xyz-def.adb} with extensive
6732 optimizations, and pragma @code{Assert}/@code{Debug} statements
6735 @item $ gcc -c -gnatc abc-def.adb
6736 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
6740 @node Binding Using gnatbind
6741 @chapter Binding Using @code{gnatbind}
6745 * Running gnatbind::
6746 * Switches for gnatbind::
6747 * Command-Line Access::
6748 * Search Paths for gnatbind::
6749 * Examples of gnatbind Usage::
6753 This chapter describes the GNAT binder, @code{gnatbind}, which is used
6754 to bind compiled GNAT objects. The @code{gnatbind} program performs
6755 four separate functions:
6759 Checks that a program is consistent, in accordance with the rules in
6760 Chapter 10 of the Ada 95 Reference Manual. In particular, error
6761 messages are generated if a program uses inconsistent versions of a
6765 Checks that an acceptable order of elaboration exists for the program
6766 and issues an error message if it cannot find an order of elaboration
6767 that satisfies the rules in Chapter 10 of the Ada 95 Language Manual.
6770 Generates a main program incorporating the given elaboration order.
6771 This program is a small Ada package (body and spec) that
6772 must be subsequently compiled
6773 using the GNAT compiler. The necessary compilation step is usually
6774 performed automatically by @code{gnatlink}. The two most important
6775 functions of this program
6776 are to call the elaboration routines of units in an appropriate order
6777 and to call the main program.
6780 Determines the set of object files required by the given main program.
6781 This information is output in the forms of comments in the generated program,
6782 to be read by the @code{gnatlink} utility used to link the Ada application.
6786 @node Running gnatbind
6787 @section Running @code{gnatbind}
6790 The form of the @code{gnatbind} command is
6793 $ gnatbind [@i{switches}] @i{mainprog}[.ali] [@i{switches}]
6797 where @file{@i{mainprog}.adb} is the Ada file containing the main program
6798 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
6799 package in two files whose names are
6800 @file{b~@i{mainprog}.ads}, and @file{b~@i{mainprog}.adb}.
6801 For example, if given the
6802 parameter @file{hello.ali}, for a main program contained in file
6803 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
6804 and @file{b~hello.adb}.
6806 When doing consistency checking, the binder takes into consideration
6807 any source files it can locate. For example, if the binder determines
6808 that the given main program requires the package @code{Pack}, whose
6810 file is @file{pack.ali} and whose corresponding source spec file is
6811 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
6812 (using the same search path conventions as previously described for the
6813 @code{gcc} command). If it can locate this source file, it checks that
6815 or source checksums of the source and its references to in @file{ALI} files
6816 match. In other words, any @file{ALI} files that mentions this spec must have
6817 resulted from compiling this version of the source file (or in the case
6818 where the source checksums match, a version close enough that the
6819 difference does not matter).
6821 @cindex Source files, use by binder
6822 The effect of this consistency checking, which includes source files, is
6823 that the binder ensures that the program is consistent with the latest
6824 version of the source files that can be located at bind time. Editing a
6825 source file without compiling files that depend on the source file cause
6826 error messages to be generated by the binder.
6828 For example, suppose you have a main program @file{hello.adb} and a
6829 package @code{P}, from file @file{p.ads} and you perform the following
6834 Enter @code{gcc -c hello.adb} to compile the main program.
6837 Enter @code{gcc -c p.ads} to compile package @code{P}.
6840 Edit file @file{p.ads}.
6843 Enter @code{gnatbind hello}.
6847 At this point, the file @file{p.ali} contains an out-of-date time stamp
6848 because the file @file{p.ads} has been edited. The attempt at binding
6849 fails, and the binder generates the following error messages:
6852 error: "hello.adb" must be recompiled ("p.ads" has been modified)
6853 error: "p.ads" has been modified and must be recompiled
6857 Now both files must be recompiled as indicated, and then the bind can
6858 succeed, generating a main program. You need not normally be concerned
6859 with the contents of this file, but for reference purposes a sample
6860 binder output file is given in @ref{Example of Binder Output File}.
6862 In most normal usage, the default mode of @command{gnatbind} which is to
6863 generate the main package in Ada, as described in the previous section.
6864 In particular, this means that any Ada programmer can read and understand
6865 the generated main program. It can also be debugged just like any other
6866 Ada code provided the @option{^-g^/DEBUG^} switch is used for
6867 @command{gnatbind} and @command{gnatlink}.
6869 However for some purposes it may be convenient to generate the main
6870 program in C rather than Ada. This may for example be helpful when you
6871 are generating a mixed language program with the main program in C. The
6872 GNAT compiler itself is an example.
6873 The use of the @option{^-C^/BIND_FILE=C^} switch
6874 for both @code{gnatbind} and @code{gnatlink} will cause the program to
6875 be generated in C (and compiled using the gnu C compiler).
6878 @node Switches for gnatbind
6879 @section Switches for @command{gnatbind}
6882 The following switches are available with @code{gnatbind}; details will
6883 be presented in subsequent sections.
6886 * Consistency-Checking Modes::
6887 * Binder Error Message Control::
6888 * Elaboration Control::
6890 * Binding with Non-Ada Main Programs::
6891 * Binding Programs with No Main Subprogram::
6896 @item ^-aO^/OBJECT_SEARCH^
6897 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
6898 Specify directory to be searched for ALI files.
6900 @item ^-aI^/SOURCE_SEARCH^
6901 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
6902 Specify directory to be searched for source file.
6904 @item ^-A^/BIND_FILE=ADA^
6905 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
6906 Generate binder program in Ada (default)
6908 @item ^-b^/REPORT_ERRORS=BRIEF^
6909 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
6910 Generate brief messages to @file{stderr} even if verbose mode set.
6912 @item ^-c^/NOOUTPUT^
6913 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
6914 Check only, no generation of binder output file.
6916 @item ^-C^/BIND_FILE=C^
6917 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
6918 Generate binder program in C
6920 @item ^-e^/ELABORATION_DEPENDENCIES^
6921 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
6922 Output complete list of elaboration-order dependencies.
6924 @item ^-E^/STORE_TRACEBACKS^
6925 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
6926 Store tracebacks in exception occurrences when the target supports it.
6927 This is the default with the zero cost exception mechanism.
6929 @c The following may get moved to an appendix
6930 This option is currently supported on the following targets:
6931 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
6933 See also the packages @code{GNAT.Traceback} and
6934 @code{GNAT.Traceback.Symbolic} for more information.
6936 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
6940 @item ^-F^/FORCE_ELABS_FLAGS^
6941 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
6942 Force the checks of elaboration flags. @command{gnatbind} does not normally
6943 generate checks of elaboration flags for the main executable, except when
6944 a Stand-Alone Library is used. However, there are cases when this cannot be
6945 detected by gnatbind. An example is importing an interface of a Stand-Alone
6946 Library through a pragma Import and only specifying through a linker switch
6947 this Stand-Alone Library. This switch is used to guarantee that elaboration
6948 flag checks are generated.
6951 @cindex @option{^-h^/HELP^} (@command{gnatbind})
6952 Output usage (help) information
6955 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
6956 Specify directory to be searched for source and ALI files.
6958 @item ^-I-^/NOCURRENT_DIRECTORY^
6959 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
6960 Do not look for sources in the current directory where @code{gnatbind} was
6961 invoked, and do not look for ALI files in the directory containing the
6962 ALI file named in the @code{gnatbind} command line.
6964 @item ^-l^/ORDER_OF_ELABORATION^
6965 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
6966 Output chosen elaboration order.
6968 @item ^-Lxxx^/BUILD_LIBRARY=xxx^
6969 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
6970 Binds the units for library building. In this case the adainit and
6971 adafinal procedures (See @pxref{Binding with Non-Ada Main Programs})
6972 are renamed to ^xxxinit^XXXINIT^ and
6973 ^xxxfinal^XXXFINAL^.
6974 Implies ^-n^/NOCOMPILE^.
6976 (@pxref{GNAT and Libraries}, for more details.)
6979 On OpenVMS, these init and final procedures are exported in uppercase
6980 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
6981 the init procedure will be "TOTOINIT" and the exported name of the final
6982 procedure will be "TOTOFINAL".
6985 @item ^-Mxyz^/RENAME_MAIN=xyz^
6986 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
6987 Rename generated main program from main to xyz
6989 @item ^-m^/ERROR_LIMIT=^@var{n}
6990 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
6991 Limit number of detected errors to @var{n}, where @var{n} is
6992 in the range 1..999_999. The default value if no switch is
6993 given is 9999. Binding is terminated if the limit is exceeded.
6995 Furthermore, under Windows, the sources pointed to by the libraries path
6996 set in the registry are not searched for.
7000 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7004 @cindex @option{-nostdinc} (@command{gnatbind})
7005 Do not look for sources in the system default directory.
7008 @cindex @option{-nostdlib} (@command{gnatbind})
7009 Do not look for library files in the system default directory.
7011 @item --RTS=@var{rts-path}
7012 @cindex @option{--RTS} (@code{gnatbind})
7013 Specifies the default location of the runtime library. Same meaning as the
7014 equivalent @code{gnatmake} flag (see @ref{Switches for gnatmake}).
7016 @item ^-o ^/OUTPUT=^@var{file}
7017 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
7018 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
7019 Note that if this option is used, then linking must be done manually,
7020 gnatlink cannot be used.
7022 @item ^-O^/OBJECT_LIST^
7023 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
7026 @item ^-p^/PESSIMISTIC_ELABORATION^
7027 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
7028 Pessimistic (worst-case) elaboration order
7030 @item ^-s^/READ_SOURCES=ALL^
7031 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
7032 Require all source files to be present.
7034 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
7035 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
7036 Specifies the value to be used when detecting uninitialized scalar
7037 objects with pragma Initialize_Scalars.
7038 The @var{xxx} ^string specified with the switch^option^ may be either
7040 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
7041 @item ``@option{^lo^LOW^}'' for the lowest possible value
7042 possible, and the low
7043 @item ``@option{^hi^HIGH^}'' for the highest possible value
7044 @item ``@option{xx}'' for a value consisting of repeated bytes with the
7045 value 16#xx# (i.e. xx is a string of two hexadecimal digits).
7048 In addition, you can specify @option{-Sev} to indicate that the value is
7049 to be set at run time. In this case, the program will look for an environment
7050 @cindex GNAT_INIT_SCALARS
7051 variable of the form @code{GNAT_INIT_SCALARS=xx}, where xx is one
7052 of @option{in/lo/hi/xx} with the same meanings as above.
7053 If no environment variable is found, or if it does not have a valid value,
7054 then the default is @option{in} (invalid values).
7058 @cindex @option{-static} (@code{gnatbind})
7059 Link against a static GNAT run time.
7062 @cindex @option{-shared} (@code{gnatbind})
7063 Link against a shared GNAT run time when available.
7066 @item ^-t^/NOTIME_STAMP_CHECK^
7067 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7068 Tolerate time stamp and other consistency errors
7070 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
7071 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
7072 Set the time slice value to @var{n} milliseconds. If the system supports
7073 the specification of a specific time slice value, then the indicated value
7074 is used. If the system does not support specific time slice values, but
7075 does support some general notion of round-robin scheduling, then any
7076 non-zero value will activate round-robin scheduling.
7078 A value of zero is treated specially. It turns off time
7079 slicing, and in addition, indicates to the tasking run time that the
7080 semantics should match as closely as possible the Annex D
7081 requirements of the Ada RM, and in particular sets the default
7082 scheduling policy to @code{FIFO_Within_Priorities}.
7084 @item ^-v^/REPORT_ERRORS=VERBOSE^
7085 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7086 Verbose mode. Write error messages, header, summary output to
7091 @cindex @option{-w} (@code{gnatbind})
7092 Warning mode (@var{x}=s/e for suppress/treat as error)
7096 @item /WARNINGS=NORMAL
7097 @cindex @option{/WARNINGS} (@code{gnatbind})
7098 Normal warnings mode. Warnings are issued but ignored
7100 @item /WARNINGS=SUPPRESS
7101 @cindex @option{/WARNINGS} (@code{gnatbind})
7102 All warning messages are suppressed
7104 @item /WARNINGS=ERROR
7105 @cindex @option{/WARNINGS} (@code{gnatbind})
7106 Warning messages are treated as fatal errors
7109 @item ^-x^/READ_SOURCES=NONE^
7110 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
7111 Exclude source files (check object consistency only).
7114 @item /READ_SOURCES=AVAILABLE
7115 @cindex @option{/READ_SOURCES} (@code{gnatbind})
7116 Default mode, in which sources are checked for consistency only if
7120 @item ^-z^/ZERO_MAIN^
7121 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7127 You may obtain this listing of switches by running @code{gnatbind} with
7132 @node Consistency-Checking Modes
7133 @subsection Consistency-Checking Modes
7136 As described earlier, by default @code{gnatbind} checks
7137 that object files are consistent with one another and are consistent
7138 with any source files it can locate. The following switches control binder
7143 @item ^-s^/READ_SOURCES=ALL^
7144 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
7145 Require source files to be present. In this mode, the binder must be
7146 able to locate all source files that are referenced, in order to check
7147 their consistency. In normal mode, if a source file cannot be located it
7148 is simply ignored. If you specify this switch, a missing source
7151 @item ^-x^/READ_SOURCES=NONE^
7152 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
7153 Exclude source files. In this mode, the binder only checks that ALI
7154 files are consistent with one another. Source files are not accessed.
7155 The binder runs faster in this mode, and there is still a guarantee that
7156 the resulting program is self-consistent.
7157 If a source file has been edited since it was last compiled, and you
7158 specify this switch, the binder will not detect that the object
7159 file is out of date with respect to the source file. Note that this is the
7160 mode that is automatically used by @code{gnatmake} because in this
7161 case the checking against sources has already been performed by
7162 @code{gnatmake} in the course of compilation (i.e. before binding).
7165 @item /READ_SOURCES=AVAILABLE
7166 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
7167 This is the default mode in which source files are checked if they are
7168 available, and ignored if they are not available.
7172 @node Binder Error Message Control
7173 @subsection Binder Error Message Control
7176 The following switches provide control over the generation of error
7177 messages from the binder:
7181 @item ^-v^/REPORT_ERRORS=VERBOSE^
7182 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
7183 Verbose mode. In the normal mode, brief error messages are generated to
7184 @file{stderr}. If this switch is present, a header is written
7185 to @file{stdout} and any error messages are directed to @file{stdout}.
7186 All that is written to @file{stderr} is a brief summary message.
7188 @item ^-b^/REPORT_ERRORS=BRIEF^
7189 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
7190 Generate brief error messages to @file{stderr} even if verbose mode is
7191 specified. This is relevant only when used with the
7192 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
7196 @cindex @option{-m} (@code{gnatbind})
7197 Limits the number of error messages to @var{n}, a decimal integer in the
7198 range 1-999. The binder terminates immediately if this limit is reached.
7201 @cindex @option{-M} (@code{gnatbind})
7202 Renames the generated main program from @code{main} to @code{xxx}.
7203 This is useful in the case of some cross-building environments, where
7204 the actual main program is separate from the one generated
7208 @item ^-ws^/WARNINGS=SUPPRESS^
7209 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
7211 Suppress all warning messages.
7213 @item ^-we^/WARNINGS=ERROR^
7214 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
7215 Treat any warning messages as fatal errors.
7218 @item /WARNINGS=NORMAL
7219 Standard mode with warnings generated, but warnings do not get treated
7223 @item ^-t^/NOTIME_STAMP_CHECK^
7224 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
7225 @cindex Time stamp checks, in binder
7226 @cindex Binder consistency checks
7227 @cindex Consistency checks, in binder
7228 The binder performs a number of consistency checks including:
7232 Check that time stamps of a given source unit are consistent
7234 Check that checksums of a given source unit are consistent
7236 Check that consistent versions of @code{GNAT} were used for compilation
7238 Check consistency of configuration pragmas as required
7242 Normally failure of such checks, in accordance with the consistency
7243 requirements of the Ada Reference Manual, causes error messages to be
7244 generated which abort the binder and prevent the output of a binder
7245 file and subsequent link to obtain an executable.
7247 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
7248 into warnings, so that
7249 binding and linking can continue to completion even in the presence of such
7250 errors. The result may be a failed link (due to missing symbols), or a
7251 non-functional executable which has undefined semantics.
7252 @emph{This means that
7253 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
7257 @node Elaboration Control
7258 @subsection Elaboration Control
7261 The following switches provide additional control over the elaboration
7262 order. For full details see @xref{Elaboration Order Handling in GNAT}.
7265 @item ^-p^/PESSIMISTIC_ELABORATION^
7266 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
7267 Normally the binder attempts to choose an elaboration order that is
7268 likely to minimize the likelihood of an elaboration order error resulting
7269 in raising a @code{Program_Error} exception. This switch reverses the
7270 action of the binder, and requests that it deliberately choose an order
7271 that is likely to maximize the likelihood of an elaboration error.
7272 This is useful in ensuring portability and avoiding dependence on
7273 accidental fortuitous elaboration ordering.
7275 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
7277 elaboration checking is used (@option{-gnatE} switch used for compilation).
7278 This is because in the default static elaboration mode, all necessary
7279 @code{Elaborate_All} pragmas are implicitly inserted.
7280 These implicit pragmas are still respected by the binder in
7281 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
7282 safe elaboration order is assured.
7285 @node Output Control
7286 @subsection Output Control
7289 The following switches allow additional control over the output
7290 generated by the binder.
7295 @item ^-A^/BIND_FILE=ADA^
7296 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
7297 Generate binder program in Ada (default). The binder program is named
7298 @file{b~@var{mainprog}.adb} by default. This can be changed with
7299 @option{^-o^/OUTPUT^} @code{gnatbind} option.
7301 @item ^-c^/NOOUTPUT^
7302 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
7303 Check only. Do not generate the binder output file. In this mode the
7304 binder performs all error checks but does not generate an output file.
7306 @item ^-C^/BIND_FILE=C^
7307 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
7308 Generate binder program in C. The binder program is named
7309 @file{b_@var{mainprog}.c}.
7310 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
7313 @item ^-e^/ELABORATION_DEPENDENCIES^
7314 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
7315 Output complete list of elaboration-order dependencies, showing the
7316 reason for each dependency. This output can be rather extensive but may
7317 be useful in diagnosing problems with elaboration order. The output is
7318 written to @file{stdout}.
7321 @cindex @option{^-h^/HELP^} (@code{gnatbind})
7322 Output usage information. The output is written to @file{stdout}.
7324 @item ^-K^/LINKER_OPTION_LIST^
7325 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
7326 Output linker options to @file{stdout}. Includes library search paths,
7327 contents of pragmas Ident and Linker_Options, and libraries added
7330 @item ^-l^/ORDER_OF_ELABORATION^
7331 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
7332 Output chosen elaboration order. The output is written to @file{stdout}.
7334 @item ^-O^/OBJECT_LIST^
7335 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
7336 Output full names of all the object files that must be linked to provide
7337 the Ada component of the program. The output is written to @file{stdout}.
7338 This list includes the files explicitly supplied and referenced by the user
7339 as well as implicitly referenced run-time unit files. The latter are
7340 omitted if the corresponding units reside in shared libraries. The
7341 directory names for the run-time units depend on the system configuration.
7343 @item ^-o ^/OUTPUT=^@var{file}
7344 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
7345 Set name of output file to @var{file} instead of the normal
7346 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
7347 binder generated body filename. In C mode you would normally give
7348 @var{file} an extension of @file{.c} because it will be a C source program.
7349 Note that if this option is used, then linking must be done manually.
7350 It is not possible to use gnatlink in this case, since it cannot locate
7353 @item ^-r^/RESTRICTION_LIST^
7354 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
7355 Generate list of @code{pragma Restrictions} that could be applied to
7356 the current unit. This is useful for code audit purposes, and also may
7357 be used to improve code generation in some cases.
7361 @node Binding with Non-Ada Main Programs
7362 @subsection Binding with Non-Ada Main Programs
7365 In our description so far we have assumed that the main
7366 program is in Ada, and that the task of the binder is to generate a
7367 corresponding function @code{main} that invokes this Ada main
7368 program. GNAT also supports the building of executable programs where
7369 the main program is not in Ada, but some of the called routines are
7370 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
7371 The following switch is used in this situation:
7375 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
7376 No main program. The main program is not in Ada.
7380 In this case, most of the functions of the binder are still required,
7381 but instead of generating a main program, the binder generates a file
7382 containing the following callable routines:
7387 You must call this routine to initialize the Ada part of the program by
7388 calling the necessary elaboration routines. A call to @code{adainit} is
7389 required before the first call to an Ada subprogram.
7391 Note that it is assumed that the basic execution environment must be setup
7392 to be appropriate for Ada execution at the point where the first Ada
7393 subprogram is called. In particular, if the Ada code will do any
7394 floating-point operations, then the FPU must be setup in an appropriate
7395 manner. For the case of the x86, for example, full precision mode is
7396 required. The procedure GNAT.Float_Control.Reset may be used to ensure
7397 that the FPU is in the right state.
7401 You must call this routine to perform any library-level finalization
7402 required by the Ada subprograms. A call to @code{adafinal} is required
7403 after the last call to an Ada subprogram, and before the program
7408 If the @option{^-n^/NOMAIN^} switch
7409 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
7410 @cindex Binder, multiple input files
7411 is given, more than one ALI file may appear on
7412 the command line for @code{gnatbind}. The normal @dfn{closure}
7413 calculation is performed for each of the specified units. Calculating
7414 the closure means finding out the set of units involved by tracing
7415 @code{with} references. The reason it is necessary to be able to
7416 specify more than one ALI file is that a given program may invoke two or
7417 more quite separate groups of Ada units.
7419 The binder takes the name of its output file from the last specified ALI
7420 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
7421 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
7422 The output is an Ada unit in source form that can
7423 be compiled with GNAT unless the -C switch is used in which case the
7424 output is a C source file, which must be compiled using the C compiler.
7425 This compilation occurs automatically as part of the @code{gnatlink}
7428 Currently the GNAT run time requires a FPU using 80 bits mode
7429 precision. Under targets where this is not the default it is required to
7430 call GNAT.Float_Control.Reset before using floating point numbers (this
7431 include float computation, float input and output) in the Ada code. A
7432 side effect is that this could be the wrong mode for the foreign code
7433 where floating point computation could be broken after this call.
7435 @node Binding Programs with No Main Subprogram
7436 @subsection Binding Programs with No Main Subprogram
7439 It is possible to have an Ada program which does not have a main
7440 subprogram. This program will call the elaboration routines of all the
7441 packages, then the finalization routines.
7443 The following switch is used to bind programs organized in this manner:
7446 @item ^-z^/ZERO_MAIN^
7447 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
7448 Normally the binder checks that the unit name given on the command line
7449 corresponds to a suitable main subprogram. When this switch is used,
7450 a list of ALI files can be given, and the execution of the program
7451 consists of elaboration of these units in an appropriate order.
7455 @node Command-Line Access
7456 @section Command-Line Access
7459 The package @code{Ada.Command_Line} provides access to the command-line
7460 arguments and program name. In order for this interface to operate
7461 correctly, the two variables
7473 are declared in one of the GNAT library routines. These variables must
7474 be set from the actual @code{argc} and @code{argv} values passed to the
7475 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
7476 generates the C main program to automatically set these variables.
7477 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
7478 set these variables. If they are not set, the procedures in
7479 @code{Ada.Command_Line} will not be available, and any attempt to use
7480 them will raise @code{Constraint_Error}. If command line access is
7481 required, your main program must set @code{gnat_argc} and
7482 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
7486 @node Search Paths for gnatbind
7487 @section Search Paths for @code{gnatbind}
7490 The binder takes the name of an ALI file as its argument and needs to
7491 locate source files as well as other ALI files to verify object consistency.
7493 For source files, it follows exactly the same search rules as @code{gcc}
7494 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
7495 directories searched are:
7499 The directory containing the ALI file named in the command line, unless
7500 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
7503 All directories specified by @option{^-I^/SEARCH^}
7504 switches on the @code{gnatbind}
7505 command line, in the order given.
7508 @findex ADA_OBJECTS_PATH
7509 Each of the directories listed in the value of the
7510 @code{ADA_OBJECTS_PATH} ^environment variable^logical name^.
7512 Construct this value
7513 exactly as the @code{PATH} environment variable: a list of directory
7514 names separated by colons (semicolons when working with the NT version
7518 Normally, define this value as a logical name containing a comma separated
7519 list of directory names.
7521 This variable can also be defined by means of an environment string
7522 (an argument to the DEC C exec* set of functions).
7526 DEFINE ANOTHER_PATH FOO:[BAG]
7527 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7530 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7531 first, followed by the standard Ada 95
7532 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
7533 If this is not redefined, the user will obtain the DEC Ada 83 IO packages
7534 (Text_IO, Sequential_IO, etc)
7535 instead of the Ada95 packages. Thus, in order to get the Ada 95
7536 packages by default, ADA_OBJECTS_PATH must be redefined.
7540 @findex ADA_PRJ_OBJECTS_FILE
7541 Each of the directories listed in the text file whose name is given
7542 by the @code{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
7545 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7546 driver when project files are used. It should not normally be set
7550 The content of the @file{ada_object_path} file which is part of the GNAT
7551 installation tree and is used to store standard libraries such as the
7552 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
7555 @ref{Installing an Ada Library}
7560 In the binder the switch @option{^-I^/SEARCH^}
7561 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
7562 is used to specify both source and
7563 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
7564 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
7565 instead if you want to specify
7566 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
7567 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
7568 if you want to specify library paths
7569 only. This means that for the binder
7570 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
7571 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
7572 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
7573 The binder generates the bind file (a C language source file) in the
7574 current working directory.
7580 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7581 children make up the GNAT Run-Time Library, together with the package
7582 GNAT and its children, which contain a set of useful additional
7583 library functions provided by GNAT. The sources for these units are
7584 needed by the compiler and are kept together in one directory. The ALI
7585 files and object files generated by compiling the RTL are needed by the
7586 binder and the linker and are kept together in one directory, typically
7587 different from the directory containing the sources. In a normal
7588 installation, you need not specify these directory names when compiling
7589 or binding. Either the environment variables or the built-in defaults
7590 cause these files to be found.
7592 Besides simplifying access to the RTL, a major use of search paths is
7593 in compiling sources from multiple directories. This can make
7594 development environments much more flexible.
7596 @node Examples of gnatbind Usage
7597 @section Examples of @code{gnatbind} Usage
7600 This section contains a number of examples of using the GNAT binding
7601 utility @code{gnatbind}.
7604 @item gnatbind hello
7605 The main program @code{Hello} (source program in @file{hello.adb}) is
7606 bound using the standard switch settings. The generated main program is
7607 @file{b~hello.adb}. This is the normal, default use of the binder.
7610 @item gnatbind hello -o mainprog.adb
7613 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
7615 The main program @code{Hello} (source program in @file{hello.adb}) is
7616 bound using the standard switch settings. The generated main program is
7617 @file{mainprog.adb} with the associated spec in
7618 @file{mainprog.ads}. Note that you must specify the body here not the
7619 spec, in the case where the output is in Ada. Note that if this option
7620 is used, then linking must be done manually, since gnatlink will not
7621 be able to find the generated file.
7624 @item gnatbind main -C -o mainprog.c -x
7627 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
7629 The main program @code{Main} (source program in
7630 @file{main.adb}) is bound, excluding source files from the
7631 consistency checking, generating
7632 the file @file{mainprog.c}.
7635 @item gnatbind -x main_program -C -o mainprog.c
7636 This command is exactly the same as the previous example. Switches may
7637 appear anywhere in the command line, and single letter switches may be
7638 combined into a single switch.
7642 @item gnatbind -n math dbase -C -o ada-control.c
7645 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
7647 The main program is in a language other than Ada, but calls to
7648 subprograms in packages @code{Math} and @code{Dbase} appear. This call
7649 to @code{gnatbind} generates the file @file{ada-control.c} containing
7650 the @code{adainit} and @code{adafinal} routines to be called before and
7651 after accessing the Ada units.
7655 @c ------------------------------------
7656 @node Linking Using gnatlink
7657 @chapter Linking Using @code{gnatlink}
7658 @c ------------------------------------
7662 This chapter discusses @code{gnatlink}, a tool that links
7663 an Ada program and builds an executable file. This utility
7664 invokes the system linker ^(via the @code{gcc} command)^^
7665 with a correct list of object files and library references.
7666 @code{gnatlink} automatically determines the list of files and
7667 references for the Ada part of a program. It uses the binder file
7668 generated by the @command{gnatbind} to determine this list.
7671 * Running gnatlink::
7672 * Switches for gnatlink::
7673 * Setting Stack Size from gnatlink::
7674 * Setting Heap Size from gnatlink::
7677 @node Running gnatlink
7678 @section Running @code{gnatlink}
7681 The form of the @code{gnatlink} command is
7684 $ gnatlink [@var{switches}] @var{mainprog}[.ali]
7685 [@var{non-Ada objects}] [@var{linker options}]
7689 The arguments of @code{gnatlink} (switches, main @file{ALI} file,
7691 or linker options) may be in any order, provided that no non-Ada object may
7692 be mistaken for a main @file{ALI} file.
7693 Any file name @file{F} without the @file{.ali}
7694 extension will be taken as the main @file{ALI} file if a file exists
7695 whose name is the concatenation of @file{F} and @file{.ali}.
7698 @file{@var{mainprog}.ali} references the ALI file of the main program.
7699 The @file{.ali} extension of this file can be omitted. From this
7700 reference, @code{gnatlink} locates the corresponding binder file
7701 @file{b~@var{mainprog}.adb} and, using the information in this file along
7702 with the list of non-Ada objects and linker options, constructs a
7703 linker command file to create the executable.
7705 The arguments other than the @code{gnatlink} switches and the main @file{ALI}
7706 file are passed to the linker uninterpreted.
7707 They typically include the names of
7708 object files for units written in other languages than Ada and any library
7709 references required to resolve references in any of these foreign language
7710 units, or in @code{Import} pragmas in any Ada units.
7712 @var{linker options} is an optional list of linker specific
7714 The default linker called by gnatlink is @var{gcc} which in
7715 turn calls the appropriate system linker.
7716 Standard options for the linker such as @option{-lmy_lib} or
7717 @option{-Ldir} can be added as is.
7718 For options that are not recognized by
7719 @var{gcc} as linker options, use the @var{gcc} switches @option{-Xlinker} or
7721 Refer to the GCC documentation for
7722 details. Here is an example showing how to generate a linker map:
7726 $ gnatlink my_prog -Wl,-Map,MAPFILE
7731 <<Need example for VMS>>
7734 Using @var{linker options} it is possible to set the program stack and
7735 heap size. See @ref{Setting Stack Size from gnatlink}, and
7736 @ref{Setting Heap Size from gnatlink}.
7738 @code{gnatlink} determines the list of objects required by the Ada
7739 program and prepends them to the list of objects passed to the linker.
7740 @code{gnatlink} also gathers any arguments set by the use of
7741 @code{pragma Linker_Options} and adds them to the list of arguments
7742 presented to the linker.
7745 @code{gnatlink} accepts the following types of extra files on the command
7746 line: objects (.OBJ), libraries (.OLB), sharable images (.EXE), and
7747 options files (.OPT). These are recognized and handled according to their
7751 @node Switches for gnatlink
7752 @section Switches for @code{gnatlink}
7755 The following switches are available with the @code{gnatlink} utility:
7760 @item ^-A^/BIND_FILE=ADA^
7761 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatlink})
7762 The binder has generated code in Ada. This is the default.
7764 @item ^-C^/BIND_FILE=C^
7765 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatlink})
7766 If instead of generating a file in Ada, the binder has generated one in
7767 C, then the linker needs to know about it. Use this switch to signal
7768 to @code{gnatlink} that the binder has generated C code rather than
7771 @item ^-f^/FORCE_OBJECT_FILE_LIST^
7772 @cindex Command line length
7773 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@code{gnatlink})
7774 On some targets, the command line length is limited, and @code{gnatlink}
7775 will generate a separate file for the linker if the list of object files
7777 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
7778 to be generated even if
7779 the limit is not exceeded. This is useful in some cases to deal with
7780 special situations where the command line length is exceeded.
7783 @cindex Debugging information, including
7784 @cindex @option{^-g^/DEBUG^} (@code{gnatlink})
7785 The option to include debugging information causes the Ada bind file (in
7786 other words, @file{b~@var{mainprog}.adb}) to be compiled with
7787 @option{^-g^/DEBUG^}.
7788 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
7789 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
7790 Without @option{^-g^/DEBUG^}, the binder removes these files by
7791 default. The same procedure apply if a C bind file was generated using
7792 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
7793 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
7795 @item ^-n^/NOCOMPILE^
7796 @cindex @option{^-n^/NOCOMPILE^} (@code{gnatlink})
7797 Do not compile the file generated by the binder. This may be used when
7798 a link is rerun with different options, but there is no need to recompile
7802 @cindex @option{^-v^/VERBOSE^} (@code{gnatlink})
7803 Causes additional information to be output, including a full list of the
7804 included object files. This switch option is most useful when you want
7805 to see what set of object files are being used in the link step.
7807 @item ^-v -v^/VERBOSE/VERBOSE^
7808 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@code{gnatlink})
7809 Very verbose mode. Requests that the compiler operate in verbose mode when
7810 it compiles the binder file, and that the system linker run in verbose mode.
7812 @item ^-o ^/EXECUTABLE=^@var{exec-name}
7813 @cindex @option{^-o^/EXECUTABLE^} (@code{gnatlink})
7814 @var{exec-name} specifies an alternate name for the generated
7815 executable program. If this switch is omitted, the executable has the same
7816 name as the main unit. For example, @code{gnatlink try.ali} creates
7817 an executable called @file{^try^TRY.EXE^}.
7820 @item -b @var{target}
7821 @cindex @option{-b} (@code{gnatlink})
7822 Compile your program to run on @var{target}, which is the name of a
7823 system configuration. You must have a GNAT cross-compiler built if
7824 @var{target} is not the same as your host system.
7827 @cindex @option{-B} (@code{gnatlink})
7828 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
7829 from @var{dir} instead of the default location. Only use this switch
7830 when multiple versions of the GNAT compiler are available. See the
7831 @code{gcc} manual page for further details. You would normally use the
7832 @option{-b} or @option{-V} switch instead.
7834 @item --GCC=@var{compiler_name}
7835 @cindex @option{--GCC=compiler_name} (@code{gnatlink})
7836 Program used for compiling the binder file. The default is
7837 `@code{gcc}'. You need to use quotes around @var{compiler_name} if
7838 @code{compiler_name} contains spaces or other separator characters. As
7839 an example @option{--GCC="foo -x -y"} will instruct @code{gnatlink} to use
7840 @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
7841 inserted after your command name. Thus in the above example the compiler
7842 command that will be used by @code{gnatlink} will be @code{foo -c -x -y}.
7843 If several @option{--GCC=compiler_name} are used, only the last
7844 @var{compiler_name} is taken into account. However, all the additional
7845 switches are also taken into account. Thus,
7846 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7847 @option{--GCC="bar -x -y -z -t"}.
7849 @item --LINK=@var{name}
7850 @cindex @option{--LINK=} (@code{gnatlink})
7851 @var{name} is the name of the linker to be invoked. This is especially
7852 useful in mixed language programs since languages such as C++ require
7853 their own linker to be used. When this switch is omitted, the default
7854 name for the linker is (@file{gcc}). When this switch is used, the
7855 specified linker is called instead of (@file{gcc}) with exactly the same
7856 parameters that would have been passed to (@file{gcc}) so if the desired
7857 linker requires different parameters it is necessary to use a wrapper
7858 script that massages the parameters before invoking the real linker. It
7859 may be useful to control the exact invocation by using the verbose
7865 @item /DEBUG=TRACEBACK
7866 @cindex @code{/DEBUG=TRACEBACK} (@code{gnatlink})
7867 This qualifier causes sufficient information to be included in the
7868 executable file to allow a traceback, but does not include the full
7869 symbol information needed by the debugger.
7871 @item /IDENTIFICATION="<string>"
7872 @code{"<string>"} specifies the string to be stored in the image file
7873 identification field in the image header.
7874 It overrides any pragma @code{Ident} specified string.
7876 @item /NOINHIBIT-EXEC
7877 Generate the executable file even if there are linker warnings.
7879 @item /NOSTART_FILES
7880 Don't link in the object file containing the ``main'' transfer address.
7881 Used when linking with a foreign language main program compiled with a
7885 Prefer linking with object libraries over sharable images, even without
7891 @node Setting Stack Size from gnatlink
7892 @section Setting Stack Size from @code{gnatlink}
7895 Under Windows systems, it is possible to specify the program stack size from
7896 @code{gnatlink} using either:
7900 @item using @option{-Xlinker} linker option
7903 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
7906 This sets the stack reserve size to 0x10000 bytes and the stack commit
7907 size to 0x1000 bytes.
7909 @item using @option{-Wl} linker option
7912 $ gnatlink hello -Wl,--stack=0x1000000
7915 This sets the stack reserve size to 0x1000000 bytes. Note that with
7916 @option{-Wl} option it is not possible to set the stack commit size
7917 because the coma is a separator for this option.
7921 @node Setting Heap Size from gnatlink
7922 @section Setting Heap Size from @code{gnatlink}
7925 Under Windows systems, it is possible to specify the program heap size from
7926 @code{gnatlink} using either:
7930 @item using @option{-Xlinker} linker option
7933 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
7936 This sets the heap reserve size to 0x10000 bytes and the heap commit
7937 size to 0x1000 bytes.
7939 @item using @option{-Wl} linker option
7942 $ gnatlink hello -Wl,--heap=0x1000000
7945 This sets the heap reserve size to 0x1000000 bytes. Note that with
7946 @option{-Wl} option it is not possible to set the heap commit size
7947 because the coma is a separator for this option.
7951 @node The GNAT Make Program gnatmake
7952 @chapter The GNAT Make Program @code{gnatmake}
7956 * Running gnatmake::
7957 * Switches for gnatmake::
7958 * Mode Switches for gnatmake::
7959 * Notes on the Command Line::
7960 * How gnatmake Works::
7961 * Examples of gnatmake Usage::
7964 A typical development cycle when working on an Ada program consists of
7965 the following steps:
7969 Edit some sources to fix bugs.
7975 Compile all sources affected.
7985 The third step can be tricky, because not only do the modified files
7986 @cindex Dependency rules
7987 have to be compiled, but any files depending on these files must also be
7988 recompiled. The dependency rules in Ada can be quite complex, especially
7989 in the presence of overloading, @code{use} clauses, generics and inlined
7992 @code{gnatmake} automatically takes care of the third and fourth steps
7993 of this process. It determines which sources need to be compiled,
7994 compiles them, and binds and links the resulting object files.
7996 Unlike some other Ada make programs, the dependencies are always
7997 accurately recomputed from the new sources. The source based approach of
7998 the GNAT compilation model makes this possible. This means that if
7999 changes to the source program cause corresponding changes in
8000 dependencies, they will always be tracked exactly correctly by
8003 @node Running gnatmake
8004 @section Running @code{gnatmake}
8007 The usual form of the @code{gnatmake} command is
8010 $ gnatmake [@var{switches}] @var{file_name}
8011 [@var{file_names}] [@var{mode_switches}]
8015 The only required argument is one @var{file_name}, which specifies
8016 a compilation unit that is a main program. Several @var{file_names} can be
8017 specified: this will result in several executables being built.
8018 If @code{switches} are present, they can be placed before the first
8019 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
8020 If @var{mode_switches} are present, they must always be placed after
8021 the last @var{file_name} and all @code{switches}.
8023 If you are using standard file extensions (.adb and .ads), then the
8024 extension may be omitted from the @var{file_name} arguments. However, if
8025 you are using non-standard extensions, then it is required that the
8026 extension be given. A relative or absolute directory path can be
8027 specified in a @var{file_name}, in which case, the input source file will
8028 be searched for in the specified directory only. Otherwise, the input
8029 source file will first be searched in the directory where
8030 @code{gnatmake} was invoked and if it is not found, it will be search on
8031 the source path of the compiler as described in
8032 @ref{Search Paths and the Run-Time Library (RTL)}.
8034 All @code{gnatmake} output (except when you specify
8035 @option{^-M^/DEPENDENCIES_LIST^}) is to
8036 @file{stderr}. The output produced by the
8037 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
8040 @node Switches for gnatmake
8041 @section Switches for @code{gnatmake}
8044 You may specify any of the following switches to @code{gnatmake}:
8049 @item --GCC=@var{compiler_name}
8050 @cindex @option{--GCC=compiler_name} (@code{gnatmake})
8051 Program used for compiling. The default is `@code{gcc}'. You need to use
8052 quotes around @var{compiler_name} if @code{compiler_name} contains
8053 spaces or other separator characters. As an example @option{--GCC="foo -x
8054 -y"} will instruct @code{gnatmake} to use @code{foo -x -y} as your
8055 compiler. Note that switch @option{-c} is always inserted after your
8056 command name. Thus in the above example the compiler command that will
8057 be used by @code{gnatmake} will be @code{foo -c -x -y}.
8058 If several @option{--GCC=compiler_name} are used, only the last
8059 @var{compiler_name} is taken into account. However, all the additional
8060 switches are also taken into account. Thus,
8061 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
8062 @option{--GCC="bar -x -y -z -t"}.
8064 @item --GNATBIND=@var{binder_name}
8065 @cindex @option{--GNATBIND=binder_name} (@code{gnatmake})
8066 Program used for binding. The default is `@code{gnatbind}'. You need to
8067 use quotes around @var{binder_name} if @var{binder_name} contains spaces
8068 or other separator characters. As an example @option{--GNATBIND="bar -x
8069 -y"} will instruct @code{gnatmake} to use @code{bar -x -y} as your
8070 binder. Binder switches that are normally appended by @code{gnatmake} to
8071 `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
8073 @item --GNATLINK=@var{linker_name}
8074 @cindex @option{--GNATLINK=linker_name} (@code{gnatmake})
8075 Program used for linking. The default is `@code{gnatlink}'. You need to
8076 use quotes around @var{linker_name} if @var{linker_name} contains spaces
8077 or other separator characters. As an example @option{--GNATLINK="lan -x
8078 -y"} will instruct @code{gnatmake} to use @code{lan -x -y} as your
8079 linker. Linker switches that are normally appended by @code{gnatmake} to
8080 `@code{gnatlink}' are now appended to the end of @code{lan -x -y}.
8084 @item ^-a^/ALL_FILES^
8085 @cindex @option{^-a^/ALL_FILES^} (@code{gnatmake})
8086 Consider all files in the make process, even the GNAT internal system
8087 files (for example, the predefined Ada library files), as well as any
8088 locked files. Locked files are files whose ALI file is write-protected.
8090 @code{gnatmake} does not check these files,
8091 because the assumption is that the GNAT internal files are properly up
8092 to date, and also that any write protected ALI files have been properly
8093 installed. Note that if there is an installation problem, such that one
8094 of these files is not up to date, it will be properly caught by the
8096 You may have to specify this switch if you are working on GNAT
8097 itself. The switch @option{^-a^/ALL_FILES^} is also useful
8098 in conjunction with @option{^-f^/FORCE_COMPILE^}
8099 if you need to recompile an entire application,
8100 including run-time files, using special configuration pragmas,
8101 such as a @code{Normalize_Scalars} pragma.
8104 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
8107 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
8110 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
8113 @item ^-b^/ACTIONS=BIND^
8114 @cindex @option{^-b^/ACTIONS=BIND^} (@code{gnatmake})
8115 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
8116 compilation and binding, but no link.
8117 Can be combined with @option{^-l^/ACTIONS=LINK^}
8118 to do binding and linking. When not combined with
8119 @option{^-c^/ACTIONS=COMPILE^}
8120 all the units in the closure of the main program must have been previously
8121 compiled and must be up to date. The root unit specified by @var{file_name}
8122 may be given without extension, with the source extension or, if no GNAT
8123 Project File is specified, with the ALI file extension.
8125 @item ^-c^/ACTIONS=COMPILE^
8126 @cindex @option{^-c^/ACTIONS=COMPILE^} (@code{gnatmake})
8127 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
8128 is also specified. Do not perform linking, except if both
8129 @option{^-b^/ACTIONS=BIND^} and
8130 @option{^-l^/ACTIONS=LINK^} are also specified.
8131 If the root unit specified by @var{file_name} is not a main unit, this is the
8132 default. Otherwise @code{gnatmake} will attempt binding and linking
8133 unless all objects are up to date and the executable is more recent than
8137 @cindex @option{^-C^/MAPPING^} (@code{gnatmake})
8138 Use a temporary mapping file. A mapping file is a way to communicate to the
8139 compiler two mappings: from unit names to file names (without any directory
8140 information) and from file names to path names (with full directory
8141 information). These mappings are used by the compiler to short-circuit the path
8142 search. When @code{gnatmake} is invoked with this switch, it will create
8143 a temporary mapping file, initially populated by the project manager,
8144 if @option{^-P^/PROJECT_FILE^} is used, otherwise initially empty.
8145 Each invocation of the compiler will add the newly accessed sources to the
8146 mapping file. This will improve the source search during the next invocation
8149 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
8150 @cindex @option{^-C=^/USE_MAPPING^} (@code{gnatmake})
8151 Use a specific mapping file. The file, specified as a path name (absolute or
8152 relative) by this switch, should already exist, otherwise the switch is
8153 ineffective. The specified mapping file will be communicated to the compiler.
8154 This switch is not compatible with a project file
8155 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
8156 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
8158 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
8159 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatmake})
8160 Put all object files and ALI file in directory @var{dir}.
8161 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
8162 and ALI files go in the current working directory.
8164 This switch cannot be used when using a project file.
8168 @cindex @option{-eL} (@code{gnatmake})
8169 Follow all symbolic links when processing project files.
8172 @item ^-f^/FORCE_COMPILE^
8173 @cindex @option{^-f^/FORCE_COMPILE^} (@code{gnatmake})
8174 Force recompilations. Recompile all sources, even though some object
8175 files may be up to date, but don't recompile predefined or GNAT internal
8176 files or locked files (files with a write-protected ALI file),
8177 unless the @option{^-a^/ALL_FILES^} switch is also specified.
8179 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
8180 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatmake})
8181 When using project files, if some errors or warnings are detected during
8182 parsing and verbose mode is not in effect (no use of switch
8183 ^-v^/VERBOSE^), then error lines start with the full path name of the project
8184 file, rather than its simple file name.
8186 @item ^-i^/IN_PLACE^
8187 @cindex @option{^-i^/IN_PLACE^} (@code{gnatmake})
8188 In normal mode, @code{gnatmake} compiles all object files and ALI files
8189 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
8190 then instead object files and ALI files that already exist are overwritten
8191 in place. This means that once a large project is organized into separate
8192 directories in the desired manner, then @code{gnatmake} will automatically
8193 maintain and update this organization. If no ALI files are found on the
8194 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
8195 the new object and ALI files are created in the
8196 directory containing the source being compiled. If another organization
8197 is desired, where objects and sources are kept in different directories,
8198 a useful technique is to create dummy ALI files in the desired directories.
8199 When detecting such a dummy file, @code{gnatmake} will be forced to recompile
8200 the corresponding source file, and it will be put the resulting object
8201 and ALI files in the directory where it found the dummy file.
8203 @item ^-j^/PROCESSES=^@var{n}
8204 @cindex @option{^-j^/PROCESSES^} (@code{gnatmake})
8205 @cindex Parallel make
8206 Use @var{n} processes to carry out the (re)compilations. On a
8207 multiprocessor machine compilations will occur in parallel. In the
8208 event of compilation errors, messages from various compilations might
8209 get interspersed (but @code{gnatmake} will give you the full ordered
8210 list of failing compiles at the end). If this is problematic, rerun
8211 the make process with n set to 1 to get a clean list of messages.
8213 @item ^-k^/CONTINUE_ON_ERROR^
8214 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@code{gnatmake})
8215 Keep going. Continue as much as possible after a compilation error. To
8216 ease the programmer's task in case of compilation errors, the list of
8217 sources for which the compile fails is given when @code{gnatmake}
8220 If @code{gnatmake} is invoked with several @file{file_names} and with this
8221 switch, if there are compilation errors when building an executable,
8222 @code{gnatmake} will not attempt to build the following executables.
8224 @item ^-l^/ACTIONS=LINK^
8225 @cindex @option{^-l^/ACTIONS=LINK^} (@code{gnatmake})
8226 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
8227 and linking. Linking will not be performed if combined with
8228 @option{^-c^/ACTIONS=COMPILE^}
8229 but not with @option{^-b^/ACTIONS=BIND^}.
8230 When not combined with @option{^-b^/ACTIONS=BIND^}
8231 all the units in the closure of the main program must have been previously
8232 compiled and must be up to date, and the main program need to have been bound.
8233 The root unit specified by @var{file_name}
8234 may be given without extension, with the source extension or, if no GNAT
8235 Project File is specified, with the ALI file extension.
8237 @item ^-m^/MINIMAL_RECOMPILATION^
8238 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@code{gnatmake})
8239 Specifies that the minimum necessary amount of recompilations
8240 be performed. In this mode @code{gnatmake} ignores time
8241 stamp differences when the only
8242 modifications to a source file consist in adding/removing comments,
8243 empty lines, spaces or tabs. This means that if you have changed the
8244 comments in a source file or have simply reformatted it, using this
8245 switch will tell gnatmake not to recompile files that depend on it
8246 (provided other sources on which these files depend have undergone no
8247 semantic modifications). Note that the debugging information may be
8248 out of date with respect to the sources if the @option{-m} switch causes
8249 a compilation to be switched, so the use of this switch represents a
8250 trade-off between compilation time and accurate debugging information.
8252 @item ^-M^/DEPENDENCIES_LIST^
8253 @cindex Dependencies, producing list
8254 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@code{gnatmake})
8255 Check if all objects are up to date. If they are, output the object
8256 dependences to @file{stdout} in a form that can be directly exploited in
8257 a @file{Makefile}. By default, each source file is prefixed with its
8258 (relative or absolute) directory name. This name is whatever you
8259 specified in the various @option{^-aI^/SOURCE_SEARCH^}
8260 and @option{^-I^/SEARCH^} switches. If you use
8261 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
8262 @option{^-q^/QUIET^}
8263 (see below), only the source file names,
8264 without relative paths, are output. If you just specify the
8265 @option{^-M^/DEPENDENCIES_LIST^}
8266 switch, dependencies of the GNAT internal system files are omitted. This
8267 is typically what you want. If you also specify
8268 the @option{^-a^/ALL_FILES^} switch,
8269 dependencies of the GNAT internal files are also listed. Note that
8270 dependencies of the objects in external Ada libraries (see switch
8271 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
8274 @item ^-n^/DO_OBJECT_CHECK^
8275 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@code{gnatmake})
8276 Don't compile, bind, or link. Checks if all objects are up to date.
8277 If they are not, the full name of the first file that needs to be
8278 recompiled is printed.
8279 Repeated use of this option, followed by compiling the indicated source
8280 file, will eventually result in recompiling all required units.
8282 @item ^-o ^/EXECUTABLE=^@var{exec_name}
8283 @cindex @option{^-o^/EXECUTABLE^} (@code{gnatmake})
8284 Output executable name. The name of the final executable program will be
8285 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
8286 name for the executable will be the name of the input file in appropriate form
8287 for an executable file on the host system.
8289 This switch cannot be used when invoking @code{gnatmake} with several
8292 @item ^-P^/PROJECT_FILE=^@var{project}
8293 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatmake})
8294 Use project file @var{project}. Only one such switch can be used.
8295 See @ref{gnatmake and Project Files}.
8298 @cindex @option{^-q^/QUIET^} (@code{gnatmake})
8299 Quiet. When this flag is not set, the commands carried out by
8300 @code{gnatmake} are displayed.
8302 @item ^-s^/SWITCH_CHECK/^
8303 @cindex @option{^-s^/SWITCH_CHECK^} (@code{gnatmake})
8304 Recompile if compiler switches have changed since last compilation.
8305 All compiler switches but -I and -o are taken into account in the
8307 orders between different ``first letter'' switches are ignored, but
8308 orders between same switches are taken into account. For example,
8309 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
8310 is equivalent to @option{-O -g}.
8312 This switch is recommended when Integrated Preprocessing is used.
8315 @cindex @option{^-u^/UNIQUE^} (@code{gnatmake})
8316 Unique. Recompile at most the main files. It implies -c. Combined with
8317 -f, it is equivalent to calling the compiler directly. Note that using
8318 ^-u^/UNIQUE^ with a project file and no main has a special meaning
8319 (see @ref{Project Files and Main Subprograms}).
8321 @item ^-U^/ALL_PROJECTS^
8322 @cindex @option{^-U^/ALL_PROJECTS^} (@code{gnatmake})
8323 When used without a project file or with one or several mains on the command
8324 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
8325 on the command line, all sources of all project files are checked and compiled
8326 if not up to date, and libraries are rebuilt, if necessary.
8329 @cindex @option{^-v^/REASONS^} (@code{gnatmake})
8330 Verbose. Displays the reason for all recompilations @code{gnatmake}
8331 decides are necessary.
8333 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
8334 Indicates the verbosity of the parsing of GNAT project files.
8335 See @ref{Switches Related to Project Files}.
8337 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
8338 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@code{gnatmake})
8339 Indicates that sources that are not part of any Project File may be compiled.
8340 Normally, when using Project Files, only sources that are part of a Project
8341 File may be compile. When this switch is used, a source outside of all Project
8342 Files may be compiled. The ALI file and the object file will be put in the
8343 object directory of the main Project. The compilation switches used will only
8344 be those specified on the command line.
8346 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
8347 Indicates that external variable @var{name} has the value @var{value}.
8348 The Project Manager will use this value for occurrences of
8349 @code{external(name)} when parsing the project file.
8350 See @ref{Switches Related to Project Files}.
8353 @cindex @option{^-z^/NOMAIN^} (@code{gnatmake})
8354 No main subprogram. Bind and link the program even if the unit name
8355 given on the command line is a package name. The resulting executable
8356 will execute the elaboration routines of the package and its closure,
8357 then the finalization routines.
8360 @cindex @option{^-g^/DEBUG^} (@code{gnatmake})
8361 Enable debugging. This switch is simply passed to the compiler and to the
8367 @item @code{gcc} @asis{switches}
8369 Any uppercase or multi-character switch that is not a @code{gnatmake} switch
8370 is passed to @code{gcc} (e.g. @option{-O}, @option{-gnato,} etc.)
8373 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
8374 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
8375 automatically treated as a compiler switch, and passed on to all
8376 compilations that are carried out.
8381 Source and library search path switches:
8385 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
8386 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatmake})
8387 When looking for source files also look in directory @var{dir}.
8388 The order in which source files search is undertaken is
8389 described in @ref{Search Paths and the Run-Time Library (RTL)}.
8391 @item ^-aL^/SKIP_MISSING=^@var{dir}
8392 @cindex @option{^-aL^/SKIP_MISSING^} (@code{gnatmake})
8393 Consider @var{dir} as being an externally provided Ada library.
8394 Instructs @code{gnatmake} to skip compilation units whose @file{.ALI}
8395 files have been located in directory @var{dir}. This allows you to have
8396 missing bodies for the units in @var{dir} and to ignore out of date bodies
8397 for the same units. You still need to specify
8398 the location of the specs for these units by using the switches
8399 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
8400 or @option{^-I^/SEARCH=^@var{dir}}.
8401 Note: this switch is provided for compatibility with previous versions
8402 of @code{gnatmake}. The easier method of causing standard libraries
8403 to be excluded from consideration is to write-protect the corresponding
8406 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
8407 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatmake})
8408 When searching for library and object files, look in directory
8409 @var{dir}. The order in which library files are searched is described in
8410 @ref{Search Paths for gnatbind}.
8412 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
8413 @cindex Search paths, for @code{gnatmake}
8414 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@code{gnatmake})
8415 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
8416 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8418 @item ^-I^/SEARCH=^@var{dir}
8419 @cindex @option{^-I^/SEARCH^} (@code{gnatmake})
8420 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
8421 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
8423 @item ^-I-^/NOCURRENT_DIRECTORY^
8424 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatmake})
8425 @cindex Source files, suppressing search
8426 Do not look for source files in the directory containing the source
8427 file named in the command line.
8428 Do not look for ALI or object files in the directory
8429 where @code{gnatmake} was invoked.
8431 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
8432 @cindex @option{^-L^/LIBRARY_SEARCH^} (@code{gnatmake})
8433 @cindex Linker libraries
8434 Add directory @var{dir} to the list of directories in which the linker
8435 will search for libraries. This is equivalent to
8436 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
8438 Furthermore, under Windows, the sources pointed to by the libraries path
8439 set in the registry are not searched for.
8443 @cindex @option{-nostdinc} (@code{gnatmake})
8444 Do not look for source files in the system default directory.
8447 @cindex @option{-nostdlib} (@code{gnatmake})
8448 Do not look for library files in the system default directory.
8450 @item --RTS=@var{rts-path}
8451 @cindex @option{--RTS} (@code{gnatmake})
8452 Specifies the default location of the runtime library. GNAT looks for the
8454 in the following directories, and stops as soon as a valid runtime is found
8455 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
8456 @file{ada_object_path} present):
8459 @item <current directory>/$rts_path
8461 @item <default-search-dir>/$rts_path
8463 @item <default-search-dir>/rts-$rts_path
8467 The selected path is handled like a normal RTS path.
8471 @node Mode Switches for gnatmake
8472 @section Mode Switches for @code{gnatmake}
8475 The mode switches (referred to as @code{mode_switches}) allow the
8476 inclusion of switches that are to be passed to the compiler itself, the
8477 binder or the linker. The effect of a mode switch is to cause all
8478 subsequent switches up to the end of the switch list, or up to the next
8479 mode switch, to be interpreted as switches to be passed on to the
8480 designated component of GNAT.
8484 @item -cargs @var{switches}
8485 @cindex @option{-cargs} (@code{gnatmake})
8486 Compiler switches. Here @var{switches} is a list of switches
8487 that are valid switches for @code{gcc}. They will be passed on to
8488 all compile steps performed by @code{gnatmake}.
8490 @item -bargs @var{switches}
8491 @cindex @option{-bargs} (@code{gnatmake})
8492 Binder switches. Here @var{switches} is a list of switches
8493 that are valid switches for @code{gnatbind}. They will be passed on to
8494 all bind steps performed by @code{gnatmake}.
8496 @item -largs @var{switches}
8497 @cindex @option{-largs} (@code{gnatmake})
8498 Linker switches. Here @var{switches} is a list of switches
8499 that are valid switches for @code{gnatlink}. They will be passed on to
8500 all link steps performed by @code{gnatmake}.
8502 @item -margs @var{switches}
8503 @cindex @option{-margs} (@code{gnatmake})
8504 Make switches. The switches are directly interpreted by @code{gnatmake},
8505 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
8509 @node Notes on the Command Line
8510 @section Notes on the Command Line
8513 This section contains some additional useful notes on the operation
8514 of the @code{gnatmake} command.
8518 @cindex Recompilation, by @code{gnatmake}
8519 If @code{gnatmake} finds no ALI files, it recompiles the main program
8520 and all other units required by the main program.
8521 This means that @code{gnatmake}
8522 can be used for the initial compile, as well as during subsequent steps of
8523 the development cycle.
8526 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
8527 is a subunit or body of a generic unit, @code{gnatmake} recompiles
8528 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
8532 In @code{gnatmake} the switch @option{^-I^/SEARCH^}
8533 is used to specify both source and
8534 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8535 instead if you just want to specify
8536 source paths only and @option{^-aO^/OBJECT_SEARCH^}
8537 if you want to specify library paths
8541 @code{gnatmake} examines both an ALI file and its corresponding object file
8542 for consistency. If an ALI is more recent than its corresponding object,
8543 or if the object file is missing, the corresponding source will be recompiled.
8544 Note that @code{gnatmake} expects an ALI and the corresponding object file
8545 to be in the same directory.
8548 @code{gnatmake} will ignore any files whose ALI file is write-protected.
8549 This may conveniently be used to exclude standard libraries from
8550 consideration and in particular it means that the use of the
8551 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
8552 unless @option{^-a^/ALL_FILES^} is also specified.
8555 @code{gnatmake} has been designed to make the use of Ada libraries
8556 particularly convenient. Assume you have an Ada library organized
8557 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
8558 of your Ada compilation units,
8559 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
8560 specs of these units, but no bodies. Then to compile a unit
8561 stored in @code{main.adb}, which uses this Ada library you would just type
8565 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
8568 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
8569 /SKIP_MISSING=@i{[OBJ_DIR]} main
8574 Using @code{gnatmake} along with the
8575 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
8576 switch provides a mechanism for avoiding unnecessary rcompilations. Using
8578 you can update the comments/format of your
8579 source files without having to recompile everything. Note, however, that
8580 adding or deleting lines in a source files may render its debugging
8581 info obsolete. If the file in question is a spec, the impact is rather
8582 limited, as that debugging info will only be useful during the
8583 elaboration phase of your program. For bodies the impact can be more
8584 significant. In all events, your debugger will warn you if a source file
8585 is more recent than the corresponding object, and alert you to the fact
8586 that the debugging information may be out of date.
8589 @node How gnatmake Works
8590 @section How @code{gnatmake} Works
8593 Generally @code{gnatmake} automatically performs all necessary
8594 recompilations and you don't need to worry about how it works. However,
8595 it may be useful to have some basic understanding of the @code{gnatmake}
8596 approach and in particular to understand how it uses the results of
8597 previous compilations without incorrectly depending on them.
8599 First a definition: an object file is considered @dfn{up to date} if the
8600 corresponding ALI file exists and its time stamp predates that of the
8601 object file and if all the source files listed in the
8602 dependency section of this ALI file have time stamps matching those in
8603 the ALI file. This means that neither the source file itself nor any
8604 files that it depends on have been modified, and hence there is no need
8605 to recompile this file.
8607 @code{gnatmake} works by first checking if the specified main unit is up
8608 to date. If so, no compilations are required for the main unit. If not,
8609 @code{gnatmake} compiles the main program to build a new ALI file that
8610 reflects the latest sources. Then the ALI file of the main unit is
8611 examined to find all the source files on which the main program depends,
8612 and @code{gnatmake} recursively applies the above procedure on all these files.
8614 This process ensures that @code{gnatmake} only trusts the dependencies
8615 in an existing ALI file if they are known to be correct. Otherwise it
8616 always recompiles to determine a new, guaranteed accurate set of
8617 dependencies. As a result the program is compiled ``upside down'' from what may
8618 be more familiar as the required order of compilation in some other Ada
8619 systems. In particular, clients are compiled before the units on which
8620 they depend. The ability of GNAT to compile in any order is critical in
8621 allowing an order of compilation to be chosen that guarantees that
8622 @code{gnatmake} will recompute a correct set of new dependencies if
8625 When invoking @code{gnatmake} with several @var{file_names}, if a unit is
8626 imported by several of the executables, it will be recompiled at most once.
8628 Note: when using non-standard naming conventions
8629 (See @ref{Using Other File Names}), changing through a configuration pragmas
8630 file the version of a source and invoking @code{gnatmake} to recompile may
8631 have no effect, if the previous version of the source is still accessible
8632 by @code{gnatmake}. It may be necessary to use the switch ^-f^/FORCE_COMPILE^.
8634 @node Examples of gnatmake Usage
8635 @section Examples of @code{gnatmake} Usage
8638 @item gnatmake hello.adb
8639 Compile all files necessary to bind and link the main program
8640 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
8641 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
8643 @item gnatmake main1 main2 main3
8644 Compile all files necessary to bind and link the main programs
8645 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
8646 (containing unit @code{Main2}) and @file{main3.adb}
8647 (containing unit @code{Main3}) and bind and link the resulting object files
8648 to generate three executable files @file{^main1^MAIN1.EXE^},
8649 @file{^main2^MAIN2.EXE^}
8650 and @file{^main3^MAIN3.EXE^}.
8653 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
8657 @item gnatmake Main_Unit /QUIET
8658 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
8659 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
8661 Compile all files necessary to bind and link the main program unit
8662 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
8663 be done with optimization level 2 and the order of elaboration will be
8664 listed by the binder. @code{gnatmake} will operate in quiet mode, not
8665 displaying commands it is executing.
8669 @c *************************
8670 @node Improving Performance
8671 @chapter Improving Performance
8672 @cindex Improving performance
8675 This chapter presents several topics related to program performance.
8676 It first describes some of the tradeoffs that need to be considered
8677 and some of the techniques for making your program run faster.
8678 It then documents the @command{gnatelim} tool, which can reduce
8679 the size of program executables.
8683 * Performance Considerations::
8684 * Reducing the Size of Ada Executables with gnatelim::
8689 @c *****************************
8690 @node Performance Considerations
8691 @section Performance Considerations
8694 The GNAT system provides a number of options that allow a trade-off
8699 performance of the generated code
8702 speed of compilation
8705 minimization of dependences and recompilation
8708 the degree of run-time checking.
8712 The defaults (if no options are selected) aim at improving the speed
8713 of compilation and minimizing dependences, at the expense of performance
8714 of the generated code:
8721 no inlining of subprogram calls
8724 all run-time checks enabled except overflow and elaboration checks
8728 These options are suitable for most program development purposes. This
8729 chapter describes how you can modify these choices, and also provides
8730 some guidelines on debugging optimized code.
8733 * Controlling Run-Time Checks::
8734 * Use of Restrictions::
8735 * Optimization Levels::
8736 * Debugging Optimized Code::
8737 * Inlining of Subprograms::
8738 * Optimization and Strict Aliasing::
8740 * Coverage Analysis::
8744 @node Controlling Run-Time Checks
8745 @subsection Controlling Run-Time Checks
8748 By default, GNAT generates all run-time checks, except arithmetic overflow
8749 checking for integer operations and checks for access before elaboration on
8750 subprogram calls. The latter are not required in default mode, because all
8751 necessary checking is done at compile time.
8752 @cindex @option{-gnatp} (@code{gcc})
8753 @cindex @option{-gnato} (@code{gcc})
8754 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
8755 be modified. @xref{Run-Time Checks}.
8757 Our experience is that the default is suitable for most development
8760 We treat integer overflow specially because these
8761 are quite expensive and in our experience are not as important as other
8762 run-time checks in the development process. Note that division by zero
8763 is not considered an overflow check, and divide by zero checks are
8764 generated where required by default.
8766 Elaboration checks are off by default, and also not needed by default, since
8767 GNAT uses a static elaboration analysis approach that avoids the need for
8768 run-time checking. This manual contains a full chapter discussing the issue
8769 of elaboration checks, and if the default is not satisfactory for your use,
8770 you should read this chapter.
8772 For validity checks, the minimal checks required by the Ada Reference
8773 Manual (for case statements and assignments to array elements) are on
8774 by default. These can be suppressed by use of the @option{-gnatVn} switch.
8775 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
8776 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
8777 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
8778 are also suppressed entirely if @option{-gnatp} is used.
8780 @cindex Overflow checks
8781 @cindex Checks, overflow
8784 @cindex pragma Suppress
8785 @cindex pragma Unsuppress
8786 Note that the setting of the switches controls the default setting of
8787 the checks. They may be modified using either @code{pragma Suppress} (to
8788 remove checks) or @code{pragma Unsuppress} (to add back suppressed
8789 checks) in the program source.
8791 @node Use of Restrictions
8792 @subsection Use of Restrictions
8795 The use of pragma Restrictions allows you to control which features are
8796 permitted in your program. Apart from the obvious point that if you avoid
8797 relatively expensive features like finalization (enforceable by the use
8798 of pragma Restrictions (No_Finalization), the use of this pragma does not
8799 affect the generated code in most cases.
8801 One notable exception to this rule is that the possibility of task abort
8802 results in some distributed overhead, particularly if finalization or
8803 exception handlers are used. The reason is that certain sections of code
8804 have to be marked as non-abortable.
8806 If you use neither the @code{abort} statement, nor asynchronous transfer
8807 of control (@code{select .. then abort}), then this distributed overhead
8808 is removed, which may have a general positive effect in improving
8809 overall performance. Especially code involving frequent use of tasking
8810 constructs and controlled types will show much improved performance.
8811 The relevant restrictions pragmas are
8814 pragma Restrictions (No_Abort_Statements);
8815 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
8819 It is recommended that these restriction pragmas be used if possible. Note
8820 that this also means that you can write code without worrying about the
8821 possibility of an immediate abort at any point.
8823 @node Optimization Levels
8824 @subsection Optimization Levels
8825 @cindex @option{^-O^/OPTIMIZE^} (@code{gcc})
8828 The default is optimization off. This results in the fastest compile
8829 times, but GNAT makes absolutely no attempt to optimize, and the
8830 generated programs are considerably larger and slower than when
8831 optimization is enabled. You can use the
8833 @option{-O@var{n}} switch, where @var{n} is an integer from 0 to 3,
8836 @code{OPTIMIZE} qualifier
8838 to @code{gcc} to control the optimization level:
8841 @item ^-O0^/OPTIMIZE=NONE^
8842 No optimization (the default);
8843 generates unoptimized code but has
8844 the fastest compilation time.
8846 @item ^-O1^/OPTIMIZE=SOME^
8847 Medium level optimization;
8848 optimizes reasonably well but does not
8849 degrade compilation time significantly.
8851 @item ^-O2^/OPTIMIZE=ALL^
8853 @itemx /OPTIMIZE=DEVELOPMENT
8856 generates highly optimized code and has
8857 the slowest compilation time.
8859 @item ^-O3^/OPTIMIZE=INLINING^
8860 Full optimization as in @option{-O2},
8861 and also attempts automatic inlining of small
8862 subprograms within a unit (@pxref{Inlining of Subprograms}).
8866 Higher optimization levels perform more global transformations on the
8867 program and apply more expensive analysis algorithms in order to generate
8868 faster and more compact code. The price in compilation time, and the
8869 resulting improvement in execution time,
8870 both depend on the particular application and the hardware environment.
8871 You should experiment to find the best level for your application.
8873 Since the precise set of optimizations done at each level will vary from
8874 release to release (and sometime from target to target), it is best to think
8875 of the optimization settings in general terms.
8876 The @cite{Using GNU GCC} manual contains details about
8877 ^the @option{-O} settings and a number of @option{-f} options that^how to^
8878 individually enable or disable specific optimizations.
8880 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
8881 been tested extensively at all optimization levels. There are some bugs
8882 which appear only with optimization turned on, but there have also been
8883 bugs which show up only in @emph{unoptimized} code. Selecting a lower
8884 level of optimization does not improve the reliability of the code
8885 generator, which in practice is highly reliable at all optimization
8888 Note regarding the use of @option{-O3}: The use of this optimization level
8889 is generally discouraged with GNAT, since it often results in larger
8890 executables which run more slowly. See further discussion of this point
8891 in @pxref{Inlining of Subprograms}.
8894 @node Debugging Optimized Code
8895 @subsection Debugging Optimized Code
8896 @cindex Debugging optimized code
8897 @cindex Optimization and debugging
8900 Although it is possible to do a reasonable amount of debugging at
8902 non-zero optimization levels,
8903 the higher the level the more likely that
8906 @option{/OPTIMIZE} settings other than @code{NONE},
8907 such settings will make it more likely that
8909 source-level constructs will have been eliminated by optimization.
8910 For example, if a loop is strength-reduced, the loop
8911 control variable may be completely eliminated and thus cannot be
8912 displayed in the debugger.
8913 This can only happen at @option{-O2} or @option{-O3}.
8914 Explicit temporary variables that you code might be eliminated at
8915 ^level^setting^ @option{-O1} or higher.
8917 The use of the @option{^-g^/DEBUG^} switch,
8918 @cindex @option{^-g^/DEBUG^} (@code{gcc})
8919 which is needed for source-level debugging,
8920 affects the size of the program executable on disk,
8921 and indeed the debugging information can be quite large.
8922 However, it has no effect on the generated code (and thus does not
8923 degrade performance)
8925 Since the compiler generates debugging tables for a compilation unit before
8926 it performs optimizations, the optimizing transformations may invalidate some
8927 of the debugging data. You therefore need to anticipate certain
8928 anomalous situations that may arise while debugging optimized code.
8929 These are the most common cases:
8933 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
8935 the PC bouncing back and forth in the code. This may result from any of
8936 the following optimizations:
8940 @i{Common subexpression elimination:} using a single instance of code for a
8941 quantity that the source computes several times. As a result you
8942 may not be able to stop on what looks like a statement.
8945 @i{Invariant code motion:} moving an expression that does not change within a
8946 loop, to the beginning of the loop.
8949 @i{Instruction scheduling:} moving instructions so as to
8950 overlap loads and stores (typically) with other code, or in
8951 general to move computations of values closer to their uses. Often
8952 this causes you to pass an assignment statement without the assignment
8953 happening and then later bounce back to the statement when the
8954 value is actually needed. Placing a breakpoint on a line of code
8955 and then stepping over it may, therefore, not always cause all the
8956 expected side-effects.
8960 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
8961 two identical pieces of code are merged and the program counter suddenly
8962 jumps to a statement that is not supposed to be executed, simply because
8963 it (and the code following) translates to the same thing as the code
8964 that @emph{was} supposed to be executed. This effect is typically seen in
8965 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
8966 a @code{break} in a C @code{^switch^switch^} statement.
8969 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
8970 There are various reasons for this effect:
8974 In a subprogram prologue, a parameter may not yet have been moved to its
8978 A variable may be dead, and its register re-used. This is
8979 probably the most common cause.
8982 As mentioned above, the assignment of a value to a variable may
8986 A variable may be eliminated entirely by value propagation or
8987 other means. In this case, GCC may incorrectly generate debugging
8988 information for the variable
8992 In general, when an unexpected value appears for a local variable or parameter
8993 you should first ascertain if that value was actually computed by
8994 your program, as opposed to being incorrectly reported by the debugger.
8996 array elements in an object designated by an access value
8997 are generally less of a problem, once you have ascertained that the access
8999 Typically, this means checking variables in the preceding code and in the
9000 calling subprogram to verify that the value observed is explainable from other
9001 values (one must apply the procedure recursively to those
9002 other values); or re-running the code and stopping a little earlier
9003 (perhaps before the call) and stepping to better see how the variable obtained
9004 the value in question; or continuing to step @emph{from} the point of the
9005 strange value to see if code motion had simply moved the variable's
9010 In light of such anomalies, a recommended technique is to use @option{-O0}
9011 early in the software development cycle, when extensive debugging capabilities
9012 are most needed, and then move to @option{-O1} and later @option{-O2} as
9013 the debugger becomes less critical.
9014 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
9015 a release management issue.
9017 Note that if you use @option{-g} you can then use the @command{strip} program
9018 on the resulting executable,
9019 which removes both debugging information and global symbols.
9023 @node Inlining of Subprograms
9024 @subsection Inlining of Subprograms
9027 A call to a subprogram in the current unit is inlined if all the
9028 following conditions are met:
9032 The optimization level is at least @option{-O1}.
9035 The called subprogram is suitable for inlining: It must be small enough
9036 and not contain nested subprograms or anything else that @code{gcc}
9037 cannot support in inlined subprograms.
9040 The call occurs after the definition of the body of the subprogram.
9043 @cindex pragma Inline
9045 Either @code{pragma Inline} applies to the subprogram or it is
9046 small and automatic inlining (optimization level @option{-O3}) is
9051 Calls to subprograms in @code{with}'ed units are normally not inlined.
9052 To achieve this level of inlining, the following conditions must all be
9057 The optimization level is at least @option{-O1}.
9060 The called subprogram is suitable for inlining: It must be small enough
9061 and not contain nested subprograms or anything else @code{gcc} cannot
9062 support in inlined subprograms.
9065 The call appears in a body (not in a package spec).
9068 There is a @code{pragma Inline} for the subprogram.
9071 @cindex @option{-gnatn} (@code{gcc})
9072 The @option{^-gnatn^/INLINE^} switch
9073 is used in the @code{gcc} command line
9076 Note that specifying the @option{-gnatn} switch causes additional
9077 compilation dependencies. Consider the following:
9079 @smallexample @c ada
9099 With the default behavior (no @option{-gnatn} switch specified), the
9100 compilation of the @code{Main} procedure depends only on its own source,
9101 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
9102 means that editing the body of @code{R} does not require recompiling
9105 On the other hand, the call @code{R.Q} is not inlined under these
9106 circumstances. If the @option{-gnatn} switch is present when @code{Main}
9107 is compiled, the call will be inlined if the body of @code{Q} is small
9108 enough, but now @code{Main} depends on the body of @code{R} in
9109 @file{r.adb} as well as on the spec. This means that if this body is edited,
9110 the main program must be recompiled. Note that this extra dependency
9111 occurs whether or not the call is in fact inlined by @code{gcc}.
9113 The use of front end inlining with @option{-gnatN} generates similar
9114 additional dependencies.
9116 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@code{gcc})
9117 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
9118 can be used to prevent
9119 all inlining. This switch overrides all other conditions and ensures
9120 that no inlining occurs. The extra dependences resulting from
9121 @option{-gnatn} will still be active, even if
9122 this switch is used to suppress the resulting inlining actions.
9124 Note regarding the use of @option{-O3}: There is no difference in inlining
9125 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
9126 pragma @code{Inline} assuming the use of @option{-gnatn}
9127 or @option{-gnatN} (the switches that activate inlining). If you have used
9128 pragma @code{Inline} in appropriate cases, then it is usually much better
9129 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
9130 in this case only has the effect of inlining subprograms you did not
9131 think should be inlined. We often find that the use of @option{-O3} slows
9132 down code by performing excessive inlining, leading to increased instruction
9133 cache pressure from the increased code size. So the bottom line here is
9134 that you should not automatically assume that @option{-O3} is better than
9135 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
9136 it actually improves performance.
9138 @node Optimization and Strict Aliasing
9139 @subsection Optimization and Strict Aliasing
9141 @cindex Strict Aliasing
9142 @cindex No_Strict_Aliasing
9145 The strong typing capabilities of Ada allow an optimizer to generate
9146 efficient code in situations where other languages would be forced to
9147 make worst case assumptions preventing such optimizations. Consider
9148 the following example:
9150 @smallexample @c ada
9153 type Int1 is new Integer;
9154 type Int2 is new Integer;
9155 type Int1A is access Int1;
9156 type Int2A is access Int2;
9163 for J in Data'Range loop
9164 if Data (J) = Int1V.all then
9165 Int2V.all := Int2V.all + 1;
9174 In this example, since the variable @code{Int1V} can only access objects
9175 of type @code{Int1}, and @code{Int2V} can only access objects of type
9176 @code{Int2}, there is no possibility that the assignment to
9177 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
9178 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
9179 for all iterations of the loop and avoid the extra memory reference
9180 required to dereference it each time through the loop.
9182 This kind of optimziation, called strict aliasing analysis, is
9183 triggered by specifying an optimization level of @option{-O2} or
9184 higher and allows @code{GNAT} to generate more efficient code
9185 when access values are involved.
9187 However, although this optimization is always correct in terms of
9188 the formal semantics of the Ada Reference Manual, difficulties can
9189 arise if features like @code{Unchecked_Conversion} are used to break
9190 the typing system. Consider the following complete program example:
9192 @smallexample @c ada
9195 type int1 is new integer;
9196 type int2 is new integer;
9197 type a1 is access int1;
9198 type a2 is access int2;
9203 function to_a2 (Input : a1) return a2;
9206 with Unchecked_Conversion;
9208 function to_a2 (Input : a1) return a2 is
9210 new Unchecked_Conversion (a1, a2);
9212 return to_a2u (Input);
9218 with Text_IO; use Text_IO;
9220 v1 : a1 := new int1;
9221 v2 : a2 := to_a2 (v1);
9225 put_line (int1'image (v1.all));
9231 This program prints out 0 in @code{-O0} or @code{-O1}
9232 mode, but it prints out 1 in @code{-O2} mode. That's
9233 because in strict aliasing mode, the compiler can and
9234 does assume that the assignment to @code{v2.all} could not
9235 affect the value of @code{v1.all}, since different types
9238 This behavior is not a case of non-conformance with the standard, since
9239 the Ada RM specifies that an unchecked conversion where the resulting
9240 bit pattern is not a correct value of the target type can result in an
9241 abnormal value and attempting to reference an abnormal value makes the
9242 execution of a program erroneous. That's the case here since the result
9243 does not point to an object of type @code{int2}. This means that the
9244 effect is entirely unpredictable.
9246 However, although that explanation may satisfy a language
9247 lawyer, in practice an applications programmer expects an
9248 unchecked conversion involving pointers to create true
9249 aliases and the behavior of printing 1 seems plain wrong.
9250 In this case, the strict aliasing optimization is unwelcome.
9252 Indeed the compiler recognizes this possibility, and the
9253 unchecked conversion generates a warning:
9256 p2.adb:5:07: warning: possible aliasing problem with type "a2"
9257 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
9258 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
9262 Unfortunately the problem is recognized when compiling the body of
9263 package @code{p2}, but the actual "bad" code is generated while
9264 compiling the body of @code{m} and this latter compilation does not see
9265 the suspicious @code{Unchecked_Conversion}.
9267 As implied by the warning message, there are approaches you can use to
9268 avoid the unwanted strict aliasing optimization in a case like this.
9270 One possibility is to simply avoid the use of @code{-O2}, but
9271 that is a bit drastic, since it throws away a number of useful
9272 optimizations that do not involve strict aliasing assumptions.
9274 A less drastic approach is to compile the program using the
9275 option @code{-fno-strict-aliasing}. Actually it is only the
9276 unit containing the dereferencing of the suspicious pointer
9277 that needs to be compiled. So in this case, if we compile
9278 unit @code{m} with this switch, then we get the expected
9279 value of zero printed. Analyzing which units might need
9280 the switch can be painful, so a more reasonable approach
9281 is to compile the entire program with options @code{-O2}
9282 and @code{-fno-strict-aliasing}. If the performance is
9283 satisfactory with this combination of options, then the
9284 advantage is that the entire issue of possible "wrong"
9285 optimization due to strict aliasing is avoided.
9287 To avoid the use of compiler switches, the configuration
9288 pragma @code{No_Strict_Aliasing} with no parameters may be
9289 used to specify that for all access types, the strict
9290 aliasing optimization should be suppressed.
9292 However, these approaches are still overkill, in that they causes
9293 all manipulations of all access values to be deoptimized. A more
9294 refined approach is to concentrate attention on the specific
9295 access type identified as problematic.
9297 First, if a careful analysis of uses of the pointer shows
9298 that there are no possible problematic references, then
9299 the warning can be suppressed by bracketing the
9300 instantiation of @code{Unchecked_Conversion} to turn
9303 @smallexample @c ada
9304 pragma Warnings (Off);
9306 new Unchecked_Conversion (a1, a2);
9307 pragma Warnings (On);
9311 Of course that approach is not appropriate for this particular
9312 example, since indeed there is a problematic reference. In this
9313 case we can take one of two other approaches.
9315 The first possibility is to move the instantiation of unchecked
9316 conversion to the unit in which the type is declared. In
9317 this example, we would move the instantiation of
9318 @code{Unchecked_Conversion} from the body of package
9319 @code{p2} to the spec of package @code{p1}. Now the
9320 warning disappears. That's because any use of the
9321 access type knows there is a suspicious unchecked
9322 conversion, and the strict aliasing optimization
9323 is automatically suppressed for the type.
9325 If it is not practical to move the unchecked conversion to the same unit
9326 in which the destination access type is declared (perhaps because the
9327 source type is not visible in that unit), you may use pragma
9328 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
9329 same declarative sequence as the declaration of the access type:
9331 @smallexample @c ada
9332 type a2 is access int2;
9333 pragma No_Strict_Aliasing (a2);
9337 Here again, the compiler now knows that the strict aliasing optimization
9338 should be suppressed for any reference to type @code{a2} and the
9339 expected behavior is obtained.
9341 Finally, note that although the compiler can generate warnings for
9342 simple cases of unchecked conversions, there are tricker and more
9343 indirect ways of creating type incorrect aliases which the compiler
9344 cannot detect. Examples are the use of address overlays and unchecked
9345 conversions involving composite types containing access types as
9346 components. In such cases, no warnings are generated, but there can
9347 still be aliasing problems. One safe coding practice is to forbid the
9348 use of address clauses for type overlaying, and to allow unchecked
9349 conversion only for primitive types. This is not really a significant
9350 restriction since any possible desired effect can be achieved by
9351 unchecked conversion of access values.
9354 @node Coverage Analysis
9355 @subsection Coverage Analysis
9358 GNAT supports the Digital Performance Coverage Analyzer (PCA), which allows
9359 the user to determine the distribution of execution time across a program,
9360 @pxref{Profiling} for details of usage.
9363 @node Reducing the Size of Ada Executables with gnatelim
9364 @section Reducing the Size of Ada Executables with @code{gnatelim}
9368 This section describes @command{gnatelim}, a tool which detects unused
9369 subprograms and helps the compiler to create a smaller executable for your
9374 * Running gnatelim::
9375 * Correcting the List of Eliminate Pragmas::
9376 * Making Your Executables Smaller::
9377 * Summary of the gnatelim Usage Cycle::
9380 @node About gnatelim
9381 @subsection About @code{gnatelim}
9384 When a program shares a set of Ada
9385 packages with other programs, it may happen that this program uses
9386 only a fraction of the subprograms defined in these packages. The code
9387 created for these unused subprograms increases the size of the executable.
9389 @code{gnatelim} tracks unused subprograms in an Ada program and
9390 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
9391 subprograms that are declared but never called. By placing the list of
9392 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
9393 recompiling your program, you may decrease the size of its executable,
9394 because the compiler will not generate the code for 'eliminated' subprograms.
9395 See GNAT Reference Manual for more information about this pragma.
9397 @code{gnatelim} needs as its input data the name of the main subprogram
9398 and a bind file for a main subprogram.
9400 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
9401 the main subprogram. @code{gnatelim} can work with both Ada and C
9402 bind files; when both are present, it uses the Ada bind file.
9403 The following commands will build the program and create the bind file:
9406 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
9407 $ gnatbind main_prog
9410 Note that @code{gnatelim} needs neither object nor ALI files.
9412 @node Running gnatelim
9413 @subsection Running @code{gnatelim}
9416 @code{gnatelim} has the following command-line interface:
9419 $ gnatelim [options] name
9423 @code{name} should be a name of a source file that contains the main subprogram
9424 of a program (partition).
9426 @code{gnatelim} has the following switches:
9431 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
9432 Quiet mode: by default @code{gnatelim} outputs to the standard error
9433 stream the number of program units left to be processed. This option turns
9437 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
9438 Verbose mode: @code{gnatelim} version information is printed as Ada
9439 comments to the standard output stream. Also, in addition to the number of
9440 program units left @code{gnatelim} will output the name of the current unit
9444 @cindex @option{^-a^/ALL^} (@command{gnatelim})
9445 Also look for subprograms from the GNAT run time that can be eliminated. Note
9446 that when @file{gnat.adc} is produced using this switch, the entire program
9447 must be recompiled with switch @option{^-a^/ALL_FILES^} to @code{gnatmake}.
9449 @item ^-I^/INCLUDE_DIRS=^@var{dir}
9450 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
9451 When looking for source files also look in directory @var{dir}. Specifying
9452 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
9453 sources in the current directory.
9455 @item ^-b^/BIND_FILE=^@var{bind_file}
9456 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
9457 Specifies @var{bind_file} as the bind file to process. If not set, the name
9458 of the bind file is computed from the full expanded Ada name
9459 of a main subprogram.
9461 @item ^-C^/CONFIG_FILE=^@var{config_file}
9462 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
9463 Specifies a file @var{config_file} that contains configuration pragmas. The
9464 file must be specified with full path.
9466 @item ^--GCC^/COMPILER^=@var{compiler_name}
9467 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
9468 Instructs @code{gnatelim} to use specific @code{gcc} compiler instead of one
9469 available on the path.
9471 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
9472 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
9473 Instructs @code{gnatelim} to use specific @code{gnatmake} instead of one
9474 available on the path.
9477 @cindex @option{-d@var{x}} (@command{gnatelim})
9478 Activate internal debugging switches. @var{x} is a letter or digit, or
9479 string of letters or digits, which specifies the type of debugging
9480 mode desired. Normally these are used only for internal development
9481 or system debugging purposes. You can find full documentation for these
9482 switches in the spec of the @code{Gnatelim} unit in the compiler
9483 source file @file{gnatelim.ads}.
9487 @code{gnatelim} sends its output to the standard output stream, and all the
9488 tracing and debug information is sent to the standard error stream.
9489 In order to produce a proper GNAT configuration file
9490 @file{gnat.adc}, redirection must be used:
9494 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
9497 $ gnatelim main_prog.adb > gnat.adc
9506 $ gnatelim main_prog.adb >> gnat.adc
9510 in order to append the @code{gnatelim} output to the existing contents of
9514 @node Correcting the List of Eliminate Pragmas
9515 @subsection Correcting the List of Eliminate Pragmas
9518 In some rare cases @code{gnatelim} may try to eliminate
9519 subprograms that are actually called in the program. In this case, the
9520 compiler will generate an error message of the form:
9523 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
9527 You will need to manually remove the wrong @code{Eliminate} pragmas from
9528 the @file{gnat.adc} file. You should recompile your program
9529 from scratch after that, because you need a consistent @file{gnat.adc} file
9530 during the entire compilation.
9533 @node Making Your Executables Smaller
9534 @subsection Making Your Executables Smaller
9537 In order to get a smaller executable for your program you now have to
9538 recompile the program completely with the new @file{gnat.adc} file
9539 created by @code{gnatelim} in your current directory:
9542 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9546 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
9547 recompile everything
9548 with the set of pragmas @code{Eliminate} that you have obtained with
9549 @command{gnatelim}).
9551 Be aware that the set of @code{Eliminate} pragmas is specific to each
9552 program. It is not recommended to merge sets of @code{Eliminate}
9553 pragmas created for different programs in one @file{gnat.adc} file.
9555 @node Summary of the gnatelim Usage Cycle
9556 @subsection Summary of the gnatelim Usage Cycle
9559 Here is a quick summary of the steps to be taken in order to reduce
9560 the size of your executables with @code{gnatelim}. You may use
9561 other GNAT options to control the optimization level,
9562 to produce the debugging information, to set search path, etc.
9569 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
9570 $ gnatbind main_prog
9574 Generate a list of @code{Eliminate} pragmas
9577 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
9580 $ gnatelim main_prog >[>] gnat.adc
9585 Recompile the application
9588 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
9596 @c ********************************
9597 @node Renaming Files Using gnatchop
9598 @chapter Renaming Files Using @code{gnatchop}
9602 This chapter discusses how to handle files with multiple units by using
9603 the @code{gnatchop} utility. This utility is also useful in renaming
9604 files to meet the standard GNAT default file naming conventions.
9607 * Handling Files with Multiple Units::
9608 * Operating gnatchop in Compilation Mode::
9609 * Command Line for gnatchop::
9610 * Switches for gnatchop::
9611 * Examples of gnatchop Usage::
9614 @node Handling Files with Multiple Units
9615 @section Handling Files with Multiple Units
9618 The basic compilation model of GNAT requires that a file submitted to the
9619 compiler have only one unit and there be a strict correspondence
9620 between the file name and the unit name.
9622 The @code{gnatchop} utility allows both of these rules to be relaxed,
9623 allowing GNAT to process files which contain multiple compilation units
9624 and files with arbitrary file names. @code{gnatchop}
9625 reads the specified file and generates one or more output files,
9626 containing one unit per file. The unit and the file name correspond,
9627 as required by GNAT.
9629 If you want to permanently restructure a set of ``foreign'' files so that
9630 they match the GNAT rules, and do the remaining development using the
9631 GNAT structure, you can simply use @command{gnatchop} once, generate the
9632 new set of files and work with them from that point on.
9634 Alternatively, if you want to keep your files in the ``foreign'' format,
9635 perhaps to maintain compatibility with some other Ada compilation
9636 system, you can set up a procedure where you use @command{gnatchop} each
9637 time you compile, regarding the source files that it writes as temporary
9638 files that you throw away.
9641 @node Operating gnatchop in Compilation Mode
9642 @section Operating gnatchop in Compilation Mode
9645 The basic function of @code{gnatchop} is to take a file with multiple units
9646 and split it into separate files. The boundary between files is reasonably
9647 clear, except for the issue of comments and pragmas. In default mode, the
9648 rule is that any pragmas between units belong to the previous unit, except
9649 that configuration pragmas always belong to the following unit. Any comments
9650 belong to the following unit. These rules
9651 almost always result in the right choice of
9652 the split point without needing to mark it explicitly and most users will
9653 find this default to be what they want. In this default mode it is incorrect to
9654 submit a file containing only configuration pragmas, or one that ends in
9655 configuration pragmas, to @code{gnatchop}.
9657 However, using a special option to activate ``compilation mode'',
9659 can perform another function, which is to provide exactly the semantics
9660 required by the RM for handling of configuration pragmas in a compilation.
9661 In the absence of configuration pragmas (at the main file level), this
9662 option has no effect, but it causes such configuration pragmas to be handled
9663 in a quite different manner.
9665 First, in compilation mode, if @code{gnatchop} is given a file that consists of
9666 only configuration pragmas, then this file is appended to the
9667 @file{gnat.adc} file in the current directory. This behavior provides
9668 the required behavior described in the RM for the actions to be taken
9669 on submitting such a file to the compiler, namely that these pragmas
9670 should apply to all subsequent compilations in the same compilation
9671 environment. Using GNAT, the current directory, possibly containing a
9672 @file{gnat.adc} file is the representation
9673 of a compilation environment. For more information on the
9674 @file{gnat.adc} file, see the section on handling of configuration
9675 pragmas @pxref{Handling of Configuration Pragmas}.
9677 Second, in compilation mode, if @code{gnatchop}
9678 is given a file that starts with
9679 configuration pragmas, and contains one or more units, then these
9680 configuration pragmas are prepended to each of the chopped files. This
9681 behavior provides the required behavior described in the RM for the
9682 actions to be taken on compiling such a file, namely that the pragmas
9683 apply to all units in the compilation, but not to subsequently compiled
9686 Finally, if configuration pragmas appear between units, they are appended
9687 to the previous unit. This results in the previous unit being illegal,
9688 since the compiler does not accept configuration pragmas that follow
9689 a unit. This provides the required RM behavior that forbids configuration
9690 pragmas other than those preceding the first compilation unit of a
9693 For most purposes, @code{gnatchop} will be used in default mode. The
9694 compilation mode described above is used only if you need exactly
9695 accurate behavior with respect to compilations, and you have files
9696 that contain multiple units and configuration pragmas. In this
9697 circumstance the use of @code{gnatchop} with the compilation mode
9698 switch provides the required behavior, and is for example the mode
9699 in which GNAT processes the ACVC tests.
9701 @node Command Line for gnatchop
9702 @section Command Line for @code{gnatchop}
9705 The @code{gnatchop} command has the form:
9708 $ gnatchop switches @var{file name} [@var{file name} @var{file name} ...]
9713 The only required argument is the file name of the file to be chopped.
9714 There are no restrictions on the form of this file name. The file itself
9715 contains one or more Ada units, in normal GNAT format, concatenated
9716 together. As shown, more than one file may be presented to be chopped.
9718 When run in default mode, @code{gnatchop} generates one output file in
9719 the current directory for each unit in each of the files.
9721 @var{directory}, if specified, gives the name of the directory to which
9722 the output files will be written. If it is not specified, all files are
9723 written to the current directory.
9725 For example, given a
9726 file called @file{hellofiles} containing
9728 @smallexample @c ada
9733 with Text_IO; use Text_IO;
9746 $ gnatchop ^hellofiles^HELLOFILES.^
9750 generates two files in the current directory, one called
9751 @file{hello.ads} containing the single line that is the procedure spec,
9752 and the other called @file{hello.adb} containing the remaining text. The
9753 original file is not affected. The generated files can be compiled in
9757 When gnatchop is invoked on a file that is empty or that contains only empty
9758 lines and/or comments, gnatchop will not fail, but will not produce any
9761 For example, given a
9762 file called @file{toto.txt} containing
9764 @smallexample @c ada
9776 $ gnatchop ^toto.txt^TOT.TXT^
9780 will not produce any new file and will result in the following warnings:
9783 toto.txt:1:01: warning: empty file, contains no compilation units
9784 no compilation units found
9785 no source files written
9788 @node Switches for gnatchop
9789 @section Switches for @code{gnatchop}
9792 @command{gnatchop} recognizes the following switches:
9797 @item ^-c^/COMPILATION^
9798 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
9799 Causes @code{gnatchop} to operate in compilation mode, in which
9800 configuration pragmas are handled according to strict RM rules. See
9801 previous section for a full description of this mode.
9805 This passes the given @option{-gnatxxx} switch to @code{gnat} which is
9806 used to parse the given file. Not all @code{xxx} options make sense,
9807 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
9808 process a source file that uses Latin-2 coding for identifiers.
9812 Causes @code{gnatchop} to generate a brief help summary to the standard
9813 output file showing usage information.
9815 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
9816 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
9817 Limit generated file names to the specified number @code{mm}
9819 This is useful if the
9820 resulting set of files is required to be interoperable with systems
9821 which limit the length of file names.
9823 If no value is given, or
9824 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
9825 a default of 39, suitable for OpenVMS Alpha
9829 No space is allowed between the @option{-k} and the numeric value. The numeric
9830 value may be omitted in which case a default of @option{-k8},
9832 with DOS-like file systems, is used. If no @option{-k} switch
9834 there is no limit on the length of file names.
9837 @item ^-p^/PRESERVE^
9838 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
9839 Causes the file ^modification^creation^ time stamp of the input file to be
9840 preserved and used for the time stamp of the output file(s). This may be
9841 useful for preserving coherency of time stamps in an environment where
9842 @code{gnatchop} is used as part of a standard build process.
9845 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
9846 Causes output of informational messages indicating the set of generated
9847 files to be suppressed. Warnings and error messages are unaffected.
9849 @item ^-r^/REFERENCE^
9850 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
9851 @findex Source_Reference
9852 Generate @code{Source_Reference} pragmas. Use this switch if the output
9853 files are regarded as temporary and development is to be done in terms
9854 of the original unchopped file. This switch causes
9855 @code{Source_Reference} pragmas to be inserted into each of the
9856 generated files to refers back to the original file name and line number.
9857 The result is that all error messages refer back to the original
9859 In addition, the debugging information placed into the object file (when
9860 the @option{^-g^/DEBUG^} switch of @code{gcc} or @code{gnatmake} is specified)
9861 also refers back to this original file so that tools like profilers and
9862 debuggers will give information in terms of the original unchopped file.
9864 If the original file to be chopped itself contains
9865 a @code{Source_Reference}
9866 pragma referencing a third file, then gnatchop respects
9867 this pragma, and the generated @code{Source_Reference} pragmas
9868 in the chopped file refer to the original file, with appropriate
9869 line numbers. This is particularly useful when @code{gnatchop}
9870 is used in conjunction with @code{gnatprep} to compile files that
9871 contain preprocessing statements and multiple units.
9874 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
9875 Causes @code{gnatchop} to operate in verbose mode. The version
9876 number and copyright notice are output, as well as exact copies of
9877 the gnat1 commands spawned to obtain the chop control information.
9879 @item ^-w^/OVERWRITE^
9880 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
9881 Overwrite existing file names. Normally @code{gnatchop} regards it as a
9882 fatal error if there is already a file with the same name as a
9883 file it would otherwise output, in other words if the files to be
9884 chopped contain duplicated units. This switch bypasses this
9885 check, and causes all but the last instance of such duplicated
9886 units to be skipped.
9890 @cindex @option{--GCC=} (@code{gnatchop})
9891 Specify the path of the GNAT parser to be used. When this switch is used,
9892 no attempt is made to add the prefix to the GNAT parser executable.
9896 @node Examples of gnatchop Usage
9897 @section Examples of @code{gnatchop} Usage
9901 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
9904 @item gnatchop -w hello_s.ada prerelease/files
9907 Chops the source file @file{hello_s.ada}. The output files will be
9908 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
9910 files with matching names in that directory (no files in the current
9911 directory are modified).
9913 @item gnatchop ^archive^ARCHIVE.^
9914 Chops the source file @file{^archive^ARCHIVE.^}
9915 into the current directory. One
9916 useful application of @code{gnatchop} is in sending sets of sources
9917 around, for example in email messages. The required sources are simply
9918 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
9920 @code{gnatchop} is used at the other end to reconstitute the original
9923 @item gnatchop file1 file2 file3 direc
9924 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
9925 the resulting files in the directory @file{direc}. Note that if any units
9926 occur more than once anywhere within this set of files, an error message
9927 is generated, and no files are written. To override this check, use the
9928 @option{^-w^/OVERWRITE^} switch,
9929 in which case the last occurrence in the last file will
9930 be the one that is output, and earlier duplicate occurrences for a given
9931 unit will be skipped.
9934 @node Configuration Pragmas
9935 @chapter Configuration Pragmas
9936 @cindex Configuration pragmas
9937 @cindex Pragmas, configuration
9940 In Ada 95, configuration pragmas include those pragmas described as
9941 such in the Ada 95 Reference Manual, as well as
9942 implementation-dependent pragmas that are configuration pragmas. See the
9943 individual descriptions of pragmas in the GNAT Reference Manual for
9944 details on these additional GNAT-specific configuration pragmas. Most
9945 notably, the pragma @code{Source_File_Name}, which allows
9946 specifying non-default names for source files, is a configuration
9947 pragma. The following is a complete list of configuration pragmas
9948 recognized by @code{GNAT}:
9960 External_Name_Casing
9961 Float_Representation
9968 Propagate_Exceptions
9977 Task_Dispatching_Policy
9986 * Handling of Configuration Pragmas::
9987 * The Configuration Pragmas Files::
9990 @node Handling of Configuration Pragmas
9991 @section Handling of Configuration Pragmas
9993 Configuration pragmas may either appear at the start of a compilation
9994 unit, in which case they apply only to that unit, or they may apply to
9995 all compilations performed in a given compilation environment.
9997 GNAT also provides the @code{gnatchop} utility to provide an automatic
9998 way to handle configuration pragmas following the semantics for
9999 compilations (that is, files with multiple units), described in the RM.
10000 See section @pxref{Operating gnatchop in Compilation Mode} for details.
10001 However, for most purposes, it will be more convenient to edit the
10002 @file{gnat.adc} file that contains configuration pragmas directly,
10003 as described in the following section.
10005 @node The Configuration Pragmas Files
10006 @section The Configuration Pragmas Files
10007 @cindex @file{gnat.adc}
10010 In GNAT a compilation environment is defined by the current
10011 directory at the time that a compile command is given. This current
10012 directory is searched for a file whose name is @file{gnat.adc}. If
10013 this file is present, it is expected to contain one or more
10014 configuration pragmas that will be applied to the current compilation.
10015 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
10018 Configuration pragmas may be entered into the @file{gnat.adc} file
10019 either by running @code{gnatchop} on a source file that consists only of
10020 configuration pragmas, or more conveniently by
10021 direct editing of the @file{gnat.adc} file, which is a standard format
10024 In addition to @file{gnat.adc}, one additional file containing configuration
10025 pragmas may be applied to the current compilation using the switch
10026 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
10027 contains only configuration pragmas. These configuration pragmas are
10028 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
10029 is present and switch @option{-gnatA} is not used).
10031 It is allowed to specify several switches @option{-gnatec}, however only
10032 the last one on the command line will be taken into account.
10034 If you are using project file, a separate mechanism is provided using
10035 project attributes, see @ref{Specifying Configuration Pragmas} for more
10039 Of special interest to GNAT OpenVMS Alpha is the following
10040 configuration pragma:
10042 @smallexample @c ada
10044 pragma Extend_System (Aux_DEC);
10049 In the presence of this pragma, GNAT adds to the definition of the
10050 predefined package SYSTEM all the additional types and subprograms that are
10051 defined in DEC Ada. See @pxref{Compatibility with DEC Ada} for details.
10054 @node Handling Arbitrary File Naming Conventions Using gnatname
10055 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
10056 @cindex Arbitrary File Naming Conventions
10059 * Arbitrary File Naming Conventions::
10060 * Running gnatname::
10061 * Switches for gnatname::
10062 * Examples of gnatname Usage::
10065 @node Arbitrary File Naming Conventions
10066 @section Arbitrary File Naming Conventions
10069 The GNAT compiler must be able to know the source file name of a compilation
10070 unit. When using the standard GNAT default file naming conventions
10071 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
10072 does not need additional information.
10075 When the source file names do not follow the standard GNAT default file naming
10076 conventions, the GNAT compiler must be given additional information through
10077 a configuration pragmas file (see @ref{Configuration Pragmas})
10079 When the non standard file naming conventions are well-defined,
10080 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
10081 (see @ref{Alternative File Naming Schemes}) may be sufficient. However,
10082 if the file naming conventions are irregular or arbitrary, a number
10083 of pragma @code{Source_File_Name} for individual compilation units
10085 To help maintain the correspondence between compilation unit names and
10086 source file names within the compiler,
10087 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
10090 @node Running gnatname
10091 @section Running @code{gnatname}
10094 The usual form of the @code{gnatname} command is
10097 $ gnatname [@var{switches}] @var{naming_pattern} [@var{naming_patterns}]
10101 All of the arguments are optional. If invoked without any argument,
10102 @code{gnatname} will display its usage.
10105 When used with at least one naming pattern, @code{gnatname} will attempt to
10106 find all the compilation units in files that follow at least one of the
10107 naming patterns. To find these compilation units,
10108 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
10112 One or several Naming Patterns may be given as arguments to @code{gnatname}.
10113 Each Naming Pattern is enclosed between double quotes.
10114 A Naming Pattern is a regular expression similar to the wildcard patterns
10115 used in file names by the Unix shells or the DOS prompt.
10118 Examples of Naming Patterns are
10127 For a more complete description of the syntax of Naming Patterns,
10128 see the second kind of regular expressions described in @file{g-regexp.ads}
10129 (the ``Glob'' regular expressions).
10132 When invoked with no switches, @code{gnatname} will create a configuration
10133 pragmas file @file{gnat.adc} in the current working directory, with pragmas
10134 @code{Source_File_Name} for each file that contains a valid Ada unit.
10136 @node Switches for gnatname
10137 @section Switches for @code{gnatname}
10140 Switches for @code{gnatname} must precede any specified Naming Pattern.
10143 You may specify any of the following switches to @code{gnatname}:
10148 @item ^-c^/CONFIG_FILE=^@file{file}
10149 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
10150 Create a configuration pragmas file @file{file} (instead of the default
10153 There may be zero, one or more space between @option{-c} and
10156 @file{file} may include directory information. @file{file} must be
10157 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
10158 When a switch @option{^-c^/CONFIG_FILE^} is
10159 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
10161 @item ^-d^/SOURCE_DIRS=^@file{dir}
10162 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
10163 Look for source files in directory @file{dir}. There may be zero, one or more
10164 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
10165 When a switch @option{^-d^/SOURCE_DIRS^}
10166 is specified, the current working directory will not be searched for source
10167 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
10168 or @option{^-D^/DIR_FILES^} switch.
10169 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
10170 If @file{dir} is a relative path, it is relative to the directory of
10171 the configuration pragmas file specified with switch
10172 @option{^-c^/CONFIG_FILE^},
10173 or to the directory of the project file specified with switch
10174 @option{^-P^/PROJECT_FILE^} or,
10175 if neither switch @option{^-c^/CONFIG_FILE^}
10176 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
10177 current working directory. The directory
10178 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
10180 @item ^-D^/DIRS_FILE=^@file{file}
10181 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
10182 Look for source files in all directories listed in text file @file{file}.
10183 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
10185 @file{file} must be an existing, readable text file.
10186 Each non empty line in @file{file} must be a directory.
10187 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
10188 switches @option{^-d^/SOURCE_DIRS^} as there are non empty lines in
10191 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
10192 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
10193 Foreign patterns. Using this switch, it is possible to add sources of languages
10194 other than Ada to the list of sources of a project file.
10195 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
10198 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
10201 will look for Ada units in all files with the @file{.ada} extension,
10202 and will add to the list of file for project @file{prj.gpr} the C files
10203 with extension ".^c^C^".
10206 @cindex @option{^-h^/HELP^} (@code{gnatname})
10207 Output usage (help) information. The output is written to @file{stdout}.
10209 @item ^-P^/PROJECT_FILE=^@file{proj}
10210 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
10211 Create or update project file @file{proj}. There may be zero, one or more space
10212 between @option{-P} and @file{proj}. @file{proj} may include directory
10213 information. @file{proj} must be writable.
10214 There may be only one switch @option{^-P^/PROJECT_FILE^}.
10215 When a switch @option{^-P^/PROJECT_FILE^} is specified,
10216 no switch @option{^-c^/CONFIG_FILE^} may be specified.
10218 @item ^-v^/VERBOSE^
10219 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
10220 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
10221 This includes name of the file written, the name of the directories to search
10222 and, for each file in those directories whose name matches at least one of
10223 the Naming Patterns, an indication of whether the file contains a unit,
10224 and if so the name of the unit.
10226 @item ^-v -v^/VERBOSE /VERBOSE^
10227 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
10228 Very Verbose mode. In addition to the output produced in verbose mode,
10229 for each file in the searched directories whose name matches none of
10230 the Naming Patterns, an indication is given that there is no match.
10232 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
10233 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
10234 Excluded patterns. Using this switch, it is possible to exclude some files
10235 that would match the name patterns. For example,
10237 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
10240 will look for Ada units in all files with the @file{.ada} extension,
10241 except those whose names end with @file{_nt.ada}.
10245 @node Examples of gnatname Usage
10246 @section Examples of @code{gnatname} Usage
10250 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
10256 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
10261 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
10262 and be writable. In addition, the directory
10263 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
10264 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
10267 Note the optional spaces after @option{-c} and @option{-d}.
10272 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
10273 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
10276 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
10277 /EXCLUDED_PATTERN=*_nt_body.ada
10278 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
10279 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
10283 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
10284 even in conjunction with one or several switches
10285 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
10286 are used in this example.
10289 @c *****************************************
10290 @c * G N A T P r o j e c t M a n a g e r *
10291 @c *****************************************
10292 @node GNAT Project Manager
10293 @chapter GNAT Project Manager
10297 * Examples of Project Files::
10298 * Project File Syntax::
10299 * Objects and Sources in Project Files::
10300 * Importing Projects::
10301 * Project Extension::
10302 * External References in Project Files::
10303 * Packages in Project Files::
10304 * Variables from Imported Projects::
10306 * Library Projects::
10307 * Using Third-Party Libraries through Projects::
10308 * Stand-alone Library Projects::
10309 * Switches Related to Project Files::
10310 * Tools Supporting Project Files::
10311 * An Extended Example::
10312 * Project File Complete Syntax::
10315 @c ****************
10316 @c * Introduction *
10317 @c ****************
10320 @section Introduction
10323 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
10324 you to manage complex builds involving a number of source files, directories,
10325 and compilation options for different system configurations. In particular,
10326 project files allow you to specify:
10329 The directory or set of directories containing the source files, and/or the
10330 names of the specific source files themselves
10332 The directory in which the compiler's output
10333 (@file{ALI} files, object files, tree files) is to be placed
10335 The directory in which the executable programs is to be placed
10337 ^Switch^Switch^ settings for any of the project-enabled tools
10338 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
10339 @code{gnatfind}); you can apply these settings either globally or to individual
10342 The source files containing the main subprogram(s) to be built
10344 The source programming language(s) (currently Ada and/or C)
10346 Source file naming conventions; you can specify these either globally or for
10347 individual compilation units
10354 @node Project Files
10355 @subsection Project Files
10358 Project files are written in a syntax close to that of Ada, using familiar
10359 notions such as packages, context clauses, declarations, default values,
10360 assignments, and inheritance. Finally, project files can be built
10361 hierarchically from other project files, simplifying complex system
10362 integration and project reuse.
10364 A @dfn{project} is a specific set of values for various compilation properties.
10365 The settings for a given project are described by means of
10366 a @dfn{project file}, which is a text file written in an Ada-like syntax.
10367 Property values in project files are either strings or lists of strings.
10368 Properties that are not explicitly set receive default values. A project
10369 file may interrogate the values of @dfn{external variables} (user-defined
10370 command-line switches or environment variables), and it may specify property
10371 settings conditionally, based on the value of such variables.
10373 In simple cases, a project's source files depend only on other source files
10374 in the same project, or on the predefined libraries. (@emph{Dependence} is
10376 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
10377 the Project Manager also allows more sophisticated arrangements,
10378 where the source files in one project depend on source files in other
10382 One project can @emph{import} other projects containing needed source files.
10384 You can organize GNAT projects in a hierarchy: a @emph{child} project
10385 can extend a @emph{parent} project, inheriting the parent's source files and
10386 optionally overriding any of them with alternative versions
10390 More generally, the Project Manager lets you structure large development
10391 efforts into hierarchical subsystems, where build decisions are delegated
10392 to the subsystem level, and thus different compilation environments
10393 (^switch^switch^ settings) used for different subsystems.
10395 The Project Manager is invoked through the
10396 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
10397 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
10399 There may be zero, one or more spaces between @option{-P} and
10400 @option{@emph{projectfile}}.
10402 If you want to define (on the command line) an external variable that is
10403 queried by the project file, you must use the
10404 @option{^-X^/EXTERNAT_REFERENCE=^@emph{vbl}=@emph{value}} switch.
10405 The Project Manager parses and interprets the project file, and drives the
10406 invoked tool based on the project settings.
10408 The Project Manager supports a wide range of development strategies,
10409 for systems of all sizes. Here are some typical practices that are
10413 Using a common set of source files, but generating object files in different
10414 directories via different ^switch^switch^ settings
10416 Using a mostly-shared set of source files, but with different versions of
10421 The destination of an executable can be controlled inside a project file
10422 using the @option{^-o^-o^}
10424 In the absence of such a ^switch^switch^ either inside
10425 the project file or on the command line, any executable files generated by
10426 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
10427 in the project file. If no @code{Exec_Dir} is specified, they will be placed
10428 in the object directory of the project.
10430 You can use project files to achieve some of the effects of a source
10431 versioning system (for example, defining separate projects for
10432 the different sets of sources that comprise different releases) but the
10433 Project Manager is independent of any source configuration management tools
10434 that might be used by the developers.
10436 The next section introduces the main features of GNAT's project facility
10437 through a sequence of examples; subsequent sections will present the syntax
10438 and semantics in more detail. A more formal description of the project
10439 facility appears in the GNAT Reference Manual.
10441 @c *****************************
10442 @c * Examples of Project Files *
10443 @c *****************************
10445 @node Examples of Project Files
10446 @section Examples of Project Files
10448 This section illustrates some of the typical uses of project files and
10449 explains their basic structure and behavior.
10452 * Common Sources with Different ^Switches^Switches^ and Directories::
10453 * Using External Variables::
10454 * Importing Other Projects::
10455 * Extending a Project::
10458 @node Common Sources with Different ^Switches^Switches^ and Directories
10459 @subsection Common Sources with Different ^Switches^Switches^ and Directories
10463 * Specifying the Object Directory::
10464 * Specifying the Exec Directory::
10465 * Project File Packages::
10466 * Specifying ^Switch^Switch^ Settings::
10467 * Main Subprograms::
10468 * Executable File Names::
10469 * Source File Naming Conventions::
10470 * Source Language(s)::
10474 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
10475 @file{proc.adb} are in the @file{/common} directory. The file
10476 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
10477 package @code{Pack}. We want to compile these source files under two sets
10478 of ^switches^switches^:
10481 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
10482 and the @option{^-gnata^-gnata^},
10483 @option{^-gnato^-gnato^},
10484 and @option{^-gnatE^-gnatE^} switches to the
10485 compiler; the compiler's output is to appear in @file{/common/debug}
10487 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
10488 to the compiler; the compiler's output is to appear in @file{/common/release}
10492 The GNAT project files shown below, respectively @file{debug.gpr} and
10493 @file{release.gpr} in the @file{/common} directory, achieve these effects.
10506 ^/common/debug^[COMMON.DEBUG]^
10511 ^/common/release^[COMMON.RELEASE]^
10516 Here are the corresponding project files:
10518 @smallexample @c projectfile
10521 for Object_Dir use "debug";
10522 for Main use ("proc");
10525 for ^Default_Switches^Default_Switches^ ("Ada")
10527 for Executable ("proc.adb") use "proc1";
10532 package Compiler is
10533 for ^Default_Switches^Default_Switches^ ("Ada")
10534 use ("-fstack-check",
10537 "^-gnatE^-gnatE^");
10543 @smallexample @c projectfile
10546 for Object_Dir use "release";
10547 for Exec_Dir use ".";
10548 for Main use ("proc");
10550 package Compiler is
10551 for ^Default_Switches^Default_Switches^ ("Ada")
10559 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
10560 insensitive), and analogously the project defined by @file{release.gpr} is
10561 @code{"Release"}. For consistency the file should have the same name as the
10562 project, and the project file's extension should be @code{"gpr"}. These
10563 conventions are not required, but a warning is issued if they are not followed.
10565 If the current directory is @file{^/temp^[TEMP]^}, then the command
10567 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
10571 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
10572 as well as the @code{^proc1^PROC1.EXE^} executable,
10573 using the ^switch^switch^ settings defined in the project file.
10575 Likewise, the command
10577 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
10581 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
10582 and the @code{^proc^PROC.EXE^}
10583 executable in @file{^/common^[COMMON]^},
10584 using the ^switch^switch^ settings from the project file.
10587 @unnumberedsubsubsec Source Files
10590 If a project file does not explicitly specify a set of source directories or
10591 a set of source files, then by default the project's source files are the
10592 Ada source files in the project file directory. Thus @file{pack.ads},
10593 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
10595 @node Specifying the Object Directory
10596 @unnumberedsubsubsec Specifying the Object Directory
10599 Several project properties are modeled by Ada-style @emph{attributes};
10600 a property is defined by supplying the equivalent of an Ada attribute
10601 definition clause in the project file.
10602 A project's object directory is another such a property; the corresponding
10603 attribute is @code{Object_Dir}, and its value is also a string expression,
10604 specified either as absolute or relative. In the later case,
10605 it is relative to the project file directory. Thus the compiler's
10606 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
10607 (for the @code{Debug} project)
10608 and to @file{^/common/release^[COMMON.RELEASE]^}
10609 (for the @code{Release} project).
10610 If @code{Object_Dir} is not specified, then the default is the project file
10613 @node Specifying the Exec Directory
10614 @unnumberedsubsubsec Specifying the Exec Directory
10617 A project's exec directory is another property; the corresponding
10618 attribute is @code{Exec_Dir}, and its value is also a string expression,
10619 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
10620 then the default is the object directory (which may also be the project file
10621 directory if attribute @code{Object_Dir} is not specified). Thus the executable
10622 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
10623 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
10624 and in @file{^/common^[COMMON]^} for the @code{Release} project.
10626 @node Project File Packages
10627 @unnumberedsubsubsec Project File Packages
10630 A GNAT tool that is integrated with the Project Manager is modeled by a
10631 corresponding package in the project file. In the example above,
10632 The @code{Debug} project defines the packages @code{Builder}
10633 (for @command{gnatmake}) and @code{Compiler};
10634 the @code{Release} project defines only the @code{Compiler} package.
10636 The Ada-like package syntax is not to be taken literally. Although packages in
10637 project files bear a surface resemblance to packages in Ada source code, the
10638 notation is simply a way to convey a grouping of properties for a named
10639 entity. Indeed, the package names permitted in project files are restricted
10640 to a predefined set, corresponding to the project-aware tools, and the contents
10641 of packages are limited to a small set of constructs.
10642 The packages in the example above contain attribute definitions.
10644 @node Specifying ^Switch^Switch^ Settings
10645 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
10648 ^Switch^Switch^ settings for a project-aware tool can be specified through
10649 attributes in the package that corresponds to the tool.
10650 The example above illustrates one of the relevant attributes,
10651 @code{^Default_Switches^Default_Switches^}, which is defined in packages
10652 in both project files.
10653 Unlike simple attributes like @code{Source_Dirs},
10654 @code{^Default_Switches^Default_Switches^} is
10655 known as an @emph{associative array}. When you define this attribute, you must
10656 supply an ``index'' (a literal string), and the effect of the attribute
10657 definition is to set the value of the array at the specified index.
10658 For the @code{^Default_Switches^Default_Switches^} attribute,
10659 the index is a programming language (in our case, Ada),
10660 and the value specified (after @code{use}) must be a list
10661 of string expressions.
10663 The attributes permitted in project files are restricted to a predefined set.
10664 Some may appear at project level, others in packages.
10665 For any attribute that is an associative array, the index must always be a
10666 literal string, but the restrictions on this string (e.g., a file name or a
10667 language name) depend on the individual attribute.
10668 Also depending on the attribute, its specified value will need to be either a
10669 string or a string list.
10671 In the @code{Debug} project, we set the switches for two tools,
10672 @command{gnatmake} and the compiler, and thus we include the two corresponding
10673 packages; each package defines the @code{^Default_Switches^Default_Switches^}
10674 attribute with index @code{"Ada"}.
10675 Note that the package corresponding to
10676 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
10677 similar, but only includes the @code{Compiler} package.
10679 In project @code{Debug} above, the ^switches^switches^ starting with
10680 @option{-gnat} that are specified in package @code{Compiler}
10681 could have been placed in package @code{Builder}, since @command{gnatmake}
10682 transmits all such ^switches^switches^ to the compiler.
10684 @node Main Subprograms
10685 @unnumberedsubsubsec Main Subprograms
10688 One of the specifiable properties of a project is a list of files that contain
10689 main subprograms. This property is captured in the @code{Main} attribute,
10690 whose value is a list of strings. If a project defines the @code{Main}
10691 attribute, it is not necessary to identify the main subprogram(s) when
10692 invoking @command{gnatmake} (see @ref{gnatmake and Project Files}).
10694 @node Executable File Names
10695 @unnumberedsubsubsec Executable File Names
10698 By default, the executable file name corresponding to a main source is
10699 deducted from the main source file name. Through the attributes
10700 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
10701 it is possible to change this default.
10702 In project @code{Debug} above, the executable file name
10703 for main source @file{^proc.adb^PROC.ADB^} is
10704 @file{^proc1^PROC1.EXE^}.
10705 Attribute @code{Executable_Suffix}, when specified, may change the suffix
10706 of the the executable files, when no attribute @code{Executable} applies:
10707 its value replace the platform-specific executable suffix.
10708 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
10709 specify a non default executable file name when several mains are built at once
10710 in a single @command{gnatmake} command.
10712 @node Source File Naming Conventions
10713 @unnumberedsubsubsec Source File Naming Conventions
10716 Since the project files above do not specify any source file naming
10717 conventions, the GNAT defaults are used. The mechanism for defining source
10718 file naming conventions -- a package named @code{Naming} --
10719 is described below (@pxref{Naming Schemes}).
10721 @node Source Language(s)
10722 @unnumberedsubsubsec Source Language(s)
10725 Since the project files do not specify a @code{Languages} attribute, by
10726 default the GNAT tools assume that the language of the project file is Ada.
10727 More generally, a project can comprise source files
10728 in Ada, C, and/or other languages.
10730 @node Using External Variables
10731 @subsection Using External Variables
10734 Instead of supplying different project files for debug and release, we can
10735 define a single project file that queries an external variable (set either
10736 on the command line or via an ^environment variable^logical name^) in order to
10737 conditionally define the appropriate settings. Again, assume that the
10738 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
10739 located in directory @file{^/common^[COMMON]^}. The following project file,
10740 @file{build.gpr}, queries the external variable named @code{STYLE} and
10741 defines an object directory and ^switch^switch^ settings based on whether
10742 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
10743 the default is @code{"deb"}.
10745 @smallexample @c projectfile
10748 for Main use ("proc");
10750 type Style_Type is ("deb", "rel");
10751 Style : Style_Type := external ("STYLE", "deb");
10755 for Object_Dir use "debug";
10758 for Object_Dir use "release";
10759 for Exec_Dir use ".";
10768 for ^Default_Switches^Default_Switches^ ("Ada")
10770 for Executable ("proc") use "proc1";
10777 package Compiler is
10781 for ^Default_Switches^Default_Switches^ ("Ada")
10782 use ("^-gnata^-gnata^",
10784 "^-gnatE^-gnatE^");
10787 for ^Default_Switches^Default_Switches^ ("Ada")
10798 @code{Style_Type} is an example of a @emph{string type}, which is the project
10799 file analog of an Ada enumeration type but whose components are string literals
10800 rather than identifiers. @code{Style} is declared as a variable of this type.
10802 The form @code{external("STYLE", "deb")} is known as an
10803 @emph{external reference}; its first argument is the name of an
10804 @emph{external variable}, and the second argument is a default value to be
10805 used if the external variable doesn't exist. You can define an external
10806 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
10807 or you can use ^an environment variable^a logical name^
10808 as an external variable.
10810 Each @code{case} construct is expanded by the Project Manager based on the
10811 value of @code{Style}. Thus the command
10814 gnatmake -P/common/build.gpr -XSTYLE=deb
10820 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
10825 is equivalent to the @command{gnatmake} invocation using the project file
10826 @file{debug.gpr} in the earlier example. So is the command
10828 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
10832 since @code{"deb"} is the default for @code{STYLE}.
10838 gnatmake -P/common/build.gpr -XSTYLE=rel
10844 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
10849 is equivalent to the @command{gnatmake} invocation using the project file
10850 @file{release.gpr} in the earlier example.
10852 @node Importing Other Projects
10853 @subsection Importing Other Projects
10856 A compilation unit in a source file in one project may depend on compilation
10857 units in source files in other projects. To compile this unit under
10858 control of a project file, the
10859 dependent project must @emph{import} the projects containing the needed source
10861 This effect is obtained using syntax similar to an Ada @code{with} clause,
10862 but where @code{with}ed entities are strings that denote project files.
10864 As an example, suppose that the two projects @code{GUI_Proj} and
10865 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
10866 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
10867 and @file{^/comm^[COMM]^}, respectively.
10868 Suppose that the source files for @code{GUI_Proj} are
10869 @file{gui.ads} and @file{gui.adb}, and that the source files for
10870 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
10871 files is located in its respective project file directory. Schematically:
10890 We want to develop an application in directory @file{^/app^[APP]^} that
10891 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
10892 the corresponding project files (e.g. the ^switch^switch^ settings
10893 and object directory).
10894 Skeletal code for a main procedure might be something like the following:
10896 @smallexample @c ada
10899 procedure App_Main is
10908 Here is a project file, @file{app_proj.gpr}, that achieves the desired
10911 @smallexample @c projectfile
10913 with "/gui/gui_proj", "/comm/comm_proj";
10914 project App_Proj is
10915 for Main use ("app_main");
10921 Building an executable is achieved through the command:
10923 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
10926 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
10927 in the directory where @file{app_proj.gpr} resides.
10929 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
10930 (as illustrated above) the @code{with} clause can omit the extension.
10932 Our example specified an absolute path for each imported project file.
10933 Alternatively, the directory name of an imported object can be omitted
10937 The imported project file is in the same directory as the importing project
10940 You have defined ^an environment variable^a logical name^
10941 that includes the directory containing
10942 the needed project file. The syntax of @code{ADA_PROJECT_PATH} is the same as
10943 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
10944 directory names separated by colons (semicolons on Windows).
10948 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{^/gui^[GUI]^} and
10949 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
10952 @smallexample @c projectfile
10954 with "gui_proj", "comm_proj";
10955 project App_Proj is
10956 for Main use ("app_main");
10962 Importing other projects can create ambiguities.
10963 For example, the same unit might be present in different imported projects, or
10964 it might be present in both the importing project and in an imported project.
10965 Both of these conditions are errors. Note that in the current version of
10966 the Project Manager, it is illegal to have an ambiguous unit even if the
10967 unit is never referenced by the importing project. This restriction may be
10968 relaxed in a future release.
10970 @node Extending a Project
10971 @subsection Extending a Project
10974 In large software systems it is common to have multiple
10975 implementations of a common interface; in Ada terms, multiple versions of a
10976 package body for the same specification. For example, one implementation
10977 might be safe for use in tasking programs, while another might only be used
10978 in sequential applications. This can be modeled in GNAT using the concept
10979 of @emph{project extension}. If one project (the ``child'') @emph{extends}
10980 another project (the ``parent'') then by default all source files of the
10981 parent project are inherited by the child, but the child project can
10982 override any of the parent's source files with new versions, and can also
10983 add new files. This facility is the project analog of a type extension in
10984 Object-Oriented Programming. Project hierarchies are permitted (a child
10985 project may be the parent of yet another project), and a project that
10986 inherits one project can also import other projects.
10988 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
10989 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
10990 @file{pack.adb}, and @file{proc.adb}:
11003 Note that the project file can simply be empty (that is, no attribute or
11004 package is defined):
11006 @smallexample @c projectfile
11008 project Seq_Proj is
11014 implying that its source files are all the Ada source files in the project
11017 Suppose we want to supply an alternate version of @file{pack.adb}, in
11018 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
11019 @file{pack.ads} and @file{proc.adb}. We can define a project
11020 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
11024 ^/tasking^[TASKING]^
11030 project Tasking_Proj extends "/seq/seq_proj" is
11036 The version of @file{pack.adb} used in a build depends on which project file
11039 Note that we could have obtained the desired behavior using project import
11040 rather than project inheritance; a @code{base} project would contain the
11041 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
11042 import @code{base} and add @file{pack.adb}, and likewise a tasking project
11043 would import @code{base} and add a different version of @file{pack.adb}. The
11044 choice depends on whether other sources in the original project need to be
11045 overridden. If they do, then project extension is necessary, otherwise,
11046 importing is sufficient.
11049 In a project file that extends another project file, it is possible to
11050 indicate that an inherited source is not part of the sources of the extending
11051 project. This is necessary sometimes when a package spec has been overloaded
11052 and no longer requires a body: in this case, it is necessary to indicate that
11053 the inherited body is not part of the sources of the project, otherwise there
11054 will be a compilation error when compiling the spec.
11056 For that purpose, the attribute @code{Locally_Removed_Files} is used.
11057 Its value is a string list: a list of file names.
11059 @smallexample @c @projectfile
11060 project B extends "a" is
11061 for Source_Files use ("pkg.ads");
11062 -- New spec of Pkg does not need a completion
11063 for Locally_Removed_Files use ("pkg.adb");
11067 Attribute @code{Locally_Removed_Files} may also be used to check if a source
11068 is still needed: if it is possible to build using @code{gnatmake} when such
11069 a source is put in attribute @code{Locally_Removed_Files} of a project P, then
11070 it is possible to remove the source completely from a system that includes
11073 @c ***********************
11074 @c * Project File Syntax *
11075 @c ***********************
11077 @node Project File Syntax
11078 @section Project File Syntax
11087 * Associative Array Attributes::
11088 * case Constructions::
11092 This section describes the structure of project files.
11094 A project may be an @emph{independent project}, entirely defined by a single
11095 project file. Any Ada source file in an independent project depends only
11096 on the predefined library and other Ada source files in the same project.
11099 A project may also @dfn{depend on} other projects, in either or both of
11100 the following ways:
11102 @item It may import any number of projects
11103 @item It may extend at most one other project
11107 The dependence relation is a directed acyclic graph (the subgraph reflecting
11108 the ``extends'' relation is a tree).
11110 A project's @dfn{immediate sources} are the source files directly defined by
11111 that project, either implicitly by residing in the project file's directory,
11112 or explicitly through any of the source-related attributes described below.
11113 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
11114 of @var{proj} together with the immediate sources (unless overridden) of any
11115 project on which @var{proj} depends (either directly or indirectly).
11118 @subsection Basic Syntax
11121 As seen in the earlier examples, project files have an Ada-like syntax.
11122 The minimal project file is:
11123 @smallexample @c projectfile
11132 The identifier @code{Empty} is the name of the project.
11133 This project name must be present after the reserved
11134 word @code{end} at the end of the project file, followed by a semi-colon.
11136 Any name in a project file, such as the project name or a variable name,
11137 has the same syntax as an Ada identifier.
11139 The reserved words of project files are the Ada reserved words plus
11140 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
11141 reserved words currently used in project file syntax are:
11169 Comments in project files have the same syntax as in Ada, two consecutives
11170 hyphens through the end of the line.
11173 @subsection Packages
11176 A project file may contain @emph{packages}. The name of a package must be one
11177 of the identifiers from the following list. A package
11178 with a given name may only appear once in a project file. Package names are
11179 case insensitive. The following package names are legal:
11195 @code{Cross_Reference}
11207 In its simplest form, a package may be empty:
11209 @smallexample @c projectfile
11219 A package may contain @emph{attribute declarations},
11220 @emph{variable declarations} and @emph{case constructions}, as will be
11223 When there is ambiguity between a project name and a package name,
11224 the name always designates the project. To avoid possible confusion, it is
11225 always a good idea to avoid naming a project with one of the
11226 names allowed for packages or any name that starts with @code{gnat}.
11229 @subsection Expressions
11232 An @emph{expression} is either a @emph{string expression} or a
11233 @emph{string list expression}.
11235 A @emph{string expression} is either a @emph{simple string expression} or a
11236 @emph{compound string expression}.
11238 A @emph{simple string expression} is one of the following:
11240 @item A literal string; e.g.@code{"comm/my_proj.gpr"}
11241 @item A string-valued variable reference (see @ref{Variables})
11242 @item A string-valued attribute reference (see @ref{Attributes})
11243 @item An external reference (see @ref{External References in Project Files})
11247 A @emph{compound string expression} is a concatenation of string expressions,
11248 using the operator @code{"&"}
11250 Path & "/" & File_Name & ".ads"
11254 A @emph{string list expression} is either a
11255 @emph{simple string list expression} or a
11256 @emph{compound string list expression}.
11258 A @emph{simple string list expression} is one of the following:
11260 @item A parenthesized list of zero or more string expressions,
11261 separated by commas
11263 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
11266 @item A string list-valued variable reference
11267 @item A string list-valued attribute reference
11271 A @emph{compound string list expression} is the concatenation (using
11272 @code{"&"}) of a simple string list expression and an expression. Note that
11273 each term in a compound string list expression, except the first, may be
11274 either a string expression or a string list expression.
11276 @smallexample @c projectfile
11278 File_Name_List := () & File_Name; -- One string in this list
11279 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
11281 Big_List := File_Name_List & Extended_File_Name_List;
11282 -- Concatenation of two string lists: three strings
11283 Illegal_List := "gnat.adc" & Extended_File_Name_List;
11284 -- Illegal: must start with a string list
11289 @subsection String Types
11292 A @emph{string type declaration} introduces a discrete set of string literals.
11293 If a string variable is declared to have this type, its value
11294 is restricted to the given set of literals.
11296 Here is an example of a string type declaration:
11298 @smallexample @c projectfile
11299 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
11303 Variables of a string type are called @emph{typed variables}; all other
11304 variables are called @emph{untyped variables}. Typed variables are
11305 particularly useful in @code{case} constructions, to support conditional
11306 attribute declarations.
11307 (see @ref{case Constructions}).
11309 The string literals in the list are case sensitive and must all be different.
11310 They may include any graphic characters allowed in Ada, including spaces.
11312 A string type may only be declared at the project level, not inside a package.
11314 A string type may be referenced by its name if it has been declared in the same
11315 project file, or by an expanded name whose prefix is the name of the project
11316 in which it is declared.
11319 @subsection Variables
11322 A variable may be declared at the project file level, or within a package.
11323 Here are some examples of variable declarations:
11325 @smallexample @c projectfile
11327 This_OS : OS := external ("OS"); -- a typed variable declaration
11328 That_OS := "GNU/Linux"; -- an untyped variable declaration
11333 The syntax of a @emph{typed variable declaration} is identical to the Ada
11334 syntax for an object declaration. By contrast, the syntax of an untyped
11335 variable declaration is identical to an Ada assignment statement. In fact,
11336 variable declarations in project files have some of the characteristics of
11337 an assignment, in that successive declarations for the same variable are
11338 allowed. Untyped variable declarations do establish the expected kind of the
11339 variable (string or string list), and successive declarations for it must
11340 respect the initial kind.
11343 A string variable declaration (typed or untyped) declares a variable
11344 whose value is a string. This variable may be used as a string expression.
11345 @smallexample @c projectfile
11346 File_Name := "readme.txt";
11347 Saved_File_Name := File_Name & ".saved";
11351 A string list variable declaration declares a variable whose value is a list
11352 of strings. The list may contain any number (zero or more) of strings.
11354 @smallexample @c projectfile
11356 List_With_One_Element := ("^-gnaty^-gnaty^");
11357 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
11358 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
11359 "pack2.ada", "util_.ada", "util.ada");
11363 The same typed variable may not be declared more than once at project level,
11364 and it may not be declared more than once in any package; it is in effect
11367 The same untyped variable may be declared several times. Declarations are
11368 elaborated in the order in which they appear, so the new value replaces
11369 the old one, and any subsequent reference to the variable uses the new value.
11370 However, as noted above, if a variable has been declared as a string, all
11372 declarations must give it a string value. Similarly, if a variable has
11373 been declared as a string list, all subsequent declarations
11374 must give it a string list value.
11376 A @emph{variable reference} may take several forms:
11379 @item The simple variable name, for a variable in the current package (if any)
11380 or in the current project
11381 @item An expanded name, whose prefix is a context name.
11385 A @emph{context} may be one of the following:
11388 @item The name of an existing package in the current project
11389 @item The name of an imported project of the current project
11390 @item The name of an ancestor project (i.e., a project extended by the current
11391 project, either directly or indirectly)
11392 @item An expanded name whose prefix is an imported/parent project name, and
11393 whose selector is a package name in that project.
11397 A variable reference may be used in an expression.
11400 @subsection Attributes
11403 A project (and its packages) may have @emph{attributes} that define
11404 the project's properties. Some attributes have values that are strings;
11405 others have values that are string lists.
11407 There are two categories of attributes: @emph{simple attributes}
11408 and @emph{associative arrays} (see @ref{Associative Array Attributes}).
11410 Legal project attribute names, and attribute names for each legal package are
11411 listed below. Attributes names are case-insensitive.
11413 The following attributes are defined on projects (all are simple attributes):
11415 @multitable @columnfractions .4 .3
11416 @item @emph{Attribute Name}
11418 @item @code{Source_Files}
11420 @item @code{Source_Dirs}
11422 @item @code{Source_List_File}
11424 @item @code{Object_Dir}
11426 @item @code{Exec_Dir}
11428 @item @code{Locally_Removed_Files}
11432 @item @code{Languages}
11434 @item @code{Main_Language}
11436 @item @code{Library_Dir}
11438 @item @code{Library_Name}
11440 @item @code{Library_Kind}
11442 @item @code{Library_Version}
11444 @item @code{Library_Interface}
11446 @item @code{Library_Auto_Init}
11448 @item @code{Library_Options}
11450 @item @code{Library_GCC}
11455 The following attributes are defined for package @code{Naming}
11456 (see @ref{Naming Schemes}):
11458 @multitable @columnfractions .4 .2 .2 .2
11459 @item Attribute Name @tab Category @tab Index @tab Value
11460 @item @code{Spec_Suffix}
11461 @tab associative array
11464 @item @code{Body_Suffix}
11465 @tab associative array
11468 @item @code{Separate_Suffix}
11469 @tab simple attribute
11472 @item @code{Casing}
11473 @tab simple attribute
11476 @item @code{Dot_Replacement}
11477 @tab simple attribute
11481 @tab associative array
11485 @tab associative array
11488 @item @code{Specification_Exceptions}
11489 @tab associative array
11492 @item @code{Implementation_Exceptions}
11493 @tab associative array
11499 The following attributes are defined for packages @code{Builder},
11500 @code{Compiler}, @code{Binder},
11501 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
11502 (see @ref{^Switches^Switches^ and Project Files}).
11504 @multitable @columnfractions .4 .2 .2 .2
11505 @item Attribute Name @tab Category @tab Index @tab Value
11506 @item @code{^Default_Switches^Default_Switches^}
11507 @tab associative array
11510 @item @code{^Switches^Switches^}
11511 @tab associative array
11517 In addition, package @code{Compiler} has a single string attribute
11518 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
11519 string attribute @code{Global_Configuration_Pragmas}.
11522 Each simple attribute has a default value: the empty string (for string-valued
11523 attributes) and the empty list (for string list-valued attributes).
11525 An attribute declaration defines a new value for an attribute.
11527 Examples of simple attribute declarations:
11529 @smallexample @c projectfile
11530 for Object_Dir use "objects";
11531 for Source_Dirs use ("units", "test/drivers");
11535 The syntax of a @dfn{simple attribute declaration} is similar to that of an
11536 attribute definition clause in Ada.
11538 Attributes references may be appear in expressions.
11539 The general form for such a reference is @code{<entity>'<attribute>}:
11540 Associative array attributes are functions. Associative
11541 array attribute references must have an argument that is a string literal.
11545 @smallexample @c projectfile
11547 Naming'Dot_Replacement
11548 Imported_Project'Source_Dirs
11549 Imported_Project.Naming'Casing
11550 Builder'^Default_Switches^Default_Switches^("Ada")
11554 The prefix of an attribute may be:
11556 @item @code{project} for an attribute of the current project
11557 @item The name of an existing package of the current project
11558 @item The name of an imported project
11559 @item The name of a parent project that is extended by the current project
11560 @item An expanded name whose prefix is imported/parent project name,
11561 and whose selector is a package name
11566 @smallexample @c projectfile
11569 for Source_Dirs use project'Source_Dirs & "units";
11570 for Source_Dirs use project'Source_Dirs & "test/drivers"
11576 In the first attribute declaration, initially the attribute @code{Source_Dirs}
11577 has the default value: an empty string list. After this declaration,
11578 @code{Source_Dirs} is a string list of one element: @code{"units"}.
11579 After the second attribute declaration @code{Source_Dirs} is a string list of
11580 two elements: @code{"units"} and @code{"test/drivers"}.
11582 Note: this example is for illustration only. In practice,
11583 the project file would contain only one attribute declaration:
11585 @smallexample @c projectfile
11586 for Source_Dirs use ("units", "test/drivers");
11589 @node Associative Array Attributes
11590 @subsection Associative Array Attributes
11593 Some attributes are defined as @emph{associative arrays}. An associative
11594 array may be regarded as a function that takes a string as a parameter
11595 and delivers a string or string list value as its result.
11597 Here are some examples of single associative array attribute associations:
11599 @smallexample @c projectfile
11600 for Body ("main") use "Main.ada";
11601 for ^Switches^Switches^ ("main.ada")
11603 "^-gnatv^-gnatv^");
11604 for ^Switches^Switches^ ("main.ada")
11605 use Builder'^Switches^Switches^ ("main.ada")
11610 Like untyped variables and simple attributes, associative array attributes
11611 may be declared several times. Each declaration supplies a new value for the
11612 attribute, and replaces the previous setting.
11615 An associative array attribute may be declared as a full associative array
11616 declaration, with the value of the same attribute in an imported or extended
11619 @smallexample @c projectfile
11621 for Default_Switches use Default.Builder'Default_Switches;
11626 In this example, @code{Default} must be either an project imported by the
11627 current project, or the project that the current project extends. If the
11628 attribute is in a package (in this case, in package @code{Builder}), the same
11629 package needs to be specified.
11632 A full associative array declaration replaces any other declaration for the
11633 attribute, including other full associative array declaration. Single
11634 associative array associations may be declare after a full associative
11635 declaration, modifying the value for a single association of the attribute.
11637 @node case Constructions
11638 @subsection @code{case} Constructions
11641 A @code{case} construction is used in a project file to effect conditional
11643 Here is a typical example:
11645 @smallexample @c projectfile
11648 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
11650 OS : OS_Type := external ("OS", "GNU/Linux");
11654 package Compiler is
11656 when "GNU/Linux" | "Unix" =>
11657 for ^Default_Switches^Default_Switches^ ("Ada")
11658 use ("^-gnath^-gnath^");
11660 for ^Default_Switches^Default_Switches^ ("Ada")
11661 use ("^-gnatP^-gnatP^");
11670 The syntax of a @code{case} construction is based on the Ada case statement
11671 (although there is no @code{null} construction for empty alternatives).
11673 The case expression must a typed string variable.
11674 Each alternative comprises the reserved word @code{when}, either a list of
11675 literal strings separated by the @code{"|"} character or the reserved word
11676 @code{others}, and the @code{"=>"} token.
11677 Each literal string must belong to the string type that is the type of the
11679 An @code{others} alternative, if present, must occur last.
11681 After each @code{=>}, there are zero or more constructions. The only
11682 constructions allowed in a case construction are other case constructions and
11683 attribute declarations. String type declarations, variable declarations and
11684 package declarations are not allowed.
11686 The value of the case variable is often given by an external reference
11687 (see @ref{External References in Project Files}).
11689 @c ****************************************
11690 @c * Objects and Sources in Project Files *
11691 @c ****************************************
11693 @node Objects and Sources in Project Files
11694 @section Objects and Sources in Project Files
11697 * Object Directory::
11699 * Source Directories::
11700 * Source File Names::
11704 Each project has exactly one object directory and one or more source
11705 directories. The source directories must contain at least one source file,
11706 unless the project file explicitly specifies that no source files are present
11707 (see @ref{Source File Names}).
11709 @node Object Directory
11710 @subsection Object Directory
11713 The object directory for a project is the directory containing the compiler's
11714 output (such as @file{ALI} files and object files) for the project's immediate
11717 The object directory is given by the value of the attribute @code{Object_Dir}
11718 in the project file.
11720 @smallexample @c projectfile
11721 for Object_Dir use "objects";
11725 The attribute @var{Object_Dir} has a string value, the path name of the object
11726 directory. The path name may be absolute or relative to the directory of the
11727 project file. This directory must already exist, and be readable and writable.
11729 By default, when the attribute @code{Object_Dir} is not given an explicit value
11730 or when its value is the empty string, the object directory is the same as the
11731 directory containing the project file.
11733 @node Exec Directory
11734 @subsection Exec Directory
11737 The exec directory for a project is the directory containing the executables
11738 for the project's main subprograms.
11740 The exec directory is given by the value of the attribute @code{Exec_Dir}
11741 in the project file.
11743 @smallexample @c projectfile
11744 for Exec_Dir use "executables";
11748 The attribute @var{Exec_Dir} has a string value, the path name of the exec
11749 directory. The path name may be absolute or relative to the directory of the
11750 project file. This directory must already exist, and be writable.
11752 By default, when the attribute @code{Exec_Dir} is not given an explicit value
11753 or when its value is the empty string, the exec directory is the same as the
11754 object directory of the project file.
11756 @node Source Directories
11757 @subsection Source Directories
11760 The source directories of a project are specified by the project file
11761 attribute @code{Source_Dirs}.
11763 This attribute's value is a string list. If the attribute is not given an
11764 explicit value, then there is only one source directory, the one where the
11765 project file resides.
11767 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
11770 @smallexample @c projectfile
11771 for Source_Dirs use ();
11775 indicates that the project contains no source files.
11777 Otherwise, each string in the string list designates one or more
11778 source directories.
11780 @smallexample @c projectfile
11781 for Source_Dirs use ("sources", "test/drivers");
11785 If a string in the list ends with @code{"/**"}, then the directory whose path
11786 name precedes the two asterisks, as well as all its subdirectories
11787 (recursively), are source directories.
11789 @smallexample @c projectfile
11790 for Source_Dirs use ("/system/sources/**");
11794 Here the directory @code{/system/sources} and all of its subdirectories
11795 (recursively) are source directories.
11797 To specify that the source directories are the directory of the project file
11798 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
11799 @smallexample @c projectfile
11800 for Source_Dirs use ("./**");
11804 Each of the source directories must exist and be readable.
11806 @node Source File Names
11807 @subsection Source File Names
11810 In a project that contains source files, their names may be specified by the
11811 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
11812 (a string). Source file names never include any directory information.
11814 If the attribute @code{Source_Files} is given an explicit value, then each
11815 element of the list is a source file name.
11817 @smallexample @c projectfile
11818 for Source_Files use ("main.adb");
11819 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
11823 If the attribute @code{Source_Files} is not given an explicit value,
11824 but the attribute @code{Source_List_File} is given a string value,
11825 then the source file names are contained in the text file whose path name
11826 (absolute or relative to the directory of the project file) is the
11827 value of the attribute @code{Source_List_File}.
11829 Each line in the file that is not empty or is not a comment
11830 contains a source file name.
11832 @smallexample @c projectfile
11833 for Source_List_File use "source_list.txt";
11837 By default, if neither the attribute @code{Source_Files} nor the attribute
11838 @code{Source_List_File} is given an explicit value, then each file in the
11839 source directories that conforms to the project's naming scheme
11840 (see @ref{Naming Schemes}) is an immediate source of the project.
11842 A warning is issued if both attributes @code{Source_Files} and
11843 @code{Source_List_File} are given explicit values. In this case, the attribute
11844 @code{Source_Files} prevails.
11846 Each source file name must be the name of one existing source file
11847 in one of the source directories.
11849 A @code{Source_Files} attribute whose value is an empty list
11850 indicates that there are no source files in the project.
11852 If the order of the source directories is known statically, that is if
11853 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
11854 be several files with the same source file name. In this case, only the file
11855 in the first directory is considered as an immediate source of the project
11856 file. If the order of the source directories is not known statically, it is
11857 an error to have several files with the same source file name.
11859 Projects can be specified to have no Ada source
11860 files: the value of (@code{Source_Dirs} or @code{Source_Files} may be an empty
11861 list, or the @code{"Ada"} may be absent from @code{Languages}:
11863 @smallexample @c projectfile
11864 for Source_Dirs use ();
11865 for Source_Files use ();
11866 for Languages use ("C", "C++");
11870 Otherwise, a project must contain at least one immediate source.
11872 Projects with no source files are useful as template packages
11873 (see @ref{Packages in Project Files}) for other projects; in particular to
11874 define a package @code{Naming} (see @ref{Naming Schemes}).
11876 @c ****************************
11877 @c * Importing Projects *
11878 @c ****************************
11880 @node Importing Projects
11881 @section Importing Projects
11884 An immediate source of a project P may depend on source files that
11885 are neither immediate sources of P nor in the predefined library.
11886 To get this effect, P must @emph{import} the projects that contain the needed
11889 @smallexample @c projectfile
11891 with "project1", "utilities.gpr";
11892 with "/namings/apex.gpr";
11899 As can be seen in this example, the syntax for importing projects is similar
11900 to the syntax for importing compilation units in Ada. However, project files
11901 use literal strings instead of names, and the @code{with} clause identifies
11902 project files rather than packages.
11904 Each literal string is the file name or path name (absolute or relative) of a
11905 project file. If a string is simply a file name, with no path, then its
11906 location is determined by the @emph{project path}:
11910 If the ^environment variable^logical name^ @env{ADA_PROJECT_PATH} exists,
11911 then the project path includes all the directories in this
11912 ^environment variable^logical name^, plus the directory of the project file.
11915 If the ^environment variable^logical name^ @env{ADA_PROJECT_PATH} does not
11916 exist, then the project path contains only one directory, namely the one where
11917 the project file is located.
11921 If a relative pathname is used, as in
11923 @smallexample @c projectfile
11928 then the path is relative to the directory where the importing project file is
11929 located. Any symbolic link will be fully resolved in the directory
11930 of the importing project file before the imported project file is examined.
11932 If the @code{with}'ed project file name does not have an extension,
11933 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
11934 then the file name as specified in the @code{with} clause (no extension) will
11935 be used. In the above example, if a file @code{project1.gpr} is found, then it
11936 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
11937 then it will be used; if neither file exists, this is an error.
11939 A warning is issued if the name of the project file does not match the
11940 name of the project; this check is case insensitive.
11942 Any source file that is an immediate source of the imported project can be
11943 used by the immediate sources of the importing project, transitively. Thus
11944 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
11945 sources of @code{A} may depend on the immediate sources of @code{C}, even if
11946 @code{A} does not import @code{C} explicitly. However, this is not recommended,
11947 because if and when @code{B} ceases to import @code{C}, some sources in
11948 @code{A} will no longer compile.
11950 A side effect of this capability is that normally cyclic dependencies are not
11951 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
11952 is not allowed to import @code{A}. However, there are cases when cyclic
11953 dependencies would be beneficial. For these cases, another form of import
11954 between projects exists, the @code{limited with}: a project @code{A} that
11955 imports a project @code{B} with a straigh @code{with} may also be imported,
11956 directly or indirectly, by @code{B} on the condition that imports from @code{B}
11957 to @code{A} include at least one @code{limited with}.
11959 @smallexample @c 0projectfile
11965 limited with "../a/a.gpr";
11973 limited with "../a/a.gpr";
11979 In the above legal example, there are two project cycles:
11982 @item A -> C -> D -> A
11986 In each of these cycle there is one @code{limited with}: import of @code{A}
11987 from @code{B} and import of @code{A} from @code{D}.
11989 The difference between straight @code{with} and @code{limited with} is that
11990 the name of a project imported with a @code{limited with} cannot be used in the
11991 project that imports it. In particular, its packages cannot be renamed and
11992 its variables cannot be referred to.
11994 An exception to the above rules for @code{limited with} is that for the main
11995 project specified to @command{gnatmake} or to the @command{GNAT} driver a
11996 @code{limited with} is equivalent to a straight @code{with}. For example,
11997 in the example above, projects @code{B} and @code{D} could not be main
11998 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
11999 each have a @code{limited with} that is the only one in a cycle of importing
12002 @c *********************
12003 @c * Project Extension *
12004 @c *********************
12006 @node Project Extension
12007 @section Project Extension
12010 During development of a large system, it is sometimes necessary to use
12011 modified versions of some of the source files, without changing the original
12012 sources. This can be achieved through the @emph{project extension} facility.
12014 @smallexample @c projectfile
12015 project Modified_Utilities extends "/baseline/utilities.gpr" is ...
12019 A project extension declaration introduces an extending project
12020 (the @emph{child}) and a project being extended (the @emph{parent}).
12022 By default, a child project inherits all the sources of its parent.
12023 However, inherited sources can be overridden: a unit in a parent is hidden
12024 by a unit of the same name in the child.
12026 Inherited sources are considered to be sources (but not immediate sources)
12027 of the child project; see @ref{Project File Syntax}.
12029 An inherited source file retains any switches specified in the parent project.
12031 For example if the project @code{Utilities} contains the specification and the
12032 body of an Ada package @code{Util_IO}, then the project
12033 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
12034 The original body of @code{Util_IO} will not be considered in program builds.
12035 However, the package specification will still be found in the project
12038 A child project can have only one parent but it may import any number of other
12041 A project is not allowed to import directly or indirectly at the same time a
12042 child project and any of its ancestors.
12044 @c ****************************************
12045 @c * External References in Project Files *
12046 @c ****************************************
12048 @node External References in Project Files
12049 @section External References in Project Files
12052 A project file may contain references to external variables; such references
12053 are called @emph{external references}.
12055 An external variable is either defined as part of the environment (an
12056 environment variable in Unix, for example) or else specified on the command
12057 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
12058 If both, then the command line value is used.
12060 The value of an external reference is obtained by means of the built-in
12061 function @code{external}, which returns a string value.
12062 This function has two forms:
12064 @item @code{external (external_variable_name)}
12065 @item @code{external (external_variable_name, default_value)}
12069 Each parameter must be a string literal. For example:
12071 @smallexample @c projectfile
12073 external ("OS", "GNU/Linux")
12077 In the form with one parameter, the function returns the value of
12078 the external variable given as parameter. If this name is not present in the
12079 environment, the function returns an empty string.
12081 In the form with two string parameters, the second argument is
12082 the value returned when the variable given as the first argument is not
12083 present in the environment. In the example above, if @code{"OS"} is not
12084 the name of ^an environment variable^a logical name^ and is not passed on
12085 the command line, then the returned value is @code{"GNU/Linux"}.
12087 An external reference may be part of a string expression or of a string
12088 list expression, and can therefore appear in a variable declaration or
12089 an attribute declaration.
12091 @smallexample @c projectfile
12093 type Mode_Type is ("Debug", "Release");
12094 Mode : Mode_Type := external ("MODE");
12101 @c *****************************
12102 @c * Packages in Project Files *
12103 @c *****************************
12105 @node Packages in Project Files
12106 @section Packages in Project Files
12109 A @emph{package} defines the settings for project-aware tools within a
12111 For each such tool one can declare a package; the names for these
12112 packages are preset (see @ref{Packages}).
12113 A package may contain variable declarations, attribute declarations, and case
12116 @smallexample @c projectfile
12119 package Builder is -- used by gnatmake
12120 for ^Default_Switches^Default_Switches^ ("Ada")
12129 The syntax of package declarations mimics that of package in Ada.
12131 Most of the packages have an attribute
12132 @code{^Default_Switches^Default_Switches^}.
12133 This attribute is an associative array, and its value is a string list.
12134 The index of the associative array is the name of a programming language (case
12135 insensitive). This attribute indicates the ^switch^switch^
12136 or ^switches^switches^ to be used
12137 with the corresponding tool.
12139 Some packages also have another attribute, @code{^Switches^Switches^},
12140 an associative array whose value is a string list.
12141 The index is the name of a source file.
12142 This attribute indicates the ^switch^switch^
12143 or ^switches^switches^ to be used by the corresponding
12144 tool when dealing with this specific file.
12146 Further information on these ^switch^switch^-related attributes is found in
12147 @ref{^Switches^Switches^ and Project Files}.
12149 A package may be declared as a @emph{renaming} of another package; e.g., from
12150 the project file for an imported project.
12152 @smallexample @c projectfile
12154 with "/global/apex.gpr";
12156 package Naming renames Apex.Naming;
12163 Packages that are renamed in other project files often come from project files
12164 that have no sources: they are just used as templates. Any modification in the
12165 template will be reflected automatically in all the project files that rename
12166 a package from the template.
12168 In addition to the tool-oriented packages, you can also declare a package
12169 named @code{Naming} to establish specialized source file naming conventions
12170 (see @ref{Naming Schemes}).
12172 @c ************************************
12173 @c * Variables from Imported Projects *
12174 @c ************************************
12176 @node Variables from Imported Projects
12177 @section Variables from Imported Projects
12180 An attribute or variable defined in an imported or parent project can
12181 be used in expressions in the importing / extending project.
12182 Such an attribute or variable is denoted by an expanded name whose prefix
12183 is either the name of the project or the expanded name of a package within
12186 @smallexample @c projectfile
12189 project Main extends "base" is
12190 Var1 := Imported.Var;
12191 Var2 := Base.Var & ".new";
12196 for ^Default_Switches^Default_Switches^ ("Ada")
12197 use Imported.Builder.Ada_^Switches^Switches^ &
12198 "^-gnatg^-gnatg^" &
12204 package Compiler is
12205 for ^Default_Switches^Default_Switches^ ("Ada")
12206 use Base.Compiler.Ada_^Switches^Switches^;
12217 The value of @code{Var1} is a copy of the variable @code{Var} defined
12218 in the project file @file{"imported.gpr"}
12220 the value of @code{Var2} is a copy of the value of variable @code{Var}
12221 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
12223 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12224 @code{Builder} is a string list that includes in its value a copy of the value
12225 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
12226 in project file @file{imported.gpr} plus two new elements:
12227 @option{"^-gnatg^-gnatg^"}
12228 and @option{"^-v^-v^"};
12230 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
12231 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
12232 defined in the @code{Compiler} package in project file @file{base.gpr},
12233 the project being extended.
12236 @c ******************
12237 @c * Naming Schemes *
12238 @c ******************
12240 @node Naming Schemes
12241 @section Naming Schemes
12244 Sometimes an Ada software system is ported from a foreign compilation
12245 environment to GNAT, and the file names do not use the default GNAT
12246 conventions. Instead of changing all the file names (which for a variety
12247 of reasons might not be possible), you can define the relevant file
12248 naming scheme in the @code{Naming} package in your project file.
12251 Note that the use of pragmas described in @ref{Alternative
12252 File Naming Schemes} by mean of a configuration pragmas file is not
12253 supported when using project files. You must use the features described
12254 in this paragraph. You can however use specify other configuration
12255 pragmas (see @ref{Specifying Configuration Pragmas}).
12258 For example, the following
12259 package models the Apex file naming rules:
12261 @smallexample @c projectfile
12264 for Casing use "lowercase";
12265 for Dot_Replacement use ".";
12266 for Spec_Suffix ("Ada") use ".1.ada";
12267 for Body_Suffix ("Ada") use ".2.ada";
12274 For example, the following package models the DEC Ada file naming rules:
12276 @smallexample @c projectfile
12279 for Casing use "lowercase";
12280 for Dot_Replacement use "__";
12281 for Spec_Suffix ("Ada") use "_.^ada^ada^";
12282 for Body_Suffix ("Ada") use ".^ada^ada^";
12288 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
12289 names in lower case)
12293 You can define the following attributes in package @code{Naming}:
12298 This must be a string with one of the three values @code{"lowercase"},
12299 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
12302 If @var{Casing} is not specified, then the default is @code{"lowercase"}.
12304 @item @var{Dot_Replacement}
12305 This must be a string whose value satisfies the following conditions:
12308 @item It must not be empty
12309 @item It cannot start or end with an alphanumeric character
12310 @item It cannot be a single underscore
12311 @item It cannot start with an underscore followed by an alphanumeric
12312 @item It cannot contain a dot @code{'.'} except if the entire string
12317 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
12319 @item @var{Spec_Suffix}
12320 This is an associative array (indexed by the programming language name, case
12321 insensitive) whose value is a string that must satisfy the following
12325 @item It must not be empty
12326 @item It must include at least one dot
12329 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
12330 @code{"^.ads^.ADS^"}.
12332 @item @var{Body_Suffix}
12333 This is an associative array (indexed by the programming language name, case
12334 insensitive) whose value is a string that must satisfy the following
12338 @item It must not be empty
12339 @item It must include at least one dot
12340 @item It cannot end with the same string as @code{Spec_Suffix ("Ada")}
12343 If @code{Body_Suffix ("Ada")} is not specified, then the default is
12344 @code{"^.adb^.ADB^"}.
12346 @item @var{Separate_Suffix}
12347 This must be a string whose value satisfies the same conditions as
12348 @code{Body_Suffix}.
12351 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
12352 value as @code{Body_Suffix ("Ada")}.
12356 You can use the associative array attribute @code{Spec} to define
12357 the source file name for an individual Ada compilation unit's spec. The array
12358 index must be a string literal that identifies the Ada unit (case insensitive).
12359 The value of this attribute must be a string that identifies the file that
12360 contains this unit's spec (case sensitive or insensitive depending on the
12363 @smallexample @c projectfile
12364 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
12369 You can use the associative array attribute @code{Body} to
12370 define the source file name for an individual Ada compilation unit's body
12371 (possibly a subunit). The array index must be a string literal that identifies
12372 the Ada unit (case insensitive). The value of this attribute must be a string
12373 that identifies the file that contains this unit's body or subunit (case
12374 sensitive or insensitive depending on the operating system).
12376 @smallexample @c projectfile
12377 for Body ("MyPack.MyChild") use "mypack.mychild.body";
12381 @c ********************
12382 @c * Library Projects *
12383 @c ********************
12385 @node Library Projects
12386 @section Library Projects
12389 @emph{Library projects} are projects whose object code is placed in a library.
12390 (Note that this facility is not yet supported on all platforms)
12392 To create a library project, you need to define in its project file
12393 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
12394 Additionally, you may define the library-related attributes
12395 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
12396 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
12398 The @code{Library_Name} attribute has a string value. There is no restriction
12399 on the name of a library. It is the responsability of the developer to
12400 choose a name that will be accepted by the platform. It is recommanded to
12401 choose names that could be Ada identifiers; such names are almost guaranteed
12402 to be acceptable on all platforms.
12404 The @code{Library_Dir} attribute has a string value that designates the path
12405 (absolute or relative) of the directory where the library will reside.
12406 It must designate an existing directory, and this directory must be
12407 different from the project's object directory. It also needs to be writable.
12409 If both @code{Library_Name} and @code{Library_Dir} are specified and
12410 are legal, then the project file defines a library project. The optional
12411 library-related attributes are checked only for such project files.
12413 The @code{Library_Kind} attribute has a string value that must be one of the
12414 following (case insensitive): @code{"static"}, @code{"dynamic"} or
12415 @code{"relocatable"}. If this attribute is not specified, the library is a
12416 static library, that is an archive of object files that can be potentially
12417 linked into an static executable. Otherwise, the library may be dynamic or
12418 relocatable, that is a library that is loaded only at the start of execution.
12419 Depending on the operating system, there may or may not be a distinction
12420 between dynamic and relocatable libraries. For Unix and VMS Unix there is no
12423 If you need to build both a static and a dynamic library, you should use two
12424 different object directories, since in some cases some extra code needs to
12425 be generated for the latter. For such cases, it is recommended to either use
12426 two different project files, or a single one which uses external variables
12427 to indicate what kind of library should be build.
12429 The @code{Library_Version} attribute has a string value whose interpretation
12430 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
12431 used only for dynamic/relocatable libraries as the internal name of the
12432 library (the @code{"soname"}). If the library file name (built from the
12433 @code{Library_Name}) is different from the @code{Library_Version}, then the
12434 library file will be a symbolic link to the actual file whose name will be
12435 @code{Library_Version}.
12439 @smallexample @c projectfile
12445 for Library_Dir use "lib_dir";
12446 for Library_Name use "dummy";
12447 for Library_Kind use "relocatable";
12448 for Library_Version use "libdummy.so." & Version;
12455 Directory @file{lib_dir} will contain the internal library file whose name
12456 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
12457 @file{libdummy.so.1}.
12459 When @command{gnatmake} detects that a project file
12460 is a library project file, it will check all immediate sources of the project
12461 and rebuild the library if any of the sources have been recompiled.
12463 Standard project files can import library project files. In such cases,
12464 the libraries will only be rebuild if some of its sources are recompiled
12465 because they are in the closure of some other source in an importing project.
12466 Sources of the library project files that are not in such a closure will
12467 not be checked, unless the full library is checked, because one of its sources
12468 needs to be recompiled.
12470 For instance, assume the project file @code{A} imports the library project file
12471 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
12472 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
12473 @file{l2.ads}, @file{l2.adb}.
12475 If @file{l1.adb} has been modified, then the library associated with @code{L}
12476 will be rebuild when compiling all the immediate sources of @code{A} only
12477 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
12480 To be sure that all the sources in the library associated with @code{L} are
12481 up to date, and that all the sources of parject @code{A} are also up to date,
12482 the following two commands needs to be used:
12489 When a library is built or rebuilt, an attempt is made first to delete all
12490 files in the library directory.
12491 All @file{ALI} files will also be copied from the object directory to the
12492 library directory. To build executables, @command{gnatmake} will use the
12493 library rather than the individual object files.
12496 @c **********************************************
12497 @c * Using Third-Party Libraries through Projects
12498 @c **********************************************
12499 @node Using Third-Party Libraries through Projects
12500 @section Using Third-Party Libraries through Projects
12502 Whether you are exporting your own library to make it available to
12503 clients, or you are using a library provided by a third party, it is
12504 convenient to have project files that automatically set the correct
12505 command line switches for the compiler and linker.
12507 Such project files are very similar to the library project files;
12508 @xref{Library Projects}. The only difference is that you set the
12509 @code{Source_Dirs} and @code{Object_Dir} attribute so that they point to the
12510 directories where, respectively, the sources and the read-only ALI files have
12513 If you need to interface with a set of libraries, as opposed to a
12514 single one, you need to create one library project for each of the
12515 libraries. In addition, a top-level project that imports all these
12516 library projects should be provided, so that the user of your library
12517 has a single @code{with} clause to add to his own projects.
12519 For instance, let's assume you are providing two static libraries
12520 @file{liba.a} and @file{libb.a}. The user needs to link with
12521 both of these libraries. Each of these is associated with its
12522 own set of header files. Let's assume furthermore that all the
12523 header files for the two libraries have been installed in the same
12524 directory @file{headers}. The @file{ALI} files are found in the same
12525 @file{headers} directory.
12527 In this case, you should provide the following three projects:
12529 @smallexample @c projectfile
12531 with "liba", "libb";
12532 project My_Library is
12533 for Source_Dirs use ("headers");
12534 for Object_Dir use "headers";
12540 for Source_Dirs use ();
12541 for Library_Dir use "lib";
12542 for Library_Name use "a";
12543 for Library_Kind use "static";
12549 for Source_Dirs use ();
12550 for Library_Dir use "lib";
12551 for Library_Name use "b";
12552 for Library_Kind use "static";
12557 @c *******************************
12558 @c * Stand-alone Library Projects *
12559 @c *******************************
12561 @node Stand-alone Library Projects
12562 @section Stand-alone Library Projects
12565 A Stand-alone Library is a library that contains the necessary code to
12566 elaborate the Ada units that are included in the library. A Stand-alone
12567 Library is suitable to be used in an executable when the main is not
12568 in Ada. However, Stand-alone Libraries may also be used with an Ada main
12571 A Stand-alone Library Project is a Library Project where the library is
12572 a Stand-alone Library.
12574 To be a Stand-alone Library Project, in addition to the two attributes
12575 that make a project a Library Project (@code{Library_Name} and
12576 @code{Library_Dir}, see @ref{Library Projects}), the attribute
12577 @code{Library_Interface} must be defined.
12579 @smallexample @c projectfile
12581 for Library_Dir use "lib_dir";
12582 for Library_Name use "dummy";
12583 for Library_Interface use ("int1", "int1.child");
12587 Attribute @code{Library_Interface} has a non empty string list value,
12588 each string in the list designating a unit contained in an immediate source
12589 of the project file.
12591 When a Stand-alone Library is built, first the binder is invoked to build
12592 a package whose name depends on the library name
12593 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
12594 This binder-generated package includes initialization and
12595 finalization procedures whose
12596 names depend on the library name (dummyinit and dummyfinal in the example
12597 above). The object corresponding to this package is included in the library.
12599 A dynamic or relocatable Stand-alone Library is automatically initialized
12600 if automatic initialization of Stand-alone Libraries is supported on the
12601 platform and if attribute @code{Library_Auto_Init} is not specified or
12602 is specified with the value "true". A static Stand-alone Library is never
12603 automatically initialized.
12605 Single string attribute @code{Library_Auto_Init} may be specified with only
12606 two possible values: "false" or "true" (case-insensitive). Specifying
12607 "false" for attribute @code{Library_Auto_Init} will prevent automatic
12608 initialization of dynamic or relocatable libraries.
12610 When a non automatically initialized Stand-alone Library is used
12611 in an executable, its initialization procedure must be called before
12612 any service of the library is used.
12613 When the main subprogram is in Ada, it may mean that the initialization
12614 procedure has to be called during elaboration of another package.
12616 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
12617 (those that are listed in attribute @code{Library_Interface}) are copied to
12618 the Library Directory. As a consequence, only the Interface Units may be
12619 imported from Ada units outside of the library. If other units are imported,
12620 the binding phase will fail.
12622 When a Stand-Alone Library is bound, the switches that are specified in
12623 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
12624 used in the call to @command{gnatbind}.
12626 The string list attribute @code{Library_Options} may be used to specified
12627 additional switches to the call to @command{gcc} to link the library.
12629 The attribute @code{Library_Src_Dir}, may be specified for a
12630 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
12631 single string value. Its value must be the path (absolute or relative to the
12632 project directory) of an existing directory. This directory cannot be the
12633 object directory or one of the source directories, but it can be the same as
12634 the library directory. The sources of the Interface
12635 Units of the library, necessary to an Ada client of the library, will be
12636 copied to the designated directory, called Interface Copy directory.
12637 These sources includes the specs of the Interface Units, but they may also
12638 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
12639 are used, or when there is a generic units in the spec. Before the sources
12640 are copied to the Interface Copy directory, an attempt is made to delete all
12641 files in the Interface Copy directory.
12643 @c *************************************
12644 @c * Switches Related to Project Files *
12645 @c *************************************
12646 @node Switches Related to Project Files
12647 @section Switches Related to Project Files
12650 The following switches are used by GNAT tools that support project files:
12654 @item ^-P^/PROJECT_FILE=^@var{project}
12655 @cindex @option{^-P^/PROJECT_FILE^} (any tool supporting project files)
12656 Indicates the name of a project file. This project file will be parsed with
12657 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
12658 if any, and using the external references indicated
12659 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
12661 There may zero, one or more spaces between @option{-P} and @var{project}.
12665 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
12668 Since the Project Manager parses the project file only after all the switches
12669 on the command line are checked, the order of the switches
12670 @option{^-P^/PROJECT_FILE^},
12671 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
12672 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
12674 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
12675 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any tool supporting project files)
12676 Indicates that external variable @var{name} has the value @var{value}.
12677 The Project Manager will use this value for occurrences of
12678 @code{external(name)} when parsing the project file.
12682 If @var{name} or @var{value} includes a space, then @var{name=value} should be
12683 put between quotes.
12691 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
12692 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
12693 @var{name}, only the last one is used.
12696 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
12697 takes precedence over the value of the same name in the environment.
12699 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
12700 @cindex @code{^-vP^/MESSAGES_PROJECT_FILE^} (any tool supporting project files)
12701 @c Previous line uses code vs option command, to stay less than 80 chars
12702 Indicates the verbosity of the parsing of GNAT project files.
12705 @option{-vP0} means Default;
12706 @option{-vP1} means Medium;
12707 @option{-vP2} means High.
12711 There are three possible options for this qualifier: DEFAULT, MEDIUM and
12716 The default is ^Default^DEFAULT^: no output for syntactically correct
12719 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
12720 only the last one is used.
12724 @c **********************************
12725 @c * Tools Supporting Project Files *
12726 @c **********************************
12728 @node Tools Supporting Project Files
12729 @section Tools Supporting Project Files
12732 * gnatmake and Project Files::
12733 * The GNAT Driver and Project Files::
12735 * Glide and Project Files::
12739 @node gnatmake and Project Files
12740 @subsection gnatmake and Project Files
12743 This section covers several topics related to @command{gnatmake} and
12744 project files: defining ^switches^switches^ for @command{gnatmake}
12745 and for the tools that it invokes; specifying configuration pragmas;
12746 the use of the @code{Main} attribute; building and rebuilding library project
12750 * ^Switches^Switches^ and Project Files::
12751 * Specifying Configuration Pragmas::
12752 * Project Files and Main Subprograms::
12753 * Library Project Files::
12756 @node ^Switches^Switches^ and Project Files
12757 @subsubsection ^Switches^Switches^ and Project Files
12760 It is not currently possible to specify VMS style qualifiers in the project
12761 files; only Unix style ^switches^switches^ may be specified.
12765 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
12766 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
12767 attribute, a @code{^Switches^Switches^} attribute, or both;
12768 as their names imply, these ^switch^switch^-related
12769 attributes affect the ^switches^switches^ that are used for each of these GNAT
12771 @command{gnatmake} is invoked. As will be explained below, these
12772 component-specific ^switches^switches^ precede
12773 the ^switches^switches^ provided on the @command{gnatmake} command line.
12775 The @code{^Default_Switches^Default_Switches^} attribute is an associative
12776 array indexed by language name (case insensitive) whose value is a string list.
12779 @smallexample @c projectfile
12781 package Compiler is
12782 for ^Default_Switches^Default_Switches^ ("Ada")
12783 use ("^-gnaty^-gnaty^",
12790 The @code{^Switches^Switches^} attribute is also an associative array,
12791 indexed by a file name (which may or may not be case sensitive, depending
12792 on the operating system) whose value is a string list. For example:
12794 @smallexample @c projectfile
12797 for ^Switches^Switches^ ("main1.adb")
12799 for ^Switches^Switches^ ("main2.adb")
12806 For the @code{Builder} package, the file names must designate source files
12807 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
12808 file names must designate @file{ALI} or source files for main subprograms.
12809 In each case just the file name without an explicit extension is acceptable.
12811 For each tool used in a program build (@command{gnatmake}, the compiler, the
12812 binder, and the linker), the corresponding package @dfn{contributes} a set of
12813 ^switches^switches^ for each file on which the tool is invoked, based on the
12814 ^switch^switch^-related attributes defined in the package.
12815 In particular, the ^switches^switches^
12816 that each of these packages contributes for a given file @var{f} comprise:
12820 the value of attribute @code{^Switches^Switches^ (@var{f})},
12821 if it is specified in the package for the given file,
12823 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
12824 if it is specified in the package.
12828 If neither of these attributes is defined in the package, then the package does
12829 not contribute any ^switches^switches^ for the given file.
12831 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
12832 two sets, in the following order: those contributed for the file
12833 by the @code{Builder} package;
12834 and the switches passed on the command line.
12836 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
12837 the ^switches^switches^ passed to the tool comprise three sets,
12838 in the following order:
12842 the applicable ^switches^switches^ contributed for the file
12843 by the @code{Builder} package in the project file supplied on the command line;
12846 those contributed for the file by the package (in the relevant project file --
12847 see below) corresponding to the tool; and
12850 the applicable switches passed on the command line.
12854 The term @emph{applicable ^switches^switches^} reflects the fact that
12855 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
12856 tools, depending on the individual ^switch^switch^.
12858 @command{gnatmake} may invoke the compiler on source files from different
12859 projects. The Project Manager will use the appropriate project file to
12860 determine the @code{Compiler} package for each source file being compiled.
12861 Likewise for the @code{Binder} and @code{Linker} packages.
12863 As an example, consider the following package in a project file:
12865 @smallexample @c projectfile
12868 package Compiler is
12869 for ^Default_Switches^Default_Switches^ ("Ada")
12871 for ^Switches^Switches^ ("a.adb")
12873 for ^Switches^Switches^ ("b.adb")
12875 "^-gnaty^-gnaty^");
12882 If @command{gnatmake} is invoked with this project file, and it needs to
12883 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
12884 @file{a.adb} will be compiled with the ^switch^switch^
12885 @option{^-O1^-O1^},
12886 @file{b.adb} with ^switches^switches^
12888 and @option{^-gnaty^-gnaty^},
12889 and @file{c.adb} with @option{^-g^-g^}.
12891 The following example illustrates the ordering of the ^switches^switches^
12892 contributed by different packages:
12894 @smallexample @c projectfile
12898 for ^Switches^Switches^ ("main.adb")
12906 package Compiler is
12907 for ^Switches^Switches^ ("main.adb")
12915 If you issue the command:
12918 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
12922 then the compiler will be invoked on @file{main.adb} with the following
12923 sequence of ^switches^switches^
12926 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
12929 with the last @option{^-O^-O^}
12930 ^switch^switch^ having precedence over the earlier ones;
12931 several other ^switches^switches^
12932 (such as @option{^-c^-c^}) are added implicitly.
12934 The ^switches^switches^
12936 and @option{^-O1^-O1^} are contributed by package
12937 @code{Builder}, @option{^-O2^-O2^} is contributed
12938 by the package @code{Compiler}
12939 and @option{^-O0^-O0^} comes from the command line.
12941 The @option{^-g^-g^}
12942 ^switch^switch^ will also be passed in the invocation of
12943 @command{Gnatlink.}
12945 A final example illustrates switch contributions from packages in different
12948 @smallexample @c projectfile
12951 for Source_Files use ("pack.ads", "pack.adb");
12952 package Compiler is
12953 for ^Default_Switches^Default_Switches^ ("Ada")
12954 use ("^-gnata^-gnata^");
12962 for Source_Files use ("foo_main.adb", "bar_main.adb");
12964 for ^Switches^Switches^ ("foo_main.adb")
12972 -- Ada source file:
12974 procedure Foo_Main is
12982 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
12986 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
12987 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
12988 @option{^-gnato^-gnato^} (passed on the command line).
12989 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
12990 are @option{^-g^-g^} from @code{Proj4.Builder},
12991 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
12992 and @option{^-gnato^-gnato^} from the command line.
12995 When using @command{gnatmake} with project files, some ^switches^switches^ or
12996 arguments may be expressed as relative paths. As the working directory where
12997 compilation occurs may change, these relative paths are converted to absolute
12998 paths. For the ^switches^switches^ found in a project file, the relative paths
12999 are relative to the project file directory, for the switches on the command
13000 line, they are relative to the directory where @command{gnatmake} is invoked.
13001 The ^switches^switches^ for which this occurs are:
13007 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
13009 ^-o^-o^, object files specified in package @code{Linker} or after
13010 -largs on the command line). The exception to this rule is the ^switch^switch^
13011 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
13013 @node Specifying Configuration Pragmas
13014 @subsubsection Specifying Configuration Pragmas
13016 When using @command{gnatmake} with project files, if there exists a file
13017 @file{gnat.adc} that contains configuration pragmas, this file will be
13020 Configuration pragmas can be defined by means of the following attributes in
13021 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
13022 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
13024 Both these attributes are single string attributes. Their values is the path
13025 name of a file containing configuration pragmas. If a path name is relative,
13026 then it is relative to the project directory of the project file where the
13027 attribute is defined.
13029 When compiling a source, the configuration pragmas used are, in order,
13030 those listed in the file designated by attribute
13031 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
13032 project file, if it is specified, and those listed in the file designated by
13033 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
13034 the project file of the source, if it exists.
13036 @node Project Files and Main Subprograms
13037 @subsubsection Project Files and Main Subprograms
13040 When using a project file, you can invoke @command{gnatmake}
13041 with one or several main subprograms, by specifying their source files on the
13045 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
13049 Each of these needs to be a source file of the same project, except
13050 when the switch ^-u^/UNIQUE^ is used.
13053 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
13054 same project, one of the project in the tree rooted at the project specified
13055 on the command line. The package @code{Builder} of this common project, the
13056 "main project" is the one that is considered by @command{gnatmake}.
13059 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
13060 imported directly or indirectly by the project specified on the command line.
13061 Note that if such a source file is not part of the project specified on the
13062 command line, the ^switches^switches^ found in package @code{Builder} of the
13063 project specified on the command line, if any, that are transmitted
13064 to the compiler will still be used, not those found in the project file of
13068 When using a project file, you can also invoke @command{gnatmake} without
13069 explicitly specifying any main, and the effect depends on whether you have
13070 defined the @code{Main} attribute. This attribute has a string list value,
13071 where each element in the list is the name of a source file (the file
13072 extension is optional) that contains a unit that can be a main subprogram.
13074 If the @code{Main} attribute is defined in a project file as a non-empty
13075 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
13076 line, then invoking @command{gnatmake} with this project file but without any
13077 main on the command line is equivalent to invoking @command{gnatmake} with all
13078 the file names in the @code{Main} attribute on the command line.
13081 @smallexample @c projectfile
13084 for Main use ("main1", "main2", "main3");
13090 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
13092 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
13094 When the project attribute @code{Main} is not specified, or is specified
13095 as an empty string list, or when the switch @option{-u} is used on the command
13096 line, then invoking @command{gnatmake} with no main on the command line will
13097 result in all immediate sources of the project file being checked, and
13098 potentially recompiled. Depending on the presence of the switch @option{-u},
13099 sources from other project files on which the immediate sources of the main
13100 project file depend are also checked and potentially recompiled. In other
13101 words, the @option{-u} switch is applied to all of the immediate sources of the
13104 When no main is specified on the command line and attribute @code{Main} exists
13105 and includes several mains, or when several mains are specified on the
13106 command line, the default ^switches^switches^ in package @code{Builder} will
13107 be used for all mains, even if there are specific ^switches^switches^
13108 specified for one or several mains.
13110 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
13111 the specific ^switches^switches^ for each main, if they are specified.
13113 @node Library Project Files
13114 @subsubsection Library Project Files
13117 When @command{gnatmake} is invoked with a main project file that is a library
13118 project file, it is not allowed to specify one or more mains on the command
13122 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
13123 ^-l^/ACTION=LINK^ have special meanings.
13126 @item ^-b^/ACTION=BIND^ is only allwed for stand-alone libraries. It indicates
13127 to @command{gnatmake} that @command{gnatbind} should be invoked for the
13130 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
13131 to @command{gnatmake} that the binder generated file should be compiled
13132 (in the case of a stand-alone library) and that the library should be built.
13136 @node The GNAT Driver and Project Files
13137 @subsection The GNAT Driver and Project Files
13140 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
13142 @command{^gnatbind^gnatbind^},
13143 @command{^gnatfind^gnatfind^},
13144 @command{^gnatlink^gnatlink^},
13145 @command{^gnatls^gnatls^},
13146 @command{^gnatelim^gnatelim^},
13147 @command{^gnatpp^gnatpp^},
13148 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
13149 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
13150 They must be invoked through the @command{gnat} driver.
13152 The @command{gnat} driver is a front-end that accepts a number of commands and
13153 call the corresponding tool. It has been designed initially for VMS to convert
13154 VMS style qualifiers to Unix style switches, but it is now available to all
13155 the GNAT supported platforms.
13157 On non VMS platforms, the @command{gnat} driver accepts the following commands
13158 (case insensitive):
13162 BIND to invoke @command{^gnatbind^gnatbind^}
13164 CHOP to invoke @command{^gnatchop^gnatchop^}
13166 CLEAN to invoke @command{^gnatclean^gnatclean^}
13168 COMP or COMPILE to invoke the compiler
13170 ELIM to invoke @command{^gnatelim^gnatelim^}
13172 FIND to invoke @command{^gnatfind^gnatfind^}
13174 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
13176 LINK to invoke @command{^gnatlink^gnatlink^}
13178 LS or LIST to invoke @command{^gnatls^gnatls^}
13180 MAKE to invoke @command{^gnatmake^gnatmake^}
13182 NAME to invoke @command{^gnatname^gnatname^}
13184 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
13186 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
13188 STUB to invoke @command{^gnatstub^gnatstub^}
13190 XREF to invoke @command{^gnatxref^gnatxref^}
13194 Note that the compiler is invoked using the command
13195 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}.
13198 The command may be followed by switches and arguments for the invoked
13202 gnat bind -C main.ali
13208 Switches may also be put in text files, one switch per line, and the text
13209 files may be specified with their path name preceded by '@@'.
13212 gnat bind @@args.txt main.ali
13216 In addition, for command BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
13217 PP or PRETTY and XREF, the project file related switches
13218 (@option{^-P^/PROJECT_FILE^},
13219 @option{^-X^/EXTERNAL_REFERENCE^} and
13220 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
13221 the switches of the invoking tool.
13224 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
13225 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
13226 the immediate sources of the specified project file.
13229 For each of these commands, there is optionally a corresponding package
13230 in the main project.
13234 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
13237 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
13240 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
13243 package @code{Eliminate} for command ELIM (invoking
13244 @code{^gnatelim^gnatelim^})
13247 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
13250 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
13253 package @code{Pretty_Printer} for command PP or PRETTY
13254 (invoking @code{^gnatpp^gnatpp^})
13257 package @code{Cross_Reference} for command XREF (invoking
13258 @code{^gnatxref^gnatxref^})
13263 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
13264 a simple variable with a string list value. It contains ^switches^switches^
13265 for the invocation of @code{^gnatls^gnatls^}.
13267 @smallexample @c projectfile
13271 for ^Switches^Switches^
13280 All other packages have two attribute @code{^Switches^Switches^} and
13281 @code{^Default_Switches^Default_Switches^}.
13284 @code{^Switches^Switches^} is an associated array attribute, indexed by the
13285 source file name, that has a string list value: the ^switches^switches^ to be
13286 used when the tool corresponding to the package is invoked for the specific
13290 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
13291 indexed by the programming language that has a string list value.
13292 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
13293 ^switches^switches^ for the invocation of the tool corresponding
13294 to the package, except if a specific @code{^Switches^Switches^} attribute
13295 is specified for the source file.
13297 @smallexample @c projectfile
13301 for Source_Dirs use ("./**");
13304 for ^Switches^Switches^ use
13311 package Compiler is
13312 for ^Default_Switches^Default_Switches^ ("Ada")
13313 use ("^-gnatv^-gnatv^",
13314 "^-gnatwa^-gnatwa^");
13320 for ^Default_Switches^Default_Switches^ ("Ada")
13328 for ^Default_Switches^Default_Switches^ ("Ada")
13330 for ^Switches^Switches^ ("main.adb")
13339 for ^Default_Switches^Default_Switches^ ("Ada")
13346 package Cross_Reference is
13347 for ^Default_Switches^Default_Switches^ ("Ada")
13352 end Cross_Reference;
13358 With the above project file, commands such as
13361 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
13362 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
13363 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
13364 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
13365 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
13369 will set up the environment properly and invoke the tool with the switches
13370 found in the package corresponding to the tool:
13371 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
13372 except @code{^Switches^Switches^ ("main.adb")}
13373 for @code{^gnatlink^gnatlink^}.
13376 @node Glide and Project Files
13377 @subsection Glide and Project Files
13380 Glide will automatically recognize the @file{.gpr} extension for
13381 project files, and will
13382 convert them to its own internal format automatically. However, it
13383 doesn't provide a syntax-oriented editor for modifying these
13385 The project file will be loaded as text when you select the menu item
13386 @code{Ada} @result{} @code{Project} @result{} @code{Edit}.
13387 You can edit this text and save the @file{gpr} file;
13388 when you next select this project file in Glide it
13389 will be automatically reloaded.
13392 @c **********************
13393 @node An Extended Example
13394 @section An Extended Example
13397 Suppose that we have two programs, @var{prog1} and @var{prog2},
13398 whose sources are in corresponding directories. We would like
13399 to build them with a single @command{gnatmake} command, and we want to place
13400 their object files into @file{build} subdirectories of the source directories.
13401 Furthermore, we want to have to have two separate subdirectories
13402 in @file{build} -- @file{release} and @file{debug} -- which will contain
13403 the object files compiled with different set of compilation flags.
13405 In other words, we have the following structure:
13422 Here are the project files that we must place in a directory @file{main}
13423 to maintain this structure:
13427 @item We create a @code{Common} project with a package @code{Compiler} that
13428 specifies the compilation ^switches^switches^:
13433 @b{project} Common @b{is}
13435 @b{for} Source_Dirs @b{use} (); -- No source files
13439 @b{type} Build_Type @b{is} ("release", "debug");
13440 Build : Build_Type := External ("BUILD", "debug");
13443 @b{package} Compiler @b{is}
13444 @b{case} Build @b{is}
13445 @b{when} "release" =>
13446 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13447 @b{use} ("^-O2^-O2^");
13448 @b{when} "debug" =>
13449 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
13450 @b{use} ("^-g^-g^");
13458 @item We create separate projects for the two programs:
13465 @b{project} Prog1 @b{is}
13467 @b{for} Source_Dirs @b{use} ("prog1");
13468 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
13470 @b{package} Compiler @b{renames} Common.Compiler;
13481 @b{project} Prog2 @b{is}
13483 @b{for} Source_Dirs @b{use} ("prog2");
13484 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
13486 @b{package} Compiler @b{renames} Common.Compiler;
13492 @item We create a wrapping project @code{Main}:
13501 @b{project} Main @b{is}
13503 @b{package} Compiler @b{renames} Common.Compiler;
13509 @item Finally we need to create a dummy procedure that @code{with}s (either
13510 explicitly or implicitly) all the sources of our two programs.
13515 Now we can build the programs using the command
13518 gnatmake ^-P^/PROJECT_FILE=^main dummy
13522 for the Debug mode, or
13526 gnatmake -Pmain -XBUILD=release
13532 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
13537 for the Release mode.
13539 @c ********************************
13540 @c * Project File Complete Syntax *
13541 @c ********************************
13543 @node Project File Complete Syntax
13544 @section Project File Complete Syntax
13548 context_clause project_declaration
13554 @b{with} path_name @{ , path_name @} ;
13559 project_declaration ::=
13560 simple_project_declaration | project_extension
13562 simple_project_declaration ::=
13563 @b{project} <project_>simple_name @b{is}
13564 @{declarative_item@}
13565 @b{end} <project_>simple_name;
13567 project_extension ::=
13568 @b{project} <project_>simple_name @b{extends} path_name @b{is}
13569 @{declarative_item@}
13570 @b{end} <project_>simple_name;
13572 declarative_item ::=
13573 package_declaration |
13574 typed_string_declaration |
13575 other_declarative_item
13577 package_declaration ::=
13578 package_specification | package_renaming
13580 package_specification ::=
13581 @b{package} package_identifier @b{is}
13582 @{simple_declarative_item@}
13583 @b{end} package_identifier ;
13585 package_identifier ::=
13586 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
13587 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
13588 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
13590 package_renaming ::==
13591 @b{package} package_identifier @b{renames}
13592 <project_>simple_name.package_identifier ;
13594 typed_string_declaration ::=
13595 @b{type} <typed_string_>_simple_name @b{is}
13596 ( string_literal @{, string_literal@} );
13598 other_declarative_item ::=
13599 attribute_declaration |
13600 typed_variable_declaration |
13601 variable_declaration |
13604 attribute_declaration ::=
13605 full_associative_array_declaration |
13606 @b{for} attribute_designator @b{use} expression ;
13608 full_associative_array_declaration ::=
13609 @b{for} <associative_array_attribute_>simple_name @b{use}
13610 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
13612 attribute_designator ::=
13613 <simple_attribute_>simple_name |
13614 <associative_array_attribute_>simple_name ( string_literal )
13616 typed_variable_declaration ::=
13617 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
13619 variable_declaration ::=
13620 <variable_>simple_name := expression;
13630 attribute_reference
13636 ( <string_>expression @{ , <string_>expression @} )
13639 @b{external} ( string_literal [, string_literal] )
13641 attribute_reference ::=
13642 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
13644 attribute_prefix ::=
13646 <project_>simple_name | package_identifier |
13647 <project_>simple_name . package_identifier
13649 case_construction ::=
13650 @b{case} <typed_variable_>name @b{is}
13655 @b{when} discrete_choice_list =>
13656 @{case_construction | attribute_declaration@}
13658 discrete_choice_list ::=
13659 string_literal @{| string_literal@} |
13663 simple_name @{. simple_name@}
13666 identifier (same as Ada)
13671 @node The Cross-Referencing Tools gnatxref and gnatfind
13672 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
13677 The compiler generates cross-referencing information (unless
13678 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
13679 This information indicates where in the source each entity is declared and
13680 referenced. Note that entities in package Standard are not included, but
13681 entities in all other predefined units are included in the output.
13683 Before using any of these two tools, you need to compile successfully your
13684 application, so that GNAT gets a chance to generate the cross-referencing
13687 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
13688 information to provide the user with the capability to easily locate the
13689 declaration and references to an entity. These tools are quite similar,
13690 the difference being that @code{gnatfind} is intended for locating
13691 definitions and/or references to a specified entity or entities, whereas
13692 @code{gnatxref} is oriented to generating a full report of all
13695 To use these tools, you must not compile your application using the
13696 @option{-gnatx} switch on the @file{gnatmake} command line
13697 (see @ref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
13698 information will not be generated.
13701 * gnatxref Switches::
13702 * gnatfind Switches::
13703 * Project Files for gnatxref and gnatfind::
13704 * Regular Expressions in gnatfind and gnatxref::
13705 * Examples of gnatxref Usage::
13706 * Examples of gnatfind Usage::
13709 @node gnatxref Switches
13710 @section @code{gnatxref} Switches
13713 The command invocation for @code{gnatxref} is:
13715 $ gnatxref [switches] sourcefile1 [sourcefile2 ...]
13722 @item sourcefile1, sourcefile2
13723 identifies the source files for which a report is to be generated. The
13724 ``with''ed units will be processed too. You must provide at least one file.
13726 These file names are considered to be regular expressions, so for instance
13727 specifying @file{source*.adb} is the same as giving every file in the current
13728 directory whose name starts with @file{source} and whose extension is
13734 The switches can be :
13737 @item ^-a^/ALL_FILES^
13738 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
13739 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
13740 the read-only files found in the library search path. Otherwise, these files
13741 will be ignored. This option can be used to protect Gnat sources or your own
13742 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
13743 much faster, and their output much smaller. Read-only here refers to access
13744 or permissions status in the file system for the current user.
13747 @cindex @option{-aIDIR} (@command{gnatxref})
13748 When looking for source files also look in directory DIR. The order in which
13749 source file search is undertaken is the same as for @file{gnatmake}.
13752 @cindex @option{-aODIR} (@command{gnatxref})
13753 When searching for library and object files, look in directory
13754 DIR. The order in which library files are searched is the same as for
13758 @cindex @option{-nostdinc} (@command{gnatxref})
13759 Do not look for sources in the system default directory.
13762 @cindex @option{-nostdlib} (@command{gnatxref})
13763 Do not look for library files in the system default directory.
13765 @item --RTS=@var{rts-path}
13766 @cindex @option{--RTS} (@command{gnatxref})
13767 Specifies the default location of the runtime library. Same meaning as the
13768 equivalent @code{gnatmake} flag (see @ref{Switches for gnatmake}).
13770 @item ^-d^/DERIVED_TYPES^
13771 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
13772 If this switch is set @code{gnatxref} will output the parent type
13773 reference for each matching derived types.
13775 @item ^-f^/FULL_PATHNAME^
13776 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
13777 If this switch is set, the output file names will be preceded by their
13778 directory (if the file was found in the search path). If this switch is
13779 not set, the directory will not be printed.
13781 @item ^-g^/IGNORE_LOCALS^
13782 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
13783 If this switch is set, information is output only for library-level
13784 entities, ignoring local entities. The use of this switch may accelerate
13785 @code{gnatfind} and @code{gnatxref}.
13788 @cindex @option{-IDIR} (@command{gnatxref})
13789 Equivalent to @samp{-aODIR -aIDIR}.
13792 @cindex @option{-pFILE} (@command{gnatxref})
13793 Specify a project file to use @xref{Project Files}. These project files are
13794 the @file{.adp} files used by Glide. If you need to use the @file{.gpr}
13795 project files, you should use gnatxref through the GNAT driver
13796 (@command{gnat xref -Pproject}).
13798 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
13799 project file in the current directory.
13801 If a project file is either specified or found by the tools, then the content
13802 of the source directory and object directory lines are added as if they
13803 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
13804 and @samp{^-aO^OBJECT_SEARCH^}.
13806 Output only unused symbols. This may be really useful if you give your
13807 main compilation unit on the command line, as @code{gnatxref} will then
13808 display every unused entity and 'with'ed package.
13812 Instead of producing the default output, @code{gnatxref} will generate a
13813 @file{tags} file that can be used by vi. For examples how to use this
13814 feature, see @xref{Examples of gnatxref Usage}. The tags file is output
13815 to the standard output, thus you will have to redirect it to a file.
13821 All these switches may be in any order on the command line, and may even
13822 appear after the file names. They need not be separated by spaces, thus
13823 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
13824 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
13826 @node gnatfind Switches
13827 @section @code{gnatfind} Switches
13830 The command line for @code{gnatfind} is:
13833 $ gnatfind [switches] pattern[:sourcefile[:line[:column]]]
13842 An entity will be output only if it matches the regular expression found
13843 in @samp{pattern}, see @xref{Regular Expressions in gnatfind and gnatxref}.
13845 Omitting the pattern is equivalent to specifying @samp{*}, which
13846 will match any entity. Note that if you do not provide a pattern, you
13847 have to provide both a sourcefile and a line.
13849 Entity names are given in Latin-1, with uppercase/lowercase equivalence
13850 for matching purposes. At the current time there is no support for
13851 8-bit codes other than Latin-1, or for wide characters in identifiers.
13854 @code{gnatfind} will look for references, bodies or declarations
13855 of symbols referenced in @file{sourcefile}, at line @samp{line}
13856 and column @samp{column}. See @pxref{Examples of gnatfind Usage}
13857 for syntax examples.
13860 is a decimal integer identifying the line number containing
13861 the reference to the entity (or entities) to be located.
13864 is a decimal integer identifying the exact location on the
13865 line of the first character of the identifier for the
13866 entity reference. Columns are numbered from 1.
13868 @item file1 file2 ...
13869 The search will be restricted to these source files. If none are given, then
13870 the search will be done for every library file in the search path.
13871 These file must appear only after the pattern or sourcefile.
13873 These file names are considered to be regular expressions, so for instance
13874 specifying 'source*.adb' is the same as giving every file in the current
13875 directory whose name starts with 'source' and whose extension is 'adb'.
13877 The location of the spec of the entity will always be displayed, even if it
13878 isn't in one of file1, file2,... The occurrences of the entity in the
13879 separate units of the ones given on the command line will also be displayed.
13881 Note that if you specify at least one file in this part, @code{gnatfind} may
13882 sometimes not be able to find the body of the subprograms...
13887 At least one of 'sourcefile' or 'pattern' has to be present on
13890 The following switches are available:
13894 @item ^-a^/ALL_FILES^
13895 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
13896 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
13897 the read-only files found in the library search path. Otherwise, these files
13898 will be ignored. This option can be used to protect Gnat sources or your own
13899 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
13900 much faster, and their output much smaller. Read-only here refers to access
13901 or permission status in the file system for the current user.
13904 @cindex @option{-aIDIR} (@command{gnatfind})
13905 When looking for source files also look in directory DIR. The order in which
13906 source file search is undertaken is the same as for @file{gnatmake}.
13909 @cindex @option{-aODIR} (@command{gnatfind})
13910 When searching for library and object files, look in directory
13911 DIR. The order in which library files are searched is the same as for
13915 @cindex @option{-nostdinc} (@command{gnatfind})
13916 Do not look for sources in the system default directory.
13919 @cindex @option{-nostdlib} (@command{gnatfind})
13920 Do not look for library files in the system default directory.
13922 @item --RTS=@var{rts-path}
13923 @cindex @option{--RTS} (@command{gnatfind})
13924 Specifies the default location of the runtime library. Same meaning as the
13925 equivalent @code{gnatmake} flag (see @ref{Switches for gnatmake}).
13927 @item ^-d^/DERIVED_TYPE_INFORMATION^
13928 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
13929 If this switch is set, then @code{gnatfind} will output the parent type
13930 reference for each matching derived types.
13932 @item ^-e^/EXPRESSIONS^
13933 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
13934 By default, @code{gnatfind} accept the simple regular expression set for
13935 @samp{pattern}. If this switch is set, then the pattern will be
13936 considered as full Unix-style regular expression.
13938 @item ^-f^/FULL_PATHNAME^
13939 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
13940 If this switch is set, the output file names will be preceded by their
13941 directory (if the file was found in the search path). If this switch is
13942 not set, the directory will not be printed.
13944 @item ^-g^/IGNORE_LOCALS^
13945 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
13946 If this switch is set, information is output only for library-level
13947 entities, ignoring local entities. The use of this switch may accelerate
13948 @code{gnatfind} and @code{gnatxref}.
13951 @cindex @option{-IDIR} (@command{gnatfind})
13952 Equivalent to @samp{-aODIR -aIDIR}.
13955 @cindex @option{-pFILE} (@command{gnatfind})
13956 Specify a project file (@pxref{Project Files}) to use.
13957 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
13958 project file in the current directory.
13960 If a project file is either specified or found by the tools, then the content
13961 of the source directory and object directory lines are added as if they
13962 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
13963 @samp{^-aO^/OBJECT_SEARCH^}.
13965 @item ^-r^/REFERENCES^
13966 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
13967 By default, @code{gnatfind} will output only the information about the
13968 declaration, body or type completion of the entities. If this switch is
13969 set, the @code{gnatfind} will locate every reference to the entities in
13970 the files specified on the command line (or in every file in the search
13971 path if no file is given on the command line).
13973 @item ^-s^/PRINT_LINES^
13974 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
13975 If this switch is set, then @code{gnatfind} will output the content
13976 of the Ada source file lines were the entity was found.
13978 @item ^-t^/TYPE_HIERARCHY^
13979 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
13980 If this switch is set, then @code{gnatfind} will output the type hierarchy for
13981 the specified type. It act like -d option but recursively from parent
13982 type to parent type. When this switch is set it is not possible to
13983 specify more than one file.
13988 All these switches may be in any order on the command line, and may even
13989 appear after the file names. They need not be separated by spaces, thus
13990 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
13991 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
13993 As stated previously, gnatfind will search in every directory in the
13994 search path. You can force it to look only in the current directory if
13995 you specify @code{*} at the end of the command line.
13997 @node Project Files for gnatxref and gnatfind
13998 @section Project Files for @command{gnatxref} and @command{gnatfind}
14001 Project files allow a programmer to specify how to compile its
14002 application, where to find sources, etc. These files are used
14004 primarily by the Glide Ada mode, but they can also be used
14007 @code{gnatxref} and @code{gnatfind}.
14009 A project file name must end with @file{.gpr}. If a single one is
14010 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
14011 extract the information from it. If multiple project files are found, none of
14012 them is read, and you have to use the @samp{-p} switch to specify the one
14015 The following lines can be included, even though most of them have default
14016 values which can be used in most cases.
14017 The lines can be entered in any order in the file.
14018 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
14019 each line. If you have multiple instances, only the last one is taken into
14024 [default: @code{"^./^[]^"}]
14025 specifies a directory where to look for source files. Multiple @code{src_dir}
14026 lines can be specified and they will be searched in the order they
14030 [default: @code{"^./^[]^"}]
14031 specifies a directory where to look for object and library files. Multiple
14032 @code{obj_dir} lines can be specified, and they will be searched in the order
14035 @item comp_opt=SWITCHES
14036 [default: @code{""}]
14037 creates a variable which can be referred to subsequently by using
14038 the @code{$@{comp_opt@}} notation. This is intended to store the default
14039 switches given to @command{gnatmake} and @command{gcc}.
14041 @item bind_opt=SWITCHES
14042 [default: @code{""}]
14043 creates a variable which can be referred to subsequently by using
14044 the @samp{$@{bind_opt@}} notation. This is intended to store the default
14045 switches given to @command{gnatbind}.
14047 @item link_opt=SWITCHES
14048 [default: @code{""}]
14049 creates a variable which can be referred to subsequently by using
14050 the @samp{$@{link_opt@}} notation. This is intended to store the default
14051 switches given to @command{gnatlink}.
14053 @item main=EXECUTABLE
14054 [default: @code{""}]
14055 specifies the name of the executable for the application. This variable can
14056 be referred to in the following lines by using the @samp{$@{main@}} notation.
14059 @item comp_cmd=COMMAND
14060 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
14063 @item comp_cmd=COMMAND
14064 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
14066 specifies the command used to compile a single file in the application.
14069 @item make_cmd=COMMAND
14070 [default: @code{"GNAT MAKE $@{main@}
14071 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
14072 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
14073 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
14076 @item make_cmd=COMMAND
14077 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
14078 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
14079 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
14081 specifies the command used to recompile the whole application.
14083 @item run_cmd=COMMAND
14084 [default: @code{"$@{main@}"}]
14085 specifies the command used to run the application.
14087 @item debug_cmd=COMMAND
14088 [default: @code{"gdb $@{main@}"}]
14089 specifies the command used to debug the application
14094 @command{gnatxref} and @command{gnatfind} only take into account the
14095 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
14097 @node Regular Expressions in gnatfind and gnatxref
14098 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
14101 As specified in the section about @command{gnatfind}, the pattern can be a
14102 regular expression. Actually, there are to set of regular expressions
14103 which are recognized by the program :
14106 @item globbing patterns
14107 These are the most usual regular expression. They are the same that you
14108 generally used in a Unix shell command line, or in a DOS session.
14110 Here is a more formal grammar :
14117 term ::= elmt -- matches elmt
14118 term ::= elmt elmt -- concatenation (elmt then elmt)
14119 term ::= * -- any string of 0 or more characters
14120 term ::= ? -- matches any character
14121 term ::= [char @{char@}] -- matches any character listed
14122 term ::= [char - char] -- matches any character in range
14126 @item full regular expression
14127 The second set of regular expressions is much more powerful. This is the
14128 type of regular expressions recognized by utilities such a @file{grep}.
14130 The following is the form of a regular expression, expressed in Ada
14131 reference manual style BNF is as follows
14138 regexp ::= term @{| term@} -- alternation (term or term ...)
14140 term ::= item @{item@} -- concatenation (item then item)
14142 item ::= elmt -- match elmt
14143 item ::= elmt * -- zero or more elmt's
14144 item ::= elmt + -- one or more elmt's
14145 item ::= elmt ? -- matches elmt or nothing
14148 elmt ::= nschar -- matches given character
14149 elmt ::= [nschar @{nschar@}] -- matches any character listed
14150 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
14151 elmt ::= [char - char] -- matches chars in given range
14152 elmt ::= \ char -- matches given character
14153 elmt ::= . -- matches any single character
14154 elmt ::= ( regexp ) -- parens used for grouping
14156 char ::= any character, including special characters
14157 nschar ::= any character except ()[].*+?^^^
14161 Following are a few examples :
14165 will match any of the two strings 'abcde' and 'fghi'.
14168 will match any string like 'abd', 'abcd', 'abccd', 'abcccd', and so on
14171 will match any string which has only lowercase characters in it (and at
14172 least one character
14177 @node Examples of gnatxref Usage
14178 @section Examples of @code{gnatxref} Usage
14180 @subsection General Usage
14183 For the following examples, we will consider the following units :
14185 @smallexample @c ada
14191 3: procedure Foo (B : in Integer);
14198 1: package body Main is
14199 2: procedure Foo (B : in Integer) is
14210 2: procedure Print (B : Integer);
14219 The first thing to do is to recompile your application (for instance, in
14220 that case just by doing a @samp{gnatmake main}, so that GNAT generates
14221 the cross-referencing information.
14222 You can then issue any of the following commands:
14224 @item gnatxref main.adb
14225 @code{gnatxref} generates cross-reference information for main.adb
14226 and every unit 'with'ed by main.adb.
14228 The output would be:
14236 Decl: main.ads 3:20
14237 Body: main.adb 2:20
14238 Ref: main.adb 4:13 5:13 6:19
14241 Ref: main.adb 6:8 7:8
14251 Decl: main.ads 3:15
14252 Body: main.adb 2:15
14255 Body: main.adb 1:14
14258 Ref: main.adb 6:12 7:12
14262 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
14263 its body is in main.adb, line 1, column 14 and is not referenced any where.
14265 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
14266 it referenced in main.adb, line 6 column 12 and line 7 column 12.
14268 @item gnatxref package1.adb package2.ads
14269 @code{gnatxref} will generates cross-reference information for
14270 package1.adb, package2.ads and any other package 'with'ed by any
14276 @subsection Using gnatxref with vi
14278 @code{gnatxref} can generate a tags file output, which can be used
14279 directly from @file{vi}. Note that the standard version of @file{vi}
14280 will not work properly with overloaded symbols. Consider using another
14281 free implementation of @file{vi}, such as @file{vim}.
14284 $ gnatxref -v gnatfind.adb > tags
14288 will generate the tags file for @code{gnatfind} itself (if the sources
14289 are in the search path!).
14291 From @file{vi}, you can then use the command @samp{:tag @i{entity}}
14292 (replacing @i{entity} by whatever you are looking for), and vi will
14293 display a new file with the corresponding declaration of entity.
14296 @node Examples of gnatfind Usage
14297 @section Examples of @code{gnatfind} Usage
14301 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
14302 Find declarations for all entities xyz referenced at least once in
14303 main.adb. The references are search in every library file in the search
14306 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
14309 The output will look like:
14311 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14312 ^directory/^[directory]^main.adb:24:10: xyz <= body
14313 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14317 that is to say, one of the entities xyz found in main.adb is declared at
14318 line 12 of main.ads (and its body is in main.adb), and another one is
14319 declared at line 45 of foo.ads
14321 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
14322 This is the same command as the previous one, instead @code{gnatfind} will
14323 display the content of the Ada source file lines.
14325 The output will look like:
14328 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
14330 ^directory/^[directory]^main.adb:24:10: xyz <= body
14332 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
14337 This can make it easier to find exactly the location your are looking
14340 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
14341 Find references to all entities containing an x that are
14342 referenced on line 123 of main.ads.
14343 The references will be searched only in main.ads and foo.adb.
14345 @item gnatfind main.ads:123
14346 Find declarations and bodies for all entities that are referenced on
14347 line 123 of main.ads.
14349 This is the same as @code{gnatfind "*":main.adb:123}.
14351 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
14352 Find the declaration for the entity referenced at column 45 in
14353 line 123 of file main.adb in directory mydir. Note that it
14354 is usual to omit the identifier name when the column is given,
14355 since the column position identifies a unique reference.
14357 The column has to be the beginning of the identifier, and should not
14358 point to any character in the middle of the identifier.
14363 @c *********************************
14364 @node The GNAT Pretty-Printer gnatpp
14365 @chapter The GNAT Pretty-Printer @command{gnatpp}
14367 @cindex Pretty-Printer
14370 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
14371 for source reformatting / pretty-printing.
14372 It takes an Ada source file as input and generates a reformatted
14374 You can specify various style directives via switches; e.g.,
14375 identifier case conventions, rules of indentation, and comment layout.
14377 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
14378 tree for the input source and thus requires the input to be syntactically and
14379 semantically legal.
14380 If this condition is not met, @command{gnatpp} will terminate with an
14381 error message; no output file will be generated.
14383 If the compilation unit
14384 contained in the input source depends semantically upon units located
14385 outside the current directory, you have to provide the source search path
14386 when invoking @command{gnatpp}; see the description of the @command{gnatpp}
14389 The @command{gnatpp} command has the form
14392 $ gnatpp [@var{switches}] @var{filename}
14399 @var{switches} is an optional sequence of switches defining such properties as
14400 the formatting rules, the source search path, and the destination for the
14404 @var{filename} is the name (including the extension) of the source file to
14405 reformat; ``wildcards'' or several file names on the same gnatpp command are
14406 allowed. The file name may contain path information; it does not have to follow
14407 the GNAT file naming rules
14412 * Switches for gnatpp::
14413 * Formatting Rules::
14416 @node Switches for gnatpp
14417 @section Switches for @command{gnatpp}
14420 The following subsections describe the various switches accepted by
14421 @command{gnatpp}, organized by category.
14424 You specify a switch by supplying a name and generally also a value.
14425 In many cases the values for a switch with a given name are incompatible with
14427 (for example the switch that controls the casing of a reserved word may have
14428 exactly one value: upper case, lower case, or
14429 mixed case) and thus exactly one such switch can be in effect for an
14430 invocation of @command{gnatpp}.
14431 If more than one is supplied, the last one is used.
14432 However, some values for the same switch are mutually compatible.
14433 You may supply several such switches to @command{gnatpp}, but then
14434 each must be specified in full, with both the name and the value.
14435 Abbreviated forms (the name appearing once, followed by each value) are
14437 For example, to set
14438 the alignment of the assignment delimiter both in declarations and in
14439 assignment statements, you must write @option{-A2A3}
14440 (or @option{-A2 -A3}), but not @option{-A23}.
14444 In many cases the set of options for a given qualifier are incompatible with
14445 each other (for example the qualifier that controls the casing of a reserved
14446 word may have exactly one option, which specifies either upper case, lower
14447 case, or mixed case), and thus exactly one such option can be in effect for
14448 an invocation of @command{gnatpp}.
14449 If more than one is supplied, the last one is used.
14450 However, some qualifiers have options that are mutually compatible,
14451 and then you may then supply several such options when invoking
14455 In most cases, it is obvious whether or not the
14456 ^values for a switch with a given name^options for a given qualifier^
14457 are compatible with each other.
14458 When the semantics might not be evident, the summaries below explicitly
14459 indicate the effect.
14462 * Alignment Control::
14464 * Construct Layout Control::
14465 * General Text Layout Control::
14466 * Other Formatting Options::
14467 * Setting the Source Search Path::
14468 * Output File Control::
14469 * Other gnatpp Switches::
14473 @node Alignment Control
14474 @subsection Alignment Control
14475 @cindex Alignment control in @command{gnatpp}
14478 Programs can be easier to read if certain constructs are vertically aligned.
14479 By default all alignments are set ON.
14480 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
14481 OFF, and then use one or more of the other
14482 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
14483 to activate alignment for specific constructs.
14486 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
14490 Set all alignments to ON
14493 @item ^-A0^/ALIGN=OFF^
14494 Set all alignments to OFF
14496 @item ^-A1^/ALIGN=COLONS^
14497 Align @code{:} in declarations
14499 @item ^-A2^/ALIGN=DECLARATIONS^
14500 Align @code{:=} in initializations in declarations
14502 @item ^-A3^/ALIGN=STATEMENTS^
14503 Align @code{:=} in assignment statements
14505 @item ^-A4^/ALIGN=ARROWS^
14506 Align @code{=>} in associations
14510 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
14514 @node Casing Control
14515 @subsection Casing Control
14516 @cindex Casing control in @command{gnatpp}
14519 @command{gnatpp} allows you to specify the casing for reserved words,
14520 pragma names, attribute designators and identifiers.
14521 For identifiers you may define a
14522 general rule for name casing but also override this rule
14523 via a set of dictionary files.
14525 Three types of casing are supported: lower case, upper case, and mixed case.
14526 Lower and upper case are self-explanatory (but since some letters in
14527 Latin1 and other GNAT-supported character sets
14528 exist only in lower-case form, an upper case conversion will have no
14530 ``Mixed case'' means that the first letter, and also each letter immediately
14531 following an underscore, are converted to their uppercase forms;
14532 all the other letters are converted to their lowercase forms.
14535 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
14536 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
14537 Attribute designators are lower case
14539 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
14540 Attribute designators are upper case
14542 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
14543 Attribute designators are mixed case (this is the default)
14545 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
14546 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
14547 Keywords (technically, these are known in Ada as @emph{reserved words}) are
14548 lower case (this is the default)
14550 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
14551 Keywords are upper case
14553 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
14554 @item ^-nD^/NAME_CASING=AS_DECLARED^
14555 Name casing for defining occurrences are as they appear in the source file
14556 (this is the default)
14558 @item ^-nU^/NAME_CASING=UPPER_CASE^
14559 Names are in upper case
14561 @item ^-nL^/NAME_CASING=LOWER_CASE^
14562 Names are in lower case
14564 @item ^-nM^/NAME_CASING=MIXED_CASE^
14565 Names are in mixed case
14567 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
14568 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
14569 Pragma names are lower case
14571 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
14572 Pragma names are upper case
14574 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
14575 Pragma names are mixed case (this is the default)
14577 @item ^-D@var{file}^/DICTIONARY=@var{file}^
14578 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
14579 Use @var{file} as a @emph{dictionary file} that defines
14580 the casing for a set of specified names,
14581 thereby overriding the effect on these names by
14582 any explicit or implicit
14583 ^-n^/NAME_CASING^ switch.
14584 To supply more than one dictionary file,
14585 use ^several @option{-D} switches^a list of files as options^.
14588 @option{gnatpp} implicitly uses a @emph{default dictionary file}
14589 to define the casing for the Ada predefined names and
14590 the names declared in the GNAT libraries.
14592 @item ^-D-^/SPECIFIC_CASING^
14593 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
14594 Do not use the default dictionary file;
14595 instead, use the casing
14596 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
14601 The structure of a dictionary file, and details on the conventions
14602 used in the default dictionary file, are defined in @ref{Name Casing}.
14604 The @option{^-D-^/SPECIFIC_CASING^} and
14605 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
14609 @node Construct Layout Control
14610 @subsection Construct Layout Control
14611 @cindex Layout control in @command{gnatpp}
14614 This group of @command{gnatpp} switches controls the layout of comments and
14615 complex syntactic constructs. See @ref{Formatting Comments}, for details
14619 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
14620 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
14621 GNAT-style comment line indentation (this is the default).
14623 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
14624 Reference-manual comment line indentation.
14626 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
14627 GNAT-style comment beginning
14629 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
14630 Reformat comment blocks
14632 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
14633 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
14634 GNAT-style layout (this is the default)
14636 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
14639 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
14642 @item ^-notab^/NOTABS^
14643 All the VT characters are removed from the comment text. All the HT characters are
14644 expanded with the sequences of space characters to get to the next tab stops.
14650 The @option{-c1} and @option{-c2} switches are incompatible.
14651 The @option{-c3} and @option{-c4} switches are compatible with each other and
14652 also with @option{-c1} and @option{-c2}.
14654 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
14659 For the @option{/COMMENTS_LAYOUT} qualifier:
14662 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
14664 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
14665 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
14669 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
14670 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
14673 @node General Text Layout Control
14674 @subsection General Text Layout Control
14677 These switches allow control over line length and indentation.
14680 @item ^-M@i{nnn}^/LINE_LENGTH_MAX=@i{nnn}^
14681 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
14682 Maximum line length, @i{nnn} from 32 ..256, the default value is 79
14684 @item ^-i@i{nnn}^/INDENTATION_LEVEL=@i{nnn}^
14685 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
14686 Indentation level, @i{nnn} from 1 .. 9, the default value is 3
14688 @item ^-cl@i{nnn}^/CONTINUATION_INDENT=@i{nnn}^
14689 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
14690 Indentation level for continuation lines (relative to the line being
14691 continued), @i{nnn} from 1 .. 9.
14693 value is one less then the (normal) indentation level, unless the
14694 indentation is set to 1 (in which case the default value for continuation
14695 line indentation is also 1)
14699 @node Other Formatting Options
14700 @subsection Other Formatting Options
14703 These switches control the inclusion of missing end/exit labels, and
14704 the indentation level in @b{case} statements.
14707 @item ^-e^/NO_MISSED_LABELS^
14708 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
14709 Do not insert missing end/exit labels. An end label is the name of
14710 a construct that may optionally be repeated at the end of the
14711 construct's declaration;
14712 e.g., the names of packages, subprograms, and tasks.
14713 An exit label is the name of a loop that may appear as target
14714 of an exit statement within the loop.
14715 By default, @command{gnatpp} inserts these end/exit labels when
14716 they are absent from the original source. This option suppresses such
14717 insertion, so that the formatted source reflects the original.
14719 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
14720 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
14721 Insert a Form Feed character after a pragma Page.
14723 @item ^-T@i{nnn}^/MAX_INDENT=@i{nnn}^
14724 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
14725 Do not use an additional indentation level for @b{case} alternatives
14726 and variants if there are @i{nnn} or more (the default
14728 If @i{nnn} is 0, an additional indentation level is
14729 used for @b{case} alternatives and variants regardless of their number.
14732 @node Setting the Source Search Path
14733 @subsection Setting the Source Search Path
14736 To define the search path for the input source file, @command{gnatpp}
14737 uses the same switches as the GNAT compiler, with the same effects.
14740 @item ^-I^/SEARCH=^@var{dir}
14741 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
14742 The same as the corresponding gcc switch
14744 @item ^-I-^/NOCURRENT_DIRECTORY^
14745 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
14746 The same as the corresponding gcc switch
14748 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
14749 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
14750 The same as the corresponding gcc switch
14752 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
14753 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
14754 The same as the corresponding gcc switch
14759 @node Output File Control
14760 @subsection Output File Control
14763 By default the output is sent to the file whose name is obtained by appending
14764 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
14765 (if the file with this name already exists, it is unconditionally overwritten).
14766 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
14767 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
14769 The output may be redirected by the following switches:
14772 @item ^-pipe^/STANDARD_OUTPUT^
14773 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
14774 Send the output to @code{Standard_Output}
14776 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
14777 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
14778 Write the output into @var{output_file}.
14779 If @var{output_file} already exists, @command{gnatpp} terminates without
14780 reading or processing the input file.
14782 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
14783 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
14784 Write the output into @var{output_file}, overwriting the existing file
14785 (if one is present).
14787 @item ^-r^/REPLACE^
14788 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
14789 Replace the input source file with the reformatted output, and copy the
14790 original input source into the file whose name is obtained by appending the
14791 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
14792 If a file with this name already exists, @command{gnatpp} terminates without
14793 reading or processing the input file.
14795 @item ^-rf^/OVERRIDING_REPLACE^
14796 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
14797 Like @option{^-r^/REPLACE^} except that if the file with the specified name
14798 already exists, it is overwritten.
14802 Options @option{^-pipe^/STANDARD_OUTPUT^},
14803 @option{^-o^/OUTPUT^} and
14804 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
14805 contains only one file to reformat
14807 @node Other gnatpp Switches
14808 @subsection Other @code{gnatpp} Switches
14811 The additional @command{gnatpp} switches are defined in this subsection.
14814 @item ^-v^/VERBOSE^
14815 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
14817 @command{gnatpp} generates version information and then
14818 a trace of the actions it takes to produce or obtain the ASIS tree.
14820 @item ^-w^/WARNINGS^
14821 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
14823 @command{gnatpp} generates a warning whenever it can not provide
14824 a required layout in the result source.
14828 @node Formatting Rules
14829 @section Formatting Rules
14832 The following subsections show how @command{gnatpp} treats ``white space'',
14833 comments, program layout, and name casing.
14834 They provide the detailed descriptions of the switches shown above.
14837 * White Space and Empty Lines::
14838 * Formatting Comments::
14839 * Construct Layout::
14844 @node White Space and Empty Lines
14845 @subsection White Space and Empty Lines
14848 @command{gnatpp} does not have an option to control space characters.
14849 It will add or remove spaces according to the style illustrated by the
14850 examples in the @cite{Ada Reference Manual}.
14852 The only format effectors
14853 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
14854 that will appear in the output file are platform-specific line breaks,
14855 and also format effectors within (but not at the end of) comments.
14856 In particular, each horizontal tab character that is not inside
14857 a comment will be treated as a space and thus will appear in the
14858 output file as zero or more spaces depending on
14859 the reformatting of the line in which it appears.
14860 The only exception is a Form Feed character, which is inserted after a
14861 pragma @code{Page} when @option{-ff} is set.
14863 The output file will contain no lines with trailing ``white space'' (spaces,
14866 Empty lines in the original source are preserved
14867 only if they separate declarations or statements.
14868 In such contexts, a
14869 sequence of two or more empty lines is replaced by exactly one empty line.
14870 Note that a blank line will be removed if it separates two ``comment blocks''
14871 (a comment block is a sequence of whole-line comments).
14872 In order to preserve a visual separation between comment blocks, use an
14873 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
14874 Likewise, if for some reason you wish to have a sequence of empty lines,
14875 use a sequence of empty comments instead.
14878 @node Formatting Comments
14879 @subsection Formatting Comments
14882 Comments in Ada code are of two kinds:
14885 a @emph{whole-line comment}, which appears by itself (possibly preceded by
14886 ``white space'') on a line
14889 an @emph{end-of-line comment}, which follows some other Ada lexical element
14894 The indentation of a whole-line comment is that of either
14895 the preceding or following line in
14896 the formatted source, depending on switch settings as will be described below.
14898 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
14899 between the end of the preceding Ada lexical element and the beginning
14900 of the comment as appear in the original source,
14901 unless either the comment has to be split to
14902 satisfy the line length limitation, or else the next line contains a
14903 whole line comment that is considered a continuation of this end-of-line
14904 comment (because it starts at the same position).
14906 cases, the start of the end-of-line comment is moved right to the nearest
14907 multiple of the indentation level.
14908 This may result in a ``line overflow'' (the right-shifted comment extending
14909 beyond the maximum line length), in which case the comment is split as
14912 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
14913 (GNAT-style comment line indentation)
14914 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
14915 (reference-manual comment line indentation).
14916 With reference-manual style, a whole-line comment is indented as if it
14917 were a declaration or statement at the same place
14918 (i.e., according to the indentation of the preceding line(s)).
14919 With GNAT style, a whole-line comment that is immediately followed by an
14920 @b{if} or @b{case} statement alternative, a record variant, or the reserved
14921 word @b{begin}, is indented based on the construct that follows it.
14924 @smallexample @c ada
14936 Reference-manual indentation produces:
14938 @smallexample @c ada
14950 while GNAT-style indentation produces:
14952 @smallexample @c ada
14964 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
14965 (GNAT style comment beginning) has the following
14970 For each whole-line comment that does not end with two hyphens,
14971 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
14972 to ensure that there are at least two spaces between these hyphens and the
14973 first non-blank character of the comment.
14977 For an end-of-line comment, if in the original source the next line is a
14978 whole-line comment that starts at the same position
14979 as the end-of-line comment,
14980 then the whole-line comment (and all whole-line comments
14981 that follow it and that start at the same position)
14982 will start at this position in the output file.
14985 That is, if in the original source we have:
14987 @smallexample @c ada
14990 A := B + C; -- B must be in the range Low1..High1
14991 -- C must be in the range Low2..High2
14992 --B+C will be in the range Low1+Low2..High1+High2
14998 Then in the formatted source we get
15000 @smallexample @c ada
15003 A := B + C; -- B must be in the range Low1..High1
15004 -- C must be in the range Low2..High2
15005 -- B+C will be in the range Low1+Low2..High1+High2
15011 A comment that exceeds the line length limit will be split.
15013 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
15014 the line belongs to a reformattable block, splitting the line generates a
15015 @command{gnatpp} warning.
15016 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
15017 comments may be reformatted in typical
15018 word processor style (that is, moving words between lines and putting as
15019 many words in a line as possible).
15022 @node Construct Layout
15023 @subsection Construct Layout
15026 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
15027 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
15028 layout on the one hand, and uncompact layout
15029 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
15030 can be illustrated by the following examples:
15034 @multitable @columnfractions .5 .5
15035 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
15038 @smallexample @c ada
15045 @smallexample @c ada
15054 @smallexample @c ada
15062 @smallexample @c ada
15072 @smallexample @c ada
15073 Clear : for J in 1 .. 10 loop
15078 @smallexample @c ada
15080 for J in 1 .. 10 loop
15091 GNAT style, compact layout Uncompact layout
15093 type q is record type q is
15094 a : integer; record
15095 b : integer; a : integer;
15096 end record; b : integer;
15100 Block : declare Block :
15101 A : Integer := 3; declare
15102 begin A : Integer := 3;
15104 end Block; Proc (A, A);
15107 Clear : for J in 1 .. 10 loop Clear :
15108 A (J) := 0; for J in 1 .. 10 loop
15109 end loop Clear; A (J) := 0;
15116 A further difference between GNAT style layout and compact layout is that
15117 GNAT style layout inserts empty lines as separation for
15118 compound statements, return statements and bodies.
15122 @subsection Name Casing
15125 @command{gnatpp} always converts the usage occurrence of a (simple) name to
15126 the same casing as the corresponding defining identifier.
15128 You control the casing for defining occurrences via the
15129 @option{^-n^/NAME_CASING^} switch.
15131 With @option{-nD} (``as declared'', which is the default),
15134 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
15136 defining occurrences appear exactly as in the source file
15137 where they are declared.
15138 The other ^values for this switch^options for this qualifier^ ---
15139 @option{^-nU^UPPER_CASE^},
15140 @option{^-nL^LOWER_CASE^},
15141 @option{^-nM^MIXED_CASE^} ---
15143 ^upper, lower, or mixed case, respectively^the corresponding casing^.
15144 If @command{gnatpp} changes the casing of a defining
15145 occurrence, it analogously changes the casing of all the
15146 usage occurrences of this name.
15148 If the defining occurrence of a name is not in the source compilation unit
15149 currently being processed by @command{gnatpp}, the casing of each reference to
15150 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
15151 switch (subject to the dictionary file mechanism described below).
15152 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
15154 casing for the defining occurrence of the name.
15156 Some names may need to be spelled with casing conventions that are not
15157 covered by the upper-, lower-, and mixed-case transformations.
15158 You can arrange correct casing by placing such names in a
15159 @emph{dictionary file},
15160 and then supplying a @option{^-D^/DICTIONARY^} switch.
15161 The casing of names from dictionary files overrides
15162 any @option{^-n^/NAME_CASING^} switch.
15164 To handle the casing of Ada predefined names and the names from GNAT libraries,
15165 @command{gnatpp} assumes a default dictionary file.
15166 The name of each predefined entity is spelled with the same casing as is used
15167 for the entity in the @cite{Ada Reference Manual}.
15168 The name of each entity in the GNAT libraries is spelled with the same casing
15169 as is used in the declaration of that entity.
15171 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
15172 default dictionary file.
15173 Instead, the casing for predefined and GNAT-defined names will be established
15174 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
15175 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
15176 will appear as just shown,
15177 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
15178 To ensure that even such names are rendered in uppercase,
15179 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
15180 (or else, less conveniently, place these names in upper case in a dictionary
15183 A dictionary file is
15184 a plain text file; each line in this file can be either a blank line
15185 (containing only space characters and ASCII.HT characters), an Ada comment
15186 line, or the specification of exactly one @emph{casing schema}.
15188 A casing schema is a string that has the following syntax:
15192 @var{casing_schema} ::= @var{identifier} | [*]@var{simple_identifier}[*]
15194 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
15199 (The @code{[]} metanotation stands for an optional part;
15200 see @cite{Ada Reference Manual}, Section 2.3) for the definition of the
15201 @var{identifier} lexical element and the @var{letter_or_digit} category).
15203 The casing schema string can be followed by white space and/or an Ada-style
15204 comment; any amount of white space is allowed before the string.
15206 If a dictionary file is passed as
15208 the value of a @option{-D@var{file}} switch
15211 an option to the @option{/DICTIONARY} qualifier
15214 simple name and every identifier, @command{gnatpp} checks if the dictionary
15215 defines the casing for the name or for some of its parts (the term ``subword''
15216 is used below to denote the part of a name which is delimited by ``_'' or by
15217 the beginning or end of the word and which does not contain any ``_'' inside):
15221 if the whole name is in the dictionary, @command{gnatpp} uses for this name
15222 the casing defined by the dictionary; no subwords are checked for this word
15225 for the first subword (that is, for the subword preceding the leftmost
15226 ``_''), @command{gnatpp} checks if the dictionary contains the corresponding
15227 string of the form @code{@var{simple_identifier}*}, and if it does, the
15228 casing of this @var{simple_identifier} is used for this subword
15231 for the last subword (following the rightmost ``_'') @command{gnatpp}
15232 checks if the dictionary contains the corresponding string of the form
15233 @code{*@var{simple_identifier}}, and if it does, the casing of this
15234 @var{simple_identifier} is used for this subword
15237 for every intermediate subword (surrounded by two'_') @command{gnatpp} checks
15238 if the dictionary contains the corresponding string of the form
15239 @code{*@var{simple_identifier}*}, and if it does, the casing of this
15240 simple_identifier is used for this subword
15243 if more than one dictionary file is passed as @command{gnatpp} switches, each
15244 dictionary adds new casing exceptions and overrides all the existing casing
15245 exceptions set by the previous dictionaries
15248 when @command{gnatpp} checks if the word or subword is in the dictionary,
15249 this check is not case sensitive
15253 For example, suppose we have the following source to reformat:
15255 @smallexample @c ada
15258 name1 : integer := 1;
15259 name4_name3_name2 : integer := 2;
15260 name2_name3_name4 : Boolean;
15263 name2_name3_name4 := name4_name3_name2 > name1;
15269 And suppose we have two dictionaries:
15286 If @command{gnatpp} is called with the following switches:
15290 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
15293 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
15298 then we will get the following name casing in the @command{gnatpp} output:
15300 @smallexample @c ada
15303 NAME1 : Integer := 1;
15304 Name4_NAME3_NAME2 : integer := 2;
15305 Name2_NAME3_Name4 : Boolean;
15308 Name2_NAME3_Name4 := Name4_NAME3_NAME2 > NAME1;
15315 @c ***********************************
15316 @node File Name Krunching Using gnatkr
15317 @chapter File Name Krunching Using @code{gnatkr}
15321 This chapter discusses the method used by the compiler to shorten
15322 the default file names chosen for Ada units so that they do not
15323 exceed the maximum length permitted. It also describes the
15324 @code{gnatkr} utility that can be used to determine the result of
15325 applying this shortening.
15329 * Krunching Method::
15330 * Examples of gnatkr Usage::
15334 @section About @code{gnatkr}
15337 The default file naming rule in GNAT
15338 is that the file name must be derived from
15339 the unit name. The exact default rule is as follows:
15342 Take the unit name and replace all dots by hyphens.
15344 If such a replacement occurs in the
15345 second character position of a name, and the first character is
15346 ^a, g, s, or i^A, G, S, or I^ then replace the dot by the character
15347 ^~ (tilde)^$ (dollar sign)^
15348 instead of a minus.
15350 The reason for this exception is to avoid clashes
15351 with the standard names for children of System, Ada, Interfaces,
15352 and GNAT, which use the prefixes ^s- a- i- and g-^S- A- I- and G-^
15355 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
15356 switch of the compiler activates a ``krunching''
15357 circuit that limits file names to nn characters (where nn is a decimal
15358 integer). For example, using OpenVMS,
15359 where the maximum file name length is
15360 39, the value of nn is usually set to 39, but if you want to generate
15361 a set of files that would be usable if ported to a system with some
15362 different maximum file length, then a different value can be specified.
15363 The default value of 39 for OpenVMS need not be specified.
15365 The @code{gnatkr} utility can be used to determine the krunched name for
15366 a given file, when krunched to a specified maximum length.
15369 @section Using @code{gnatkr}
15372 The @code{gnatkr} command has the form
15376 $ gnatkr @var{name} [@var{length}]
15382 $ gnatkr @var{name} /COUNT=nn
15387 @var{name} is the uncrunched file name, derived from the name of the unit
15388 in the standard manner described in the previous section (i.e. in particular
15389 all dots are replaced by hyphens). The file name may or may not have an
15390 extension (defined as a suffix of the form period followed by arbitrary
15391 characters other than period). If an extension is present then it will
15392 be preserved in the output. For example, when krunching @file{hellofile.ads}
15393 to eight characters, the result will be hellofil.ads.
15395 Note: for compatibility with previous versions of @code{gnatkr} dots may
15396 appear in the name instead of hyphens, but the last dot will always be
15397 taken as the start of an extension. So if @code{gnatkr} is given an argument
15398 such as @file{Hello.World.adb} it will be treated exactly as if the first
15399 period had been a hyphen, and for example krunching to eight characters
15400 gives the result @file{hellworl.adb}.
15402 Note that the result is always all lower case (except on OpenVMS where it is
15403 all upper case). Characters of the other case are folded as required.
15405 @var{length} represents the length of the krunched name. The default
15406 when no argument is given is ^8^39^ characters. A length of zero stands for
15407 unlimited, in other words do not chop except for system files where the
15408 impled crunching length is always eight characters.
15411 The output is the krunched name. The output has an extension only if the
15412 original argument was a file name with an extension.
15414 @node Krunching Method
15415 @section Krunching Method
15418 The initial file name is determined by the name of the unit that the file
15419 contains. The name is formed by taking the full expanded name of the
15420 unit and replacing the separating dots with hyphens and
15421 using ^lowercase^uppercase^
15422 for all letters, except that a hyphen in the second character position is
15423 replaced by a ^tilde^dollar sign^ if the first character is
15424 ^a, i, g, or s^A, I, G, or S^.
15425 The extension is @code{.ads} for a
15426 specification and @code{.adb} for a body.
15427 Krunching does not affect the extension, but the file name is shortened to
15428 the specified length by following these rules:
15432 The name is divided into segments separated by hyphens, tildes or
15433 underscores and all hyphens, tildes, and underscores are
15434 eliminated. If this leaves the name short enough, we are done.
15437 If the name is too long, the longest segment is located (left-most
15438 if there are two of equal length), and shortened by dropping
15439 its last character. This is repeated until the name is short enough.
15441 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
15442 to fit the name into 8 characters as required by some operating systems.
15445 our-strings-wide_fixed 22
15446 our strings wide fixed 19
15447 our string wide fixed 18
15448 our strin wide fixed 17
15449 our stri wide fixed 16
15450 our stri wide fixe 15
15451 our str wide fixe 14
15452 our str wid fixe 13
15458 Final file name: oustwifi.adb
15462 The file names for all predefined units are always krunched to eight
15463 characters. The krunching of these predefined units uses the following
15464 special prefix replacements:
15468 replaced by @file{^a^A^-}
15471 replaced by @file{^g^G^-}
15474 replaced by @file{^i^I^-}
15477 replaced by @file{^s^S^-}
15480 These system files have a hyphen in the second character position. That
15481 is why normal user files replace such a character with a
15482 ^tilde^dollar sign^, to
15483 avoid confusion with system file names.
15485 As an example of this special rule, consider
15486 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
15489 ada-strings-wide_fixed 22
15490 a- strings wide fixed 18
15491 a- string wide fixed 17
15492 a- strin wide fixed 16
15493 a- stri wide fixed 15
15494 a- stri wide fixe 14
15495 a- str wide fixe 13
15501 Final file name: a-stwifi.adb
15505 Of course no file shortening algorithm can guarantee uniqueness over all
15506 possible unit names, and if file name krunching is used then it is your
15507 responsibility to ensure that no name clashes occur. The utility
15508 program @code{gnatkr} is supplied for conveniently determining the
15509 krunched name of a file.
15511 @node Examples of gnatkr Usage
15512 @section Examples of @code{gnatkr} Usage
15519 $ gnatkr very_long_unit_name.ads --> velounna.ads
15520 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
15521 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
15522 $ gnatkr grandparent-parent-child --> grparchi
15524 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
15525 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
15528 @node Preprocessing Using gnatprep
15529 @chapter Preprocessing Using @code{gnatprep}
15533 The @code{gnatprep} utility provides
15534 a simple preprocessing capability for Ada programs.
15535 It is designed for use with GNAT, but is not dependent on any special
15540 * Switches for gnatprep::
15541 * Form of Definitions File::
15542 * Form of Input Text for gnatprep::
15545 @node Using gnatprep
15546 @section Using @code{gnatprep}
15549 To call @code{gnatprep} use
15552 $ gnatprep [-bcrsu] [-Dsymbol=value] infile outfile [deffile]
15559 is the full name of the input file, which is an Ada source
15560 file containing preprocessor directives.
15563 is the full name of the output file, which is an Ada source
15564 in standard Ada form. When used with GNAT, this file name will
15565 normally have an ads or adb suffix.
15568 is the full name of a text file containing definitions of
15569 symbols to be referenced by the preprocessor. This argument is
15570 optional, and can be replaced by the use of the @option{-D} switch.
15573 is an optional sequence of switches as described in the next section.
15576 @node Switches for gnatprep
15577 @section Switches for @code{gnatprep}
15582 @item ^-b^/BLANK_LINES^
15583 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
15584 Causes both preprocessor lines and the lines deleted by
15585 preprocessing to be replaced by blank lines in the output source file,
15586 preserving line numbers in the output file.
15588 @item ^-c^/COMMENTS^
15589 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
15590 Causes both preprocessor lines and the lines deleted
15591 by preprocessing to be retained in the output source as comments marked
15592 with the special string @code{"--! "}. This option will result in line numbers
15593 being preserved in the output file.
15595 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
15596 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
15597 Defines a new symbol, associated with value. If no value is given on the
15598 command line, then symbol is considered to be @code{True}. This switch
15599 can be used in place of a definition file.
15603 @cindex @option{/REMOVE} (@command{gnatprep})
15604 This is the default setting which causes lines deleted by preprocessing
15605 to be entirely removed from the output file.
15608 @item ^-r^/REFERENCE^
15609 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
15610 Causes a @code{Source_Reference} pragma to be generated that
15611 references the original input file, so that error messages will use
15612 the file name of this original file. The use of this switch implies
15613 that preprocessor lines are not to be removed from the file, so its
15614 use will force @option{^-b^/BLANK_LINES^} mode if
15615 @option{^-c^/COMMENTS^}
15616 has not been specified explicitly.
15618 Note that if the file to be preprocessed contains multiple units, then
15619 it will be necessary to @code{gnatchop} the output file from
15620 @code{gnatprep}. If a @code{Source_Reference} pragma is present
15621 in the preprocessed file, it will be respected by
15622 @code{gnatchop ^-r^/REFERENCE^}
15623 so that the final chopped files will correctly refer to the original
15624 input source file for @code{gnatprep}.
15626 @item ^-s^/SYMBOLS^
15627 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
15628 Causes a sorted list of symbol names and values to be
15629 listed on the standard output file.
15631 @item ^-u^/UNDEFINED^
15632 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
15633 Causes undefined symbols to be treated as having the value FALSE in the context
15634 of a preprocessor test. In the absence of this option, an undefined symbol in
15635 a @code{#if} or @code{#elsif} test will be treated as an error.
15641 Note: if neither @option{-b} nor @option{-c} is present,
15642 then preprocessor lines and
15643 deleted lines are completely removed from the output, unless -r is
15644 specified, in which case -b is assumed.
15647 @node Form of Definitions File
15648 @section Form of Definitions File
15651 The definitions file contains lines of the form
15658 where symbol is an identifier, following normal Ada (case-insensitive)
15659 rules for its syntax, and value is one of the following:
15663 Empty, corresponding to a null substitution
15665 A string literal using normal Ada syntax
15667 Any sequence of characters from the set
15668 (letters, digits, period, underline).
15672 Comment lines may also appear in the definitions file, starting with
15673 the usual @code{--},
15674 and comments may be added to the definitions lines.
15676 @node Form of Input Text for gnatprep
15677 @section Form of Input Text for @code{gnatprep}
15680 The input text may contain preprocessor conditional inclusion lines,
15681 as well as general symbol substitution sequences.
15683 The preprocessor conditional inclusion commands have the form
15688 #if @i{expression} [then]
15690 #elsif @i{expression} [then]
15692 #elsif @i{expression} [then]
15703 In this example, @i{expression} is defined by the following grammar:
15705 @i{expression} ::= <symbol>
15706 @i{expression} ::= <symbol> = "<value>"
15707 @i{expression} ::= <symbol> = <symbol>
15708 @i{expression} ::= <symbol> 'Defined
15709 @i{expression} ::= not @i{expression}
15710 @i{expression} ::= @i{expression} and @i{expression}
15711 @i{expression} ::= @i{expression} or @i{expression}
15712 @i{expression} ::= @i{expression} and then @i{expression}
15713 @i{expression} ::= @i{expression} or else @i{expression}
15714 @i{expression} ::= ( @i{expression} )
15718 For the first test (@i{expression} ::= <symbol>) the symbol must have
15719 either the value true or false, that is to say the right-hand of the
15720 symbol definition must be one of the (case-insensitive) literals
15721 @code{True} or @code{False}. If the value is true, then the
15722 corresponding lines are included, and if the value is false, they are
15725 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
15726 the symbol has been defined in the definition file or by a @option{-D}
15727 switch on the command line. Otherwise, the test is false.
15729 The equality tests are case insensitive, as are all the preprocessor lines.
15731 If the symbol referenced is not defined in the symbol definitions file,
15732 then the effect depends on whether or not switch @option{-u}
15733 is specified. If so, then the symbol is treated as if it had the value
15734 false and the test fails. If this switch is not specified, then
15735 it is an error to reference an undefined symbol. It is also an error to
15736 reference a symbol that is defined with a value other than @code{True}
15739 The use of the @code{not} operator inverts the sense of this logical test, so
15740 that the lines are included only if the symbol is not defined.
15741 The @code{then} keyword is optional as shown
15743 The @code{#} must be the first non-blank character on a line, but
15744 otherwise the format is free form. Spaces or tabs may appear between
15745 the @code{#} and the keyword. The keywords and the symbols are case
15746 insensitive as in normal Ada code. Comments may be used on a
15747 preprocessor line, but other than that, no other tokens may appear on a
15748 preprocessor line. Any number of @code{elsif} clauses can be present,
15749 including none at all. The @code{else} is optional, as in Ada.
15751 The @code{#} marking the start of a preprocessor line must be the first
15752 non-blank character on the line, i.e. it must be preceded only by
15753 spaces or horizontal tabs.
15755 Symbol substitution outside of preprocessor lines is obtained by using
15763 anywhere within a source line, except in a comment or within a
15764 string literal. The identifier
15765 following the @code{$} must match one of the symbols defined in the symbol
15766 definition file, and the result is to substitute the value of the
15767 symbol in place of @code{$symbol} in the output file.
15769 Note that although the substitution of strings within a string literal
15770 is not possible, it is possible to have a symbol whose defined value is
15771 a string literal. So instead of setting XYZ to @code{hello} and writing:
15774 Header : String := "$XYZ";
15778 you should set XYZ to @code{"hello"} and write:
15781 Header : String := $XYZ;
15785 and then the substitution will occur as desired.
15788 @node The GNAT Run-Time Library Builder gnatlbr
15789 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
15791 @cindex Library builder
15794 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
15795 supplied configuration pragmas.
15798 * Running gnatlbr::
15799 * Switches for gnatlbr::
15800 * Examples of gnatlbr Usage::
15803 @node Running gnatlbr
15804 @section Running @code{gnatlbr}
15807 The @code{gnatlbr} command has the form
15810 $ GNAT LIBRARY /[CREATE | SET | DELETE]=directory [/CONFIG=file]
15813 @node Switches for gnatlbr
15814 @section Switches for @code{gnatlbr}
15817 @code{gnatlbr} recognizes the following switches:
15821 @item /CREATE=directory
15822 @cindex @code{/CREATE} (@code{gnatlbr})
15823 Create the new run-time library in the specified directory.
15825 @item /SET=directory
15826 @cindex @code{/SET} (@code{gnatlbr})
15827 Make the library in the specified directory the current run-time
15830 @item /DELETE=directory
15831 @cindex @code{/DELETE} (@code{gnatlbr})
15832 Delete the run-time library in the specified directory.
15835 @cindex @code{/CONFIG} (@code{gnatlbr})
15837 Use the configuration pragmas in the specified file when building
15841 Use the configuration pragmas in the specified file when compiling.
15845 @node Examples of gnatlbr Usage
15846 @section Example of @code{gnatlbr} Usage
15849 Contents of VAXFLOAT.ADC:
15850 pragma Float_Representation (VAX_Float);
15852 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
15854 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
15859 @node The GNAT Library Browser gnatls
15860 @chapter The GNAT Library Browser @code{gnatls}
15862 @cindex Library browser
15865 @code{gnatls} is a tool that outputs information about compiled
15866 units. It gives the relationship between objects, unit names and source
15867 files. It can also be used to check the source dependencies of a unit
15868 as well as various characteristics.
15872 * Switches for gnatls::
15873 * Examples of gnatls Usage::
15876 @node Running gnatls
15877 @section Running @code{gnatls}
15880 The @code{gnatls} command has the form
15883 $ gnatls switches @var{object_or_ali_file}
15887 The main argument is the list of object or @file{ali} files
15888 (@pxref{The Ada Library Information Files})
15889 for which information is requested.
15891 In normal mode, without additional option, @code{gnatls} produces a
15892 four-column listing. Each line represents information for a specific
15893 object. The first column gives the full path of the object, the second
15894 column gives the name of the principal unit in this object, the third
15895 column gives the status of the source and the fourth column gives the
15896 full path of the source representing this unit.
15897 Here is a simple example of use:
15901 ^./^[]^demo1.o demo1 DIF demo1.adb
15902 ^./^[]^demo2.o demo2 OK demo2.adb
15903 ^./^[]^hello.o h1 OK hello.adb
15904 ^./^[]^instr-child.o instr.child MOK instr-child.adb
15905 ^./^[]^instr.o instr OK instr.adb
15906 ^./^[]^tef.o tef DIF tef.adb
15907 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
15908 ^./^[]^tgef.o tgef DIF tgef.adb
15912 The first line can be interpreted as follows: the main unit which is
15914 object file @file{demo1.o} is demo1, whose main source is in
15915 @file{demo1.adb}. Furthermore, the version of the source used for the
15916 compilation of demo1 has been modified (DIF). Each source file has a status
15917 qualifier which can be:
15920 @item OK (unchanged)
15921 The version of the source file used for the compilation of the
15922 specified unit corresponds exactly to the actual source file.
15924 @item MOK (slightly modified)
15925 The version of the source file used for the compilation of the
15926 specified unit differs from the actual source file but not enough to
15927 require recompilation. If you use gnatmake with the qualifier
15928 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
15929 MOK will not be recompiled.
15931 @item DIF (modified)
15932 No version of the source found on the path corresponds to the source
15933 used to build this object.
15935 @item ??? (file not found)
15936 No source file was found for this unit.
15938 @item HID (hidden, unchanged version not first on PATH)
15939 The version of the source that corresponds exactly to the source used
15940 for compilation has been found on the path but it is hidden by another
15941 version of the same source that has been modified.
15945 @node Switches for gnatls
15946 @section Switches for @code{gnatls}
15949 @code{gnatls} recognizes the following switches:
15953 @item ^-a^/ALL_UNITS^
15954 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
15955 Consider all units, including those of the predefined Ada library.
15956 Especially useful with @option{^-d^/DEPENDENCIES^}.
15958 @item ^-d^/DEPENDENCIES^
15959 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
15960 List sources from which specified units depend on.
15962 @item ^-h^/OUTPUT=OPTIONS^
15963 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
15964 Output the list of options.
15966 @item ^-o^/OUTPUT=OBJECTS^
15967 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
15968 Only output information about object files.
15970 @item ^-s^/OUTPUT=SOURCES^
15971 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
15972 Only output information about source files.
15974 @item ^-u^/OUTPUT=UNITS^
15975 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
15976 Only output information about compilation units.
15978 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
15979 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
15980 @itemx ^-I^/SEARCH=^@var{dir}
15981 @itemx ^-I-^/NOCURRENT_DIRECTORY^
15983 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
15984 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
15985 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
15986 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
15987 Source path manipulation. Same meaning as the equivalent @code{gnatmake} flags
15988 (see @ref{Switches for gnatmake}).
15990 @item --RTS=@var{rts-path}
15991 @cindex @option{--RTS} (@code{gnatls})
15992 Specifies the default location of the runtime library. Same meaning as the
15993 equivalent @code{gnatmake} flag (see @ref{Switches for gnatmake}).
15995 @item ^-v^/OUTPUT=VERBOSE^
15996 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
15997 Verbose mode. Output the complete source and object paths. Do not use
15998 the default column layout but instead use long format giving as much as
15999 information possible on each requested units, including special
16000 characteristics such as:
16003 @item Preelaborable
16004 The unit is preelaborable in the Ada 95 sense.
16007 No elaboration code has been produced by the compiler for this unit.
16010 The unit is pure in the Ada 95 sense.
16012 @item Elaborate_Body
16013 The unit contains a pragma Elaborate_Body.
16016 The unit contains a pragma Remote_Types.
16018 @item Shared_Passive
16019 The unit contains a pragma Shared_Passive.
16022 This unit is part of the predefined environment and cannot be modified
16025 @item Remote_Call_Interface
16026 The unit contains a pragma Remote_Call_Interface.
16032 @node Examples of gnatls Usage
16033 @section Example of @code{gnatls} Usage
16037 Example of using the verbose switch. Note how the source and
16038 object paths are affected by the -I switch.
16041 $ gnatls -v -I.. demo1.o
16043 GNATLS 3.10w (970212) Copyright 1999 Free Software Foundation, Inc.
16045 Source Search Path:
16046 <Current_Directory>
16048 /home/comar/local/adainclude/
16050 Object Search Path:
16051 <Current_Directory>
16053 /home/comar/local/lib/gcc-lib/mips-sni-sysv4/2.7.2/adalib/
16058 Kind => subprogram body
16059 Flags => No_Elab_Code
16060 Source => demo1.adb modified
16064 The following is an example of use of the dependency list.
16065 Note the use of the -s switch
16066 which gives a straight list of source files. This can be useful for
16067 building specialized scripts.
16070 $ gnatls -d demo2.o
16071 ./demo2.o demo2 OK demo2.adb
16077 $ gnatls -d -s -a demo1.o
16079 /home/comar/local/adainclude/ada.ads
16080 /home/comar/local/adainclude/a-finali.ads
16081 /home/comar/local/adainclude/a-filico.ads
16082 /home/comar/local/adainclude/a-stream.ads
16083 /home/comar/local/adainclude/a-tags.ads
16086 /home/comar/local/adainclude/gnat.ads
16087 /home/comar/local/adainclude/g-io.ads
16089 /home/comar/local/adainclude/system.ads
16090 /home/comar/local/adainclude/s-exctab.ads
16091 /home/comar/local/adainclude/s-finimp.ads
16092 /home/comar/local/adainclude/s-finroo.ads
16093 /home/comar/local/adainclude/s-secsta.ads
16094 /home/comar/local/adainclude/s-stalib.ads
16095 /home/comar/local/adainclude/s-stoele.ads
16096 /home/comar/local/adainclude/s-stratt.ads
16097 /home/comar/local/adainclude/s-tasoli.ads
16098 /home/comar/local/adainclude/s-unstyp.ads
16099 /home/comar/local/adainclude/unchconv.ads
16105 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
16107 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
16108 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
16109 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
16110 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
16111 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
16115 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
16116 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
16118 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
16119 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
16120 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
16121 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
16122 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
16123 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
16124 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
16125 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
16126 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
16127 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
16128 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
16132 @node Cleaning Up Using gnatclean
16133 @chapter Cleaning Up Using @code{gnatclean}
16135 @cindex Cleaning tool
16138 @code{gnatclean} is a tool that allows the deletion of files produced by the
16139 compiler, binder and linker, including ALI files, object files, tree files,
16140 expanded source files, library files, interface copy source files, binder
16141 generated files and executable files.
16144 * Running gnatclean::
16145 * Switches for gnatclean::
16146 * Examples of gnatclean Usage::
16149 @node Running gnatclean
16150 @section Running @code{gnatclean}
16153 The @code{gnatclean} command has the form:
16156 $ gnatclean switches @var{names}
16160 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
16161 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
16162 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
16165 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
16166 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
16167 the linker. In informative-only mode, specified by switch
16168 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
16169 normal mode is listed, but no file is actually deleted.
16171 @node Switches for gnatclean
16172 @section Switches for @code{gnatclean}
16175 @code{gnatclean} recognizes the following switches:
16179 @item ^-c^/COMPILER_FILES_ONLY^
16180 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
16181 Only attempt to delete the files produced by the compiler, not those produced
16182 by the binder or the linker. The files that are not to be deleted are library
16183 files, interface copy files, binder generated files and executable files.
16185 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
16186 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
16187 Indicate that ALI and object files should normally be found in directory
16190 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
16191 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
16192 When using project files, if some errors or warnings are detected during
16193 parsing and verbose mode is not in effect (no use of switch
16194 ^-v^/VERBOSE^), then error lines start with the full path name of the project
16195 file, rather than its simple file name.
16198 @cindex @option{^-h^/HELP^} (@code{gnatclean})
16199 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
16201 @item ^-n^/NODELETE^
16202 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
16203 Informative-only mode. Do not delete any files. Output the list of the files
16204 that would have been deleted if this switch was not specified.
16206 @item ^-P^/PROJECT_FILE=^@var{project}
16207 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
16208 Use project file @var{project}. Only one such switch can be used.
16209 When cleaning a project file, the files produced by the compilation of the
16210 immediate sources or inherited sources of the project files are to be
16211 deleted. This is not depending on the presence or not of executable names
16212 on the command line.
16215 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
16216 Quiet output. If there are no error, do not ouuput anything, except in
16217 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
16218 (switch ^-n^/NODELETE^).
16220 @item ^-r^/RECURSIVE^
16221 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
16222 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
16223 clean all imported and extended project files, recursively. If this switch
16224 is not specified, only the files related to the main project file are to be
16225 deleted. This switch has no effect if no project file is specified.
16227 @item ^-v^/VERBOSE^
16228 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
16231 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
16232 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
16233 Indicates the verbosity of the parsing of GNAT project files.
16234 See @ref{Switches Related to Project Files}.
16236 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
16237 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
16238 Indicates that external variable @var{name} has the value @var{value}.
16239 The Project Manager will use this value for occurrences of
16240 @code{external(name)} when parsing the project file.
16241 See @ref{Switches Related to Project Files}.
16243 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
16244 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
16245 When searching for ALI and object files, look in directory
16248 @item ^-I^/SEARCH=^@var{dir}
16249 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
16250 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
16252 @item ^-I-^/NOCURRENT_DIRECTORY^
16253 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
16254 @cindex Source files, suppressing search
16255 Do not look for ALI or object files in the directory
16256 where @code{gnatclean} was invoked.
16260 @node Examples of gnatclean Usage
16261 @section Examples of @code{gnatclean} Usage
16264 @node GNAT and Libraries
16265 @chapter GNAT and Libraries
16266 @cindex Library, building, installing
16269 This chapter addresses some of the issues related to building and using
16270 a library with GNAT. It also shows how the GNAT run-time library can be
16274 * Creating an Ada Library::
16275 * Installing an Ada Library::
16276 * Using an Ada Library::
16277 * Creating an Ada Library to be Used in a Non-Ada Context::
16278 * Rebuilding the GNAT Run-Time Library::
16281 @node Creating an Ada Library
16282 @section Creating an Ada Library
16285 In the GNAT environment, a library has two components:
16290 Compiled code and Ali files. See @ref{The Ada Library Information Files}.
16294 In order to use other packages @ref{The GNAT Compilation Model}
16295 requires a certain number of sources to be available to the compiler.
16297 sources required includes the specs of all the packages that make up the
16298 visible part of the library as well as all the sources upon which they
16299 depend. The bodies of all visible generic units must also be provided.
16301 Although it is not strictly mandatory, it is recommended that all sources
16302 needed to recompile the library be provided, so that the user can make
16303 full use of inter-unit inlining and source-level debugging. This can also
16304 make the situation easier for users that need to upgrade their compilation
16305 toolchain and thus need to recompile the library from sources.
16308 The compiled code can be provided in different ways. The simplest way is
16309 to provide directly the set of objects produced by the compiler during
16310 the compilation of the library. It is also possible to group the objects
16311 into an archive using whatever commands are provided by the operating
16312 system. Finally, it is also possible to create a shared library (see
16313 option -shared in the GCC manual).
16316 There are various possibilities for compiling the units that make up the
16317 library: for example with a Makefile @ref{Using the GNU make Utility},
16318 or with a conventional script.
16319 For simple libraries, it is also possible to create a
16320 dummy main program which depends upon all the packages that comprise the
16321 interface of the library. This dummy main program can then be given to
16322 gnatmake, in order to build all the necessary objects. Here is an example
16323 of such a dummy program and the generic commands used to build an
16324 archive or a shared library.
16326 @smallexample @c ada
16330 with My_Lib.Service1;
16331 with My_Lib.Service2;
16332 with My_Lib.Service3;
16333 procedure My_Lib_Dummy is
16340 # compiling the library
16341 $ gnatmake -c my_lib_dummy.adb
16343 # we don't need the dummy object itself
16344 $ rm my_lib_dummy.o my_lib_dummy.ali
16346 # create an archive with the remaining objects
16347 $ ar rc libmy_lib.a *.o
16348 # some systems may require "ranlib" to be run as well
16350 # or create a shared library
16351 $ gcc -shared -o libmy_lib.so *.o
16352 # some systems may require the code to have been compiled with -fPIC
16354 # remove the object files that are now in the library
16357 # Make the ALI files read-only so that gnatmake will not try to
16358 # regenerate the objects that are in the library
16364 When the objects are grouped in an archive or a shared library, the user
16365 needs to specify the desired library at link time, unless a pragma
16366 linker_options has been used in one of the sources:
16367 @smallexample @c ada
16368 pragma Linker_Options ("-lmy_lib");
16372 Please note that the library must have a name of the form libxxx.a or
16373 libxxx.so in order to be accessed by the directive -lxxx at link
16376 @node Installing an Ada Library
16377 @section Installing an Ada Library
16380 In the GNAT model, installing a library consists in copying into a specific
16381 location the files that make up this library. It is possible to install
16382 the sources in a different directory from the other files (ALI, objects,
16383 archives) since the source path and the object path can easily be
16384 specified separately.
16387 For general purpose libraries, it is possible for the system
16388 administrator to put those libraries in the default compiler paths. To
16389 achieve this, he must specify their location in the configuration files
16390 @file{ada_source_path} and @file{ada_object_path} that must be located in
16392 installation tree at the same place as the gcc spec file. The location of
16393 the gcc spec file can be determined as follows:
16399 The configuration files mentioned above have simple format: each line in them
16400 must contain one unique
16401 directory name. Those names are added to the corresponding path
16402 in their order of appearance in the file. The names can be either absolute
16403 or relative, in the latter case, they are relative to where theses files
16407 @file{ada_source_path} and @file{ada_object_path} might actually not be
16409 GNAT installation, in which case, GNAT will look for its run-time library in
16410 he directories @file{adainclude} for the sources and @file{adalib} for the
16411 objects and @file{ALI} files. When the files exist, the compiler does not
16412 look in @file{adainclude} and @file{adalib} at all, and thus the
16413 @file{ada_source_path} file
16414 must contain the location for the GNAT run-time sources (which can simply
16415 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
16416 contain the location for the GNAT run-time objects (which can simply
16420 You can also specify a new default path to the runtime library at compilation
16421 time with the switch @option{--RTS=rts-path}. You can easily choose and change
16422 the runtime you want your program to be compiled with. This switch is
16423 recognized by gcc, gnatmake, gnatbind, gnatls, gnatfind and gnatxref.
16426 It is possible to install a library before or after the standard GNAT
16427 library, by reordering the lines in the configuration files. In general, a
16428 library must be installed before the GNAT library if it redefines
16431 @node Using an Ada Library
16432 @section Using an Ada Library
16435 In order to use a Ada library, you need to make sure that this
16436 library is on both your source and object path
16437 @ref{Search Paths and the Run-Time Library (RTL)}
16438 and @ref{Search Paths for gnatbind}. For
16439 instance, you can use the library @file{mylib} installed in
16440 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
16443 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
16448 This can be simplified down to the following:
16452 when the following conditions are met:
16455 @file{/dir/my_lib_src} has been added by the user to the environment
16456 variable @code{ADA_INCLUDE_PATH}, or by the administrator to the file
16457 @file{ada_source_path}
16459 @file{/dir/my_lib_obj} has been added by the user to the environment
16460 variable @code{ADA_OBJECTS_PATH}, or by the administrator to the file
16461 @file{ada_object_path}
16463 a pragma @code{Linker_Options}, as mentioned in @ref{Creating an Ada Library},
16464 has been added to the sources.
16468 @node Creating an Ada Library to be Used in a Non-Ada Context
16469 @section Creating an Ada Library to be Used in a Non-Ada Context
16472 The previous sections detailed how to create and install a library that
16473 was usable from an Ada main program. Using this library in a non-Ada
16474 context is not possible, because the elaboration of the library is
16475 automatically done as part of the main program elaboration.
16477 GNAT also provides the ability to build libraries that can be used both
16478 in an Ada and non-Ada context. This section describes how to build such
16479 a library, and then how to use it from a C program. The method for
16480 interfacing with the library from other languages such as Fortran for
16481 instance remains the same.
16483 @subsection Creating the Library
16486 @item Identify the units representing the interface of the library.
16488 Here is an example of simple library interface:
16490 @smallexample @c ada
16491 package Interface is
16493 procedure Do_Something;
16495 procedure Do_Something_Else;
16500 @item Use @code{pragma Export} or @code{pragma Convention} for the
16503 Our package @code{Interface} is then updated as follow:
16504 @smallexample @c ada
16505 package Interface is
16507 procedure Do_Something;
16508 pragma Export (C, Do_Something, "do_something");
16510 procedure Do_Something_Else;
16511 pragma Export (C, Do_Something_Else, "do_something_else");
16516 @item Compile all the units composing the library.
16518 @item Bind the library objects.
16520 This step is performed by invoking gnatbind with the @option{-L<prefix>}
16521 switch. @code{gnatbind} will then generate the library elaboration
16522 procedure (named @code{<prefix>init}) and the run-time finalization
16523 procedure (named @code{<prefix>final}).
16526 # generate the binder file in Ada
16527 $ gnatbind -Lmylib interface
16529 # generate the binder file in C
16530 $ gnatbind -C -Lmylib interface
16533 @item Compile the files generated by the binder
16536 $ gcc -c b~interface.adb
16539 @item Create the library;
16541 The procedure is identical to the procedure explained in
16542 @ref{Creating an Ada Library},
16543 except that @file{b~interface.o} needs to be added to
16544 the list of objects.
16547 # create an archive file
16548 $ ar cr libmylib.a b~interface.o <other object files>
16550 # create a shared library
16551 $ gcc -shared -o libmylib.so b~interface.o <other object files>
16554 @item Provide a ``foreign'' view of the library interface;
16556 The example below shows the content of @code{mylib_interface.h} (note
16557 that there is no rule for the naming of this file, any name can be used)
16559 /* the library elaboration procedure */
16560 extern void mylibinit (void);
16562 /* the library finalization procedure */
16563 extern void mylibfinal (void);
16565 /* the interface exported by the library */
16566 extern void do_something (void);
16567 extern void do_something_else (void);
16571 @subsection Using the Library
16574 Libraries built as explained above can be used from any program, provided
16575 that the elaboration procedures (named @code{mylibinit} in the previous
16576 example) are called before the library services are used. Any number of
16577 libraries can be used simultaneously, as long as the elaboration
16578 procedure of each library is called.
16580 Below is an example of C program that uses our @code{mylib} library.
16583 #include "mylib_interface.h"
16588 /* First, elaborate the library before using it */
16591 /* Main program, using the library exported entities */
16593 do_something_else ();
16595 /* Library finalization at the end of the program */
16602 Note that this same library can be used from an equivalent Ada main
16603 program. In addition, if the libraries are installed as detailed in
16604 @ref{Installing an Ada Library}, it is not necessary to invoke the
16605 library elaboration and finalization routines. The binder will ensure
16606 that this is done as part of the main program elaboration and
16607 finalization phases.
16609 @subsection The Finalization Phase
16612 Invoking any library finalization procedure generated by @code{gnatbind}
16613 shuts down the Ada run time permanently. Consequently, the finalization
16614 of all Ada libraries must be performed at the end of the program. No
16615 call to these libraries nor the Ada run time should be made past the
16616 finalization phase.
16618 @subsection Restrictions in Libraries
16621 The pragmas listed below should be used with caution inside libraries,
16622 as they can create incompatibilities with other Ada libraries:
16624 @item pragma @code{Locking_Policy}
16625 @item pragma @code{Queuing_Policy}
16626 @item pragma @code{Task_Dispatching_Policy}
16627 @item pragma @code{Unreserve_All_Interrupts}
16629 When using a library that contains such pragmas, the user must make sure
16630 that all libraries use the same pragmas with the same values. Otherwise,
16631 a @code{Program_Error} will
16632 be raised during the elaboration of the conflicting
16633 libraries. The usage of these pragmas and its consequences for the user
16634 should therefore be well documented.
16636 Similarly, the traceback in exception occurrences mechanism should be
16637 enabled or disabled in a consistent manner across all libraries.
16638 Otherwise, a Program_Error will be raised during the elaboration of the
16639 conflicting libraries.
16641 If the @code{'Version} and @code{'Body_Version}
16642 attributes are used inside a library, then it is necessary to
16643 perform a @code{gnatbind} step that mentions all @file{ALI} files in all
16644 libraries, so that version identifiers can be properly computed.
16645 In practice these attributes are rarely used, so this is unlikely
16646 to be a consideration.
16648 @node Rebuilding the GNAT Run-Time Library
16649 @section Rebuilding the GNAT Run-Time Library
16652 It may be useful to recompile the GNAT library in various contexts, the
16653 most important one being the use of partition-wide configuration pragmas
16654 such as Normalize_Scalar. A special Makefile called
16655 @code{Makefile.adalib} is provided to that effect and can be found in
16656 the directory containing the GNAT library. The location of this
16657 directory depends on the way the GNAT environment has been installed and can
16658 be determined by means of the command:
16665 The last entry in the object search path usually contains the
16666 gnat library. This Makefile contains its own documentation and in
16667 particular the set of instructions needed to rebuild a new library and
16670 @node Using the GNU make Utility
16671 @chapter Using the GNU @code{make} Utility
16675 This chapter offers some examples of makefiles that solve specific
16676 problems. It does not explain how to write a makefile (see the GNU make
16677 documentation), nor does it try to replace the @code{gnatmake} utility
16678 (@pxref{The GNAT Make Program gnatmake}).
16680 All the examples in this section are specific to the GNU version of
16681 make. Although @code{make} is a standard utility, and the basic language
16682 is the same, these examples use some advanced features found only in
16686 * Using gnatmake in a Makefile::
16687 * Automatically Creating a List of Directories::
16688 * Generating the Command Line Switches::
16689 * Overcoming Command Line Length Limits::
16692 @node Using gnatmake in a Makefile
16693 @section Using gnatmake in a Makefile
16698 Complex project organizations can be handled in a very powerful way by
16699 using GNU make combined with gnatmake. For instance, here is a Makefile
16700 which allows you to build each subsystem of a big project into a separate
16701 shared library. Such a makefile allows you to significantly reduce the link
16702 time of very big applications while maintaining full coherence at
16703 each step of the build process.
16705 The list of dependencies are handled automatically by
16706 @code{gnatmake}. The Makefile is simply used to call gnatmake in each of
16707 the appropriate directories.
16709 Note that you should also read the example on how to automatically
16710 create the list of directories
16711 (@pxref{Automatically Creating a List of Directories})
16712 which might help you in case your project has a lot of subdirectories.
16717 @font@heightrm=cmr8
16720 ## This Makefile is intended to be used with the following directory
16722 ## - The sources are split into a series of csc (computer software components)
16723 ## Each of these csc is put in its own directory.
16724 ## Their name are referenced by the directory names.
16725 ## They will be compiled into shared library (although this would also work
16726 ## with static libraries
16727 ## - The main program (and possibly other packages that do not belong to any
16728 ## csc is put in the top level directory (where the Makefile is).
16729 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
16730 ## \_ second_csc (sources) __ lib (will contain the library)
16732 ## Although this Makefile is build for shared library, it is easy to modify
16733 ## to build partial link objects instead (modify the lines with -shared and
16736 ## With this makefile, you can change any file in the system or add any new
16737 ## file, and everything will be recompiled correctly (only the relevant shared
16738 ## objects will be recompiled, and the main program will be re-linked).
16740 # The list of computer software component for your project. This might be
16741 # generated automatically.
16744 # Name of the main program (no extension)
16747 # If we need to build objects with -fPIC, uncomment the following line
16750 # The following variable should give the directory containing libgnat.so
16751 # You can get this directory through 'gnatls -v'. This is usually the last
16752 # directory in the Object_Path.
16755 # The directories for the libraries
16756 # (This macro expands the list of CSC to the list of shared libraries, you
16757 # could simply use the expanded form :
16758 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
16759 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
16761 $@{MAIN@}: objects $@{LIB_DIR@}
16762 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
16763 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
16766 # recompile the sources
16767 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
16769 # Note: In a future version of GNAT, the following commands will be simplified
16770 # by a new tool, gnatmlib
16772 mkdir -p $@{dir $@@ @}
16773 cd $@{dir $@@ @}; gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
16774 cd $@{dir $@@ @}; cp -f ../*.ali .
16776 # The dependencies for the modules
16777 # Note that we have to force the expansion of *.o, since in some cases
16778 # make won't be able to do it itself.
16779 aa/lib/libaa.so: $@{wildcard aa/*.o@}
16780 bb/lib/libbb.so: $@{wildcard bb/*.o@}
16781 cc/lib/libcc.so: $@{wildcard cc/*.o@}
16783 # Make sure all of the shared libraries are in the path before starting the
16786 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
16789 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
16790 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
16791 $@{RM@} $@{CSC_LIST:%=%/*.o@}
16792 $@{RM@} *.o *.ali $@{MAIN@}
16795 @node Automatically Creating a List of Directories
16796 @section Automatically Creating a List of Directories
16799 In most makefiles, you will have to specify a list of directories, and
16800 store it in a variable. For small projects, it is often easier to
16801 specify each of them by hand, since you then have full control over what
16802 is the proper order for these directories, which ones should be
16805 However, in larger projects, which might involve hundreds of
16806 subdirectories, it might be more convenient to generate this list
16809 The example below presents two methods. The first one, although less
16810 general, gives you more control over the list. It involves wildcard
16811 characters, that are automatically expanded by @code{make}. Its
16812 shortcoming is that you need to explicitly specify some of the
16813 organization of your project, such as for instance the directory tree
16814 depth, whether some directories are found in a separate tree,...
16816 The second method is the most general one. It requires an external
16817 program, called @code{find}, which is standard on all Unix systems. All
16818 the directories found under a given root directory will be added to the
16824 @font@heightrm=cmr8
16827 # The examples below are based on the following directory hierarchy:
16828 # All the directories can contain any number of files
16829 # ROOT_DIRECTORY -> a -> aa -> aaa
16832 # -> b -> ba -> baa
16835 # This Makefile creates a variable called DIRS, that can be reused any time
16836 # you need this list (see the other examples in this section)
16838 # The root of your project's directory hierarchy
16842 # First method: specify explicitly the list of directories
16843 # This allows you to specify any subset of all the directories you need.
16846 DIRS := a/aa/ a/ab/ b/ba/
16849 # Second method: use wildcards
16850 # Note that the argument(s) to wildcard below should end with a '/'.
16851 # Since wildcards also return file names, we have to filter them out
16852 # to avoid duplicate directory names.
16853 # We thus use make's @code{dir} and @code{sort} functions.
16854 # It sets DIRs to the following value (note that the directories aaa and baa
16855 # are not given, unless you change the arguments to wildcard).
16856 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
16859 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
16860 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
16863 # Third method: use an external program
16864 # This command is much faster if run on local disks, avoiding NFS slowdowns.
16865 # This is the most complete command: it sets DIRs to the following value:
16866 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
16869 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
16873 @node Generating the Command Line Switches
16874 @section Generating the Command Line Switches
16877 Once you have created the list of directories as explained in the
16878 previous section (@pxref{Automatically Creating a List of Directories}),
16879 you can easily generate the command line arguments to pass to gnatmake.
16881 For the sake of completeness, this example assumes that the source path
16882 is not the same as the object path, and that you have two separate lists
16886 # see "Automatically creating a list of directories" to create
16891 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
16892 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
16895 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
16898 @node Overcoming Command Line Length Limits
16899 @section Overcoming Command Line Length Limits
16902 One problem that might be encountered on big projects is that many
16903 operating systems limit the length of the command line. It is thus hard to give
16904 gnatmake the list of source and object directories.
16906 This example shows how you can set up environment variables, which will
16907 make @code{gnatmake} behave exactly as if the directories had been
16908 specified on the command line, but have a much higher length limit (or
16909 even none on most systems).
16911 It assumes that you have created a list of directories in your Makefile,
16912 using one of the methods presented in
16913 @ref{Automatically Creating a List of Directories}.
16914 For the sake of completeness, we assume that the object
16915 path (where the ALI files are found) is different from the sources patch.
16917 Note a small trick in the Makefile below: for efficiency reasons, we
16918 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
16919 expanded immediately by @code{make}. This way we overcome the standard
16920 make behavior which is to expand the variables only when they are
16923 On Windows, if you are using the standard Windows command shell, you must
16924 replace colons with semicolons in the assignments to these variables.
16929 @font@heightrm=cmr8
16932 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
16933 # This is the same thing as putting the -I arguments on the command line.
16934 # (the equivalent of using -aI on the command line would be to define
16935 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
16936 # You can of course have different values for these variables.
16938 # Note also that we need to keep the previous values of these variables, since
16939 # they might have been set before running 'make' to specify where the GNAT
16940 # library is installed.
16942 # see "Automatically creating a list of directories" to create these
16948 space:=$@{empty@} $@{empty@}
16949 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
16950 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
16951 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
16952 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
16953 export ADA_INCLUDE_PATH
16954 export ADA_OBJECT_PATH
16962 @node Finding Memory Problems
16963 @chapter Finding Memory Problems
16966 This chapter describes
16968 the @command{gnatmem} tool, which can be used to track down
16969 ``memory leaks'', and
16971 the GNAT Debug Pool facility, which can be used to detect incorrect uses of
16972 access values (including ``dangling references'').
16976 * The gnatmem Tool::
16978 * The GNAT Debug Pool Facility::
16983 @node The gnatmem Tool
16984 @section The @command{gnatmem} Tool
16988 The @code{gnatmem} utility monitors dynamic allocation and
16989 deallocation activity in a program, and displays information about
16990 incorrect deallocations and possible sources of memory leaks.
16991 It provides three type of information:
16994 General information concerning memory management, such as the total
16995 number of allocations and deallocations, the amount of allocated
16996 memory and the high water mark, i.e. the largest amount of allocated
16997 memory in the course of program execution.
17000 Backtraces for all incorrect deallocations, that is to say deallocations
17001 which do not correspond to a valid allocation.
17004 Information on each allocation that is potentially the origin of a memory
17009 * Running gnatmem::
17010 * Switches for gnatmem::
17011 * Example of gnatmem Usage::
17014 @node Running gnatmem
17015 @subsection Running @code{gnatmem}
17018 @code{gnatmem} makes use of the output created by the special version of
17019 allocation and deallocation routines that record call information. This
17020 allows to obtain accurate dynamic memory usage history at a minimal cost to
17021 the execution speed. Note however, that @code{gnatmem} is not supported on
17022 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux x86,
17023 Solaris (sparc and x86) and Windows NT/2000/XP (x86).
17026 The @code{gnatmem} command has the form
17029 $ gnatmem [switches] user_program
17033 The program must have been linked with the instrumented version of the
17034 allocation and deallocation routines. This is done by linking with the
17035 @file{libgmem.a} library. For correct symbolic backtrace information,
17036 the user program should be compiled with debugging options
17037 @ref{Switches for gcc}. For example to build @file{my_program}:
17040 $ gnatmake -g my_program -largs -lgmem
17044 When running @file{my_program} the file @file{gmem.out} is produced. This file
17045 contains information about all allocations and deallocations done by the
17046 program. It is produced by the instrumented allocations and
17047 deallocations routines and will be used by @code{gnatmem}.
17050 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
17051 examine. If the location of @file{gmem.out} file was not explicitly supplied by
17052 @code{-i} switch, gnatmem will assume that this file can be found in the
17053 current directory. For example, after you have executed @file{my_program},
17054 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
17057 $ gnatmem my_program
17061 This will produce the output with the following format:
17063 *************** debut cc
17065 $ gnatmem my_program
17069 Total number of allocations : 45
17070 Total number of deallocations : 6
17071 Final Water Mark (non freed mem) : 11.29 Kilobytes
17072 High Water Mark : 11.40 Kilobytes
17077 Allocation Root # 2
17078 -------------------
17079 Number of non freed allocations : 11
17080 Final Water Mark (non freed mem) : 1.16 Kilobytes
17081 High Water Mark : 1.27 Kilobytes
17083 my_program.adb:23 my_program.alloc
17089 The first block of output gives general information. In this case, the
17090 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
17091 Unchecked_Deallocation routine occurred.
17094 Subsequent paragraphs display information on all allocation roots.
17095 An allocation root is a specific point in the execution of the program
17096 that generates some dynamic allocation, such as a ``@code{@b{new}}''
17097 construct. This root is represented by an execution backtrace (or subprogram
17098 call stack). By default the backtrace depth for allocations roots is 1, so
17099 that a root corresponds exactly to a source location. The backtrace can
17100 be made deeper, to make the root more specific.
17102 @node Switches for gnatmem
17103 @subsection Switches for @code{gnatmem}
17106 @code{gnatmem} recognizes the following switches:
17111 @cindex @option{-q} (@code{gnatmem})
17112 Quiet. Gives the minimum output needed to identify the origin of the
17113 memory leaks. Omits statistical information.
17116 @cindex @var{N} (@code{gnatmem})
17117 N is an integer literal (usually between 1 and 10) which controls the
17118 depth of the backtraces defining allocation root. The default value for
17119 N is 1. The deeper the backtrace, the more precise the localization of
17120 the root. Note that the total number of roots can depend on this
17121 parameter. This parameter must be specified @emph{before} the name of the
17122 executable to be analyzed, to avoid ambiguity.
17125 @cindex @option{-b} (@code{gnatmem})
17126 This switch has the same effect as just depth parameter.
17128 @item -i @var{file}
17129 @cindex @option{-i} (@code{gnatmem})
17130 Do the @code{gnatmem} processing starting from @file{file}, rather than
17131 @file{gmem.out} in the current directory.
17134 @cindex @option{-m} (@code{gnatmem})
17135 This switch causes @code{gnatmem} to mask the allocation roots that have less
17136 than n leaks. The default value is 1. Specifying the value of 0 will allow to
17137 examine even the roots that didn't result in leaks.
17140 @cindex @option{-s} (@code{gnatmem})
17141 This switch causes @code{gnatmem} to sort the allocation roots according to the
17142 specified order of sort criteria, each identified by a single letter. The
17143 currently supported criteria are @code{n, h, w} standing respectively for
17144 number of unfreed allocations, high watermark, and final watermark
17145 corresponding to a specific root. The default order is @code{nwh}.
17149 @node Example of gnatmem Usage
17150 @subsection Example of @code{gnatmem} Usage
17153 The following example shows the use of @code{gnatmem}
17154 on a simple memory-leaking program.
17155 Suppose that we have the following Ada program:
17157 @smallexample @c ada
17160 with Unchecked_Deallocation;
17161 procedure Test_Gm is
17163 type T is array (1..1000) of Integer;
17164 type Ptr is access T;
17165 procedure Free is new Unchecked_Deallocation (T, Ptr);
17168 procedure My_Alloc is
17173 procedure My_DeAlloc is
17181 for I in 1 .. 5 loop
17182 for J in I .. 5 loop
17193 The program needs to be compiled with debugging option and linked with
17194 @code{gmem} library:
17197 $ gnatmake -g test_gm -largs -lgmem
17201 Then we execute the program as usual:
17208 Then @code{gnatmem} is invoked simply with
17214 which produces the following output (result may vary on different platforms):
17219 Total number of allocations : 18
17220 Total number of deallocations : 5
17221 Final Water Mark (non freed mem) : 53.00 Kilobytes
17222 High Water Mark : 56.90 Kilobytes
17224 Allocation Root # 1
17225 -------------------
17226 Number of non freed allocations : 11
17227 Final Water Mark (non freed mem) : 42.97 Kilobytes
17228 High Water Mark : 46.88 Kilobytes
17230 test_gm.adb:11 test_gm.my_alloc
17232 Allocation Root # 2
17233 -------------------
17234 Number of non freed allocations : 1
17235 Final Water Mark (non freed mem) : 10.02 Kilobytes
17236 High Water Mark : 10.02 Kilobytes
17238 s-secsta.adb:81 system.secondary_stack.ss_init
17240 Allocation Root # 3
17241 -------------------
17242 Number of non freed allocations : 1
17243 Final Water Mark (non freed mem) : 12 Bytes
17244 High Water Mark : 12 Bytes
17246 s-secsta.adb:181 system.secondary_stack.ss_init
17250 Note that the GNAT run time contains itself a certain number of
17251 allocations that have no corresponding deallocation,
17252 as shown here for root #2 and root
17253 #3. This is a normal behavior when the number of non freed allocations
17254 is one, it allocates dynamic data structures that the run time needs for
17255 the complete lifetime of the program. Note also that there is only one
17256 allocation root in the user program with a single line back trace:
17257 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
17258 program shows that 'My_Alloc' is called at 2 different points in the
17259 source (line 21 and line 24). If those two allocation roots need to be
17260 distinguished, the backtrace depth parameter can be used:
17263 $ gnatmem 3 test_gm
17267 which will give the following output:
17272 Total number of allocations : 18
17273 Total number of deallocations : 5
17274 Final Water Mark (non freed mem) : 53.00 Kilobytes
17275 High Water Mark : 56.90 Kilobytes
17277 Allocation Root # 1
17278 -------------------
17279 Number of non freed allocations : 10
17280 Final Water Mark (non freed mem) : 39.06 Kilobytes
17281 High Water Mark : 42.97 Kilobytes
17283 test_gm.adb:11 test_gm.my_alloc
17284 test_gm.adb:24 test_gm
17285 b_test_gm.c:52 main
17287 Allocation Root # 2
17288 -------------------
17289 Number of non freed allocations : 1
17290 Final Water Mark (non freed mem) : 10.02 Kilobytes
17291 High Water Mark : 10.02 Kilobytes
17293 s-secsta.adb:81 system.secondary_stack.ss_init
17294 s-secsta.adb:283 <system__secondary_stack___elabb>
17295 b_test_gm.c:33 adainit
17297 Allocation Root # 3
17298 -------------------
17299 Number of non freed allocations : 1
17300 Final Water Mark (non freed mem) : 3.91 Kilobytes
17301 High Water Mark : 3.91 Kilobytes
17303 test_gm.adb:11 test_gm.my_alloc
17304 test_gm.adb:21 test_gm
17305 b_test_gm.c:52 main
17307 Allocation Root # 4
17308 -------------------
17309 Number of non freed allocations : 1
17310 Final Water Mark (non freed mem) : 12 Bytes
17311 High Water Mark : 12 Bytes
17313 s-secsta.adb:181 system.secondary_stack.ss_init
17314 s-secsta.adb:283 <system__secondary_stack___elabb>
17315 b_test_gm.c:33 adainit
17319 The allocation root #1 of the first example has been split in 2 roots #1
17320 and #3 thanks to the more precise associated backtrace.
17325 @node The GNAT Debug Pool Facility
17326 @section The GNAT Debug Pool Facility
17328 @cindex storage, pool, memory corruption
17331 The use of unchecked deallocation and unchecked conversion can easily
17332 lead to incorrect memory references. The problems generated by such
17333 references are usually difficult to tackle because the symptoms can be
17334 very remote from the origin of the problem. In such cases, it is
17335 very helpful to detect the problem as early as possible. This is the
17336 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
17338 In order to use the GNAT specific debugging pool, the user must
17339 associate a debug pool object with each of the access types that may be
17340 related to suspected memory problems. See Ada Reference Manual 13.11.
17341 @smallexample @c ada
17342 type Ptr is access Some_Type;
17343 Pool : GNAT.Debug_Pools.Debug_Pool;
17344 for Ptr'Storage_Pool use Pool;
17348 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
17349 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
17350 allow the user to redefine allocation and deallocation strategies. They
17351 also provide a checkpoint for each dereference, through the use of
17352 the primitive operation @code{Dereference} which is implicitly called at
17353 each dereference of an access value.
17355 Once an access type has been associated with a debug pool, operations on
17356 values of the type may raise four distinct exceptions,
17357 which correspond to four potential kinds of memory corruption:
17360 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
17362 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
17364 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
17366 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
17370 For types associated with a Debug_Pool, dynamic allocation is performed using
17372 GNAT allocation routine. References to all allocated chunks of memory
17373 are kept in an internal dictionary.
17374 Several deallocation strategies are provided, whereupon the user can choose
17375 to release the memory to the system, keep it allocated for further invalid
17376 access checks, or fill it with an easily recognizable pattern for debug
17378 The memory pattern is the old IBM hexadecimal convention: @code{16#DEADBEEF#}.
17380 See the documentation in the file g-debpoo.ads for more information on the
17381 various strategies.
17383 Upon each dereference, a check is made that the access value denotes a
17384 properly allocated memory location. Here is a complete example of use of
17385 @code{Debug_Pools}, that includes typical instances of memory corruption:
17386 @smallexample @c ada
17390 with Gnat.Io; use Gnat.Io;
17391 with Unchecked_Deallocation;
17392 with Unchecked_Conversion;
17393 with GNAT.Debug_Pools;
17394 with System.Storage_Elements;
17395 with Ada.Exceptions; use Ada.Exceptions;
17396 procedure Debug_Pool_Test is
17398 type T is access Integer;
17399 type U is access all T;
17401 P : GNAT.Debug_Pools.Debug_Pool;
17402 for T'Storage_Pool use P;
17404 procedure Free is new Unchecked_Deallocation (Integer, T);
17405 function UC is new Unchecked_Conversion (U, T);
17408 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
17418 Put_Line (Integer'Image(B.all));
17420 when E : others => Put_Line ("raised: " & Exception_Name (E));
17425 when E : others => Put_Line ("raised: " & Exception_Name (E));
17429 Put_Line (Integer'Image(B.all));
17431 when E : others => Put_Line ("raised: " & Exception_Name (E));
17436 when E : others => Put_Line ("raised: " & Exception_Name (E));
17439 end Debug_Pool_Test;
17443 The debug pool mechanism provides the following precise diagnostics on the
17444 execution of this erroneous program:
17447 Total allocated bytes : 0
17448 Total deallocated bytes : 0
17449 Current Water Mark: 0
17453 Total allocated bytes : 8
17454 Total deallocated bytes : 0
17455 Current Water Mark: 8
17458 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
17459 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
17460 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
17461 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
17463 Total allocated bytes : 8
17464 Total deallocated bytes : 4
17465 Current Water Mark: 4
17470 @node Creating Sample Bodies Using gnatstub
17471 @chapter Creating Sample Bodies Using @command{gnatstub}
17475 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
17476 for library unit declarations.
17478 To create a body stub, @command{gnatstub} has to compile the library
17479 unit declaration. Therefore, bodies can be created only for legal
17480 library units. Moreover, if a library unit depends semantically upon
17481 units located outside the current directory, you have to provide
17482 the source search path when calling @command{gnatstub}, see the description
17483 of @command{gnatstub} switches below.
17486 * Running gnatstub::
17487 * Switches for gnatstub::
17490 @node Running gnatstub
17491 @section Running @command{gnatstub}
17494 @command{gnatstub} has the command-line interface of the form
17497 $ gnatstub [switches] filename [directory]
17504 is the name of the source file that contains a library unit declaration
17505 for which a body must be created. The file name may contain the path
17507 The file name does not have to follow the GNAT file name conventions. If the
17509 does not follow GNAT file naming conventions, the name of the body file must
17511 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
17512 If the file name follows the GNAT file naming
17513 conventions and the name of the body file is not provided,
17516 of the body file from the argument file name by replacing the @file{.ads}
17518 with the @file{.adb} suffix.
17521 indicates the directory in which the body stub is to be placed (the default
17526 is an optional sequence of switches as described in the next section
17529 @node Switches for gnatstub
17530 @section Switches for @command{gnatstub}
17536 @cindex @option{^-f^/FULL^} (@command{gnatstub})
17537 If the destination directory already contains a file with the name of the
17539 for the argument spec file, replace it with the generated body stub.
17541 @item ^-hs^/HEADER=SPEC^
17542 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
17543 Put the comment header (i.e., all the comments preceding the
17544 compilation unit) from the source of the library unit declaration
17545 into the body stub.
17547 @item ^-hg^/HEADER=GENERAL^
17548 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
17549 Put a sample comment header into the body stub.
17553 @cindex @option{-IDIR} (@command{gnatstub})
17555 @cindex @option{-I-} (@command{gnatstub})
17558 @item /NOCURRENT_DIRECTORY
17559 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
17561 ^These switches have ^This switch has^ the same meaning as in calls to
17563 ^They define ^It defines ^ the source search path in the call to
17564 @command{gcc} issued
17565 by @command{gnatstub} to compile an argument source file.
17567 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
17568 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
17569 This switch has the same meaning as in calls to @command{gcc}.
17570 It defines the additional configuration file to be passed to the call to
17571 @command{gcc} issued
17572 by @command{gnatstub} to compile an argument source file.
17574 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
17575 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
17576 (@var{n} is a non-negative integer). Set the maximum line length in the
17577 body stub to @var{n}; the default is 79. The maximum value that can be
17578 specified is 32767. Note that in the special case of configuration
17579 pragma files, the maximum is always 32767 regardless of whether or
17580 not this switch appears.
17582 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
17583 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
17584 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
17585 the generated body sample to @var{n}.
17586 The default indentation is 3.
17588 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
17589 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
17590 Order local bodies alphabetically. (By default local bodies are ordered
17591 in the same way as the corresponding local specs in the argument spec file.)
17593 @item ^-i^/INDENTATION=^@var{n}
17594 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
17595 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
17597 @item ^-k^/TREE_FILE=SAVE^
17598 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
17599 Do not remove the tree file (i.e., the snapshot of the compiler internal
17600 structures used by @command{gnatstub}) after creating the body stub.
17602 @item ^-l^/LINE_LENGTH=^@var{n}
17603 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
17604 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
17606 @item ^-o^/BODY=^@var{body-name}
17607 @cindex @option{^-o^/BODY^} (@command{gnatstub})
17608 Body file name. This should be set if the argument file name does not
17610 the GNAT file naming
17611 conventions. If this switch is omitted the default name for the body will be
17613 from the argument file name according to the GNAT file naming conventions.
17616 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
17617 Quiet mode: do not generate a confirmation when a body is
17618 successfully created, and do not generate a message when a body is not
17622 @item ^-r^/TREE_FILE=REUSE^
17623 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
17624 Reuse the tree file (if it exists) instead of creating it. Instead of
17625 creating the tree file for the library unit declaration, @command{gnatstub}
17626 tries to find it in the current directory and use it for creating
17627 a body. If the tree file is not found, no body is created. This option
17628 also implies @option{^-k^/SAVE^}, whether or not
17629 the latter is set explicitly.
17631 @item ^-t^/TREE_FILE=OVERWRITE^
17632 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
17633 Overwrite the existing tree file. If the current directory already
17634 contains the file which, according to the GNAT file naming rules should
17635 be considered as a tree file for the argument source file,
17637 will refuse to create the tree file needed to create a sample body
17638 unless this option is set.
17640 @item ^-v^/VERBOSE^
17641 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
17642 Verbose mode: generate version information.
17647 @node Other Utility Programs
17648 @chapter Other Utility Programs
17651 This chapter discusses some other utility programs available in the Ada
17655 * Using Other Utility Programs with GNAT::
17656 * The External Symbol Naming Scheme of GNAT::
17658 * Ada Mode for Glide::
17660 * Converting Ada Files to html with gnathtml::
17661 * Installing gnathtml::
17668 @node Using Other Utility Programs with GNAT
17669 @section Using Other Utility Programs with GNAT
17672 The object files generated by GNAT are in standard system format and in
17673 particular the debugging information uses this format. This means
17674 programs generated by GNAT can be used with existing utilities that
17675 depend on these formats.
17678 In general, any utility program that works with C will also often work with
17679 Ada programs generated by GNAT. This includes software utilities such as
17680 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
17684 @node The External Symbol Naming Scheme of GNAT
17685 @section The External Symbol Naming Scheme of GNAT
17688 In order to interpret the output from GNAT, when using tools that are
17689 originally intended for use with other languages, it is useful to
17690 understand the conventions used to generate link names from the Ada
17693 All link names are in all lowercase letters. With the exception of library
17694 procedure names, the mechanism used is simply to use the full expanded
17695 Ada name with dots replaced by double underscores. For example, suppose
17696 we have the following package spec:
17698 @smallexample @c ada
17709 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
17710 the corresponding link name is @code{qrs__mn}.
17712 Of course if a @code{pragma Export} is used this may be overridden:
17714 @smallexample @c ada
17719 pragma Export (Var1, C, External_Name => "var1_name");
17721 pragma Export (Var2, C, Link_Name => "var2_link_name");
17728 In this case, the link name for @var{Var1} is whatever link name the
17729 C compiler would assign for the C function @var{var1_name}. This typically
17730 would be either @var{var1_name} or @var{_var1_name}, depending on operating
17731 system conventions, but other possibilities exist. The link name for
17732 @var{Var2} is @var{var2_link_name}, and this is not operating system
17736 One exception occurs for library level procedures. A potential ambiguity
17737 arises between the required name @code{_main} for the C main program,
17738 and the name we would otherwise assign to an Ada library level procedure
17739 called @code{Main} (which might well not be the main program).
17741 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
17742 names. So if we have a library level procedure such as
17744 @smallexample @c ada
17747 procedure Hello (S : String);
17753 the external name of this procedure will be @var{_ada_hello}.
17756 @node Ada Mode for Glide
17757 @section Ada Mode for @code{Glide}
17758 @cindex Ada mode (for Glide)
17761 The Glide mode for programming in Ada (both Ada83 and Ada95) helps the
17762 user to understand and navigate existing code, and facilitates writing
17763 new code. It furthermore provides some utility functions for easier
17764 integration of standard Emacs features when programming in Ada.
17766 Its general features include:
17770 An Integrated Development Environment with functionality such as the
17775 ``Project files'' for configuration-specific aspects
17776 (e.g. directories and compilation options)
17779 Compiling and stepping through error messages.
17782 Running and debugging an applications within Glide.
17789 User configurability
17792 Some of the specific Ada mode features are:
17796 Functions for easy and quick stepping through Ada code
17799 Getting cross reference information for identifiers (e.g., finding a
17800 defining occurrence)
17803 Displaying an index menu of types and subprograms, allowing
17804 direct selection for browsing
17807 Automatic color highlighting of the various Ada entities
17810 Glide directly supports writing Ada code, via several facilities:
17814 Switching between spec and body files with possible
17815 autogeneration of body files
17818 Automatic formating of subprogram parameter lists
17821 Automatic indentation according to Ada syntax
17824 Automatic completion of identifiers
17827 Automatic (and configurable) casing of identifiers, keywords, and attributes
17830 Insertion of syntactic templates
17833 Block commenting / uncommenting
17837 For more information, please refer to the online documentation
17838 available in the @code{Glide} @result{} @code{Help} menu.
17842 @node Converting Ada Files to html with gnathtml
17843 @section Converting Ada Files to HTML with @code{gnathtml}
17846 This @code{Perl} script allows Ada source files to be browsed using
17847 standard Web browsers. For installation procedure, see the section
17848 @xref{Installing gnathtml}.
17850 Ada reserved keywords are highlighted in a bold font and Ada comments in
17851 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
17852 switch to suppress the generation of cross-referencing information, user
17853 defined variables and types will appear in a different color; you will
17854 be able to click on any identifier and go to its declaration.
17856 The command line is as follow:
17858 $ perl gnathtml.pl [switches] ada-files
17862 You can pass it as many Ada files as you want. @code{gnathtml} will generate
17863 an html file for every ada file, and a global file called @file{index.htm}.
17864 This file is an index of every identifier defined in the files.
17866 The available switches are the following ones :
17870 @cindex @option{-83} (@code{gnathtml})
17871 Only the subset on the Ada 83 keywords will be highlighted, not the full
17872 Ada 95 keywords set.
17874 @item -cc @var{color}
17875 @cindex @option{-cc} (@code{gnathtml})
17876 This option allows you to change the color used for comments. The default
17877 value is green. The color argument can be any name accepted by html.
17880 @cindex @option{-d} (@code{gnathtml})
17881 If the ada files depend on some other files (using for instance the
17882 @code{with} command, the latter will also be converted to html.
17883 Only the files in the user project will be converted to html, not the files
17884 in the run-time library itself.
17887 @cindex @option{-D} (@code{gnathtml})
17888 This command is the same as @option{-d} above, but @command{gnathtml} will
17889 also look for files in the run-time library, and generate html files for them.
17891 @item -ext @var{extension}
17892 @cindex @option{-ext} (@code{gnathtml})
17893 This option allows you to change the extension of the generated HTML files.
17894 If you do not specify an extension, it will default to @file{htm}.
17897 @cindex @option{-f} (@code{gnathtml})
17898 By default, gnathtml will generate html links only for global entities
17899 ('with'ed units, global variables and types,...). If you specify the
17900 @option{-f} on the command line, then links will be generated for local
17903 @item -l @var{number}
17904 @cindex @option{-l} (@code{gnathtml})
17905 If this switch is provided and @var{number} is not 0, then @code{gnathtml}
17906 will number the html files every @var{number} line.
17909 @cindex @option{-I} (@code{gnathtml})
17910 Specify a directory to search for library files (@file{.ALI} files) and
17911 source files. You can provide several -I switches on the command line,
17912 and the directories will be parsed in the order of the command line.
17915 @cindex @option{-o} (@code{gnathtml})
17916 Specify the output directory for html files. By default, gnathtml will
17917 saved the generated html files in a subdirectory named @file{html/}.
17919 @item -p @var{file}
17920 @cindex @option{-p} (@code{gnathtml})
17921 If you are using Emacs and the most recent Emacs Ada mode, which provides
17922 a full Integrated Development Environment for compiling, checking,
17923 running and debugging applications, you may use @file{.gpr} files
17924 to give the directories where Emacs can find sources and object files.
17926 Using this switch, you can tell gnathtml to use these files. This allows
17927 you to get an html version of your application, even if it is spread
17928 over multiple directories.
17930 @item -sc @var{color}
17931 @cindex @option{-sc} (@code{gnathtml})
17932 This option allows you to change the color used for symbol definitions.
17933 The default value is red. The color argument can be any name accepted by html.
17935 @item -t @var{file}
17936 @cindex @option{-t} (@code{gnathtml})
17937 This switch provides the name of a file. This file contains a list of
17938 file names to be converted, and the effect is exactly as though they had
17939 appeared explicitly on the command line. This
17940 is the recommended way to work around the command line length limit on some
17945 @node Installing gnathtml
17946 @section Installing @code{gnathtml}
17949 @code{Perl} needs to be installed on your machine to run this script.
17950 @code{Perl} is freely available for almost every architecture and
17951 Operating System via the Internet.
17953 On Unix systems, you may want to modify the first line of the script
17954 @code{gnathtml}, to explicitly tell the Operating system where Perl
17955 is. The syntax of this line is :
17957 #!full_path_name_to_perl
17961 Alternatively, you may run the script using the following command line:
17964 $ perl gnathtml.pl [switches] files
17973 The GNAT distribution provides an Ada 95 template for the Digital Language
17974 Sensitive Editor (LSE), a component of DECset. In order to
17975 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
17982 GNAT supports The Digital Performance Coverage Analyzer (PCA), a component
17983 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
17984 the collection phase with the /DEBUG qualifier.
17987 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
17988 $ DEFINE LIB$DEBUG PCA$COLLECTOR
17989 $ RUN/DEBUG <PROGRAM_NAME>
17994 @node Running and Debugging Ada Programs
17995 @chapter Running and Debugging Ada Programs
17999 This chapter discusses how to debug Ada programs. An incorrect Ada program
18000 may be handled in three ways by the GNAT compiler:
18004 The illegality may be a violation of the static semantics of Ada. In
18005 that case GNAT diagnoses the constructs in the program that are illegal.
18006 It is then a straightforward matter for the user to modify those parts of
18010 The illegality may be a violation of the dynamic semantics of Ada. In
18011 that case the program compiles and executes, but may generate incorrect
18012 results, or may terminate abnormally with some exception.
18015 When presented with a program that contains convoluted errors, GNAT
18016 itself may terminate abnormally without providing full diagnostics on
18017 the incorrect user program.
18021 * The GNAT Debugger GDB::
18023 * Introduction to GDB Commands::
18024 * Using Ada Expressions::
18025 * Calling User-Defined Subprograms::
18026 * Using the Next Command in a Function::
18029 * Debugging Generic Units::
18030 * GNAT Abnormal Termination or Failure to Terminate::
18031 * Naming Conventions for GNAT Source Files::
18032 * Getting Internal Debugging Information::
18033 * Stack Traceback::
18039 @node The GNAT Debugger GDB
18040 @section The GNAT Debugger GDB
18043 @code{GDB} is a general purpose, platform-independent debugger that
18044 can be used to debug mixed-language programs compiled with @code{GCC},
18045 and in particular is capable of debugging Ada programs compiled with
18046 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
18047 complex Ada data structures.
18049 The manual @cite{Debugging with GDB}
18051 , located in the GNU:[DOCS] directory,
18053 contains full details on the usage of @code{GDB}, including a section on
18054 its usage on programs. This manual should be consulted for full
18055 details. The section that follows is a brief introduction to the
18056 philosophy and use of @code{GDB}.
18058 When GNAT programs are compiled, the compiler optionally writes debugging
18059 information into the generated object file, including information on
18060 line numbers, and on declared types and variables. This information is
18061 separate from the generated code. It makes the object files considerably
18062 larger, but it does not add to the size of the actual executable that
18063 will be loaded into memory, and has no impact on run-time performance. The
18064 generation of debug information is triggered by the use of the
18065 ^-g^/DEBUG^ switch in the gcc or gnatmake command used to carry out
18066 the compilations. It is important to emphasize that the use of these
18067 options does not change the generated code.
18069 The debugging information is written in standard system formats that
18070 are used by many tools, including debuggers and profilers. The format
18071 of the information is typically designed to describe C types and
18072 semantics, but GNAT implements a translation scheme which allows full
18073 details about Ada types and variables to be encoded into these
18074 standard C formats. Details of this encoding scheme may be found in
18075 the file exp_dbug.ads in the GNAT source distribution. However, the
18076 details of this encoding are, in general, of no interest to a user,
18077 since @code{GDB} automatically performs the necessary decoding.
18079 When a program is bound and linked, the debugging information is
18080 collected from the object files, and stored in the executable image of
18081 the program. Again, this process significantly increases the size of
18082 the generated executable file, but it does not increase the size of
18083 the executable program itself. Furthermore, if this program is run in
18084 the normal manner, it runs exactly as if the debug information were
18085 not present, and takes no more actual memory.
18087 However, if the program is run under control of @code{GDB}, the
18088 debugger is activated. The image of the program is loaded, at which
18089 point it is ready to run. If a run command is given, then the program
18090 will run exactly as it would have if @code{GDB} were not present. This
18091 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
18092 entirely non-intrusive until a breakpoint is encountered. If no
18093 breakpoint is ever hit, the program will run exactly as it would if no
18094 debugger were present. When a breakpoint is hit, @code{GDB} accesses
18095 the debugging information and can respond to user commands to inspect
18096 variables, and more generally to report on the state of execution.
18100 @section Running GDB
18103 The debugger can be launched directly and simply from @code{glide} or
18104 through its graphical interface: @code{gvd}. It can also be used
18105 directly in text mode. Here is described the basic use of @code{GDB}
18106 in text mode. All the commands described below can be used in the
18107 @code{gvd} console window even though there is usually other more
18108 graphical ways to achieve the same goals.
18112 The command to run the graphical interface of the debugger is
18119 The command to run @code{GDB} in text mode is
18122 $ ^gdb program^$ GDB PROGRAM^
18126 where @code{^program^PROGRAM^} is the name of the executable file. This
18127 activates the debugger and results in a prompt for debugger commands.
18128 The simplest command is simply @code{run}, which causes the program to run
18129 exactly as if the debugger were not present. The following section
18130 describes some of the additional commands that can be given to @code{GDB}.
18133 @c *******************************
18134 @node Introduction to GDB Commands
18135 @section Introduction to GDB Commands
18138 @code{GDB} contains a large repertoire of commands. The manual
18139 @cite{Debugging with GDB}
18141 , located in the GNU:[DOCS] directory,
18143 includes extensive documentation on the use
18144 of these commands, together with examples of their use. Furthermore,
18145 the command @var{help} invoked from within @code{GDB} activates a simple help
18146 facility which summarizes the available commands and their options.
18147 In this section we summarize a few of the most commonly
18148 used commands to give an idea of what @code{GDB} is about. You should create
18149 a simple program with debugging information and experiment with the use of
18150 these @code{GDB} commands on the program as you read through the
18154 @item set args @var{arguments}
18155 The @var{arguments} list above is a list of arguments to be passed to
18156 the program on a subsequent run command, just as though the arguments
18157 had been entered on a normal invocation of the program. The @code{set args}
18158 command is not needed if the program does not require arguments.
18161 The @code{run} command causes execution of the program to start from
18162 the beginning. If the program is already running, that is to say if
18163 you are currently positioned at a breakpoint, then a prompt will ask
18164 for confirmation that you want to abandon the current execution and
18167 @item breakpoint @var{location}
18168 The breakpoint command sets a breakpoint, that is to say a point at which
18169 execution will halt and @code{GDB} will await further
18170 commands. @var{location} is
18171 either a line number within a file, given in the format @code{file:linenumber},
18172 or it is the name of a subprogram. If you request that a breakpoint be set on
18173 a subprogram that is overloaded, a prompt will ask you to specify on which of
18174 those subprograms you want to breakpoint. You can also
18175 specify that all of them should be breakpointed. If the program is run
18176 and execution encounters the breakpoint, then the program
18177 stops and @code{GDB} signals that the breakpoint was encountered by
18178 printing the line of code before which the program is halted.
18180 @item breakpoint exception @var{name}
18181 A special form of the breakpoint command which breakpoints whenever
18182 exception @var{name} is raised.
18183 If @var{name} is omitted,
18184 then a breakpoint will occur when any exception is raised.
18186 @item print @var{expression}
18187 This will print the value of the given expression. Most simple
18188 Ada expression formats are properly handled by @code{GDB}, so the expression
18189 can contain function calls, variables, operators, and attribute references.
18192 Continues execution following a breakpoint, until the next breakpoint or the
18193 termination of the program.
18196 Executes a single line after a breakpoint. If the next statement
18197 is a subprogram call, execution continues into (the first statement of)
18198 the called subprogram.
18201 Executes a single line. If this line is a subprogram call, executes and
18202 returns from the call.
18205 Lists a few lines around the current source location. In practice, it
18206 is usually more convenient to have a separate edit window open with the
18207 relevant source file displayed. Successive applications of this command
18208 print subsequent lines. The command can be given an argument which is a
18209 line number, in which case it displays a few lines around the specified one.
18212 Displays a backtrace of the call chain. This command is typically
18213 used after a breakpoint has occurred, to examine the sequence of calls that
18214 leads to the current breakpoint. The display includes one line for each
18215 activation record (frame) corresponding to an active subprogram.
18218 At a breakpoint, @code{GDB} can display the values of variables local
18219 to the current frame. The command @code{up} can be used to
18220 examine the contents of other active frames, by moving the focus up
18221 the stack, that is to say from callee to caller, one frame at a time.
18224 Moves the focus of @code{GDB} down from the frame currently being
18225 examined to the frame of its callee (the reverse of the previous command),
18227 @item frame @var{n}
18228 Inspect the frame with the given number. The value 0 denotes the frame
18229 of the current breakpoint, that is to say the top of the call stack.
18233 The above list is a very short introduction to the commands that
18234 @code{GDB} provides. Important additional capabilities, including conditional
18235 breakpoints, the ability to execute command sequences on a breakpoint,
18236 the ability to debug at the machine instruction level and many other
18237 features are described in detail in @cite{Debugging with GDB}.
18238 Note that most commands can be abbreviated
18239 (for example, c for continue, bt for backtrace).
18241 @node Using Ada Expressions
18242 @section Using Ada Expressions
18243 @cindex Ada expressions
18246 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
18247 extensions. The philosophy behind the design of this subset is
18251 That @code{GDB} should provide basic literals and access to operations for
18252 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
18253 leaving more sophisticated computations to subprograms written into the
18254 program (which therefore may be called from @code{GDB}).
18257 That type safety and strict adherence to Ada language restrictions
18258 are not particularly important to the @code{GDB} user.
18261 That brevity is important to the @code{GDB} user.
18264 Thus, for brevity, the debugger acts as if there were
18265 implicit @code{with} and @code{use} clauses in effect for all user-written
18266 packages, thus making it unnecessary to fully qualify most names with
18267 their packages, regardless of context. Where this causes ambiguity,
18268 @code{GDB} asks the user's intent.
18270 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
18272 @node Calling User-Defined Subprograms
18273 @section Calling User-Defined Subprograms
18276 An important capability of @code{GDB} is the ability to call user-defined
18277 subprograms while debugging. This is achieved simply by entering
18278 a subprogram call statement in the form:
18281 call subprogram-name (parameters)
18285 The keyword @code{call} can be omitted in the normal case where the
18286 @code{subprogram-name} does not coincide with any of the predefined
18287 @code{GDB} commands.
18289 The effect is to invoke the given subprogram, passing it the
18290 list of parameters that is supplied. The parameters can be expressions and
18291 can include variables from the program being debugged. The
18292 subprogram must be defined
18293 at the library level within your program, and @code{GDB} will call the
18294 subprogram within the environment of your program execution (which
18295 means that the subprogram is free to access or even modify variables
18296 within your program).
18298 The most important use of this facility is in allowing the inclusion of
18299 debugging routines that are tailored to particular data structures
18300 in your program. Such debugging routines can be written to provide a suitably
18301 high-level description of an abstract type, rather than a low-level dump
18302 of its physical layout. After all, the standard
18303 @code{GDB print} command only knows the physical layout of your
18304 types, not their abstract meaning. Debugging routines can provide information
18305 at the desired semantic level and are thus enormously useful.
18307 For example, when debugging GNAT itself, it is crucial to have access to
18308 the contents of the tree nodes used to represent the program internally.
18309 But tree nodes are represented simply by an integer value (which in turn
18310 is an index into a table of nodes).
18311 Using the @code{print} command on a tree node would simply print this integer
18312 value, which is not very useful. But the PN routine (defined in file
18313 treepr.adb in the GNAT sources) takes a tree node as input, and displays
18314 a useful high level representation of the tree node, which includes the
18315 syntactic category of the node, its position in the source, the integers
18316 that denote descendant nodes and parent node, as well as varied
18317 semantic information. To study this example in more detail, you might want to
18318 look at the body of the PN procedure in the stated file.
18320 @node Using the Next Command in a Function
18321 @section Using the Next Command in a Function
18324 When you use the @code{next} command in a function, the current source
18325 location will advance to the next statement as usual. A special case
18326 arises in the case of a @code{return} statement.
18328 Part of the code for a return statement is the ``epilog'' of the function.
18329 This is the code that returns to the caller. There is only one copy of
18330 this epilog code, and it is typically associated with the last return
18331 statement in the function if there is more than one return. In some
18332 implementations, this epilog is associated with the first statement
18335 The result is that if you use the @code{next} command from a return
18336 statement that is not the last return statement of the function you
18337 may see a strange apparent jump to the last return statement or to
18338 the start of the function. You should simply ignore this odd jump.
18339 The value returned is always that from the first return statement
18340 that was stepped through.
18342 @node Ada Exceptions
18343 @section Breaking on Ada Exceptions
18347 You can set breakpoints that trip when your program raises
18348 selected exceptions.
18351 @item break exception
18352 Set a breakpoint that trips whenever (any task in the) program raises
18355 @item break exception @var{name}
18356 Set a breakpoint that trips whenever (any task in the) program raises
18357 the exception @var{name}.
18359 @item break exception unhandled
18360 Set a breakpoint that trips whenever (any task in the) program raises an
18361 exception for which there is no handler.
18363 @item info exceptions
18364 @itemx info exceptions @var{regexp}
18365 The @code{info exceptions} command permits the user to examine all defined
18366 exceptions within Ada programs. With a regular expression, @var{regexp}, as
18367 argument, prints out only those exceptions whose name matches @var{regexp}.
18375 @code{GDB} allows the following task-related commands:
18379 This command shows a list of current Ada tasks, as in the following example:
18386 ID TID P-ID Thread Pri State Name
18387 1 8088000 0 807e000 15 Child Activation Wait main_task
18388 2 80a4000 1 80ae000 15 Accept/Select Wait b
18389 3 809a800 1 80a4800 15 Child Activation Wait a
18390 * 4 80ae800 3 80b8000 15 Running c
18394 In this listing, the asterisk before the first task indicates it to be the
18395 currently running task. The first column lists the task ID that is used
18396 to refer to tasks in the following commands.
18398 @item break @var{linespec} task @var{taskid}
18399 @itemx break @var{linespec} task @var{taskid} if @dots{}
18400 @cindex Breakpoints and tasks
18401 These commands are like the @code{break @dots{} thread @dots{}}.
18402 @var{linespec} specifies source lines.
18404 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
18405 to specify that you only want @code{GDB} to stop the program when a
18406 particular Ada task reaches this breakpoint. @var{taskid} is one of the
18407 numeric task identifiers assigned by @code{GDB}, shown in the first
18408 column of the @samp{info tasks} display.
18410 If you do not specify @samp{task @var{taskid}} when you set a
18411 breakpoint, the breakpoint applies to @emph{all} tasks of your
18414 You can use the @code{task} qualifier on conditional breakpoints as
18415 well; in this case, place @samp{task @var{taskid}} before the
18416 breakpoint condition (before the @code{if}).
18418 @item task @var{taskno}
18419 @cindex Task switching
18421 This command allows to switch to the task referred by @var{taskno}. In
18422 particular, This allows to browse the backtrace of the specified
18423 task. It is advised to switch back to the original task before
18424 continuing execution otherwise the scheduling of the program may be
18429 For more detailed information on the tasking support,
18430 see @cite{Debugging with GDB}.
18432 @node Debugging Generic Units
18433 @section Debugging Generic Units
18434 @cindex Debugging Generic Units
18438 GNAT always uses code expansion for generic instantiation. This means that
18439 each time an instantiation occurs, a complete copy of the original code is
18440 made, with appropriate substitutions of formals by actuals.
18442 It is not possible to refer to the original generic entities in
18443 @code{GDB}, but it is always possible to debug a particular instance of
18444 a generic, by using the appropriate expanded names. For example, if we have
18446 @smallexample @c ada
18451 generic package k is
18452 procedure kp (v1 : in out integer);
18456 procedure kp (v1 : in out integer) is
18462 package k1 is new k;
18463 package k2 is new k;
18465 var : integer := 1;
18478 Then to break on a call to procedure kp in the k2 instance, simply
18482 (gdb) break g.k2.kp
18486 When the breakpoint occurs, you can step through the code of the
18487 instance in the normal manner and examine the values of local variables, as for
18490 @node GNAT Abnormal Termination or Failure to Terminate
18491 @section GNAT Abnormal Termination or Failure to Terminate
18492 @cindex GNAT Abnormal Termination or Failure to Terminate
18495 When presented with programs that contain serious errors in syntax
18497 GNAT may on rare occasions experience problems in operation, such
18499 segmentation fault or illegal memory access, raising an internal
18500 exception, terminating abnormally, or failing to terminate at all.
18501 In such cases, you can activate
18502 various features of GNAT that can help you pinpoint the construct in your
18503 program that is the likely source of the problem.
18505 The following strategies are presented in increasing order of
18506 difficulty, corresponding to your experience in using GNAT and your
18507 familiarity with compiler internals.
18511 Run @code{gcc} with the @option{-gnatf}. This first
18512 switch causes all errors on a given line to be reported. In its absence,
18513 only the first error on a line is displayed.
18515 The @option{-gnatdO} switch causes errors to be displayed as soon as they
18516 are encountered, rather than after compilation is terminated. If GNAT
18517 terminates prematurely or goes into an infinite loop, the last error
18518 message displayed may help to pinpoint the culprit.
18521 Run @code{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this mode,
18522 @code{gcc} produces ongoing information about the progress of the
18523 compilation and provides the name of each procedure as code is
18524 generated. This switch allows you to find which Ada procedure was being
18525 compiled when it encountered a code generation problem.
18528 @cindex @option{-gnatdc} switch
18529 Run @code{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
18530 switch that does for the front-end what @option{^-v^VERBOSE^} does
18531 for the back end. The system prints the name of each unit,
18532 either a compilation unit or nested unit, as it is being analyzed.
18534 Finally, you can start
18535 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
18536 front-end of GNAT, and can be run independently (normally it is just
18537 called from @code{gcc}). You can use @code{gdb} on @code{gnat1} as you
18538 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
18539 @code{where} command is the first line of attack; the variable
18540 @code{lineno} (seen by @code{print lineno}), used by the second phase of
18541 @code{gnat1} and by the @code{gcc} backend, indicates the source line at
18542 which the execution stopped, and @code{input_file name} indicates the name of
18546 @node Naming Conventions for GNAT Source Files
18547 @section Naming Conventions for GNAT Source Files
18550 In order to examine the workings of the GNAT system, the following
18551 brief description of its organization may be helpful:
18555 Files with prefix @file{^sc^SC^} contain the lexical scanner.
18558 All files prefixed with @file{^par^PAR^} are components of the parser. The
18559 numbers correspond to chapters of the Ada 95 Reference Manual. For example,
18560 parsing of select statements can be found in @file{par-ch9.adb}.
18563 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
18564 numbers correspond to chapters of the Ada standard. For example, all
18565 issues involving context clauses can be found in @file{sem_ch10.adb}. In
18566 addition, some features of the language require sufficient special processing
18567 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
18568 dynamic dispatching, etc.
18571 All files prefixed with @file{^exp^EXP^} perform normalization and
18572 expansion of the intermediate representation (abstract syntax tree, or AST).
18573 these files use the same numbering scheme as the parser and semantics files.
18574 For example, the construction of record initialization procedures is done in
18575 @file{exp_ch3.adb}.
18578 The files prefixed with @file{^bind^BIND^} implement the binder, which
18579 verifies the consistency of the compilation, determines an order of
18580 elaboration, and generates the bind file.
18583 The files @file{atree.ads} and @file{atree.adb} detail the low-level
18584 data structures used by the front-end.
18587 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
18588 the abstract syntax tree as produced by the parser.
18591 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
18592 all entities, computed during semantic analysis.
18595 Library management issues are dealt with in files with prefix
18601 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
18602 defined in Annex A.
18607 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
18608 defined in Annex B.
18612 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
18613 both language-defined children and GNAT run-time routines.
18617 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
18618 general-purpose packages, fully documented in their specifications. All
18619 the other @file{.c} files are modifications of common @code{gcc} files.
18622 @node Getting Internal Debugging Information
18623 @section Getting Internal Debugging Information
18626 Most compilers have internal debugging switches and modes. GNAT
18627 does also, except GNAT internal debugging switches and modes are not
18628 secret. A summary and full description of all the compiler and binder
18629 debug flags are in the file @file{debug.adb}. You must obtain the
18630 sources of the compiler to see the full detailed effects of these flags.
18632 The switches that print the source of the program (reconstructed from
18633 the internal tree) are of general interest for user programs, as are the
18635 the full internal tree, and the entity table (the symbol table
18636 information). The reconstructed source provides a readable version of the
18637 program after the front-end has completed analysis and expansion,
18638 and is useful when studying the performance of specific constructs.
18639 For example, constraint checks are indicated, complex aggregates
18640 are replaced with loops and assignments, and tasking primitives
18641 are replaced with run-time calls.
18643 @node Stack Traceback
18644 @section Stack Traceback
18646 @cindex stack traceback
18647 @cindex stack unwinding
18650 Traceback is a mechanism to display the sequence of subprogram calls that
18651 leads to a specified execution point in a program. Often (but not always)
18652 the execution point is an instruction at which an exception has been raised.
18653 This mechanism is also known as @i{stack unwinding} because it obtains
18654 its information by scanning the run-time stack and recovering the activation
18655 records of all active subprograms. Stack unwinding is one of the most
18656 important tools for program debugging.
18658 The first entry stored in traceback corresponds to the deepest calling level,
18659 that is to say the subprogram currently executing the instruction
18660 from which we want to obtain the traceback.
18662 Note that there is no runtime performance penalty when stack traceback
18663 is enabled, and no exception is raised during program execution.
18666 * Non-Symbolic Traceback::
18667 * Symbolic Traceback::
18670 @node Non-Symbolic Traceback
18671 @subsection Non-Symbolic Traceback
18672 @cindex traceback, non-symbolic
18675 Note: this feature is not supported on all platforms. See
18676 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
18680 * Tracebacks From an Unhandled Exception::
18681 * Tracebacks From Exception Occurrences (non-symbolic)::
18682 * Tracebacks From Anywhere in a Program (non-symbolic)::
18685 @node Tracebacks From an Unhandled Exception
18686 @subsubsection Tracebacks From an Unhandled Exception
18689 A runtime non-symbolic traceback is a list of addresses of call instructions.
18690 To enable this feature you must use the @option{-E}
18691 @code{gnatbind}'s option. With this option a stack traceback is stored as part
18692 of exception information. You can retrieve this information using the
18693 @code{addr2line} tool.
18695 Here is a simple example:
18697 @smallexample @c ada
18703 raise Constraint_Error;
18718 $ gnatmake stb -bargs -E
18721 Execution terminated by unhandled exception
18722 Exception name: CONSTRAINT_ERROR
18724 Call stack traceback locations:
18725 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
18729 As we see the traceback lists a sequence of addresses for the unhandled
18730 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
18731 guess that this exception come from procedure P1. To translate these
18732 addresses into the source lines where the calls appear, the
18733 @code{addr2line} tool, described below, is invaluable. The use of this tool
18734 requires the program to be compiled with debug information.
18737 $ gnatmake -g stb -bargs -E
18740 Execution terminated by unhandled exception
18741 Exception name: CONSTRAINT_ERROR
18743 Call stack traceback locations:
18744 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
18746 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
18747 0x4011f1 0x77e892a4
18749 00401373 at d:/stb/stb.adb:5
18750 0040138B at d:/stb/stb.adb:10
18751 0040139C at d:/stb/stb.adb:14
18752 00401335 at d:/stb/b~stb.adb:104
18753 004011C4 at /build/.../crt1.c:200
18754 004011F1 at /build/.../crt1.c:222
18755 77E892A4 in ?? at ??:0
18759 The @code{addr2line} tool has several other useful options:
18763 to get the function name corresponding to any location
18765 @item --demangle=gnat
18766 to use the gnat decoding mode for the function names. Note that
18767 for binutils version 2.9.x the option is simply @option{--demangle}.
18771 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
18772 0x40139c 0x401335 0x4011c4 0x4011f1
18774 00401373 in stb.p1 at d:/stb/stb.adb:5
18775 0040138B in stb.p2 at d:/stb/stb.adb:10
18776 0040139C in stb at d:/stb/stb.adb:14
18777 00401335 in main at d:/stb/b~stb.adb:104
18778 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
18779 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
18783 From this traceback we can see that the exception was raised in
18784 @file{stb.adb} at line 5, which was reached from a procedure call in
18785 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
18786 which contains the call to the main program.
18787 @pxref{Running gnatbind}. The remaining entries are assorted runtime routines,
18788 and the output will vary from platform to platform.
18790 It is also possible to use @code{GDB} with these traceback addresses to debug
18791 the program. For example, we can break at a given code location, as reported
18792 in the stack traceback:
18798 Furthermore, this feature is not implemented inside Windows DLL. Only
18799 the non-symbolic traceback is reported in this case.
18802 (gdb) break *0x401373
18803 Breakpoint 1 at 0x401373: file stb.adb, line 5.
18807 It is important to note that the stack traceback addresses
18808 do not change when debug information is included. This is particularly useful
18809 because it makes it possible to release software without debug information (to
18810 minimize object size), get a field report that includes a stack traceback
18811 whenever an internal bug occurs, and then be able to retrieve the sequence
18812 of calls with the same program compiled with debug information.
18814 @node Tracebacks From Exception Occurrences (non-symbolic)
18815 @subsubsection Tracebacks From Exception Occurrences
18818 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
18819 The stack traceback is attached to the exception information string, and can
18820 be retrieved in an exception handler within the Ada program, by means of the
18821 Ada95 facilities defined in @code{Ada.Exceptions}. Here is a simple example:
18823 @smallexample @c ada
18825 with Ada.Exceptions;
18830 use Ada.Exceptions;
18838 Text_IO.Put_Line (Exception_Information (E));
18852 This program will output:
18857 Exception name: CONSTRAINT_ERROR
18858 Message: stb.adb:12
18859 Call stack traceback locations:
18860 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
18863 @node Tracebacks From Anywhere in a Program (non-symbolic)
18864 @subsubsection Tracebacks From Anywhere in a Program
18867 It is also possible to retrieve a stack traceback from anywhere in a
18868 program. For this you need to
18869 use the @code{GNAT.Traceback} API. This package includes a procedure called
18870 @code{Call_Chain} that computes a complete stack traceback, as well as useful
18871 display procedures described below. It is not necessary to use the
18872 @option{-E gnatbind} option in this case, because the stack traceback mechanism
18873 is invoked explicitly.
18876 In the following example we compute a traceback at a specific location in
18877 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
18878 convert addresses to strings:
18880 @smallexample @c ada
18882 with GNAT.Traceback;
18883 with GNAT.Debug_Utilities;
18889 use GNAT.Traceback;
18892 TB : Tracebacks_Array (1 .. 10);
18893 -- We are asking for a maximum of 10 stack frames.
18895 -- Len will receive the actual number of stack frames returned.
18897 Call_Chain (TB, Len);
18899 Text_IO.Put ("In STB.P1 : ");
18901 for K in 1 .. Len loop
18902 Text_IO.Put (Debug_Utilities.Image (TB (K)));
18923 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
18924 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
18928 You can then get further information by invoking the @code{addr2line}
18929 tool as described earlier (note that the hexadecimal addresses
18930 need to be specified in C format, with a leading ``0x'').
18933 @node Symbolic Traceback
18934 @subsection Symbolic Traceback
18935 @cindex traceback, symbolic
18938 A symbolic traceback is a stack traceback in which procedure names are
18939 associated with each code location.
18942 Note that this feature is not supported on all platforms. See
18943 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
18944 list of currently supported platforms.
18947 Note that the symbolic traceback requires that the program be compiled
18948 with debug information. If it is not compiled with debug information
18949 only the non-symbolic information will be valid.
18952 * Tracebacks From Exception Occurrences (symbolic)::
18953 * Tracebacks From Anywhere in a Program (symbolic)::
18956 @node Tracebacks From Exception Occurrences (symbolic)
18957 @subsubsection Tracebacks From Exception Occurrences
18959 @smallexample @c ada
18961 with GNAT.Traceback.Symbolic;
18967 raise Constraint_Error;
18984 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
18989 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
18992 0040149F in stb.p1 at stb.adb:8
18993 004014B7 in stb.p2 at stb.adb:13
18994 004014CF in stb.p3 at stb.adb:18
18995 004015DD in ada.stb at stb.adb:22
18996 00401461 in main at b~stb.adb:168
18997 004011C4 in __mingw_CRTStartup at crt1.c:200
18998 004011F1 in mainCRTStartup at crt1.c:222
18999 77E892A4 in ?? at ??:0
19003 In the above example the ``.\'' syntax in the @command{gnatmake} command
19004 is currently required by @command{addr2line} for files that are in
19005 the current working directory.
19006 Moreover, the exact sequence of linker options may vary from platform
19008 The above @option{-largs} section is for Windows platforms. By contrast,
19009 under Unix there is no need for the @option{-largs} section.
19010 Differences across platforms are due to details of linker implementation.
19012 @node Tracebacks From Anywhere in a Program (symbolic)
19013 @subsubsection Tracebacks From Anywhere in a Program
19016 It is possible to get a symbolic stack traceback
19017 from anywhere in a program, just as for non-symbolic tracebacks.
19018 The first step is to obtain a non-symbolic
19019 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
19020 information. Here is an example:
19022 @smallexample @c ada
19024 with GNAT.Traceback;
19025 with GNAT.Traceback.Symbolic;
19030 use GNAT.Traceback;
19031 use GNAT.Traceback.Symbolic;
19034 TB : Tracebacks_Array (1 .. 10);
19035 -- We are asking for a maximum of 10 stack frames.
19037 -- Len will receive the actual number of stack frames returned.
19039 Call_Chain (TB, Len);
19040 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
19054 @node Compatibility with DEC Ada
19055 @chapter Compatibility with DEC Ada
19056 @cindex Compatibility
19059 This section of the manual compares DEC Ada for OpenVMS Alpha and GNAT
19060 OpenVMS Alpha. GNAT achieves a high level of compatibility
19061 with DEC Ada, and it should generally be straightforward to port code
19062 from the DEC Ada environment to GNAT. However, there are a few language
19063 and implementation differences of which the user must be aware. These
19064 differences are discussed in this section. In
19065 addition, the operating environment and command structure for the
19066 compiler are different, and these differences are also discussed.
19068 Note that this discussion addresses specifically the implementation
19069 of Ada 83 for DIGITAL OpenVMS Alpha Systems. In cases where the implementation
19070 of DEC Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
19071 GNAT always follows the Alpha implementation.
19074 * Ada 95 Compatibility::
19075 * Differences in the Definition of Package System::
19076 * Language-Related Features::
19077 * The Package STANDARD::
19078 * The Package SYSTEM::
19079 * Tasking and Task-Related Features::
19080 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
19081 * Pragmas and Pragma-Related Features::
19082 * Library of Predefined Units::
19084 * Main Program Definition::
19085 * Implementation-Defined Attributes::
19086 * Compiler and Run-Time Interfacing::
19087 * Program Compilation and Library Management::
19089 * Implementation Limits::
19093 @node Ada 95 Compatibility
19094 @section Ada 95 Compatibility
19097 GNAT is an Ada 95 compiler, and DEC Ada is an Ada 83
19098 compiler. Ada 95 is almost completely upwards compatible
19099 with Ada 83, and therefore Ada 83 programs will compile
19100 and run under GNAT with
19101 no changes or only minor changes. The Ada 95 Reference
19102 Manual (ANSI/ISO/IEC-8652:1995) provides details on specific
19105 GNAT provides the switch /83 on the GNAT COMPILE command,
19106 as well as the pragma ADA_83, to force the compiler to
19107 operate in Ada 83 mode. This mode does not guarantee complete
19108 conformance to Ada 83, but in practice is sufficient to
19109 eliminate most sources of incompatibilities.
19110 In particular, it eliminates the recognition of the
19111 additional Ada 95 keywords, so that their use as identifiers
19112 in Ada83 program is legal, and handles the cases of packages
19113 with optional bodies, and generics that instantiate unconstrained
19114 types without the use of @code{(<>)}.
19116 @node Differences in the Definition of Package System
19117 @section Differences in the Definition of Package System
19120 Both the Ada 95 and Ada 83 reference manuals permit a compiler to add
19121 implementation-dependent declarations to package System. In normal mode,
19122 GNAT does not take advantage of this permission, and the version of System
19123 provided by GNAT exactly matches that in the Ada 95 Reference Manual.
19125 However, DEC Ada adds an extensive set of declarations to package System,
19126 as fully documented in the DEC Ada manuals. To minimize changes required
19127 for programs that make use of these extensions, GNAT provides the pragma
19128 Extend_System for extending the definition of package System. By using:
19130 @smallexample @c ada
19133 pragma Extend_System (Aux_DEC);
19139 The set of definitions in System is extended to include those in package
19140 @code{System.Aux_DEC}.
19141 These definitions are incorporated directly into package
19142 System, as though they had been declared there in the first place. For a
19143 list of the declarations added, see the specification of this package,
19144 which can be found in the file @code{s-auxdec.ads} in the GNAT library.
19145 The pragma Extend_System is a configuration pragma, which means that
19146 it can be placed in the file @file{gnat.adc}, so that it will automatically
19147 apply to all subsequent compilations. See the section on Configuration
19148 Pragmas for further details.
19150 An alternative approach that avoids the use of the non-standard
19151 Extend_System pragma is to add a context clause to the unit that
19152 references these facilities:
19154 @smallexample @c ada
19157 with System.Aux_DEC;
19158 use System.Aux_DEC;
19164 The effect is not quite semantically identical to incorporating
19165 the declarations directly into package @code{System},
19166 but most programs will not notice a difference
19167 unless they use prefix notation (e.g. @code{System.Integer_8})
19169 entities directly in package @code{System}.
19170 For units containing such references,
19171 the prefixes must either be removed, or the pragma @code{Extend_System}
19174 @node Language-Related Features
19175 @section Language-Related Features
19178 The following sections highlight differences in types,
19179 representations of types, operations, alignment, and
19183 * Integer Types and Representations::
19184 * Floating-Point Types and Representations::
19185 * Pragmas Float_Representation and Long_Float::
19186 * Fixed-Point Types and Representations::
19187 * Record and Array Component Alignment::
19188 * Address Clauses::
19189 * Other Representation Clauses::
19192 @node Integer Types and Representations
19193 @subsection Integer Types and Representations
19196 The set of predefined integer types is identical in DEC Ada and GNAT.
19197 Furthermore the representation of these integer types is also identical,
19198 including the capability of size clauses forcing biased representation.
19201 DEC Ada for OpenVMS Alpha systems has defined the
19202 following additional integer types in package System:
19223 When using GNAT, the first four of these types may be obtained from the
19224 standard Ada 95 package @code{Interfaces}.
19225 Alternatively, by use of the pragma
19226 @code{Extend_System}, identical
19227 declarations can be referenced directly in package @code{System}.
19228 On both GNAT and DEC Ada, the maximum integer size is 64 bits.
19230 @node Floating-Point Types and Representations
19231 @subsection Floating-Point Types and Representations
19232 @cindex Floating-Point types
19235 The set of predefined floating-point types is identical in DEC Ada and GNAT.
19236 Furthermore the representation of these floating-point
19237 types is also identical. One important difference is that the default
19238 representation for DEC Ada is VAX_Float, but the default representation
19241 Specific types may be declared to be VAX_Float or IEEE, using the pragma
19242 @code{Float_Representation} as described in the DEC Ada documentation.
19243 For example, the declarations:
19245 @smallexample @c ada
19248 type F_Float is digits 6;
19249 pragma Float_Representation (VAX_Float, F_Float);
19255 declare a type F_Float that will be represented in VAX_Float format.
19256 This set of declarations actually appears in System.Aux_DEC, which provides
19257 the full set of additional floating-point declarations provided in
19258 the DEC Ada version of package
19259 System. This and similar declarations may be accessed in a user program
19260 by using pragma @code{Extend_System}. The use of this
19261 pragma, and the related pragma @code{Long_Float} is described in further
19262 detail in the following section.
19264 @node Pragmas Float_Representation and Long_Float
19265 @subsection Pragmas Float_Representation and Long_Float
19268 DEC Ada provides the pragma @code{Float_Representation}, which
19269 acts as a program library switch to allow control over
19270 the internal representation chosen for the predefined
19271 floating-point types declared in the package @code{Standard}.
19272 The format of this pragma is as follows:
19277 @b{pragma} @code{Float_Representation}(VAX_Float | IEEE_Float);
19283 This pragma controls the representation of floating-point
19288 @code{VAX_Float} specifies that floating-point
19289 types are represented by default with the VAX hardware types
19290 F-floating, D-floating, G-floating. Note that the H-floating
19291 type is available only on DIGITAL Vax systems, and is not available
19292 in either DEC Ada or GNAT for Alpha systems.
19295 @code{IEEE_Float} specifies that floating-point
19296 types are represented by default with the IEEE single and
19297 double floating-point types.
19301 GNAT provides an identical implementation of the pragma
19302 @code{Float_Representation}, except that it functions as a
19303 configuration pragma, as defined by Ada 95. Note that the
19304 notion of configuration pragma corresponds closely to the
19305 DEC Ada notion of a program library switch.
19307 When no pragma is used in GNAT, the default is IEEE_Float, which is different
19308 from DEC Ada 83, where the default is VAX_Float. In addition, the
19309 predefined libraries in GNAT are built using IEEE_Float, so it is not
19310 advisable to change the format of numbers passed to standard library
19311 routines, and if necessary explicit type conversions may be needed.
19313 The use of IEEE_Float is recommended in GNAT since it is more efficient,
19314 and (given that it conforms to an international standard) potentially more
19315 portable. The situation in which VAX_Float may be useful is in interfacing
19316 to existing code and data that expects the use of VAX_Float. There are
19317 two possibilities here. If the requirement for the use of VAX_Float is
19318 localized, then the best approach is to use the predefined VAX_Float
19319 types in package @code{System}, as extended by
19320 @code{Extend_System}. For example, use @code{System.F_Float}
19321 to specify the 32-bit @code{F-Float} format.
19323 Alternatively, if an entire program depends heavily on the use of
19324 the @code{VAX_Float} and in particular assumes that the types in
19325 package @code{Standard} are in @code{Vax_Float} format, then it
19326 may be desirable to reconfigure GNAT to assume Vax_Float by default.
19327 This is done by using the GNAT LIBRARY command to rebuild the library, and
19328 then using the general form of the @code{Float_Representation}
19329 pragma to ensure that this default format is used throughout.
19330 The form of the GNAT LIBRARY command is:
19333 GNAT LIBRARY /CONFIG=@i{file} /CREATE=@i{directory}
19337 where @i{file} contains the new configuration pragmas
19338 and @i{directory} is the directory to be created to contain
19342 On OpenVMS systems, DEC Ada provides the pragma @code{Long_Float}
19343 to allow control over the internal representation chosen
19344 for the predefined type @code{Long_Float} and for floating-point
19345 type declarations with digits specified in the range 7 .. 15.
19346 The format of this pragma is as follows:
19348 @smallexample @c ada
19350 pragma Long_Float (D_FLOAT | G_FLOAT);
19354 @node Fixed-Point Types and Representations
19355 @subsection Fixed-Point Types and Representations
19358 On DEC Ada for OpenVMS Alpha systems, rounding is
19359 away from zero for both positive and negative numbers.
19360 Therefore, +0.5 rounds to 1 and -0.5 rounds to -1.
19362 On GNAT for OpenVMS Alpha, the results of operations
19363 on fixed-point types are in accordance with the Ada 95
19364 rules. In particular, results of operations on decimal
19365 fixed-point types are truncated.
19367 @node Record and Array Component Alignment
19368 @subsection Record and Array Component Alignment
19371 On DEC Ada for OpenVMS Alpha, all non composite components
19372 are aligned on natural boundaries. For example, 1-byte
19373 components are aligned on byte boundaries, 2-byte
19374 components on 2-byte boundaries, 4-byte components on 4-byte
19375 byte boundaries, and so on. The OpenVMS Alpha hardware
19376 runs more efficiently with naturally aligned data.
19378 ON GNAT for OpenVMS Alpha, alignment rules are compatible
19379 with DEC Ada for OpenVMS Alpha.
19381 @node Address Clauses
19382 @subsection Address Clauses
19385 In DEC Ada and GNAT, address clauses are supported for
19386 objects and imported subprograms.
19387 The predefined type @code{System.Address} is a private type
19388 in both compilers, with the same representation (it is simply
19389 a machine pointer). Addition, subtraction, and comparison
19390 operations are available in the standard Ada 95 package
19391 @code{System.Storage_Elements}, or in package @code{System}
19392 if it is extended to include @code{System.Aux_DEC} using a
19393 pragma @code{Extend_System} as previously described.
19395 Note that code that with's both this extended package @code{System}
19396 and the package @code{System.Storage_Elements} should not @code{use}
19397 both packages, or ambiguities will result. In general it is better
19398 not to mix these two sets of facilities. The Ada 95 package was
19399 designed specifically to provide the kind of features that DEC Ada
19400 adds directly to package @code{System}.
19402 GNAT is compatible with DEC Ada in its handling of address
19403 clauses, except for some limitations in
19404 the form of address clauses for composite objects with
19405 initialization. Such address clauses are easily replaced
19406 by the use of an explicitly-defined constant as described
19407 in the Ada 95 Reference Manual (13.1(22)). For example, the sequence
19410 @smallexample @c ada
19412 X, Y : Integer := Init_Func;
19413 Q : String (X .. Y) := "abc";
19415 for Q'Address use Compute_Address;
19420 will be rejected by GNAT, since the address cannot be computed at the time
19421 that Q is declared. To achieve the intended effect, write instead:
19423 @smallexample @c ada
19426 X, Y : Integer := Init_Func;
19427 Q_Address : constant Address := Compute_Address;
19428 Q : String (X .. Y) := "abc";
19430 for Q'Address use Q_Address;
19436 which will be accepted by GNAT (and other Ada 95 compilers), and is also
19437 backwards compatible with Ada 83. A fuller description of the restrictions
19438 on address specifications is found in the GNAT Reference Manual.
19440 @node Other Representation Clauses
19441 @subsection Other Representation Clauses
19444 GNAT supports in a compatible manner all the representation
19445 clauses supported by DEC Ada. In addition, it
19446 supports representation clause forms that are new in Ada 95
19447 including COMPONENT_SIZE and SIZE clauses for objects.
19449 @node The Package STANDARD
19450 @section The Package STANDARD
19453 The package STANDARD, as implemented by DEC Ada, is fully
19454 described in the Reference Manual for the Ada Programming
19455 Language (ANSI/MIL-STD-1815A-1983) and in the DEC Ada
19456 Language Reference Manual. As implemented by GNAT, the
19457 package STANDARD is described in the Ada 95 Reference
19460 In addition, DEC Ada supports the Latin-1 character set in
19461 the type CHARACTER. GNAT supports the Latin-1 character set
19462 in the type CHARACTER and also Unicode (ISO 10646 BMP) in
19463 the type WIDE_CHARACTER.
19465 The floating-point types supported by GNAT are those
19466 supported by DEC Ada, but defaults are different, and are controlled by
19467 pragmas. See @pxref{Floating-Point Types and Representations} for details.
19469 @node The Package SYSTEM
19470 @section The Package SYSTEM
19473 DEC Ada provides a system-specific version of the package
19474 SYSTEM for each platform on which the language ships.
19475 For the complete specification of the package SYSTEM, see
19476 Appendix F of the DEC Ada Language Reference Manual.
19478 On DEC Ada, the package SYSTEM includes the following conversion functions:
19480 @item TO_ADDRESS(INTEGER)
19482 @item TO_ADDRESS(UNSIGNED_LONGWORD)
19484 @item TO_ADDRESS(universal_integer)
19486 @item TO_INTEGER(ADDRESS)
19488 @item TO_UNSIGNED_LONGWORD(ADDRESS)
19490 @item Function IMPORT_VALUE return UNSIGNED_LONGWORD and the
19491 functions IMPORT_ADDRESS and IMPORT_LARGEST_VALUE
19495 By default, GNAT supplies a version of SYSTEM that matches
19496 the definition given in the Ada 95 Reference Manual.
19498 is a subset of the DIGITAL system definitions, which is as
19499 close as possible to the original definitions. The only difference
19500 is that the definition of SYSTEM_NAME is different:
19502 @smallexample @c ada
19505 type Name is (SYSTEM_NAME_GNAT);
19506 System_Name : constant Name := SYSTEM_NAME_GNAT;
19512 Also, GNAT adds the new Ada 95 declarations for
19513 BIT_ORDER and DEFAULT_BIT_ORDER.
19515 However, the use of the following pragma causes GNAT
19516 to extend the definition of package SYSTEM so that it
19517 encompasses the full set of DIGITAL-specific extensions,
19518 including the functions listed above:
19520 @smallexample @c ada
19522 pragma Extend_System (Aux_DEC);
19527 The pragma Extend_System is a configuration pragma that
19528 is most conveniently placed in the @file{gnat.adc} file. See the
19529 GNAT Reference Manual for further details.
19531 DEC Ada does not allow the recompilation of the package
19532 SYSTEM. Instead DEC Ada provides several pragmas (SYSTEM_
19533 NAME, STORAGE_UNIT, and MEMORY_SIZE) to modify values in
19534 the package SYSTEM. On OpenVMS Alpha systems, the pragma
19535 SYSTEM_NAME takes the enumeration literal OPENVMS_AXP as
19536 its single argument.
19538 GNAT does permit the recompilation of package SYSTEM using
19539 a special switch (@option{-gnatg}) and this switch can be used if
19540 it is necessary to modify the definitions in SYSTEM. GNAT does
19541 not permit the specification of SYSTEM_NAME, STORAGE_UNIT
19542 or MEMORY_SIZE by any other means.
19544 On GNAT systems, the pragma SYSTEM_NAME takes the
19545 enumeration literal SYSTEM_NAME_GNAT.
19547 The definitions provided by the use of
19549 @smallexample @c ada
19550 pragma Extend_System (AUX_Dec);
19554 are virtually identical to those provided by the DEC Ada 83 package
19555 System. One important difference is that the name of the TO_ADDRESS
19556 function for type UNSIGNED_LONGWORD is changed to TO_ADDRESS_LONG.
19557 See the GNAT Reference manual for a discussion of why this change was
19561 The version of TO_ADDRESS taking a universal integer argument is in fact
19562 an extension to Ada 83 not strictly compatible with the reference manual.
19563 In GNAT, we are constrained to be exactly compatible with the standard,
19564 and this means we cannot provide this capability. In DEC Ada 83, the
19565 point of this definition is to deal with a call like:
19567 @smallexample @c ada
19568 TO_ADDRESS (16#12777#);
19572 Normally, according to the Ada 83 standard, one would expect this to be
19573 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
19574 of TO_ADDRESS. However, in DEC Ada 83, there is no ambiguity, since the
19575 definition using universal_integer takes precedence.
19577 In GNAT, since the version with universal_integer cannot be supplied, it is
19578 not possible to be 100% compatible. Since there are many programs using
19579 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
19580 to change the name of the function in the UNSIGNED_LONGWORD case, so the
19581 declarations provided in the GNAT version of AUX_Dec are:
19583 @smallexample @c ada
19584 function To_Address (X : Integer) return Address;
19585 pragma Pure_Function (To_Address);
19587 function To_Address_Long (X : Unsigned_Longword) return Address;
19588 pragma Pure_Function (To_Address_Long);
19592 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
19593 change the name to TO_ADDRESS_LONG.
19595 @node Tasking and Task-Related Features
19596 @section Tasking and Task-Related Features
19599 The concepts relevant to a comparison of tasking on GNAT
19600 and on DEC Ada for OpenVMS Alpha systems are discussed in
19601 the following sections.
19603 For detailed information on concepts related to tasking in
19604 DEC Ada, see the DEC Ada Language Reference Manual and the
19605 relevant run-time reference manual.
19607 @node Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
19608 @section Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
19611 On OpenVMS Alpha systems, each Ada task (except a passive
19612 task) is implemented as a single stream of execution
19613 that is created and managed by the kernel. On these
19614 systems, DEC Ada tasking support is based on DECthreads,
19615 an implementation of the POSIX standard for threads.
19617 Although tasks are implemented as threads, all tasks in
19618 an Ada program are part of the same process. As a result,
19619 resources such as open files and virtual memory can be
19620 shared easily among tasks. Having all tasks in one process
19621 allows better integration with the programming environment
19622 (the shell and the debugger, for example).
19624 Also, on OpenVMS Alpha systems, DEC Ada tasks and foreign
19625 code that calls DECthreads routines can be used together.
19626 The interaction between Ada tasks and DECthreads routines
19627 can have some benefits. For example when on OpenVMS Alpha,
19628 DEC Ada can call C code that is already threaded.
19629 GNAT on OpenVMS Alpha uses the facilities of DECthreads,
19630 and Ada tasks are mapped to threads.
19633 * Assigning Task IDs::
19634 * Task IDs and Delays::
19635 * Task-Related Pragmas::
19636 * Scheduling and Task Priority::
19638 * External Interrupts::
19641 @node Assigning Task IDs
19642 @subsection Assigning Task IDs
19645 The DEC Ada Run-Time Library always assigns %TASK 1 to
19646 the environment task that executes the main program. On
19647 OpenVMS Alpha systems, %TASK 0 is often used for tasks
19648 that have been created but are not yet activated.
19650 On OpenVMS Alpha systems, task IDs are assigned at
19651 activation. On GNAT systems, task IDs are also assigned at
19652 task creation but do not have the same form or values as
19653 task ID values in DEC Ada. There is no null task, and the
19654 environment task does not have a specific task ID value.
19656 @node Task IDs and Delays
19657 @subsection Task IDs and Delays
19660 On OpenVMS Alpha systems, tasking delays are implemented
19661 using Timer System Services. The Task ID is used for the
19662 identification of the timer request (the REQIDT parameter).
19663 If Timers are used in the application take care not to use
19664 0 for the identification, because cancelling such a timer
19665 will cancel all timers and may lead to unpredictable results.
19667 @node Task-Related Pragmas
19668 @subsection Task-Related Pragmas
19671 Ada supplies the pragma TASK_STORAGE, which allows
19672 specification of the size of the guard area for a task
19673 stack. (The guard area forms an area of memory that has no
19674 read or write access and thus helps in the detection of
19675 stack overflow.) On OpenVMS Alpha systems, if the pragma
19676 TASK_STORAGE specifies a value of zero, a minimal guard
19677 area is created. In the absence of a pragma TASK_STORAGE, a default guard
19680 GNAT supplies the following task-related pragmas:
19685 This pragma appears within a task definition and
19686 applies to the task in which it appears. The argument
19687 must be of type SYSTEM.TASK_INFO.TASK_INFO_TYPE.
19691 GNAT implements pragma TASK_STORAGE in the same way as
19693 Both DEC Ada and GNAT supply the pragmas PASSIVE,
19694 SUPPRESS, and VOLATILE.
19696 @node Scheduling and Task Priority
19697 @subsection Scheduling and Task Priority
19700 DEC Ada implements the Ada language requirement that
19701 when two tasks are eligible for execution and they have
19702 different priorities, the lower priority task does not
19703 execute while the higher priority task is waiting. The DEC
19704 Ada Run-Time Library keeps a task running until either the
19705 task is suspended or a higher priority task becomes ready.
19707 On OpenVMS Alpha systems, the default strategy is round-
19708 robin with preemption. Tasks of equal priority take turns
19709 at the processor. A task is run for a certain period of
19710 time and then placed at the rear of the ready queue for
19711 its priority level.
19713 DEC Ada provides the implementation-defined pragma TIME_SLICE,
19714 which can be used to enable or disable round-robin
19715 scheduling of tasks with the same priority.
19716 See the relevant DEC Ada run-time reference manual for
19717 information on using the pragmas to control DEC Ada task
19720 GNAT follows the scheduling rules of Annex D (real-time
19721 Annex) of the Ada 95 Reference Manual. In general, this
19722 scheduling strategy is fully compatible with DEC Ada
19723 although it provides some additional constraints (as
19724 fully documented in Annex D).
19725 GNAT implements time slicing control in a manner compatible with
19726 DEC Ada 83, by means of the pragma Time_Slice, whose semantics are identical
19727 to the DEC Ada 83 pragma of the same name.
19728 Note that it is not possible to mix GNAT tasking and
19729 DEC Ada 83 tasking in the same program, since the two run times are
19732 @node The Task Stack
19733 @subsection The Task Stack
19736 In DEC Ada, a task stack is allocated each time a
19737 non passive task is activated. As soon as the task is
19738 terminated, the storage for the task stack is deallocated.
19739 If you specify a size of zero (bytes) with T'STORAGE_SIZE,
19740 a default stack size is used. Also, regardless of the size
19741 specified, some additional space is allocated for task
19742 management purposes. On OpenVMS Alpha systems, at least
19743 one page is allocated.
19745 GNAT handles task stacks in a similar manner. According to
19746 the Ada 95 rules, it provides the pragma STORAGE_SIZE as
19747 an alternative method for controlling the task stack size.
19748 The specification of the attribute T'STORAGE_SIZE is also
19749 supported in a manner compatible with DEC Ada.
19751 @node External Interrupts
19752 @subsection External Interrupts
19755 On DEC Ada, external interrupts can be associated with task entries.
19756 GNAT is compatible with DEC Ada in its handling of external interrupts.
19758 @node Pragmas and Pragma-Related Features
19759 @section Pragmas and Pragma-Related Features
19762 Both DEC Ada and GNAT supply all language-defined pragmas
19763 as specified by the Ada 83 standard. GNAT also supplies all
19764 language-defined pragmas specified in the Ada 95 Reference Manual.
19765 In addition, GNAT implements the implementation-defined pragmas
19771 @item COMMON_OBJECT
19773 @item COMPONENT_ALIGNMENT
19775 @item EXPORT_EXCEPTION
19777 @item EXPORT_FUNCTION
19779 @item EXPORT_OBJECT
19781 @item EXPORT_PROCEDURE
19783 @item EXPORT_VALUED_PROCEDURE
19785 @item FLOAT_REPRESENTATION
19789 @item IMPORT_EXCEPTION
19791 @item IMPORT_FUNCTION
19793 @item IMPORT_OBJECT
19795 @item IMPORT_PROCEDURE
19797 @item IMPORT_VALUED_PROCEDURE
19799 @item INLINE_GENERIC
19801 @item INTERFACE_NAME
19811 @item SHARE_GENERIC
19823 These pragmas are all fully implemented, with the exception of @code{Title},
19824 @code{Passive}, and @code{Share_Generic}, which are
19825 recognized, but which have no
19826 effect in GNAT. The effect of @code{Passive} may be obtained by the
19827 use of protected objects in Ada 95. In GNAT, all generics are inlined.
19829 Unlike DEC Ada, the GNAT 'EXPORT_@i{subprogram}' pragmas require
19830 a separate subprogram specification which must appear before the
19833 GNAT also supplies a number of implementation-defined pragmas as follows:
19835 @item C_PASS_BY_COPY
19837 @item EXTEND_SYSTEM
19839 @item SOURCE_FILE_NAME
19857 @item CPP_CONSTRUCTOR
19859 @item CPP_DESTRUCTOR
19869 @item LINKER_SECTION
19871 @item MACHINE_ATTRIBUTE
19875 @item PURE_FUNCTION
19877 @item SOURCE_REFERENCE
19881 @item UNCHECKED_UNION
19883 @item UNIMPLEMENTED_UNIT
19885 @item UNIVERSAL_DATA
19887 @item WEAK_EXTERNAL
19891 For full details on these GNAT implementation-defined pragmas, see
19892 the GNAT Reference Manual.
19895 * Restrictions on the Pragma INLINE::
19896 * Restrictions on the Pragma INTERFACE::
19897 * Restrictions on the Pragma SYSTEM_NAME::
19900 @node Restrictions on the Pragma INLINE
19901 @subsection Restrictions on the Pragma INLINE
19904 DEC Ada applies the following restrictions to the pragma INLINE:
19906 @item Parameters cannot be a task type.
19908 @item Function results cannot be task types, unconstrained
19909 array types, or unconstrained types with discriminants.
19911 @item Bodies cannot declare the following:
19913 @item Subprogram body or stub (imported subprogram is allowed)
19917 @item Generic declarations
19919 @item Instantiations
19923 @item Access types (types derived from access types allowed)
19925 @item Array or record types
19927 @item Dependent tasks
19929 @item Direct recursive calls of subprogram or containing
19930 subprogram, directly or via a renaming
19936 In GNAT, the only restriction on pragma INLINE is that the
19937 body must occur before the call if both are in the same
19938 unit, and the size must be appropriately small. There are
19939 no other specific restrictions which cause subprograms to
19940 be incapable of being inlined.
19942 @node Restrictions on the Pragma INTERFACE
19943 @subsection Restrictions on the Pragma INTERFACE
19946 The following lists and describes the restrictions on the
19947 pragma INTERFACE on DEC Ada and GNAT:
19949 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
19950 Default is the default on OpenVMS Alpha systems.
19952 @item Parameter passing: Language specifies default
19953 mechanisms but can be overridden with an EXPORT pragma.
19956 @item Ada: Use internal Ada rules.
19958 @item Bliss, C: Parameters must be mode @code{in}; cannot be
19959 record or task type. Result cannot be a string, an
19960 array, or a record.
19962 @item Fortran: Parameters cannot be a task. Result cannot
19963 be a string, an array, or a record.
19968 GNAT is entirely upwards compatible with DEC Ada, and in addition allows
19969 record parameters for all languages.
19971 @node Restrictions on the Pragma SYSTEM_NAME
19972 @subsection Restrictions on the Pragma SYSTEM_NAME
19975 For DEC Ada for OpenVMS Alpha, the enumeration literal
19976 for the type NAME is OPENVMS_AXP. In GNAT, the enumeration
19977 literal for the type NAME is SYSTEM_NAME_GNAT.
19979 @node Library of Predefined Units
19980 @section Library of Predefined Units
19983 A library of predefined units is provided as part of the
19984 DEC Ada and GNAT implementations. DEC Ada does not provide
19985 the package MACHINE_CODE but instead recommends importing
19988 The GNAT versions of the DEC Ada Run-Time Library (ADA$PREDEFINED:)
19989 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
19990 version. During GNAT installation, the DEC Ada Predefined
19991 Library units are copied into the GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
19992 (aka DECLIB) directory and patched to remove Ada 95 incompatibilities
19993 and to make them interoperable with GNAT, @pxref{Changes to DECLIB}
19996 The GNAT RTL is contained in
19997 the GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB] (aka ADALIB) directory and
19998 the default search path is set up to find DECLIB units in preference
19999 to ADALIB units with the same name (TEXT_IO, SEQUENTIAL_IO, and DIRECT_IO,
20002 However, it is possible to change the default so that the
20003 reverse is true, or even to mix them using child package
20004 notation. The DEC Ada 83 units are available as DEC.xxx where xxx
20005 is the package name, and the Ada units are available in the
20006 standard manner defined for Ada 95, that is to say as Ada.xxx. To
20007 change the default, set ADA_INCLUDE_PATH and ADA_OBJECTS_PATH
20008 appropriately. For example, to change the default to use the Ada95
20012 $ DEFINE ADA_INCLUDE_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADAINCLUDE],-
20013 GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
20014 $ DEFINE ADA_OBJECTS_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB],-
20015 GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
20019 * Changes to DECLIB::
20022 @node Changes to DECLIB
20023 @subsection Changes to DECLIB
20026 The changes made to the DEC Ada predefined library for GNAT and Ada 95
20027 compatibility are minor and include the following:
20030 @item Adjusting the location of pragmas and record representation
20031 clauses to obey Ada 95 rules
20033 @item Adding the proper notation to generic formal parameters
20034 that take unconstrained types in instantiation
20036 @item Adding pragma ELABORATE_BODY to package specifications
20037 that have package bodies not otherwise allowed
20039 @item Occurrences of the identifier @code{"PROTECTED"} are renamed to
20041 Currently these are found only in the STARLET package spec.
20045 None of the above changes is visible to users.
20051 On OpenVMS Alpha, DEC Ada provides the following strongly-typed bindings:
20054 @item Command Language Interpreter (CLI interface)
20056 @item DECtalk Run-Time Library (DTK interface)
20058 @item Librarian utility routines (LBR interface)
20060 @item General Purpose Run-Time Library (LIB interface)
20062 @item Math Run-Time Library (MTH interface)
20064 @item National Character Set Run-Time Library (NCS interface)
20066 @item Compiled Code Support Run-Time Library (OTS interface)
20068 @item Parallel Processing Run-Time Library (PPL interface)
20070 @item Screen Management Run-Time Library (SMG interface)
20072 @item Sort Run-Time Library (SOR interface)
20074 @item String Run-Time Library (STR interface)
20076 @item STARLET System Library
20079 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
20081 @item X Windows Toolkit (XT interface)
20083 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
20087 GNAT provides implementations of these DEC bindings in the DECLIB directory.
20089 The X/Motif bindings used to build DECLIB are whatever versions are in the
20090 DEC Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
20091 The build script will
20092 automatically add a pragma Linker_Options to packages @code{Xm}, @code{Xt},
20094 causing the default X/Motif sharable image libraries to be linked in. This
20095 is done via options files named @file{xm.opt}, @file{xt.opt}, and
20096 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
20098 It may be necessary to edit these options files to update or correct the
20099 library names if, for example, the newer X/Motif bindings from
20100 @file{ADA$EXAMPLES}
20101 had been (previous to installing GNAT) copied and renamed to supersede the
20102 default @file{ADA$PREDEFINED} versions.
20105 * Shared Libraries and Options Files::
20106 * Interfaces to C::
20109 @node Shared Libraries and Options Files
20110 @subsection Shared Libraries and Options Files
20113 When using the DEC Ada
20114 predefined X and Motif bindings, the linking with their sharable images is
20115 done automatically by @command{GNAT LINK}.
20116 When using other X and Motif bindings, you need
20117 to add the corresponding sharable images to the command line for
20118 @code{GNAT LINK}. When linking with shared libraries, or with
20119 @file{.OPT} files, you must
20120 also add them to the command line for @command{GNAT LINK}.
20122 A shared library to be used with GNAT is built in the same way as other
20123 libraries under VMS. The VMS Link command can be used in standard fashion.
20125 @node Interfaces to C
20126 @subsection Interfaces to C
20130 provides the following Ada types and operations:
20133 @item C types package (C_TYPES)
20135 @item C strings (C_TYPES.NULL_TERMINATED)
20137 @item Other_types (SHORT_INT)
20141 Interfacing to C with GNAT, one can use the above approach
20142 described for DEC Ada or the facilities of Annex B of
20143 the Ada 95 Reference Manual (packages INTERFACES.C,
20144 INTERFACES.C.STRINGS and INTERFACES.C.POINTERS). For more
20145 information, see the section ``Interfacing to C'' in the
20146 @cite{GNAT Reference Manual}.
20148 The @option{-gnatF} qualifier forces default and explicit
20149 @code{External_Name} parameters in pragmas Import and Export
20150 to be uppercased for compatibility with the default behavior
20151 of Compaq C. The qualifier has no effect on @code{Link_Name} parameters.
20153 @node Main Program Definition
20154 @section Main Program Definition
20157 The following section discusses differences in the
20158 definition of main programs on DEC Ada and GNAT.
20159 On DEC Ada, main programs are defined to meet the
20160 following conditions:
20162 @item Procedure with no formal parameters (returns 0 upon
20165 @item Procedure with no formal parameters (returns 42 when
20166 unhandled exceptions are raised)
20168 @item Function with no formal parameters whose returned value
20169 is of a discrete type
20171 @item Procedure with one OUT formal of a discrete type for
20172 which a specification of pragma EXPORT_VALUED_PROCEDURE is given.
20177 When declared with the pragma EXPORT_VALUED_PROCEDURE,
20178 a main function or main procedure returns a discrete
20179 value whose size is less than 64 bits (32 on VAX systems),
20180 the value is zero- or sign-extended as appropriate.
20181 On GNAT, main programs are defined as follows:
20183 @item Must be a non-generic, parameter-less subprogram that
20184 is either a procedure or function returning an Ada
20185 STANDARD.INTEGER (the predefined type)
20187 @item Cannot be a generic subprogram or an instantiation of a
20191 @node Implementation-Defined Attributes
20192 @section Implementation-Defined Attributes
20195 GNAT provides all DEC Ada implementation-defined
20198 @node Compiler and Run-Time Interfacing
20199 @section Compiler and Run-Time Interfacing
20202 DEC Ada provides the following ways to pass options to the linker
20205 @item /WAIT and /SUBMIT qualifiers
20207 @item /COMMAND qualifier
20209 @item /[NO]MAP qualifier
20211 @item /OUTPUT=file-spec
20213 @item /[NO]DEBUG and /[NO]TRACEBACK qualifiers
20217 To pass options to the linker, GNAT provides the following
20221 @item @option{/EXECUTABLE=exec-name}
20223 @item @option{/VERBOSE qualifier}
20225 @item @option{/[NO]DEBUG} and @option{/[NO]TRACEBACK} qualifiers
20229 For more information on these switches, see
20230 @ref{Switches for gnatlink}.
20231 In DEC Ada, the command-line switch @option{/OPTIMIZE} is available
20232 to control optimization. DEC Ada also supplies the
20235 @item @code{OPTIMIZE}
20237 @item @code{INLINE}
20239 @item @code{INLINE_GENERIC}
20241 @item @code{SUPPRESS_ALL}
20243 @item @code{PASSIVE}
20247 In GNAT, optimization is controlled strictly by command
20248 line parameters, as described in the corresponding section of this guide.
20249 The DIGITAL pragmas for control of optimization are
20250 recognized but ignored.
20252 Note that in GNAT, the default is optimization off, whereas in DEC Ada 83,
20253 the default is that optimization is turned on.
20255 @node Program Compilation and Library Management
20256 @section Program Compilation and Library Management
20259 DEC Ada and GNAT provide a comparable set of commands to
20260 build programs. DEC Ada also provides a program library,
20261 which is a concept that does not exist on GNAT. Instead,
20262 GNAT provides directories of sources that are compiled as
20265 The following table summarizes
20266 the DEC Ada commands and provides
20267 equivalent GNAT commands. In this table, some GNAT
20268 equivalents reflect the fact that GNAT does not use the
20269 concept of a program library. Instead, it uses a model
20270 in which collections of source and object files are used
20271 in a manner consistent with other languages like C and
20272 Fortran. Therefore, standard system file commands are used
20273 to manipulate these elements. Those GNAT commands are marked with
20275 Note that, unlike DEC Ada, none of the GNAT commands accepts wild cards.
20278 @multitable @columnfractions .35 .65
20280 @item @emph{DEC Ada Command}
20281 @tab @emph{GNAT Equivalent / Description}
20283 @item @command{ADA}
20284 @tab @command{GNAT COMPILE}@*
20285 Invokes the compiler to compile one or more Ada source files.
20287 @item @command{ACS ATTACH}@*
20288 @tab [No equivalent]@*
20289 Switches control of terminal from current process running the program
20292 @item @command{ACS CHECK}
20293 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
20294 Forms the execution closure of one
20295 or more compiled units and checks completeness and currency.
20297 @item @command{ACS COMPILE}
20298 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
20299 Forms the execution closure of one or
20300 more specified units, checks completeness and currency,
20301 identifies units that have revised source files, compiles same,
20302 and recompiles units that are or will become obsolete.
20303 Also completes incomplete generic instantiations.
20305 @item @command{ACS COPY FOREIGN}
20307 Copies a foreign object file into the program library as a
20310 @item @command{ACS COPY UNIT}
20312 Copies a compiled unit from one program library to another.
20314 @item @command{ACS CREATE LIBRARY}
20315 @tab Create /directory (*)@*
20316 Creates a program library.
20318 @item @command{ACS CREATE SUBLIBRARY}
20319 @tab Create /directory (*)@*
20320 Creates a program sublibrary.
20322 @item @command{ACS DELETE LIBRARY}
20324 Deletes a program library and its contents.
20326 @item @command{ACS DELETE SUBLIBRARY}
20328 Deletes a program sublibrary and its contents.
20330 @item @command{ACS DELETE UNIT}
20331 @tab Delete file (*)@*
20332 On OpenVMS systems, deletes one or more compiled units from
20333 the current program library.
20335 @item @command{ACS DIRECTORY}
20336 @tab Directory (*)@*
20337 On OpenVMS systems, lists units contained in the current
20340 @item @command{ACS ENTER FOREIGN}
20342 Allows the import of a foreign body as an Ada library
20343 specification and enters a reference to a pointer.
20345 @item @command{ACS ENTER UNIT}
20347 Enters a reference (pointer) from the current program library to
20348 a unit compiled into another program library.
20350 @item @command{ACS EXIT}
20351 @tab [No equivalent]@*
20352 Exits from the program library manager.
20354 @item @command{ACS EXPORT}
20356 Creates an object file that contains system-specific object code
20357 for one or more units. With GNAT, object files can simply be copied
20358 into the desired directory.
20360 @item @command{ACS EXTRACT SOURCE}
20362 Allows access to the copied source file for each Ada compilation unit
20364 @item @command{ACS HELP}
20365 @tab @command{HELP GNAT}@*
20366 Provides online help.
20368 @item @command{ACS LINK}
20369 @tab @command{GNAT LINK}@*
20370 Links an object file containing Ada units into an executable file.
20372 @item @command{ACS LOAD}
20374 Loads (partially compiles) Ada units into the program library.
20375 Allows loading a program from a collection of files into a library
20376 without knowing the relationship among units.
20378 @item @command{ACS MERGE}
20380 Merges into the current program library, one or more units from
20381 another library where they were modified.
20383 @item @command{ACS RECOMPILE}
20384 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
20385 Recompiles from external or copied source files any obsolete
20386 unit in the closure. Also, completes any incomplete generic
20389 @item @command{ACS REENTER}
20390 @tab @command{GNAT MAKE}@*
20391 Reenters current references to units compiled after last entered
20392 with the @command{ACS ENTER UNIT} command.
20394 @item @command{ACS SET LIBRARY}
20395 @tab Set default (*)@*
20396 Defines a program library to be the compilation context as well
20397 as the target library for compiler output and commands in general.
20399 @item @command{ACS SET PRAGMA}
20400 @tab Edit @file{gnat.adc} (*)@*
20401 Redefines specified values of the library characteristics
20402 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
20403 and @code{Float_Representation}.
20405 @item @command{ACS SET SOURCE}
20406 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
20407 Defines the source file search list for the @command{ACS COMPILE} command.
20409 @item @command{ACS SHOW LIBRARY}
20410 @tab Directory (*)@*
20411 Lists information about one or more program libraries.
20413 @item @command{ACS SHOW PROGRAM}
20414 @tab [No equivalent]@*
20415 Lists information about the execution closure of one or
20416 more units in the program library.
20418 @item @command{ACS SHOW SOURCE}
20419 @tab Show logical @code{ADA_INCLUDE_PATH}@*
20420 Shows the source file search used when compiling units.
20422 @item @command{ACS SHOW VERSION}
20423 @tab Compile with @option{VERBOSE} option
20424 Displays the version number of the compiler and program library
20427 @item @command{ACS SPAWN}
20428 @tab [No equivalent]@*
20429 Creates a subprocess of the current process (same as @command{DCL SPAWN}
20432 @item @command{ACS VERIFY}
20433 @tab [No equivalent]@*
20434 Performs a series of consistency checks on a program library to
20435 determine whether the library structure and library files are in
20442 @section Input-Output
20445 On OpenVMS Alpha systems, DEC Ada uses OpenVMS Record
20446 Management Services (RMS) to perform operations on
20450 DEC Ada and GNAT predefine an identical set of input-
20451 output packages. To make the use of the
20452 generic TEXT_IO operations more convenient, DEC Ada
20453 provides predefined library packages that instantiate the
20454 integer and floating-point operations for the predefined
20455 integer and floating-point types as shown in the following table.
20457 @multitable @columnfractions .45 .55
20458 @item @emph{Package Name} @tab Instantiation
20460 @item @code{INTEGER_TEXT_IO}
20461 @tab @code{INTEGER_IO(INTEGER)}
20463 @item @code{SHORT_INTEGER_TEXT_IO}
20464 @tab @code{INTEGER_IO(SHORT_INTEGER)}
20466 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
20467 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
20469 @item @code{FLOAT_TEXT_IO}
20470 @tab @code{FLOAT_IO(FLOAT)}
20472 @item @code{LONG_FLOAT_TEXT_IO}
20473 @tab @code{FLOAT_IO(LONG_FLOAT)}
20477 The DEC Ada predefined packages and their operations
20478 are implemented using OpenVMS Alpha files and input-
20479 output facilities. DEC Ada supports asynchronous input-
20480 output on OpenVMS Alpha. Familiarity with the following is
20483 @item RMS file organizations and access methods
20485 @item OpenVMS file specifications and directories
20487 @item OpenVMS File Definition Language (FDL)
20491 GNAT provides I/O facilities that are completely
20492 compatible with DEC Ada. The distribution includes the
20493 standard DEC Ada versions of all I/O packages, operating
20494 in a manner compatible with DEC Ada. In particular, the
20495 following packages are by default the DEC Ada (Ada 83)
20496 versions of these packages rather than the renamings
20497 suggested in annex J of the Ada 95 Reference Manual:
20499 @item @code{TEXT_IO}
20501 @item @code{SEQUENTIAL_IO}
20503 @item @code{DIRECT_IO}
20507 The use of the standard Ada 95 syntax for child packages (for
20508 example, @code{ADA.TEXT_IO}) retrieves the Ada 95 versions of these
20509 packages, as defined in the Ada 95 Reference Manual.
20510 GNAT provides DIGITAL-compatible predefined instantiations
20511 of the @code{TEXT_IO} packages, and also
20512 provides the standard predefined instantiations required
20513 by the Ada 95 Reference Manual.
20515 For further information on how GNAT interfaces to the file
20516 system or how I/O is implemented in programs written in
20517 mixed languages, see the chapter ``Implementation of the
20518 Standard I/O'' in the @cite{GNAT Reference Manual}.
20519 This chapter covers the following:
20521 @item Standard I/O packages
20523 @item @code{FORM} strings
20525 @item @code{ADA.DIRECT_IO}
20527 @item @code{ADA.SEQUENTIAL_IO}
20529 @item @code{ADA.TEXT_IO}
20531 @item Stream pointer positioning
20533 @item Reading and writing non-regular files
20535 @item @code{GET_IMMEDIATE}
20537 @item Treating @code{TEXT_IO} files as streams
20544 @node Implementation Limits
20545 @section Implementation Limits
20548 The following table lists implementation limits for DEC Ada
20550 @multitable @columnfractions .60 .20 .20
20552 @item @emph{Compilation Parameter}
20553 @tab @emph{DEC Ada}
20557 @item In a subprogram or entry declaration, maximum number of
20558 formal parameters that are of an unconstrained record type
20563 @item Maximum identifier length (number of characters)
20568 @item Maximum number of characters in a source line
20573 @item Maximum collection size (number of bytes)
20578 @item Maximum number of discriminants for a record type
20583 @item Maximum number of formal parameters in an entry or
20584 subprogram declaration
20589 @item Maximum number of dimensions in an array type
20594 @item Maximum number of library units and subunits in a compilation.
20599 @item Maximum number of library units and subunits in an execution.
20604 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
20605 or @code{PSECT_OBJECT}
20610 @item Maximum number of enumeration literals in an enumeration type
20616 @item Maximum number of lines in a source file
20621 @item Maximum number of bits in any object
20626 @item Maximum size of the static portion of a stack frame (approximate)
20637 @c **************************************
20638 @node Platform-Specific Information for the Run-Time Libraries
20639 @appendix Platform-Specific Information for the Run-Time Libraries
20640 @cindex Tasking and threads libraries
20641 @cindex Threads libraries and tasking
20642 @cindex Run-time libraries (platform-specific information)
20645 The GNAT run-time implementation
20646 may vary with respect to both the underlying threads library and
20647 the exception handling scheme.
20648 For threads support, one or more of the following are supplied:
20650 @item @b{native threads library}, a binding to the thread package from
20651 the underlying operating system
20653 @item @b{FSU threads library}, a binding to the Florida State University
20654 threads implementation, which complies fully with the requirements of Annex D
20656 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
20657 POSIX thread package
20661 For exception handling, either or both of two models are supplied:
20663 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
20664 Most programs should experience a substantial speed improvement by
20665 being compiled with a ZCX run-time.
20666 This is especially true for
20667 tasking applications or applications with many exception handlers.}
20668 @cindex Zero-Cost Exceptions
20669 @cindex ZCX (Zero-Cost Exceptions)
20670 which uses binder-generated tables that
20671 are interrogated at run time to locate a handler
20673 @item @b{setjmp / longjmp} (``SJLJ''),
20674 @cindex setjmp/longjmp Exception Model
20675 @cindex SJLJ (setjmp/longjmp Exception Model)
20676 which uses dynamically-set data to establish
20677 the set of handlers
20681 This appendix summarizes which combinations of threads and exception support
20682 are supplied on various GNAT platforms.
20683 It then shows how to select a particular library either
20684 permanently or temporarily,
20685 explains the properties of (and tradeoffs among) the various threads
20686 libraries, and provides some additional
20687 information about several specific platforms.
20690 * Summary of Run-Time Configurations::
20691 * Specifying a Run-Time Library::
20692 * Choosing between Native and FSU Threads Libraries::
20693 * Choosing the Scheduling Policy::
20694 * Solaris-Specific Considerations::
20695 * IRIX-Specific Considerations::
20696 * Linux-Specific Considerations::
20700 @node Summary of Run-Time Configurations
20701 @section Summary of Run-Time Configurations
20704 @multitable @columnfractions .30 .70
20705 @item @b{alpha-openvms}
20706 @item @code{@ @ }@i{rts-native (default)}
20707 @item @code{@ @ @ @ }Tasking @tab native VMS threads
20708 @item @code{@ @ @ @ }Exceptions @tab ZCX
20711 @item @code{@ @ }@i{rts-native (default)}
20712 @item @code{@ @ @ @ }Tasking @tab native HP threads library
20713 @item @code{@ @ @ @ }Exceptions @tab ZCX
20715 @item @code{@ @ }@i{rts-sjlj}
20716 @item @code{@ @ @ @ }Tasking @tab native HP threads library
20717 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20719 @item @b{sparc-solaris} @tab
20720 @item @code{@ @ }@i{rts-native (default)}
20721 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
20722 @item @code{@ @ @ @ }Exceptions @tab ZCX
20724 @item @code{@ @ }@i{rts-fsu} @tab
20725 @item @code{@ @ @ @ }Tasking @tab FSU threads library
20726 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20728 @item @code{@ @ }@i{rts-m64}
20729 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
20730 @item @code{@ @ @ @ }Exceptions @tab ZCX
20731 @item @code{@ @ @ @ }Constraints @tab Use only when compiling in 64-bit mode;
20732 @item @tab Use only on Solaris 8 or later.
20733 @item @tab @xref{Building and Debugging 64-bit Applications}, for details.
20735 @item @code{@ @ }@i{rts-pthread}
20736 @item @code{@ @ @ @ }Tasking @tab pthreads library
20737 @item @code{@ @ @ @ }Exceptions @tab ZCX
20739 @item @code{@ @ }@i{rts-sjlj}
20740 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
20741 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20743 @item @b{x86-linux}
20744 @item @code{@ @ }@i{rts-native (default)}
20745 @item @code{@ @ @ @ }Tasking @tab LinuxThread library
20746 @item @code{@ @ @ @ }Exceptions @tab ZCX
20748 @item @code{@ @ }@i{rts-fsu}
20749 @item @code{@ @ @ @ }Tasking @tab FSU threads library
20750 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20752 @item @code{@ @ }@i{rts-sjlj}
20753 @item @code{@ @ @ @ }Tasking @tab LinuxThread library
20754 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20756 @item @b{x86-windows}
20757 @item @code{@ @ }@i{rts-native (default)}
20758 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
20759 @item @code{@ @ @ @ }Exceptions @tab SJLJ
20765 @node Specifying a Run-Time Library
20766 @section Specifying a Run-Time Library
20769 The @file{adainclude} subdirectory containing the sources of the GNAT
20770 run-time library, and the @file{adalib} subdirectory containing the
20771 @file{ALI} files and the static and/or shared GNAT library, are located
20772 in the gcc target-dependent area:
20775 target=$prefix/lib/gcc-lib/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
20779 As indicated above, on some platforms several run-time libraries are supplied.
20780 These libraries are installed in the target dependent area and
20781 contain a complete source and binary subdirectory. The detailed description
20782 below explains the differences between the different libraries in terms of
20783 their thread support.
20785 The default run-time library (when GNAT is installed) is @emph{rts-native}.
20786 This default run time is selected by the means of soft links.
20787 For example on x86-linux:
20793 +--- adainclude----------+
20795 +--- adalib-----------+ |
20797 +--- rts-native | |
20799 | +--- adainclude <---+
20801 | +--- adalib <----+
20818 If the @i{rts-fsu} library is to be selected on a permanent basis,
20819 these soft links can be modified with the following commands:
20823 $ rm -f adainclude adalib
20824 $ ln -s rts-fsu/adainclude adainclude
20825 $ ln -s rts-fsu/adalib adalib
20829 Alternatively, you can specify @file{rts-fsu/adainclude} in the file
20830 @file{$target/ada_source_path} and @file{rts-fsu/adalib} in
20831 @file{$target/ada_object_path}.
20833 Selecting another run-time library temporarily can be
20834 achieved by the regular mechanism for GNAT object or source path selection:
20838 Set the environment variables:
20841 $ ADA_INCLUDE_PATH=$target/rts-fsu/adainclude:$ADA_INCLUDE_PATH
20842 $ ADA_OBJECTS_PATH=$target/rts-fsu/adalib:$ADA_OBJECTS_PATH
20843 $ export ADA_INCLUDE_PATH ADA_OBJECTS_PATH
20847 Use @option{-aI$target/rts-fsu/adainclude}
20848 and @option{-aO$target/rts-fsu/adalib}
20849 on the @command{gnatmake} command line
20852 Use the switch @option{--RTS}; e.g., @option{--RTS=fsu}
20853 @cindex @option{--RTS} option
20857 You can similarly switch to @emph{rts-sjlj}.
20859 @node Choosing between Native and FSU Threads Libraries
20860 @section Choosing between Native and FSU Threads Libraries
20861 @cindex Native threads library
20862 @cindex FSU threads library
20865 Some GNAT implementations offer a choice between
20866 native threads and FSU threads.
20870 The @emph{native threads} library correspond to the standard system threads
20871 implementation (e.g. LinuxThreads on GNU/Linux,
20872 @cindex LinuxThreads library
20873 POSIX threads on AIX, or
20874 Solaris threads on Solaris). When this option is chosen, GNAT provides
20875 a full and accurate implementation of the core language tasking model
20876 as described in Chapter 9 of the Ada Reference Manual,
20877 but might not (and probably does not) implement
20878 the exact semantics as specified in @w{Annex D} (the Real-Time Systems Annex).
20879 @cindex Annex D (Real-Time Systems Annex) compliance
20880 @cindex Real-Time Systems Annex compliance
20881 Indeed, the reason that a choice of libraries is offered
20882 on a given target is because some of the
20883 ACATS tests for @w{Annex D} fail using the native threads library.
20884 As far as possible, this library is implemented
20885 in accordance with Ada semantics (e.g., modifying priorities as required
20886 to simulate ceiling locking),
20887 but there are often slight inaccuracies, most often in the area of
20888 absolutely respecting the priority rules on a single
20890 Moreover, it is not possible in general to define the exact behavior,
20891 because the native threads implementations
20892 are not well enough documented.
20894 On systems where the @code{SCHED_FIFO} POSIX scheduling policy is supported,
20895 @cindex POSIX scheduling policies
20896 @cindex @code{SCHED_FIFO} scheduling policy
20897 native threads will provide a behavior very close to the @w{Annex D}
20898 requirements (i.e., a run-till-blocked scheduler with fixed priorities), but
20899 on some systems (in particular GNU/Linux and Solaris), you need to have root
20900 privileges to use the @code{SCHED_FIFO} policy.
20903 The @emph{FSU threads} library provides a completely accurate implementation
20905 Thus, operating with this library, GNAT is 100% compliant with both the core
20906 and all @w{Annex D}
20908 The formal validations for implementations offering
20909 a choice of threads packages are always carried out using the FSU
20914 From these considerations, it might seem that FSU threads are the
20916 but that is by no means always the case. The FSU threads package
20917 operates with all Ada tasks appearing to the system to be a single
20918 thread. This is often considerably more efficient than operating
20919 with separate threads, since for example, switching between tasks
20920 can be accomplished without the (in some cases considerable)
20921 overhead of a context switch between two system threads. However,
20922 it means that you may well lose concurrency at the system
20923 level. Notably, some system operations (such as I/O) may block all
20924 tasks in a program and not just the calling task. More
20925 significantly, the FSU threads approach likely means you cannot
20926 take advantage of multiple processors, since for this you need
20927 separate threads (or even separate processes) to operate on
20928 different processors.
20930 For most programs, the native threads library is
20931 usually the better choice. Use the FSU threads if absolute
20932 conformance to @w{Annex D} is important for your application, or if
20933 you find that the improved efficiency of FSU threads is significant to you.
20935 Note also that to take full advantage of Florist and Glade, it is highly
20936 recommended that you use native threads.
20939 @node Choosing the Scheduling Policy
20940 @section Choosing the Scheduling Policy
20943 When using a POSIX threads implementation, you have a choice of several
20944 scheduling policies: @code{SCHED_FIFO},
20945 @cindex @code{SCHED_FIFO} scheduling policy
20947 @cindex @code{SCHED_RR} scheduling policy
20948 and @code{SCHED_OTHER}.
20949 @cindex @code{SCHED_OTHER} scheduling policy
20950 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
20951 or @code{SCHED_RR} requires special (e.g., root) privileges.
20953 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
20955 @cindex @code{SCHED_FIFO} scheduling policy
20956 you can use one of the following:
20960 @code{pragma Time_Slice (0.0)}
20961 @cindex pragma Time_Slice
20963 the corresponding binder option @option{-T0}
20964 @cindex @option{-T0} option
20966 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
20967 @cindex pragma Task_Dispatching_Policy
20971 To specify @code{SCHED_RR},
20972 @cindex @code{SCHED_RR} scheduling policy
20973 you should use @code{pragma Time_Slice} with a
20974 value greater than @code{0.0}, or else use the corresponding @option{-T}
20979 @node Solaris-Specific Considerations
20980 @section Solaris-Specific Considerations
20981 @cindex Solaris Sparc threads libraries
20984 This section addresses some topics related to the various threads libraries
20985 on Sparc Solaris and then provides some information on building and
20986 debugging 64-bit applications.
20989 * Solaris Threads Issues::
20990 * Building and Debugging 64-bit Applications::
20994 @node Solaris Threads Issues
20995 @subsection Solaris Threads Issues
20998 Starting with version 3.14, GNAT under Solaris comes with a new tasking
20999 run-time library based on POSIX threads --- @emph{rts-pthread}.
21000 @cindex rts-pthread threads library
21001 This run-time library has the advantage of being mostly shared across all
21002 POSIX-compliant thread implementations, and it also provides under
21003 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
21004 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
21005 and @code{PTHREAD_PRIO_PROTECT}
21006 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
21007 semantics that can be selected using the predefined pragma
21008 @code{Locking_Policy}
21009 @cindex pragma Locking_Policy (under rts-pthread)
21011 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
21012 @cindex @code{Inheritance_Locking} (under rts-pthread)
21013 @cindex @code{Ceiling_Locking} (under rts-pthread)
21015 As explained above, the native run-time library is based on the Solaris thread
21016 library (@code{libthread}) and is the default library.
21017 The FSU run-time library is based on the FSU threads.
21018 @cindex FSU threads library
21020 Starting with Solaris 2.5.1, when the Solaris threads library is used
21021 (this is the default), programs
21022 compiled with GNAT can automatically take advantage of
21023 and can thus execute on multiple processors.
21024 The user can alternatively specify a processor on which the program should run
21025 to emulate a single-processor system. The multiprocessor / uniprocessor choice
21027 setting the environment variable @code{GNAT_PROCESSOR}
21028 @cindex @code{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
21029 to one of the following:
21033 Use the default configuration (run the program on all
21034 available processors) - this is the same as having
21035 @code{GNAT_PROCESSOR} unset
21038 Let the run-time implementation choose one processor and run the program on
21041 @item 0 .. Last_Proc
21042 Run the program on the specified processor.
21043 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
21044 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
21048 @node Building and Debugging 64-bit Applications
21049 @subsection Building and Debugging 64-bit Applications
21052 In a 64-bit application, all the sources involved must be compiled with the
21053 @option{-m64} command-line option, and a specific GNAT library (compiled with
21054 this option) is required.
21055 The easiest way to build a 64bit application is to add
21056 @option{-m64 --RTS=m64} to the @command{gnatmake} flags.
21058 To debug these applications, dwarf-2 debug information is required, so you
21059 have to add @option{-gdwarf-2} to your gnatmake arguments.
21060 In addition, a special
21061 version of gdb, called @command{gdb64}, needs to be used.
21063 To summarize, building and debugging a ``Hello World'' program in 64-bit mode
21067 $ gnatmake -m64 -gdwarf-2 --RTS=m64 hello.adb
21073 @node IRIX-Specific Considerations
21074 @section IRIX-Specific Considerations
21075 @cindex IRIX thread library
21078 On SGI IRIX, the thread library depends on which compiler is used.
21079 The @emph{o32 ABI} compiler comes with a run-time library based on the
21080 user-level @code{athread}
21081 library. Thus kernel-level capabilities such as nonblocking system
21082 calls or time slicing can only be achieved reliably by specifying different
21083 @code{sprocs} via the pragma @code{Task_Info}
21084 @cindex pragma Task_Info (and IRIX threads)
21086 @code{System.Task_Info} package.
21087 @cindex @code{System.Task_Info} package (and IRIX threads)
21088 See the @cite{GNAT Reference Manual} for further information.
21090 The @emph{n32 ABI} compiler comes with a run-time library based on the
21091 kernel POSIX threads and thus does not have the limitations mentioned above.
21094 @node Linux-Specific Considerations
21095 @section Linux-Specific Considerations
21096 @cindex Linux threads libraries
21099 The default thread library under GNU/Linux has the following disadvantages
21100 compared to other native thread libraries:
21103 @item The size of the task's stack is limited to 2 megabytes.
21104 @item The signal model is not POSIX compliant, which means that to send a
21105 signal to the process, you need to send the signal to all threads,
21106 e.g. by using @code{killpg()}.
21111 @c *******************************
21112 @node Example of Binder Output File
21113 @appendix Example of Binder Output File
21116 This Appendix displays the source code for @command{gnatbind}'s output
21117 file generated for a simple ``Hello World'' program.
21118 Comments have been added for clarification purposes.
21121 @smallexample @c adanocomment
21125 -- The package is called Ada_Main unless this name is actually used
21126 -- as a unit name in the partition, in which case some other unique
21130 package ada_main is
21132 Elab_Final_Code : Integer;
21133 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
21135 -- The main program saves the parameters (argument count,
21136 -- argument values, environment pointer) in global variables
21137 -- for later access by other units including
21138 -- Ada.Command_Line.
21140 gnat_argc : Integer;
21141 gnat_argv : System.Address;
21142 gnat_envp : System.Address;
21144 -- The actual variables are stored in a library routine. This
21145 -- is useful for some shared library situations, where there
21146 -- are problems if variables are not in the library.
21148 pragma Import (C, gnat_argc);
21149 pragma Import (C, gnat_argv);
21150 pragma Import (C, gnat_envp);
21152 -- The exit status is similarly an external location
21154 gnat_exit_status : Integer;
21155 pragma Import (C, gnat_exit_status);
21157 GNAT_Version : constant String :=
21158 "GNAT Version: 3.15w (20010315)";
21159 pragma Export (C, GNAT_Version, "__gnat_version");
21161 -- This is the generated adafinal routine that performs
21162 -- finalization at the end of execution. In the case where
21163 -- Ada is the main program, this main program makes a call
21164 -- to adafinal at program termination.
21166 procedure adafinal;
21167 pragma Export (C, adafinal, "adafinal");
21169 -- This is the generated adainit routine that performs
21170 -- initialization at the start of execution. In the case
21171 -- where Ada is the main program, this main program makes
21172 -- a call to adainit at program startup.
21175 pragma Export (C, adainit, "adainit");
21177 -- This routine is called at the start of execution. It is
21178 -- a dummy routine that is used by the debugger to breakpoint
21179 -- at the start of execution.
21181 procedure Break_Start;
21182 pragma Import (C, Break_Start, "__gnat_break_start");
21184 -- This is the actual generated main program (it would be
21185 -- suppressed if the no main program switch were used). As
21186 -- required by standard system conventions, this program has
21187 -- the external name main.
21191 argv : System.Address;
21192 envp : System.Address)
21194 pragma Export (C, main, "main");
21196 -- The following set of constants give the version
21197 -- identification values for every unit in the bound
21198 -- partition. This identification is computed from all
21199 -- dependent semantic units, and corresponds to the
21200 -- string that would be returned by use of the
21201 -- Body_Version or Version attributes.
21203 type Version_32 is mod 2 ** 32;
21204 u00001 : constant Version_32 := 16#7880BEB3#;
21205 u00002 : constant Version_32 := 16#0D24CBD0#;
21206 u00003 : constant Version_32 := 16#3283DBEB#;
21207 u00004 : constant Version_32 := 16#2359F9ED#;
21208 u00005 : constant Version_32 := 16#664FB847#;
21209 u00006 : constant Version_32 := 16#68E803DF#;
21210 u00007 : constant Version_32 := 16#5572E604#;
21211 u00008 : constant Version_32 := 16#46B173D8#;
21212 u00009 : constant Version_32 := 16#156A40CF#;
21213 u00010 : constant Version_32 := 16#033DABE0#;
21214 u00011 : constant Version_32 := 16#6AB38FEA#;
21215 u00012 : constant Version_32 := 16#22B6217D#;
21216 u00013 : constant Version_32 := 16#68A22947#;
21217 u00014 : constant Version_32 := 16#18CC4A56#;
21218 u00015 : constant Version_32 := 16#08258E1B#;
21219 u00016 : constant Version_32 := 16#367D5222#;
21220 u00017 : constant Version_32 := 16#20C9ECA4#;
21221 u00018 : constant Version_32 := 16#50D32CB6#;
21222 u00019 : constant Version_32 := 16#39A8BB77#;
21223 u00020 : constant Version_32 := 16#5CF8FA2B#;
21224 u00021 : constant Version_32 := 16#2F1EB794#;
21225 u00022 : constant Version_32 := 16#31AB6444#;
21226 u00023 : constant Version_32 := 16#1574B6E9#;
21227 u00024 : constant Version_32 := 16#5109C189#;
21228 u00025 : constant Version_32 := 16#56D770CD#;
21229 u00026 : constant Version_32 := 16#02F9DE3D#;
21230 u00027 : constant Version_32 := 16#08AB6B2C#;
21231 u00028 : constant Version_32 := 16#3FA37670#;
21232 u00029 : constant Version_32 := 16#476457A0#;
21233 u00030 : constant Version_32 := 16#731E1B6E#;
21234 u00031 : constant Version_32 := 16#23C2E789#;
21235 u00032 : constant Version_32 := 16#0F1BD6A1#;
21236 u00033 : constant Version_32 := 16#7C25DE96#;
21237 u00034 : constant Version_32 := 16#39ADFFA2#;
21238 u00035 : constant Version_32 := 16#571DE3E7#;
21239 u00036 : constant Version_32 := 16#5EB646AB#;
21240 u00037 : constant Version_32 := 16#4249379B#;
21241 u00038 : constant Version_32 := 16#0357E00A#;
21242 u00039 : constant Version_32 := 16#3784FB72#;
21243 u00040 : constant Version_32 := 16#2E723019#;
21244 u00041 : constant Version_32 := 16#623358EA#;
21245 u00042 : constant Version_32 := 16#107F9465#;
21246 u00043 : constant Version_32 := 16#6843F68A#;
21247 u00044 : constant Version_32 := 16#63305874#;
21248 u00045 : constant Version_32 := 16#31E56CE1#;
21249 u00046 : constant Version_32 := 16#02917970#;
21250 u00047 : constant Version_32 := 16#6CCBA70E#;
21251 u00048 : constant Version_32 := 16#41CD4204#;
21252 u00049 : constant Version_32 := 16#572E3F58#;
21253 u00050 : constant Version_32 := 16#20729FF5#;
21254 u00051 : constant Version_32 := 16#1D4F93E8#;
21255 u00052 : constant Version_32 := 16#30B2EC3D#;
21256 u00053 : constant Version_32 := 16#34054F96#;
21257 u00054 : constant Version_32 := 16#5A199860#;
21258 u00055 : constant Version_32 := 16#0E7F912B#;
21259 u00056 : constant Version_32 := 16#5760634A#;
21260 u00057 : constant Version_32 := 16#5D851835#;
21262 -- The following Export pragmas export the version numbers
21263 -- with symbolic names ending in B (for body) or S
21264 -- (for spec) so that they can be located in a link. The
21265 -- information provided here is sufficient to track down
21266 -- the exact versions of units used in a given build.
21268 pragma Export (C, u00001, "helloB");
21269 pragma Export (C, u00002, "system__standard_libraryB");
21270 pragma Export (C, u00003, "system__standard_libraryS");
21271 pragma Export (C, u00004, "adaS");
21272 pragma Export (C, u00005, "ada__text_ioB");
21273 pragma Export (C, u00006, "ada__text_ioS");
21274 pragma Export (C, u00007, "ada__exceptionsB");
21275 pragma Export (C, u00008, "ada__exceptionsS");
21276 pragma Export (C, u00009, "gnatS");
21277 pragma Export (C, u00010, "gnat__heap_sort_aB");
21278 pragma Export (C, u00011, "gnat__heap_sort_aS");
21279 pragma Export (C, u00012, "systemS");
21280 pragma Export (C, u00013, "system__exception_tableB");
21281 pragma Export (C, u00014, "system__exception_tableS");
21282 pragma Export (C, u00015, "gnat__htableB");
21283 pragma Export (C, u00016, "gnat__htableS");
21284 pragma Export (C, u00017, "system__exceptionsS");
21285 pragma Export (C, u00018, "system__machine_state_operationsB");
21286 pragma Export (C, u00019, "system__machine_state_operationsS");
21287 pragma Export (C, u00020, "system__machine_codeS");
21288 pragma Export (C, u00021, "system__storage_elementsB");
21289 pragma Export (C, u00022, "system__storage_elementsS");
21290 pragma Export (C, u00023, "system__secondary_stackB");
21291 pragma Export (C, u00024, "system__secondary_stackS");
21292 pragma Export (C, u00025, "system__parametersB");
21293 pragma Export (C, u00026, "system__parametersS");
21294 pragma Export (C, u00027, "system__soft_linksB");
21295 pragma Export (C, u00028, "system__soft_linksS");
21296 pragma Export (C, u00029, "system__stack_checkingB");
21297 pragma Export (C, u00030, "system__stack_checkingS");
21298 pragma Export (C, u00031, "system__tracebackB");
21299 pragma Export (C, u00032, "system__tracebackS");
21300 pragma Export (C, u00033, "ada__streamsS");
21301 pragma Export (C, u00034, "ada__tagsB");
21302 pragma Export (C, u00035, "ada__tagsS");
21303 pragma Export (C, u00036, "system__string_opsB");
21304 pragma Export (C, u00037, "system__string_opsS");
21305 pragma Export (C, u00038, "interfacesS");
21306 pragma Export (C, u00039, "interfaces__c_streamsB");
21307 pragma Export (C, u00040, "interfaces__c_streamsS");
21308 pragma Export (C, u00041, "system__file_ioB");
21309 pragma Export (C, u00042, "system__file_ioS");
21310 pragma Export (C, u00043, "ada__finalizationB");
21311 pragma Export (C, u00044, "ada__finalizationS");
21312 pragma Export (C, u00045, "system__finalization_rootB");
21313 pragma Export (C, u00046, "system__finalization_rootS");
21314 pragma Export (C, u00047, "system__finalization_implementationB");
21315 pragma Export (C, u00048, "system__finalization_implementationS");
21316 pragma Export (C, u00049, "system__string_ops_concat_3B");
21317 pragma Export (C, u00050, "system__string_ops_concat_3S");
21318 pragma Export (C, u00051, "system__stream_attributesB");
21319 pragma Export (C, u00052, "system__stream_attributesS");
21320 pragma Export (C, u00053, "ada__io_exceptionsS");
21321 pragma Export (C, u00054, "system__unsigned_typesS");
21322 pragma Export (C, u00055, "system__file_control_blockS");
21323 pragma Export (C, u00056, "ada__finalization__list_controllerB");
21324 pragma Export (C, u00057, "ada__finalization__list_controllerS");
21326 -- BEGIN ELABORATION ORDER
21329 -- gnat.heap_sort_a (spec)
21330 -- gnat.heap_sort_a (body)
21331 -- gnat.htable (spec)
21332 -- gnat.htable (body)
21333 -- interfaces (spec)
21335 -- system.machine_code (spec)
21336 -- system.parameters (spec)
21337 -- system.parameters (body)
21338 -- interfaces.c_streams (spec)
21339 -- interfaces.c_streams (body)
21340 -- system.standard_library (spec)
21341 -- ada.exceptions (spec)
21342 -- system.exception_table (spec)
21343 -- system.exception_table (body)
21344 -- ada.io_exceptions (spec)
21345 -- system.exceptions (spec)
21346 -- system.storage_elements (spec)
21347 -- system.storage_elements (body)
21348 -- system.machine_state_operations (spec)
21349 -- system.machine_state_operations (body)
21350 -- system.secondary_stack (spec)
21351 -- system.stack_checking (spec)
21352 -- system.soft_links (spec)
21353 -- system.soft_links (body)
21354 -- system.stack_checking (body)
21355 -- system.secondary_stack (body)
21356 -- system.standard_library (body)
21357 -- system.string_ops (spec)
21358 -- system.string_ops (body)
21361 -- ada.streams (spec)
21362 -- system.finalization_root (spec)
21363 -- system.finalization_root (body)
21364 -- system.string_ops_concat_3 (spec)
21365 -- system.string_ops_concat_3 (body)
21366 -- system.traceback (spec)
21367 -- system.traceback (body)
21368 -- ada.exceptions (body)
21369 -- system.unsigned_types (spec)
21370 -- system.stream_attributes (spec)
21371 -- system.stream_attributes (body)
21372 -- system.finalization_implementation (spec)
21373 -- system.finalization_implementation (body)
21374 -- ada.finalization (spec)
21375 -- ada.finalization (body)
21376 -- ada.finalization.list_controller (spec)
21377 -- ada.finalization.list_controller (body)
21378 -- system.file_control_block (spec)
21379 -- system.file_io (spec)
21380 -- system.file_io (body)
21381 -- ada.text_io (spec)
21382 -- ada.text_io (body)
21384 -- END ELABORATION ORDER
21388 -- The following source file name pragmas allow the generated file
21389 -- names to be unique for different main programs. They are needed
21390 -- since the package name will always be Ada_Main.
21392 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
21393 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
21395 -- Generated package body for Ada_Main starts here
21397 package body ada_main is
21399 -- The actual finalization is performed by calling the
21400 -- library routine in System.Standard_Library.Adafinal
21402 procedure Do_Finalize;
21403 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
21410 procedure adainit is
21412 -- These booleans are set to True once the associated unit has
21413 -- been elaborated. It is also used to avoid elaborating the
21414 -- same unit twice.
21417 pragma Import (Ada, E040, "interfaces__c_streams_E");
21420 pragma Import (Ada, E008, "ada__exceptions_E");
21423 pragma Import (Ada, E014, "system__exception_table_E");
21426 pragma Import (Ada, E053, "ada__io_exceptions_E");
21429 pragma Import (Ada, E017, "system__exceptions_E");
21432 pragma Import (Ada, E024, "system__secondary_stack_E");
21435 pragma Import (Ada, E030, "system__stack_checking_E");
21438 pragma Import (Ada, E028, "system__soft_links_E");
21441 pragma Import (Ada, E035, "ada__tags_E");
21444 pragma Import (Ada, E033, "ada__streams_E");
21447 pragma Import (Ada, E046, "system__finalization_root_E");
21450 pragma Import (Ada, E048, "system__finalization_implementation_E");
21453 pragma Import (Ada, E044, "ada__finalization_E");
21456 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
21459 pragma Import (Ada, E055, "system__file_control_block_E");
21462 pragma Import (Ada, E042, "system__file_io_E");
21465 pragma Import (Ada, E006, "ada__text_io_E");
21467 -- Set_Globals is a library routine that stores away the
21468 -- value of the indicated set of global values in global
21469 -- variables within the library.
21471 procedure Set_Globals
21472 (Main_Priority : Integer;
21473 Time_Slice_Value : Integer;
21474 WC_Encoding : Character;
21475 Locking_Policy : Character;
21476 Queuing_Policy : Character;
21477 Task_Dispatching_Policy : Character;
21478 Adafinal : System.Address;
21479 Unreserve_All_Interrupts : Integer;
21480 Exception_Tracebacks : Integer);
21481 @findex __gnat_set_globals
21482 pragma Import (C, Set_Globals, "__gnat_set_globals");
21484 -- SDP_Table_Build is a library routine used to build the
21485 -- exception tables. See unit Ada.Exceptions in files
21486 -- a-except.ads/adb for full details of how zero cost
21487 -- exception handling works. This procedure, the call to
21488 -- it, and the two following tables are all omitted if the
21489 -- build is in longjmp/setjump exception mode.
21491 @findex SDP_Table_Build
21492 @findex Zero Cost Exceptions
21493 procedure SDP_Table_Build
21494 (SDP_Addresses : System.Address;
21495 SDP_Count : Natural;
21496 Elab_Addresses : System.Address;
21497 Elab_Addr_Count : Natural);
21498 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
21500 -- Table of Unit_Exception_Table addresses. Used for zero
21501 -- cost exception handling to build the top level table.
21503 ST : aliased constant array (1 .. 23) of System.Address := (
21505 Ada.Text_Io'UET_Address,
21506 Ada.Exceptions'UET_Address,
21507 Gnat.Heap_Sort_A'UET_Address,
21508 System.Exception_Table'UET_Address,
21509 System.Machine_State_Operations'UET_Address,
21510 System.Secondary_Stack'UET_Address,
21511 System.Parameters'UET_Address,
21512 System.Soft_Links'UET_Address,
21513 System.Stack_Checking'UET_Address,
21514 System.Traceback'UET_Address,
21515 Ada.Streams'UET_Address,
21516 Ada.Tags'UET_Address,
21517 System.String_Ops'UET_Address,
21518 Interfaces.C_Streams'UET_Address,
21519 System.File_Io'UET_Address,
21520 Ada.Finalization'UET_Address,
21521 System.Finalization_Root'UET_Address,
21522 System.Finalization_Implementation'UET_Address,
21523 System.String_Ops_Concat_3'UET_Address,
21524 System.Stream_Attributes'UET_Address,
21525 System.File_Control_Block'UET_Address,
21526 Ada.Finalization.List_Controller'UET_Address);
21528 -- Table of addresses of elaboration routines. Used for
21529 -- zero cost exception handling to make sure these
21530 -- addresses are included in the top level procedure
21533 EA : aliased constant array (1 .. 23) of System.Address := (
21534 adainit'Code_Address,
21535 Do_Finalize'Code_Address,
21536 Ada.Exceptions'Elab_Spec'Address,
21537 System.Exceptions'Elab_Spec'Address,
21538 Interfaces.C_Streams'Elab_Spec'Address,
21539 System.Exception_Table'Elab_Body'Address,
21540 Ada.Io_Exceptions'Elab_Spec'Address,
21541 System.Stack_Checking'Elab_Spec'Address,
21542 System.Soft_Links'Elab_Body'Address,
21543 System.Secondary_Stack'Elab_Body'Address,
21544 Ada.Tags'Elab_Spec'Address,
21545 Ada.Tags'Elab_Body'Address,
21546 Ada.Streams'Elab_Spec'Address,
21547 System.Finalization_Root'Elab_Spec'Address,
21548 Ada.Exceptions'Elab_Body'Address,
21549 System.Finalization_Implementation'Elab_Spec'Address,
21550 System.Finalization_Implementation'Elab_Body'Address,
21551 Ada.Finalization'Elab_Spec'Address,
21552 Ada.Finalization.List_Controller'Elab_Spec'Address,
21553 System.File_Control_Block'Elab_Spec'Address,
21554 System.File_Io'Elab_Body'Address,
21555 Ada.Text_Io'Elab_Spec'Address,
21556 Ada.Text_Io'Elab_Body'Address);
21558 -- Start of processing for adainit
21562 -- Call SDP_Table_Build to build the top level procedure
21563 -- table for zero cost exception handling (omitted in
21564 -- longjmp/setjump mode).
21566 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
21568 -- Call Set_Globals to record various information for
21569 -- this partition. The values are derived by the binder
21570 -- from information stored in the ali files by the compiler.
21572 @findex __gnat_set_globals
21574 (Main_Priority => -1,
21575 -- Priority of main program, -1 if no pragma Priority used
21577 Time_Slice_Value => -1,
21578 -- Time slice from Time_Slice pragma, -1 if none used
21580 WC_Encoding => 'b',
21581 -- Wide_Character encoding used, default is brackets
21583 Locking_Policy => ' ',
21584 -- Locking_Policy used, default of space means not
21585 -- specified, otherwise it is the first character of
21586 -- the policy name.
21588 Queuing_Policy => ' ',
21589 -- Queuing_Policy used, default of space means not
21590 -- specified, otherwise it is the first character of
21591 -- the policy name.
21593 Task_Dispatching_Policy => ' ',
21594 -- Task_Dispatching_Policy used, default of space means
21595 -- not specified, otherwise first character of the
21598 Adafinal => System.Null_Address,
21599 -- Address of Adafinal routine, not used anymore
21601 Unreserve_All_Interrupts => 0,
21602 -- Set true if pragma Unreserve_All_Interrupts was used
21604 Exception_Tracebacks => 0);
21605 -- Indicates if exception tracebacks are enabled
21607 Elab_Final_Code := 1;
21609 -- Now we have the elaboration calls for all units in the partition.
21610 -- The Elab_Spec and Elab_Body attributes generate references to the
21611 -- implicit elaboration procedures generated by the compiler for
21612 -- each unit that requires elaboration.
21615 Interfaces.C_Streams'Elab_Spec;
21619 Ada.Exceptions'Elab_Spec;
21622 System.Exception_Table'Elab_Body;
21626 Ada.Io_Exceptions'Elab_Spec;
21630 System.Exceptions'Elab_Spec;
21634 System.Stack_Checking'Elab_Spec;
21637 System.Soft_Links'Elab_Body;
21642 System.Secondary_Stack'Elab_Body;
21646 Ada.Tags'Elab_Spec;
21649 Ada.Tags'Elab_Body;
21653 Ada.Streams'Elab_Spec;
21657 System.Finalization_Root'Elab_Spec;
21661 Ada.Exceptions'Elab_Body;
21665 System.Finalization_Implementation'Elab_Spec;
21668 System.Finalization_Implementation'Elab_Body;
21672 Ada.Finalization'Elab_Spec;
21676 Ada.Finalization.List_Controller'Elab_Spec;
21680 System.File_Control_Block'Elab_Spec;
21684 System.File_Io'Elab_Body;
21688 Ada.Text_Io'Elab_Spec;
21691 Ada.Text_Io'Elab_Body;
21695 Elab_Final_Code := 0;
21703 procedure adafinal is
21712 -- main is actually a function, as in the ANSI C standard,
21713 -- defined to return the exit status. The three parameters
21714 -- are the argument count, argument values and environment
21717 @findex Main Program
21720 argv : System.Address;
21721 envp : System.Address)
21724 -- The initialize routine performs low level system
21725 -- initialization using a standard library routine which
21726 -- sets up signal handling and performs any other
21727 -- required setup. The routine can be found in file
21730 @findex __gnat_initialize
21731 procedure initialize;
21732 pragma Import (C, initialize, "__gnat_initialize");
21734 -- The finalize routine performs low level system
21735 -- finalization using a standard library routine. The
21736 -- routine is found in file a-final.c and in the standard
21737 -- distribution is a dummy routine that does nothing, so
21738 -- really this is a hook for special user finalization.
21740 @findex __gnat_finalize
21741 procedure finalize;
21742 pragma Import (C, finalize, "__gnat_finalize");
21744 -- We get to the main program of the partition by using
21745 -- pragma Import because if we try to with the unit and
21746 -- call it Ada style, then not only do we waste time
21747 -- recompiling it, but also, we don't really know the right
21748 -- switches (e.g. identifier character set) to be used
21751 procedure Ada_Main_Program;
21752 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
21754 -- Start of processing for main
21757 -- Save global variables
21763 -- Call low level system initialization
21767 -- Call our generated Ada initialization routine
21771 -- This is the point at which we want the debugger to get
21776 -- Now we call the main program of the partition
21780 -- Perform Ada finalization
21784 -- Perform low level system finalization
21788 -- Return the proper exit status
21789 return (gnat_exit_status);
21792 -- This section is entirely comments, so it has no effect on the
21793 -- compilation of the Ada_Main package. It provides the list of
21794 -- object files and linker options, as well as some standard
21795 -- libraries needed for the link. The gnatlink utility parses
21796 -- this b~hello.adb file to read these comment lines to generate
21797 -- the appropriate command line arguments for the call to the
21798 -- system linker. The BEGIN/END lines are used for sentinels for
21799 -- this parsing operation.
21801 -- The exact file names will of course depend on the environment,
21802 -- host/target and location of files on the host system.
21804 @findex Object file list
21805 -- BEGIN Object file/option list
21808 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
21809 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
21810 -- END Object file/option list
21816 The Ada code in the above example is exactly what is generated by the
21817 binder. We have added comments to more clearly indicate the function
21818 of each part of the generated @code{Ada_Main} package.
21820 The code is standard Ada in all respects, and can be processed by any
21821 tools that handle Ada. In particular, it is possible to use the debugger
21822 in Ada mode to debug the generated @code{Ada_Main} package. For example,
21823 suppose that for reasons that you do not understand, your program is crashing
21824 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
21825 you can place a breakpoint on the call:
21827 @smallexample @c ada
21828 Ada.Text_Io'Elab_Body;
21832 and trace the elaboration routine for this package to find out where
21833 the problem might be (more usually of course you would be debugging
21834 elaboration code in your own application).
21837 @node Elaboration Order Handling in GNAT
21838 @appendix Elaboration Order Handling in GNAT
21839 @cindex Order of elaboration
21840 @cindex Elaboration control
21843 * Elaboration Code in Ada 95::
21844 * Checking the Elaboration Order in Ada 95::
21845 * Controlling the Elaboration Order in Ada 95::
21846 * Controlling Elaboration in GNAT - Internal Calls::
21847 * Controlling Elaboration in GNAT - External Calls::
21848 * Default Behavior in GNAT - Ensuring Safety::
21849 * Treatment of Pragma Elaborate::
21850 * Elaboration Issues for Library Tasks::
21851 * Mixing Elaboration Models::
21852 * What to Do If the Default Elaboration Behavior Fails::
21853 * Elaboration for Access-to-Subprogram Values::
21854 * Summary of Procedures for Elaboration Control::
21855 * Other Elaboration Order Considerations::
21859 This chapter describes the handling of elaboration code in Ada 95 and
21860 in GNAT, and discusses how the order of elaboration of program units can
21861 be controlled in GNAT, either automatically or with explicit programming
21864 @node Elaboration Code in Ada 95
21865 @section Elaboration Code in Ada 95
21868 Ada 95 provides rather general mechanisms for executing code at elaboration
21869 time, that is to say before the main program starts executing. Such code arises
21873 @item Initializers for variables.
21874 Variables declared at the library level, in package specs or bodies, can
21875 require initialization that is performed at elaboration time, as in:
21876 @smallexample @c ada
21878 Sqrt_Half : Float := Sqrt (0.5);
21882 @item Package initialization code
21883 Code in a @code{BEGIN-END} section at the outer level of a package body is
21884 executed as part of the package body elaboration code.
21886 @item Library level task allocators
21887 Tasks that are declared using task allocators at the library level
21888 start executing immediately and hence can execute at elaboration time.
21892 Subprogram calls are possible in any of these contexts, which means that
21893 any arbitrary part of the program may be executed as part of the elaboration
21894 code. It is even possible to write a program which does all its work at
21895 elaboration time, with a null main program, although stylistically this
21896 would usually be considered an inappropriate way to structure
21899 An important concern arises in the context of elaboration code:
21900 we have to be sure that it is executed in an appropriate order. What we
21901 have is a series of elaboration code sections, potentially one section
21902 for each unit in the program. It is important that these execute
21903 in the correct order. Correctness here means that, taking the above
21904 example of the declaration of @code{Sqrt_Half},
21905 if some other piece of
21906 elaboration code references @code{Sqrt_Half},
21907 then it must run after the
21908 section of elaboration code that contains the declaration of
21911 There would never be any order of elaboration problem if we made a rule
21912 that whenever you @code{with} a unit, you must elaborate both the spec and body
21913 of that unit before elaborating the unit doing the @code{with}'ing:
21915 @smallexample @c ada
21919 package Unit_2 is ...
21925 would require that both the body and spec of @code{Unit_1} be elaborated
21926 before the spec of @code{Unit_2}. However, a rule like that would be far too
21927 restrictive. In particular, it would make it impossible to have routines
21928 in separate packages that were mutually recursive.
21930 You might think that a clever enough compiler could look at the actual
21931 elaboration code and determine an appropriate correct order of elaboration,
21932 but in the general case, this is not possible. Consider the following
21935 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
21937 the variable @code{Sqrt_1}, which is declared in the elaboration code
21938 of the body of @code{Unit_1}:
21940 @smallexample @c ada
21942 Sqrt_1 : Float := Sqrt (0.1);
21947 The elaboration code of the body of @code{Unit_1} also contains:
21949 @smallexample @c ada
21952 if expression_1 = 1 then
21953 Q := Unit_2.Func_2;
21960 @code{Unit_2} is exactly parallel,
21961 it has a procedure @code{Func_2} that references
21962 the variable @code{Sqrt_2}, which is declared in the elaboration code of
21963 the body @code{Unit_2}:
21965 @smallexample @c ada
21967 Sqrt_2 : Float := Sqrt (0.1);
21972 The elaboration code of the body of @code{Unit_2} also contains:
21974 @smallexample @c ada
21977 if expression_2 = 2 then
21978 Q := Unit_1.Func_1;
21985 Now the question is, which of the following orders of elaboration is
22010 If you carefully analyze the flow here, you will see that you cannot tell
22011 at compile time the answer to this question.
22012 If @code{expression_1} is not equal to 1,
22013 and @code{expression_2} is not equal to 2,
22014 then either order is acceptable, because neither of the function calls is
22015 executed. If both tests evaluate to true, then neither order is acceptable
22016 and in fact there is no correct order.
22018 If one of the two expressions is true, and the other is false, then one
22019 of the above orders is correct, and the other is incorrect. For example,
22020 if @code{expression_1} = 1 and @code{expression_2} /= 2,
22021 then the call to @code{Func_2}
22022 will occur, but not the call to @code{Func_1.}
22023 This means that it is essential
22024 to elaborate the body of @code{Unit_1} before
22025 the body of @code{Unit_2}, so the first
22026 order of elaboration is correct and the second is wrong.
22028 By making @code{expression_1} and @code{expression_2}
22029 depend on input data, or perhaps
22030 the time of day, we can make it impossible for the compiler or binder
22031 to figure out which of these expressions will be true, and hence it
22032 is impossible to guarantee a safe order of elaboration at run time.
22034 @node Checking the Elaboration Order in Ada 95
22035 @section Checking the Elaboration Order in Ada 95
22038 In some languages that involve the same kind of elaboration problems,
22039 e.g. Java and C++, the programmer is expected to worry about these
22040 ordering problems himself, and it is common to
22041 write a program in which an incorrect elaboration order gives
22042 surprising results, because it references variables before they
22044 Ada 95 is designed to be a safe language, and a programmer-beware approach is
22045 clearly not sufficient. Consequently, the language provides three lines
22049 @item Standard rules
22050 Some standard rules restrict the possible choice of elaboration
22051 order. In particular, if you @code{with} a unit, then its spec is always
22052 elaborated before the unit doing the @code{with}. Similarly, a parent
22053 spec is always elaborated before the child spec, and finally
22054 a spec is always elaborated before its corresponding body.
22056 @item Dynamic elaboration checks
22057 @cindex Elaboration checks
22058 @cindex Checks, elaboration
22059 Dynamic checks are made at run time, so that if some entity is accessed
22060 before it is elaborated (typically by means of a subprogram call)
22061 then the exception (@code{Program_Error}) is raised.
22063 @item Elaboration control
22064 Facilities are provided for the programmer to specify the desired order
22068 Let's look at these facilities in more detail. First, the rules for
22069 dynamic checking. One possible rule would be simply to say that the
22070 exception is raised if you access a variable which has not yet been
22071 elaborated. The trouble with this approach is that it could require
22072 expensive checks on every variable reference. Instead Ada 95 has two
22073 rules which are a little more restrictive, but easier to check, and
22077 @item Restrictions on calls
22078 A subprogram can only be called at elaboration time if its body
22079 has been elaborated. The rules for elaboration given above guarantee
22080 that the spec of the subprogram has been elaborated before the
22081 call, but not the body. If this rule is violated, then the
22082 exception @code{Program_Error} is raised.
22084 @item Restrictions on instantiations
22085 A generic unit can only be instantiated if the body of the generic
22086 unit has been elaborated. Again, the rules for elaboration given above
22087 guarantee that the spec of the generic unit has been elaborated
22088 before the instantiation, but not the body. If this rule is
22089 violated, then the exception @code{Program_Error} is raised.
22093 The idea is that if the body has been elaborated, then any variables
22094 it references must have been elaborated; by checking for the body being
22095 elaborated we guarantee that none of its references causes any
22096 trouble. As we noted above, this is a little too restrictive, because a
22097 subprogram that has no non-local references in its body may in fact be safe
22098 to call. However, it really would be unsafe to rely on this, because
22099 it would mean that the caller was aware of details of the implementation
22100 in the body. This goes against the basic tenets of Ada.
22102 A plausible implementation can be described as follows.
22103 A Boolean variable is associated with each subprogram
22104 and each generic unit. This variable is initialized to False, and is set to
22105 True at the point body is elaborated. Every call or instantiation checks the
22106 variable, and raises @code{Program_Error} if the variable is False.
22108 Note that one might think that it would be good enough to have one Boolean
22109 variable for each package, but that would not deal with cases of trying
22110 to call a body in the same package as the call
22111 that has not been elaborated yet.
22112 Of course a compiler may be able to do enough analysis to optimize away
22113 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
22114 does such optimizations, but still the easiest conceptual model is to
22115 think of there being one variable per subprogram.
22117 @node Controlling the Elaboration Order in Ada 95
22118 @section Controlling the Elaboration Order in Ada 95
22121 In the previous section we discussed the rules in Ada 95 which ensure
22122 that @code{Program_Error} is raised if an incorrect elaboration order is
22123 chosen. This prevents erroneous executions, but we need mechanisms to
22124 specify a correct execution and avoid the exception altogether.
22125 To achieve this, Ada 95 provides a number of features for controlling
22126 the order of elaboration. We discuss these features in this section.
22128 First, there are several ways of indicating to the compiler that a given
22129 unit has no elaboration problems:
22132 @item packages that do not require a body
22133 In Ada 95, a library package that does not require a body does not permit
22134 a body. This means that if we have a such a package, as in:
22136 @smallexample @c ada
22139 package Definitions is
22141 type m is new integer;
22143 type a is array (1 .. 10) of m;
22144 type b is array (1 .. 20) of m;
22152 A package that @code{with}'s @code{Definitions} may safely instantiate
22153 @code{Definitions.Subp} because the compiler can determine that there
22154 definitely is no package body to worry about in this case
22157 @cindex pragma Pure
22159 Places sufficient restrictions on a unit to guarantee that
22160 no call to any subprogram in the unit can result in an
22161 elaboration problem. This means that the compiler does not need
22162 to worry about the point of elaboration of such units, and in
22163 particular, does not need to check any calls to any subprograms
22166 @item pragma Preelaborate
22167 @findex Preelaborate
22168 @cindex pragma Preelaborate
22169 This pragma places slightly less stringent restrictions on a unit than
22171 but these restrictions are still sufficient to ensure that there
22172 are no elaboration problems with any calls to the unit.
22174 @item pragma Elaborate_Body
22175 @findex Elaborate_Body
22176 @cindex pragma Elaborate_Body
22177 This pragma requires that the body of a unit be elaborated immediately
22178 after its spec. Suppose a unit @code{A} has such a pragma,
22179 and unit @code{B} does
22180 a @code{with} of unit @code{A}. Recall that the standard rules require
22181 the spec of unit @code{A}
22182 to be elaborated before the @code{with}'ing unit; given the pragma in
22183 @code{A}, we also know that the body of @code{A}
22184 will be elaborated before @code{B}, so
22185 that calls to @code{A} are safe and do not need a check.
22190 unlike pragma @code{Pure} and pragma @code{Preelaborate},
22192 @code{Elaborate_Body} does not guarantee that the program is
22193 free of elaboration problems, because it may not be possible
22194 to satisfy the requested elaboration order.
22195 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
22197 marks @code{Unit_1} as @code{Elaborate_Body},
22198 and not @code{Unit_2,} then the order of
22199 elaboration will be:
22211 Now that means that the call to @code{Func_1} in @code{Unit_2}
22212 need not be checked,
22213 it must be safe. But the call to @code{Func_2} in
22214 @code{Unit_1} may still fail if
22215 @code{Expression_1} is equal to 1,
22216 and the programmer must still take
22217 responsibility for this not being the case.
22219 If all units carry a pragma @code{Elaborate_Body}, then all problems are
22220 eliminated, except for calls entirely within a body, which are
22221 in any case fully under programmer control. However, using the pragma
22222 everywhere is not always possible.
22223 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
22224 we marked both of them as having pragma @code{Elaborate_Body}, then
22225 clearly there would be no possible elaboration order.
22227 The above pragmas allow a server to guarantee safe use by clients, and
22228 clearly this is the preferable approach. Consequently a good rule in
22229 Ada 95 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
22230 and if this is not possible,
22231 mark them as @code{Elaborate_Body} if possible.
22232 As we have seen, there are situations where neither of these
22233 three pragmas can be used.
22234 So we also provide methods for clients to control the
22235 order of elaboration of the servers on which they depend:
22238 @item pragma Elaborate (unit)
22240 @cindex pragma Elaborate
22241 This pragma is placed in the context clause, after a @code{with} clause,
22242 and it requires that the body of the named unit be elaborated before
22243 the unit in which the pragma occurs. The idea is to use this pragma
22244 if the current unit calls at elaboration time, directly or indirectly,
22245 some subprogram in the named unit.
22247 @item pragma Elaborate_All (unit)
22248 @findex Elaborate_All
22249 @cindex pragma Elaborate_All
22250 This is a stronger version of the Elaborate pragma. Consider the
22254 Unit A @code{with}'s unit B and calls B.Func in elab code
22255 Unit B @code{with}'s unit C, and B.Func calls C.Func
22259 Now if we put a pragma @code{Elaborate (B)}
22260 in unit @code{A}, this ensures that the
22261 body of @code{B} is elaborated before the call, but not the
22262 body of @code{C}, so
22263 the call to @code{C.Func} could still cause @code{Program_Error} to
22266 The effect of a pragma @code{Elaborate_All} is stronger, it requires
22267 not only that the body of the named unit be elaborated before the
22268 unit doing the @code{with}, but also the bodies of all units that the
22269 named unit uses, following @code{with} links transitively. For example,
22270 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
22272 not only that the body of @code{B} be elaborated before @code{A},
22274 body of @code{C}, because @code{B} @code{with}'s @code{C}.
22278 We are now in a position to give a usage rule in Ada 95 for avoiding
22279 elaboration problems, at least if dynamic dispatching and access to
22280 subprogram values are not used. We will handle these cases separately
22283 The rule is simple. If a unit has elaboration code that can directly or
22284 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
22285 a generic unit in a @code{with}'ed unit,
22286 then if the @code{with}'ed unit does not have
22287 pragma @code{Pure} or @code{Preelaborate}, then the client should have
22288 a pragma @code{Elaborate_All}
22289 for the @code{with}'ed unit. By following this rule a client is
22290 assured that calls can be made without risk of an exception.
22291 If this rule is not followed, then a program may be in one of four
22295 @item No order exists
22296 No order of elaboration exists which follows the rules, taking into
22297 account any @code{Elaborate}, @code{Elaborate_All},
22298 or @code{Elaborate_Body} pragmas. In
22299 this case, an Ada 95 compiler must diagnose the situation at bind
22300 time, and refuse to build an executable program.
22302 @item One or more orders exist, all incorrect
22303 One or more acceptable elaboration orders exists, and all of them
22304 generate an elaboration order problem. In this case, the binder
22305 can build an executable program, but @code{Program_Error} will be raised
22306 when the program is run.
22308 @item Several orders exist, some right, some incorrect
22309 One or more acceptable elaboration orders exists, and some of them
22310 work, and some do not. The programmer has not controlled
22311 the order of elaboration, so the binder may or may not pick one of
22312 the correct orders, and the program may or may not raise an
22313 exception when it is run. This is the worst case, because it means
22314 that the program may fail when moved to another compiler, or even
22315 another version of the same compiler.
22317 @item One or more orders exists, all correct
22318 One ore more acceptable elaboration orders exist, and all of them
22319 work. In this case the program runs successfully. This state of
22320 affairs can be guaranteed by following the rule we gave above, but
22321 may be true even if the rule is not followed.
22325 Note that one additional advantage of following our Elaborate_All rule
22326 is that the program continues to stay in the ideal (all orders OK) state
22327 even if maintenance
22328 changes some bodies of some subprograms. Conversely, if a program that does
22329 not follow this rule happens to be safe at some point, this state of affairs
22330 may deteriorate silently as a result of maintenance changes.
22332 You may have noticed that the above discussion did not mention
22333 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
22334 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
22335 code in the body makes calls to some other unit, so it is still necessary
22336 to use @code{Elaborate_All} on such units.
22338 @node Controlling Elaboration in GNAT - Internal Calls
22339 @section Controlling Elaboration in GNAT - Internal Calls
22342 In the case of internal calls, i.e. calls within a single package, the
22343 programmer has full control over the order of elaboration, and it is up
22344 to the programmer to elaborate declarations in an appropriate order. For
22347 @smallexample @c ada
22350 function One return Float;
22354 function One return Float is
22363 will obviously raise @code{Program_Error} at run time, because function
22364 One will be called before its body is elaborated. In this case GNAT will
22365 generate a warning that the call will raise @code{Program_Error}:
22371 2. function One return Float;
22373 4. Q : Float := One;
22375 >>> warning: cannot call "One" before body is elaborated
22376 >>> warning: Program_Error will be raised at run time
22379 6. function One return Float is
22392 Note that in this particular case, it is likely that the call is safe, because
22393 the function @code{One} does not access any global variables.
22394 Nevertheless in Ada 95, we do not want the validity of the check to depend on
22395 the contents of the body (think about the separate compilation case), so this
22396 is still wrong, as we discussed in the previous sections.
22398 The error is easily corrected by rearranging the declarations so that the
22399 body of One appears before the declaration containing the call
22400 (note that in Ada 95,
22401 declarations can appear in any order, so there is no restriction that
22402 would prevent this reordering, and if we write:
22404 @smallexample @c ada
22407 function One return Float;
22409 function One return Float is
22420 then all is well, no warning is generated, and no
22421 @code{Program_Error} exception
22423 Things are more complicated when a chain of subprograms is executed:
22425 @smallexample @c ada
22428 function A return Integer;
22429 function B return Integer;
22430 function C return Integer;
22432 function B return Integer is begin return A; end;
22433 function C return Integer is begin return B; end;
22437 function A return Integer is begin return 1; end;
22443 Now the call to @code{C}
22444 at elaboration time in the declaration of @code{X} is correct, because
22445 the body of @code{C} is already elaborated,
22446 and the call to @code{B} within the body of
22447 @code{C} is correct, but the call
22448 to @code{A} within the body of @code{B} is incorrect, because the body
22449 of @code{A} has not been elaborated, so @code{Program_Error}
22450 will be raised on the call to @code{A}.
22451 In this case GNAT will generate a
22452 warning that @code{Program_Error} may be
22453 raised at the point of the call. Let's look at the warning:
22459 2. function A return Integer;
22460 3. function B return Integer;
22461 4. function C return Integer;
22463 6. function B return Integer is begin return A; end;
22465 >>> warning: call to "A" before body is elaborated may
22466 raise Program_Error
22467 >>> warning: "B" called at line 7
22468 >>> warning: "C" called at line 9
22470 7. function C return Integer is begin return B; end;
22472 9. X : Integer := C;
22474 11. function A return Integer is begin return 1; end;
22484 Note that the message here says ``may raise'', instead of the direct case,
22485 where the message says ``will be raised''. That's because whether
22487 actually called depends in general on run-time flow of control.
22488 For example, if the body of @code{B} said
22490 @smallexample @c ada
22493 function B return Integer is
22495 if some-condition-depending-on-input-data then
22506 then we could not know until run time whether the incorrect call to A would
22507 actually occur, so @code{Program_Error} might
22508 or might not be raised. It is possible for a compiler to
22509 do a better job of analyzing bodies, to
22510 determine whether or not @code{Program_Error}
22511 might be raised, but it certainly
22512 couldn't do a perfect job (that would require solving the halting problem
22513 and is provably impossible), and because this is a warning anyway, it does
22514 not seem worth the effort to do the analysis. Cases in which it
22515 would be relevant are rare.
22517 In practice, warnings of either of the forms given
22518 above will usually correspond to
22519 real errors, and should be examined carefully and eliminated.
22520 In the rare case where a warning is bogus, it can be suppressed by any of
22521 the following methods:
22525 Compile with the @option{-gnatws} switch set
22528 Suppress @code{Elaboration_Check} for the called subprogram
22531 Use pragma @code{Warnings_Off} to turn warnings off for the call
22535 For the internal elaboration check case,
22536 GNAT by default generates the
22537 necessary run-time checks to ensure
22538 that @code{Program_Error} is raised if any
22539 call fails an elaboration check. Of course this can only happen if a
22540 warning has been issued as described above. The use of pragma
22541 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
22542 some of these checks, meaning that it may be possible (but is not
22543 guaranteed) for a program to be able to call a subprogram whose body
22544 is not yet elaborated, without raising a @code{Program_Error} exception.
22546 @node Controlling Elaboration in GNAT - External Calls
22547 @section Controlling Elaboration in GNAT - External Calls
22550 The previous section discussed the case in which the execution of a
22551 particular thread of elaboration code occurred entirely within a
22552 single unit. This is the easy case to handle, because a programmer
22553 has direct and total control over the order of elaboration, and
22554 furthermore, checks need only be generated in cases which are rare
22555 and which the compiler can easily detect.
22556 The situation is more complex when separate compilation is taken into account.
22557 Consider the following:
22559 @smallexample @c ada
22563 function Sqrt (Arg : Float) return Float;
22566 package body Math is
22567 function Sqrt (Arg : Float) return Float is
22576 X : Float := Math.Sqrt (0.5);
22589 where @code{Main} is the main program. When this program is executed, the
22590 elaboration code must first be executed, and one of the jobs of the
22591 binder is to determine the order in which the units of a program are
22592 to be elaborated. In this case we have four units: the spec and body
22594 the spec of @code{Stuff} and the body of @code{Main}).
22595 In what order should the four separate sections of elaboration code
22598 There are some restrictions in the order of elaboration that the binder
22599 can choose. In particular, if unit U has a @code{with}
22600 for a package @code{X}, then you
22601 are assured that the spec of @code{X}
22602 is elaborated before U , but you are
22603 not assured that the body of @code{X}
22604 is elaborated before U.
22605 This means that in the above case, the binder is allowed to choose the
22616 but that's not good, because now the call to @code{Math.Sqrt}
22617 that happens during
22618 the elaboration of the @code{Stuff}
22619 spec happens before the body of @code{Math.Sqrt} is
22620 elaborated, and hence causes @code{Program_Error} exception to be raised.
22621 At first glance, one might say that the binder is misbehaving, because
22622 obviously you want to elaborate the body of something you @code{with}
22624 that is not a general rule that can be followed in all cases. Consider
22626 @smallexample @c ada
22634 package body Y is ...
22637 package body X is ...
22643 This is a common arrangement, and, apart from the order of elaboration
22644 problems that might arise in connection with elaboration code, this works fine.
22645 A rule that says that you must first elaborate the body of anything you
22646 @code{with} cannot work in this case:
22647 the body of @code{X} @code{with}'s @code{Y},
22648 which means you would have to
22649 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
22651 you have to elaborate the body of @code{X} first, but ... and we have a
22652 loop that cannot be broken.
22654 It is true that the binder can in many cases guess an order of elaboration
22655 that is unlikely to cause a @code{Program_Error}
22656 exception to be raised, and it tries to do so (in the
22657 above example of @code{Math/Stuff/Spec}, the GNAT binder will
22659 elaborate the body of @code{Math} right after its spec, so all will be well).
22661 However, a program that blindly relies on the binder to be helpful can
22662 get into trouble, as we discussed in the previous sections, so
22664 provides a number of facilities for assisting the programmer in
22665 developing programs that are robust with respect to elaboration order.
22667 @node Default Behavior in GNAT - Ensuring Safety
22668 @section Default Behavior in GNAT - Ensuring Safety
22671 The default behavior in GNAT ensures elaboration safety. In its
22672 default mode GNAT implements the
22673 rule we previously described as the right approach. Let's restate it:
22677 @emph{If a unit has elaboration code that can directly or indirectly make a
22678 call to a subprogram in a @code{with}'ed unit, or instantiate a generic unit
22679 in a @code{with}'ed unit, then if the @code{with}'ed unit
22680 does not have pragma @code{Pure} or
22681 @code{Preelaborate}, then the client should have an
22682 @code{Elaborate_All} for the @code{with}'ed unit.}
22686 By following this rule a client is assured that calls and instantiations
22687 can be made without risk of an exception.
22689 In this mode GNAT traces all calls that are potentially made from
22690 elaboration code, and puts in any missing implicit @code{Elaborate_All}
22692 The advantage of this approach is that no elaboration problems
22693 are possible if the binder can find an elaboration order that is
22694 consistent with these implicit @code{Elaborate_All} pragmas. The
22695 disadvantage of this approach is that no such order may exist.
22697 If the binder does not generate any diagnostics, then it means that it
22698 has found an elaboration order that is guaranteed to be safe. However,
22699 the binder may still be relying on implicitly generated
22700 @code{Elaborate_All} pragmas so portability to other compilers than
22701 GNAT is not guaranteed.
22703 If it is important to guarantee portability, then the compilations should
22706 (warn on elaboration problems) switch. This will cause warning messages
22707 to be generated indicating the missing @code{Elaborate_All} pragmas.
22708 Consider the following source program:
22710 @smallexample @c ada
22715 m : integer := k.r;
22722 where it is clear that there
22723 should be a pragma @code{Elaborate_All}
22724 for unit @code{k}. An implicit pragma will be generated, and it is
22725 likely that the binder will be able to honor it. However, if you want
22726 to port this program to some other Ada compiler than GNAT.
22727 it is safer to include the pragma explicitly in the source. If this
22728 unit is compiled with the
22730 switch, then the compiler outputs a warning:
22737 3. m : integer := k.r;
22739 >>> warning: call to "r" may raise Program_Error
22740 >>> warning: missing pragma Elaborate_All for "k"
22748 and these warnings can be used as a guide for supplying manually
22749 the missing pragmas. It is usually a bad idea to use this warning
22750 option during development. That's because it will warn you when
22751 you need to put in a pragma, but cannot warn you when it is time
22752 to take it out. So the use of pragma Elaborate_All may lead to
22753 unnecessary dependencies and even false circularities.
22755 This default mode is more restrictive than the Ada Reference
22756 Manual, and it is possible to construct programs which will compile
22757 using the dynamic model described there, but will run into a
22758 circularity using the safer static model we have described.
22760 Of course any Ada compiler must be able to operate in a mode
22761 consistent with the requirements of the Ada Reference Manual,
22762 and in particular must have the capability of implementing the
22763 standard dynamic model of elaboration with run-time checks.
22765 In GNAT, this standard mode can be achieved either by the use of
22766 the @option{-gnatE} switch on the compiler (@code{gcc} or @code{gnatmake})
22767 command, or by the use of the configuration pragma:
22769 @smallexample @c ada
22770 pragma Elaboration_Checks (RM);
22774 Either approach will cause the unit affected to be compiled using the
22775 standard dynamic run-time elaboration checks described in the Ada
22776 Reference Manual. The static model is generally preferable, since it
22777 is clearly safer to rely on compile and link time checks rather than
22778 run-time checks. However, in the case of legacy code, it may be
22779 difficult to meet the requirements of the static model. This
22780 issue is further discussed in
22781 @ref{What to Do If the Default Elaboration Behavior Fails}.
22783 Note that the static model provides a strict subset of the allowed
22784 behavior and programs of the Ada Reference Manual, so if you do
22785 adhere to the static model and no circularities exist,
22786 then you are assured that your program will
22787 work using the dynamic model, providing that you remove any
22788 pragma Elaborate statements from the source.
22790 @node Treatment of Pragma Elaborate
22791 @section Treatment of Pragma Elaborate
22792 @cindex Pragma Elaborate
22795 The use of @code{pragma Elaborate}
22796 should generally be avoided in Ada 95 programs.
22797 The reason for this is that there is no guarantee that transitive calls
22798 will be properly handled. Indeed at one point, this pragma was placed
22799 in Annex J (Obsolescent Features), on the grounds that it is never useful.
22801 Now that's a bit restrictive. In practice, the case in which
22802 @code{pragma Elaborate} is useful is when the caller knows that there
22803 are no transitive calls, or that the called unit contains all necessary
22804 transitive @code{pragma Elaborate} statements, and legacy code often
22805 contains such uses.
22807 Strictly speaking the static mode in GNAT should ignore such pragmas,
22808 since there is no assurance at compile time that the necessary safety
22809 conditions are met. In practice, this would cause GNAT to be incompatible
22810 with correctly written Ada 83 code that had all necessary
22811 @code{pragma Elaborate} statements in place. Consequently, we made the
22812 decision that GNAT in its default mode will believe that if it encounters
22813 a @code{pragma Elaborate} then the programmer knows what they are doing,
22814 and it will trust that no elaboration errors can occur.
22816 The result of this decision is two-fold. First to be safe using the
22817 static mode, you should remove all @code{pragma Elaborate} statements.
22818 Second, when fixing circularities in existing code, you can selectively
22819 use @code{pragma Elaborate} statements to convince the static mode of
22820 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
22823 When using the static mode with @option{-gnatwl}, any use of
22824 @code{pragma Elaborate} will generate a warning about possible
22827 @node Elaboration Issues for Library Tasks
22828 @section Elaboration Issues for Library Tasks
22829 @cindex Library tasks, elaboration issues
22830 @cindex Elaboration of library tasks
22833 In this section we examine special elaboration issues that arise for
22834 programs that declare library level tasks.
22836 Generally the model of execution of an Ada program is that all units are
22837 elaborated, and then execution of the program starts. However, the
22838 declaration of library tasks definitely does not fit this model. The
22839 reason for this is that library tasks start as soon as they are declared
22840 (more precisely, as soon as the statement part of the enclosing package
22841 body is reached), that is to say before elaboration
22842 of the program is complete. This means that if such a task calls a
22843 subprogram, or an entry in another task, the callee may or may not be
22844 elaborated yet, and in the standard
22845 Reference Manual model of dynamic elaboration checks, you can even
22846 get timing dependent Program_Error exceptions, since there can be
22847 a race between the elaboration code and the task code.
22849 The static model of elaboration in GNAT seeks to avoid all such
22850 dynamic behavior, by being conservative, and the conservative
22851 approach in this particular case is to assume that all the code
22852 in a task body is potentially executed at elaboration time if
22853 a task is declared at the library level.
22855 This can definitely result in unexpected circularities. Consider
22856 the following example
22858 @smallexample @c ada
22864 type My_Int is new Integer;
22866 function Ident (M : My_Int) return My_Int;
22870 package body Decls is
22871 task body Lib_Task is
22877 function Ident (M : My_Int) return My_Int is
22885 procedure Put_Val (Arg : Decls.My_Int);
22889 package body Utils is
22890 procedure Put_Val (Arg : Decls.My_Int) is
22892 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
22899 Decls.Lib_Task.Start;
22904 If the above example is compiled in the default static elaboration
22905 mode, then a circularity occurs. The circularity comes from the call
22906 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
22907 this call occurs in elaboration code, we need an implicit pragma
22908 @code{Elaborate_All} for @code{Utils}. This means that not only must
22909 the spec and body of @code{Utils} be elaborated before the body
22910 of @code{Decls}, but also the spec and body of any unit that is
22911 @code{with'ed} by the body of @code{Utils} must also be elaborated before
22912 the body of @code{Decls}. This is the transitive implication of
22913 pragma @code{Elaborate_All} and it makes sense, because in general
22914 the body of @code{Put_Val} might have a call to something in a
22915 @code{with'ed} unit.
22917 In this case, the body of Utils (actually its spec) @code{with's}
22918 @code{Decls}. Unfortunately this means that the body of @code{Decls}
22919 must be elaborated before itself, in case there is a call from the
22920 body of @code{Utils}.
22922 Here is the exact chain of events we are worrying about:
22926 In the body of @code{Decls} a call is made from within the body of a library
22927 task to a subprogram in the package @code{Utils}. Since this call may
22928 occur at elaboration time (given that the task is activated at elaboration
22929 time), we have to assume the worst, i.e. that the
22930 call does happen at elaboration time.
22933 This means that the body and spec of @code{Util} must be elaborated before
22934 the body of @code{Decls} so that this call does not cause an access before
22938 Within the body of @code{Util}, specifically within the body of
22939 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
22943 One such @code{with}'ed package is package @code{Decls}, so there
22944 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
22945 In fact there is such a call in this example, but we would have to
22946 assume that there was such a call even if it were not there, since
22947 we are not supposed to write the body of @code{Decls} knowing what
22948 is in the body of @code{Utils}; certainly in the case of the
22949 static elaboration model, the compiler does not know what is in
22950 other bodies and must assume the worst.
22953 This means that the spec and body of @code{Decls} must also be
22954 elaborated before we elaborate the unit containing the call, but
22955 that unit is @code{Decls}! This means that the body of @code{Decls}
22956 must be elaborated before itself, and that's a circularity.
22960 Indeed, if you add an explicit pragma Elaborate_All for @code{Utils} in
22961 the body of @code{Decls} you will get a true Ada Reference Manual
22962 circularity that makes the program illegal.
22964 In practice, we have found that problems with the static model of
22965 elaboration in existing code often arise from library tasks, so
22966 we must address this particular situation.
22968 Note that if we compile and run the program above, using the dynamic model of
22969 elaboration (that is to say use the @option{-gnatE} switch),
22970 then it compiles, binds,
22971 links, and runs, printing the expected result of 2. Therefore in some sense
22972 the circularity here is only apparent, and we need to capture
22973 the properties of this program that distinguish it from other library-level
22974 tasks that have real elaboration problems.
22976 We have four possible answers to this question:
22981 Use the dynamic model of elaboration.
22983 If we use the @option{-gnatE} switch, then as noted above, the program works.
22984 Why is this? If we examine the task body, it is apparent that the task cannot
22986 @code{accept} statement until after elaboration has been completed, because
22987 the corresponding entry call comes from the main program, not earlier.
22988 This is why the dynamic model works here. But that's really giving
22989 up on a precise analysis, and we prefer to take this approach only if we cannot
22991 problem in any other manner. So let us examine two ways to reorganize
22992 the program to avoid the potential elaboration problem.
22995 Split library tasks into separate packages.
22997 Write separate packages, so that library tasks are isolated from
22998 other declarations as much as possible. Let us look at a variation on
23001 @smallexample @c ada
23009 package body Decls1 is
23010 task body Lib_Task is
23018 type My_Int is new Integer;
23019 function Ident (M : My_Int) return My_Int;
23023 package body Decls2 is
23024 function Ident (M : My_Int) return My_Int is
23032 procedure Put_Val (Arg : Decls2.My_Int);
23036 package body Utils is
23037 procedure Put_Val (Arg : Decls2.My_Int) is
23039 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
23046 Decls1.Lib_Task.Start;
23051 All we have done is to split @code{Decls} into two packages, one
23052 containing the library task, and one containing everything else. Now
23053 there is no cycle, and the program compiles, binds, links and executes
23054 using the default static model of elaboration.
23057 Declare separate task types.
23059 A significant part of the problem arises because of the use of the
23060 single task declaration form. This means that the elaboration of
23061 the task type, and the elaboration of the task itself (i.e. the
23062 creation of the task) happen at the same time. A good rule
23063 of style in Ada 95 is to always create explicit task types. By
23064 following the additional step of placing task objects in separate
23065 packages from the task type declaration, many elaboration problems
23066 are avoided. Here is another modified example of the example program:
23068 @smallexample @c ada
23070 task type Lib_Task_Type is
23074 type My_Int is new Integer;
23076 function Ident (M : My_Int) return My_Int;
23080 package body Decls is
23081 task body Lib_Task_Type is
23087 function Ident (M : My_Int) return My_Int is
23095 procedure Put_Val (Arg : Decls.My_Int);
23099 package body Utils is
23100 procedure Put_Val (Arg : Decls.My_Int) is
23102 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
23108 Lib_Task : Decls.Lib_Task_Type;
23114 Declst.Lib_Task.Start;
23119 What we have done here is to replace the @code{task} declaration in
23120 package @code{Decls} with a @code{task type} declaration. Then we
23121 introduce a separate package @code{Declst} to contain the actual
23122 task object. This separates the elaboration issues for
23123 the @code{task type}
23124 declaration, which causes no trouble, from the elaboration issues
23125 of the task object, which is also unproblematic, since it is now independent
23126 of the elaboration of @code{Utils}.
23127 This separation of concerns also corresponds to
23128 a generally sound engineering principle of separating declarations
23129 from instances. This version of the program also compiles, binds, links,
23130 and executes, generating the expected output.
23133 Use No_Entry_Calls_In_Elaboration_Code restriction.
23134 @cindex No_Entry_Calls_In_Elaboration_Code
23136 The previous two approaches described how a program can be restructured
23137 to avoid the special problems caused by library task bodies. in practice,
23138 however, such restructuring may be difficult to apply to existing legacy code,
23139 so we must consider solutions that do not require massive rewriting.
23141 Let us consider more carefully why our original sample program works
23142 under the dynamic model of elaboration. The reason is that the code
23143 in the task body blocks immediately on the @code{accept}
23144 statement. Now of course there is nothing to prohibit elaboration
23145 code from making entry calls (for example from another library level task),
23146 so we cannot tell in isolation that
23147 the task will not execute the accept statement during elaboration.
23149 However, in practice it is very unusual to see elaboration code
23150 make any entry calls, and the pattern of tasks starting
23151 at elaboration time and then immediately blocking on @code{accept} or
23152 @code{select} statements is very common. What this means is that
23153 the compiler is being too pessimistic when it analyzes the
23154 whole package body as though it might be executed at elaboration
23157 If we know that the elaboration code contains no entry calls, (a very safe
23158 assumption most of the time, that could almost be made the default
23159 behavior), then we can compile all units of the program under control
23160 of the following configuration pragma:
23163 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
23167 This pragma can be placed in the @file{gnat.adc} file in the usual
23168 manner. If we take our original unmodified program and compile it
23169 in the presence of a @file{gnat.adc} containing the above pragma,
23170 then once again, we can compile, bind, link, and execute, obtaining
23171 the expected result. In the presence of this pragma, the compiler does
23172 not trace calls in a task body, that appear after the first @code{accept}
23173 or @code{select} statement, and therefore does not report a potential
23174 circularity in the original program.
23176 The compiler will check to the extent it can that the above
23177 restriction is not violated, but it is not always possible to do a
23178 complete check at compile time, so it is important to use this
23179 pragma only if the stated restriction is in fact met, that is to say
23180 no task receives an entry call before elaboration of all units is completed.
23184 @node Mixing Elaboration Models
23185 @section Mixing Elaboration Models
23187 So far, we have assumed that the entire program is either compiled
23188 using the dynamic model or static model, ensuring consistency. It
23189 is possible to mix the two models, but rules have to be followed
23190 if this mixing is done to ensure that elaboration checks are not
23193 The basic rule is that @emph{a unit compiled with the static model cannot
23194 be @code{with'ed} by a unit compiled with the dynamic model}. The
23195 reason for this is that in the static model, a unit assumes that
23196 its clients guarantee to use (the equivalent of) pragma
23197 @code{Elaborate_All} so that no elaboration checks are required
23198 in inner subprograms, and this assumption is violated if the
23199 client is compiled with dynamic checks.
23201 The precise rule is as follows. A unit that is compiled with dynamic
23202 checks can only @code{with} a unit that meets at least one of the
23203 following criteria:
23208 The @code{with'ed} unit is itself compiled with dynamic elaboration
23209 checks (that is with the @option{-gnatE} switch.
23212 The @code{with'ed} unit is an internal GNAT implementation unit from
23213 the System, Interfaces, Ada, or GNAT hierarchies.
23216 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
23219 The @code{with'ing} unit (that is the client) has an explicit pragma
23220 @code{Elaborate_All} for the @code{with'ed} unit.
23225 If this rule is violated, that is if a unit with dynamic elaboration
23226 checks @code{with's} a unit that does not meet one of the above four
23227 criteria, then the binder (@code{gnatbind}) will issue a warning
23228 similar to that in the following example:
23231 warning: "x.ads" has dynamic elaboration checks and with's
23232 warning: "y.ads" which has static elaboration checks
23236 These warnings indicate that the rule has been violated, and that as a result
23237 elaboration checks may be missed in the resulting executable file.
23238 This warning may be suppressed using the @option{-ws} binder switch
23239 in the usual manner.
23241 One useful application of this mixing rule is in the case of a subsystem
23242 which does not itself @code{with} units from the remainder of the
23243 application. In this case, the entire subsystem can be compiled with
23244 dynamic checks to resolve a circularity in the subsystem, while
23245 allowing the main application that uses this subsystem to be compiled
23246 using the more reliable default static model.
23248 @node What to Do If the Default Elaboration Behavior Fails
23249 @section What to Do If the Default Elaboration Behavior Fails
23252 If the binder cannot find an acceptable order, it outputs detailed
23253 diagnostics. For example:
23259 error: elaboration circularity detected
23260 info: "proc (body)" must be elaborated before "pack (body)"
23261 info: reason: Elaborate_All probably needed in unit "pack (body)"
23262 info: recompile "pack (body)" with -gnatwl
23263 info: for full details
23264 info: "proc (body)"
23265 info: is needed by its spec:
23266 info: "proc (spec)"
23267 info: which is withed by:
23268 info: "pack (body)"
23269 info: "pack (body)" must be elaborated before "proc (body)"
23270 info: reason: pragma Elaborate in unit "proc (body)"
23276 In this case we have a cycle that the binder cannot break. On the one
23277 hand, there is an explicit pragma Elaborate in @code{proc} for
23278 @code{pack}. This means that the body of @code{pack} must be elaborated
23279 before the body of @code{proc}. On the other hand, there is elaboration
23280 code in @code{pack} that calls a subprogram in @code{proc}. This means
23281 that for maximum safety, there should really be a pragma
23282 Elaborate_All in @code{pack} for @code{proc} which would require that
23283 the body of @code{proc} be elaborated before the body of
23284 @code{pack}. Clearly both requirements cannot be satisfied.
23285 Faced with a circularity of this kind, you have three different options.
23288 @item Fix the program
23289 The most desirable option from the point of view of long-term maintenance
23290 is to rearrange the program so that the elaboration problems are avoided.
23291 One useful technique is to place the elaboration code into separate
23292 child packages. Another is to move some of the initialization code to
23293 explicitly called subprograms, where the program controls the order
23294 of initialization explicitly. Although this is the most desirable option,
23295 it may be impractical and involve too much modification, especially in
23296 the case of complex legacy code.
23298 @item Perform dynamic checks
23299 If the compilations are done using the
23301 (dynamic elaboration check) switch, then GNAT behaves in
23302 a quite different manner. Dynamic checks are generated for all calls
23303 that could possibly result in raising an exception. With this switch,
23304 the compiler does not generate implicit @code{Elaborate_All} pragmas.
23305 The behavior then is exactly as specified in the Ada 95 Reference Manual.
23306 The binder will generate an executable program that may or may not
23307 raise @code{Program_Error}, and then it is the programmer's job to ensure
23308 that it does not raise an exception. Note that it is important to
23309 compile all units with the switch, it cannot be used selectively.
23311 @item Suppress checks
23312 The drawback of dynamic checks is that they generate a
23313 significant overhead at run time, both in space and time. If you
23314 are absolutely sure that your program cannot raise any elaboration
23315 exceptions, and you still want to use the dynamic elaboration model,
23316 then you can use the configuration pragma
23317 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
23318 example this pragma could be placed in the @file{gnat.adc} file.
23320 @item Suppress checks selectively
23321 When you know that certain calls in elaboration code cannot possibly
23322 lead to an elaboration error, and the binder nevertheless generates warnings
23323 on those calls and inserts Elaborate_All pragmas that lead to elaboration
23324 circularities, it is possible to remove those warnings locally and obtain
23325 a program that will bind. Clearly this can be unsafe, and it is the
23326 responsibility of the programmer to make sure that the resulting program has
23327 no elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can
23328 be used with different granularity to suppress warnings and break
23329 elaboration circularities:
23333 Place the pragma that names the called subprogram in the declarative part
23334 that contains the call.
23337 Place the pragma in the declarative part, without naming an entity. This
23338 disables warnings on all calls in the corresponding declarative region.
23341 Place the pragma in the package spec that declares the called subprogram,
23342 and name the subprogram. This disables warnings on all elaboration calls to
23346 Place the pragma in the package spec that declares the called subprogram,
23347 without naming any entity. This disables warnings on all elaboration calls to
23348 all subprograms declared in this spec.
23350 @item Use Pragma Elaborate
23351 As previously described in section @xref{Treatment of Pragma Elaborate},
23352 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
23353 that no elaboration checks are required on calls to the designated unit.
23354 There may be cases in which the caller knows that no transitive calls
23355 can occur, so that a @code{pragma Elaborate} will be sufficient in a
23356 case where @code{pragma Elaborate_All} would cause a circularity.
23360 These five cases are listed in order of decreasing safety, and therefore
23361 require increasing programmer care in their application. Consider the
23364 @smallexample @c adanocomment
23366 function F1 return Integer;
23371 function F2 return Integer;
23372 function Pure (x : integer) return integer;
23373 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
23374 -- pragma Suppress (Elaboration_Check); -- (4)
23378 package body Pack1 is
23379 function F1 return Integer is
23383 Val : integer := Pack2.Pure (11); -- Elab. call (1)
23386 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
23387 -- pragma Suppress(Elaboration_Check); -- (2)
23389 X1 := Pack2.F2 + 1; -- Elab. call (2)
23394 package body Pack2 is
23395 function F2 return Integer is
23399 function Pure (x : integer) return integer is
23401 return x ** 3 - 3 * x;
23405 with Pack1, Ada.Text_IO;
23408 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
23411 In the absence of any pragmas, an attempt to bind this program produces
23412 the following diagnostics:
23418 error: elaboration circularity detected
23419 info: "pack1 (body)" must be elaborated before "pack1 (body)"
23420 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
23421 info: recompile "pack1 (body)" with -gnatwl for full details
23422 info: "pack1 (body)"
23423 info: must be elaborated along with its spec:
23424 info: "pack1 (spec)"
23425 info: which is withed by:
23426 info: "pack2 (body)"
23427 info: which must be elaborated along with its spec:
23428 info: "pack2 (spec)"
23429 info: which is withed by:
23430 info: "pack1 (body)"
23433 The sources of the circularity are the two calls to @code{Pack2.Pure} and
23434 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
23435 F2 is safe, even though F2 calls F1, because the call appears after the
23436 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
23437 remove the warning on the call. It is also possible to use pragma (2)
23438 because there are no other potentially unsafe calls in the block.
23441 The call to @code{Pure} is safe because this function does not depend on the
23442 state of @code{Pack2}. Therefore any call to this function is safe, and it
23443 is correct to place pragma (3) in the corresponding package spec.
23446 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
23447 warnings on all calls to functions declared therein. Note that this is not
23448 necessarily safe, and requires more detailed examination of the subprogram
23449 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
23450 be already elaborated.
23454 It is hard to generalize on which of these four approaches should be
23455 taken. Obviously if it is possible to fix the program so that the default
23456 treatment works, this is preferable, but this may not always be practical.
23457 It is certainly simple enough to use
23459 but the danger in this case is that, even if the GNAT binder
23460 finds a correct elaboration order, it may not always do so,
23461 and certainly a binder from another Ada compiler might not. A
23462 combination of testing and analysis (for which the warnings generated
23465 switch can be useful) must be used to ensure that the program is free
23466 of errors. One switch that is useful in this testing is the
23467 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
23470 Normally the binder tries to find an order that has the best chance of
23471 of avoiding elaboration problems. With this switch, the binder
23472 plays a devil's advocate role, and tries to choose the order that
23473 has the best chance of failing. If your program works even with this
23474 switch, then it has a better chance of being error free, but this is still
23477 For an example of this approach in action, consider the C-tests (executable
23478 tests) from the ACVC suite. If these are compiled and run with the default
23479 treatment, then all but one of them succeed without generating any error
23480 diagnostics from the binder. However, there is one test that fails, and
23481 this is not surprising, because the whole point of this test is to ensure
23482 that the compiler can handle cases where it is impossible to determine
23483 a correct order statically, and it checks that an exception is indeed
23484 raised at run time.
23486 This one test must be compiled and run using the
23488 switch, and then it passes. Alternatively, the entire suite can
23489 be run using this switch. It is never wrong to run with the dynamic
23490 elaboration switch if your code is correct, and we assume that the
23491 C-tests are indeed correct (it is less efficient, but efficiency is
23492 not a factor in running the ACVC tests.)
23494 @node Elaboration for Access-to-Subprogram Values
23495 @section Elaboration for Access-to-Subprogram Values
23496 @cindex Access-to-subprogram
23499 The introduction of access-to-subprogram types in Ada 95 complicates
23500 the handling of elaboration. The trouble is that it becomes
23501 impossible to tell at compile time which procedure
23502 is being called. This means that it is not possible for the binder
23503 to analyze the elaboration requirements in this case.
23505 If at the point at which the access value is created
23506 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
23507 the body of the subprogram is
23508 known to have been elaborated, then the access value is safe, and its use
23509 does not require a check. This may be achieved by appropriate arrangement
23510 of the order of declarations if the subprogram is in the current unit,
23511 or, if the subprogram is in another unit, by using pragma
23512 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
23513 on the referenced unit.
23515 If the referenced body is not known to have been elaborated at the point
23516 the access value is created, then any use of the access value must do a
23517 dynamic check, and this dynamic check will fail and raise a
23518 @code{Program_Error} exception if the body has not been elaborated yet.
23519 GNAT will generate the necessary checks, and in addition, if the
23521 switch is set, will generate warnings that such checks are required.
23523 The use of dynamic dispatching for tagged types similarly generates
23524 a requirement for dynamic checks, and premature calls to any primitive
23525 operation of a tagged type before the body of the operation has been
23526 elaborated, will result in the raising of @code{Program_Error}.
23528 @node Summary of Procedures for Elaboration Control
23529 @section Summary of Procedures for Elaboration Control
23530 @cindex Elaboration control
23533 First, compile your program with the default options, using none of
23534 the special elaboration control switches. If the binder successfully
23535 binds your program, then you can be confident that, apart from issues
23536 raised by the use of access-to-subprogram types and dynamic dispatching,
23537 the program is free of elaboration errors. If it is important that the
23538 program be portable, then use the
23540 switch to generate warnings about missing @code{Elaborate_All}
23541 pragmas, and supply the missing pragmas.
23543 If the program fails to bind using the default static elaboration
23544 handling, then you can fix the program to eliminate the binder
23545 message, or recompile the entire program with the
23546 @option{-gnatE} switch to generate dynamic elaboration checks,
23547 and, if you are sure there really are no elaboration problems,
23548 use a global pragma @code{Suppress (Elaboration_Check)}.
23550 @node Other Elaboration Order Considerations
23551 @section Other Elaboration Order Considerations
23553 This section has been entirely concerned with the issue of finding a valid
23554 elaboration order, as defined by the Ada Reference Manual. In a case
23555 where several elaboration orders are valid, the task is to find one
23556 of the possible valid elaboration orders (and the static model in GNAT
23557 will ensure that this is achieved).
23559 The purpose of the elaboration rules in the Ada Reference Manual is to
23560 make sure that no entity is accessed before it has been elaborated. For
23561 a subprogram, this means that the spec and body must have been elaborated
23562 before the subprogram is called. For an object, this means that the object
23563 must have been elaborated before its value is read or written. A violation
23564 of either of these two requirements is an access before elaboration order,
23565 and this section has been all about avoiding such errors.
23567 In the case where more than one order of elaboration is possible, in the
23568 sense that access before elaboration errors are avoided, then any one of
23569 the orders is ``correct'' in the sense that it meets the requirements of
23570 the Ada Reference Manual, and no such error occurs.
23572 However, it may be the case for a given program, that there are
23573 constraints on the order of elaboration that come not from consideration
23574 of avoiding elaboration errors, but rather from extra-lingual logic
23575 requirements. Consider this example:
23577 @smallexample @c ada
23578 with Init_Constants;
23579 package Constants is
23584 package Init_Constants is
23585 procedure P; -- require a body
23586 end Init_Constants;
23589 package body Init_Constants is
23590 procedure P is begin null; end;
23594 end Init_Constants;
23598 Z : Integer := Constants.X + Constants.Y;
23602 with Text_IO; use Text_IO;
23605 Put_Line (Calc.Z'Img);
23610 In this example, there is more than one valid order of elaboration. For
23611 example both the following are correct orders:
23614 Init_Constants spec
23617 Init_Constants body
23622 Init_Constants spec
23623 Init_Constants body
23630 There is no language rule to prefer one or the other, both are correct
23631 from an order of elaboration point of view. But the programmatic effects
23632 of the two orders are very different. In the first, the elaboration routine
23633 of @code{Calc} initializes @code{Z} to zero, and then the main program
23634 runs with this value of zero. But in the second order, the elaboration
23635 routine of @code{Calc} runs after the body of Init_Constants has set
23636 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
23639 One could perhaps by applying pretty clever non-artificial intelligence
23640 to the situation guess that it is more likely that the second order of
23641 elaboration is the one desired, but there is no formal linguistic reason
23642 to prefer one over the other. In fact in this particular case, GNAT will
23643 prefer the second order, because of the rule that bodies are elaborated
23644 as soon as possible, but it's just luck that this is what was wanted
23645 (if indeed the second order was preferred).
23647 If the program cares about the order of elaboration routines in a case like
23648 this, it is important to specify the order required. In this particular
23649 case, that could have been achieved by adding to the spec of Calc:
23651 @smallexample @c ada
23652 pragma Elaborate_All (Constants);
23656 which requires that the body (if any) and spec of @code{Constants},
23657 as well as the body and spec of any unit @code{with}'ed by
23658 @code{Constants} be elaborated before @code{Calc} is elaborated.
23660 Clearly no automatic method can always guess which alternative you require,
23661 and if you are working with legacy code that had constraints of this kind
23662 which were not properly specified by adding @code{Elaborate} or
23663 @code{Elaborate_All} pragmas, then indeed it is possible that two different
23664 compilers can choose different orders.
23666 The @code{gnatbind}
23667 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
23668 out problems. This switch causes bodies to be elaborated as late as possible
23669 instead of as early as possible. In the example above, it would have forced
23670 the choice of the first elaboration order. If you get different results
23671 when using this switch, and particularly if one set of results is right,
23672 and one is wrong as far as you are concerned, it shows that you have some
23673 missing @code{Elaborate} pragmas. For the example above, we have the
23677 gnatmake -f -q main
23680 gnatmake -f -q main -bargs -p
23686 It is of course quite unlikely that both these results are correct, so
23687 it is up to you in a case like this to investigate the source of the
23688 difference, by looking at the two elaboration orders that are chosen,
23689 and figuring out which is correct, and then adding the necessary
23690 @code{Elaborate_All} pragmas to ensure the desired order.
23693 @node Inline Assembler
23694 @appendix Inline Assembler
23697 If you need to write low-level software that interacts directly
23698 with the hardware, Ada provides two ways to incorporate assembly
23699 language code into your program. First, you can import and invoke
23700 external routines written in assembly language, an Ada feature fully
23701 supported by GNAT. However, for small sections of code it may be simpler
23702 or more efficient to include assembly language statements directly
23703 in your Ada source program, using the facilities of the implementation-defined
23704 package @code{System.Machine_Code}, which incorporates the gcc
23705 Inline Assembler. The Inline Assembler approach offers a number of advantages,
23706 including the following:
23709 @item No need to use non-Ada tools
23710 @item Consistent interface over different targets
23711 @item Automatic usage of the proper calling conventions
23712 @item Access to Ada constants and variables
23713 @item Definition of intrinsic routines
23714 @item Possibility of inlining a subprogram comprising assembler code
23715 @item Code optimizer can take Inline Assembler code into account
23718 This chapter presents a series of examples to show you how to use
23719 the Inline Assembler. Although it focuses on the Intel x86,
23720 the general approach applies also to other processors.
23721 It is assumed that you are familiar with Ada
23722 and with assembly language programming.
23725 * Basic Assembler Syntax::
23726 * A Simple Example of Inline Assembler::
23727 * Output Variables in Inline Assembler::
23728 * Input Variables in Inline Assembler::
23729 * Inlining Inline Assembler Code::
23730 * Other Asm Functionality::
23731 * A Complete Example::
23734 @c ---------------------------------------------------------------------------
23735 @node Basic Assembler Syntax
23736 @section Basic Assembler Syntax
23739 The assembler used by GNAT and gcc is based not on the Intel assembly
23740 language, but rather on a language that descends from the AT&T Unix
23741 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
23742 The following table summarizes the main features of @emph{as} syntax
23743 and points out the differences from the Intel conventions.
23744 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
23745 pre-processor) documentation for further information.
23748 @item Register names
23749 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
23751 Intel: No extra punctuation; for example @code{eax}
23753 @item Immediate operand
23754 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
23756 Intel: No extra punctuation; for example @code{4}
23759 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
23761 Intel: No extra punctuation; for example @code{loc}
23763 @item Memory contents
23764 gcc / @emph{as}: No extra punctuation; for example @code{loc}
23766 Intel: Square brackets; for example @code{[loc]}
23768 @item Register contents
23769 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
23771 Intel: Square brackets; for example @code{[eax]}
23773 @item Hexadecimal numbers
23774 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
23776 Intel: Trailing ``h''; for example @code{A0h}
23779 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
23782 Intel: Implicit, deduced by assembler; for example @code{mov}
23784 @item Instruction repetition
23785 gcc / @emph{as}: Split into two lines; for example
23791 Intel: Keep on one line; for example @code{rep stosl}
23793 @item Order of operands
23794 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
23796 Intel: Destination first; for example @code{mov eax, 4}
23799 @c ---------------------------------------------------------------------------
23800 @node A Simple Example of Inline Assembler
23801 @section A Simple Example of Inline Assembler
23804 The following example will generate a single assembly language statement,
23805 @code{nop}, which does nothing. Despite its lack of run-time effect,
23806 the example will be useful in illustrating the basics of
23807 the Inline Assembler facility.
23809 @smallexample @c ada
23811 with System.Machine_Code; use System.Machine_Code;
23812 procedure Nothing is
23819 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
23820 here it takes one parameter, a @emph{template string} that must be a static
23821 expression and that will form the generated instruction.
23822 @code{Asm} may be regarded as a compile-time procedure that parses
23823 the template string and additional parameters (none here),
23824 from which it generates a sequence of assembly language instructions.
23826 The examples in this chapter will illustrate several of the forms
23827 for invoking @code{Asm}; a complete specification of the syntax
23828 is found in the @cite{GNAT Reference Manual}.
23830 Under the standard GNAT conventions, the @code{Nothing} procedure
23831 should be in a file named @file{nothing.adb}.
23832 You can build the executable in the usual way:
23836 However, the interesting aspect of this example is not its run-time behavior
23837 but rather the generated assembly code.
23838 To see this output, invoke the compiler as follows:
23840 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
23842 where the options are:
23846 compile only (no bind or link)
23848 generate assembler listing
23849 @item -fomit-frame-pointer
23850 do not set up separate stack frames
23852 do not add runtime checks
23855 This gives a human-readable assembler version of the code. The resulting
23856 file will have the same name as the Ada source file, but with a @code{.s}
23857 extension. In our example, the file @file{nothing.s} has the following
23862 .file "nothing.adb"
23864 ___gnu_compiled_ada:
23867 .globl __ada_nothing
23879 The assembly code you included is clearly indicated by
23880 the compiler, between the @code{#APP} and @code{#NO_APP}
23881 delimiters. The character before the 'APP' and 'NOAPP'
23882 can differ on different targets. For example, GNU/Linux uses '#APP' while
23883 on NT you will see '/APP'.
23885 If you make a mistake in your assembler code (such as using the
23886 wrong size modifier, or using a wrong operand for the instruction) GNAT
23887 will report this error in a temporary file, which will be deleted when
23888 the compilation is finished. Generating an assembler file will help
23889 in such cases, since you can assemble this file separately using the
23890 @emph{as} assembler that comes with gcc.
23892 Assembling the file using the command
23895 as @file{nothing.s}
23898 will give you error messages whose lines correspond to the assembler
23899 input file, so you can easily find and correct any mistakes you made.
23900 If there are no errors, @emph{as} will generate an object file
23901 @file{nothing.out}.
23903 @c ---------------------------------------------------------------------------
23904 @node Output Variables in Inline Assembler
23905 @section Output Variables in Inline Assembler
23908 The examples in this section, showing how to access the processor flags,
23909 illustrate how to specify the destination operands for assembly language
23912 @smallexample @c ada
23914 with Interfaces; use Interfaces;
23915 with Ada.Text_IO; use Ada.Text_IO;
23916 with System.Machine_Code; use System.Machine_Code;
23917 procedure Get_Flags is
23918 Flags : Unsigned_32;
23921 Asm ("pushfl" & LF & HT & -- push flags on stack
23922 "popl %%eax" & LF & HT & -- load eax with flags
23923 "movl %%eax, %0", -- store flags in variable
23924 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
23925 Put_Line ("Flags register:" & Flags'Img);
23930 In order to have a nicely aligned assembly listing, we have separated
23931 multiple assembler statements in the Asm template string with linefeed
23932 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
23933 The resulting section of the assembly output file is:
23940 movl %eax, -40(%ebp)
23945 It would have been legal to write the Asm invocation as:
23948 Asm ("pushfl popl %%eax movl %%eax, %0")
23951 but in the generated assembler file, this would come out as:
23955 pushfl popl %eax movl %eax, -40(%ebp)
23959 which is not so convenient for the human reader.
23961 We use Ada comments
23962 at the end of each line to explain what the assembler instructions
23963 actually do. This is a useful convention.
23965 When writing Inline Assembler instructions, you need to precede each register
23966 and variable name with a percent sign. Since the assembler already requires
23967 a percent sign at the beginning of a register name, you need two consecutive
23968 percent signs for such names in the Asm template string, thus @code{%%eax}.
23969 In the generated assembly code, one of the percent signs will be stripped off.
23971 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
23972 variables: operands you later define using @code{Input} or @code{Output}
23973 parameters to @code{Asm}.
23974 An output variable is illustrated in
23975 the third statement in the Asm template string:
23979 The intent is to store the contents of the eax register in a variable that can
23980 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
23981 necessarily work, since the compiler might optimize by using a register
23982 to hold Flags, and the expansion of the @code{movl} instruction would not be
23983 aware of this optimization. The solution is not to store the result directly
23984 but rather to advise the compiler to choose the correct operand form;
23985 that is the purpose of the @code{%0} output variable.
23987 Information about the output variable is supplied in the @code{Outputs}
23988 parameter to @code{Asm}:
23990 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
23993 The output is defined by the @code{Asm_Output} attribute of the target type;
23994 the general format is
23996 Type'Asm_Output (constraint_string, variable_name)
23999 The constraint string directs the compiler how
24000 to store/access the associated variable. In the example
24002 Unsigned_32'Asm_Output ("=m", Flags);
24004 the @code{"m"} (memory) constraint tells the compiler that the variable
24005 @code{Flags} should be stored in a memory variable, thus preventing
24006 the optimizer from keeping it in a register. In contrast,
24008 Unsigned_32'Asm_Output ("=r", Flags);
24010 uses the @code{"r"} (register) constraint, telling the compiler to
24011 store the variable in a register.
24013 If the constraint is preceded by the equal character (@strong{=}), it tells
24014 the compiler that the variable will be used to store data into it.
24016 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
24017 allowing the optimizer to choose whatever it deems best.
24019 There are a fairly large number of constraints, but the ones that are
24020 most useful (for the Intel x86 processor) are the following:
24026 global (i.e. can be stored anywhere)
24044 use one of eax, ebx, ecx or edx
24046 use one of eax, ebx, ecx, edx, esi or edi
24049 The full set of constraints is described in the gcc and @emph{as}
24050 documentation; note that it is possible to combine certain constraints
24051 in one constraint string.
24053 You specify the association of an output variable with an assembler operand
24054 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
24056 @smallexample @c ada
24058 Asm ("pushfl" & LF & HT & -- push flags on stack
24059 "popl %%eax" & LF & HT & -- load eax with flags
24060 "movl %%eax, %0", -- store flags in variable
24061 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
24065 @code{%0} will be replaced in the expanded code by the appropriate operand,
24067 the compiler decided for the @code{Flags} variable.
24069 In general, you may have any number of output variables:
24072 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
24074 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
24075 of @code{Asm_Output} attributes
24079 @smallexample @c ada
24081 Asm ("movl %%eax, %0" & LF & HT &
24082 "movl %%ebx, %1" & LF & HT &
24084 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
24085 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
24086 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
24090 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
24091 in the Ada program.
24093 As a variation on the @code{Get_Flags} example, we can use the constraints
24094 string to direct the compiler to store the eax register into the @code{Flags}
24095 variable, instead of including the store instruction explicitly in the
24096 @code{Asm} template string:
24098 @smallexample @c ada
24100 with Interfaces; use Interfaces;
24101 with Ada.Text_IO; use Ada.Text_IO;
24102 with System.Machine_Code; use System.Machine_Code;
24103 procedure Get_Flags_2 is
24104 Flags : Unsigned_32;
24107 Asm ("pushfl" & LF & HT & -- push flags on stack
24108 "popl %%eax", -- save flags in eax
24109 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
24110 Put_Line ("Flags register:" & Flags'Img);
24116 The @code{"a"} constraint tells the compiler that the @code{Flags}
24117 variable will come from the eax register. Here is the resulting code:
24125 movl %eax,-40(%ebp)
24130 The compiler generated the store of eax into Flags after
24131 expanding the assembler code.
24133 Actually, there was no need to pop the flags into the eax register;
24134 more simply, we could just pop the flags directly into the program variable:
24136 @smallexample @c ada
24138 with Interfaces; use Interfaces;
24139 with Ada.Text_IO; use Ada.Text_IO;
24140 with System.Machine_Code; use System.Machine_Code;
24141 procedure Get_Flags_3 is
24142 Flags : Unsigned_32;
24145 Asm ("pushfl" & LF & HT & -- push flags on stack
24146 "pop %0", -- save flags in Flags
24147 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
24148 Put_Line ("Flags register:" & Flags'Img);
24153 @c ---------------------------------------------------------------------------
24154 @node Input Variables in Inline Assembler
24155 @section Input Variables in Inline Assembler
24158 The example in this section illustrates how to specify the source operands
24159 for assembly language statements.
24160 The program simply increments its input value by 1:
24162 @smallexample @c ada
24164 with Interfaces; use Interfaces;
24165 with Ada.Text_IO; use Ada.Text_IO;
24166 with System.Machine_Code; use System.Machine_Code;
24167 procedure Increment is
24169 function Incr (Value : Unsigned_32) return Unsigned_32 is
24170 Result : Unsigned_32;
24173 Inputs => Unsigned_32'Asm_Input ("a", Value),
24174 Outputs => Unsigned_32'Asm_Output ("=a", Result));
24178 Value : Unsigned_32;
24182 Put_Line ("Value before is" & Value'Img);
24183 Value := Incr (Value);
24184 Put_Line ("Value after is" & Value'Img);
24189 The @code{Outputs} parameter to @code{Asm} specifies
24190 that the result will be in the eax register and that it is to be stored
24191 in the @code{Result} variable.
24193 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
24194 but with an @code{Asm_Input} attribute.
24195 The @code{"="} constraint, indicating an output value, is not present.
24197 You can have multiple input variables, in the same way that you can have more
24198 than one output variable.
24200 The parameter count (%0, %1) etc, now starts at the first input
24201 statement, and continues with the output statements.
24202 When both parameters use the same variable, the
24203 compiler will treat them as the same %n operand, which is the case here.
24205 Just as the @code{Outputs} parameter causes the register to be stored into the
24206 target variable after execution of the assembler statements, so does the
24207 @code{Inputs} parameter cause its variable to be loaded into the register
24208 before execution of the assembler statements.
24210 Thus the effect of the @code{Asm} invocation is:
24212 @item load the 32-bit value of @code{Value} into eax
24213 @item execute the @code{incl %eax} instruction
24214 @item store the contents of eax into the @code{Result} variable
24217 The resulting assembler file (with @option{-O2} optimization) contains:
24220 _increment__incr.1:
24233 @c ---------------------------------------------------------------------------
24234 @node Inlining Inline Assembler Code
24235 @section Inlining Inline Assembler Code
24238 For a short subprogram such as the @code{Incr} function in the previous
24239 section, the overhead of the call and return (creating / deleting the stack
24240 frame) can be significant, compared to the amount of code in the subprogram
24241 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
24242 which directs the compiler to expand invocations of the subprogram at the
24243 point(s) of call, instead of setting up a stack frame for out-of-line calls.
24244 Here is the resulting program:
24246 @smallexample @c ada
24248 with Interfaces; use Interfaces;
24249 with Ada.Text_IO; use Ada.Text_IO;
24250 with System.Machine_Code; use System.Machine_Code;
24251 procedure Increment_2 is
24253 function Incr (Value : Unsigned_32) return Unsigned_32 is
24254 Result : Unsigned_32;
24257 Inputs => Unsigned_32'Asm_Input ("a", Value),
24258 Outputs => Unsigned_32'Asm_Output ("=a", Result));
24261 pragma Inline (Increment);
24263 Value : Unsigned_32;
24267 Put_Line ("Value before is" & Value'Img);
24268 Value := Increment (Value);
24269 Put_Line ("Value after is" & Value'Img);
24274 Compile the program with both optimization (@option{-O2}) and inlining
24275 enabled (@option{-gnatpn} instead of @option{-gnatp}).
24277 The @code{Incr} function is still compiled as usual, but at the
24278 point in @code{Increment} where our function used to be called:
24283 call _increment__incr.1
24288 the code for the function body directly appears:
24301 thus saving the overhead of stack frame setup and an out-of-line call.
24303 @c ---------------------------------------------------------------------------
24304 @node Other Asm Functionality
24305 @section Other @code{Asm} Functionality
24308 This section describes two important parameters to the @code{Asm}
24309 procedure: @code{Clobber}, which identifies register usage;
24310 and @code{Volatile}, which inhibits unwanted optimizations.
24313 * The Clobber Parameter::
24314 * The Volatile Parameter::
24317 @c ---------------------------------------------------------------------------
24318 @node The Clobber Parameter
24319 @subsection The @code{Clobber} Parameter
24322 One of the dangers of intermixing assembly language and a compiled language
24323 such as Ada is that the compiler needs to be aware of which registers are
24324 being used by the assembly code. In some cases, such as the earlier examples,
24325 the constraint string is sufficient to indicate register usage (e.g.,
24327 the eax register). But more generally, the compiler needs an explicit
24328 identification of the registers that are used by the Inline Assembly
24331 Using a register that the compiler doesn't know about
24332 could be a side effect of an instruction (like @code{mull}
24333 storing its result in both eax and edx).
24334 It can also arise from explicit register usage in your
24335 assembly code; for example:
24338 Asm ("movl %0, %%ebx" & LF & HT &
24340 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
24341 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
24345 where the compiler (since it does not analyze the @code{Asm} template string)
24346 does not know you are using the ebx register.
24348 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
24349 to identify the registers that will be used by your assembly code:
24353 Asm ("movl %0, %%ebx" & LF & HT &
24355 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
24356 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
24361 The Clobber parameter is a static string expression specifying the
24362 register(s) you are using. Note that register names are @emph{not} prefixed
24363 by a percent sign. Also, if more than one register is used then their names
24364 are separated by commas; e.g., @code{"eax, ebx"}
24366 The @code{Clobber} parameter has several additional uses:
24368 @item Use ``register'' name @code{cc} to indicate that flags might have changed
24369 @item Use ``register'' name @code{memory} if you changed a memory location
24372 @c ---------------------------------------------------------------------------
24373 @node The Volatile Parameter
24374 @subsection The @code{Volatile} Parameter
24375 @cindex Volatile parameter
24378 Compiler optimizations in the presence of Inline Assembler may sometimes have
24379 unwanted effects. For example, when an @code{Asm} invocation with an input
24380 variable is inside a loop, the compiler might move the loading of the input
24381 variable outside the loop, regarding it as a one-time initialization.
24383 If this effect is not desired, you can disable such optimizations by setting
24384 the @code{Volatile} parameter to @code{True}; for example:
24386 @smallexample @c ada
24388 Asm ("movl %0, %%ebx" & LF & HT &
24390 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
24391 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
24397 By default, @code{Volatile} is set to @code{False} unless there is no
24398 @code{Outputs} parameter.
24400 Although setting @code{Volatile} to @code{True} prevents unwanted
24401 optimizations, it will also disable other optimizations that might be
24402 important for efficiency. In general, you should set @code{Volatile}
24403 to @code{True} only if the compiler's optimizations have created
24406 @c ---------------------------------------------------------------------------
24407 @node A Complete Example
24408 @section A Complete Example
24411 This section contains a complete program illustrating a realistic usage
24412 of GNAT's Inline Assembler capabilities. It comprises a main procedure
24413 @code{Check_CPU} and a package @code{Intel_CPU}.
24414 The package declares a collection of functions that detect the properties
24415 of the 32-bit x86 processor that is running the program.
24416 The main procedure invokes these functions and displays the information.
24418 The Intel_CPU package could be enhanced by adding functions to
24419 detect the type of x386 co-processor, the processor caching options and
24420 special operations such as the SIMD extensions.
24422 Although the Intel_CPU package has been written for 32-bit Intel
24423 compatible CPUs, it is OS neutral. It has been tested on DOS,
24424 Windows/NT and GNU/Linux.
24427 * Check_CPU Procedure::
24428 * Intel_CPU Package Specification::
24429 * Intel_CPU Package Body::
24432 @c ---------------------------------------------------------------------------
24433 @node Check_CPU Procedure
24434 @subsection @code{Check_CPU} Procedure
24435 @cindex Check_CPU procedure
24437 @smallexample @c adanocomment
24438 ---------------------------------------------------------------------
24440 -- Uses the Intel_CPU package to identify the CPU the program is --
24441 -- running on, and some of the features it supports. --
24443 ---------------------------------------------------------------------
24445 with Intel_CPU; -- Intel CPU detection functions
24446 with Ada.Text_IO; -- Standard text I/O
24447 with Ada.Command_Line; -- To set the exit status
24449 procedure Check_CPU is
24451 Type_Found : Boolean := False;
24452 -- Flag to indicate that processor was identified
24454 Features : Intel_CPU.Processor_Features;
24455 -- The processor features
24457 Signature : Intel_CPU.Processor_Signature;
24458 -- The processor type signature
24462 -----------------------------------
24463 -- Display the program banner. --
24464 -----------------------------------
24466 Ada.Text_IO.Put_Line (Ada.Command_Line.Command_Name &
24467 ": check Intel CPU version and features, v1.0");
24468 Ada.Text_IO.Put_Line ("distribute freely, but no warranty whatsoever");
24469 Ada.Text_IO.New_Line;
24471 -----------------------------------------------------------------------
24472 -- We can safely start with the assumption that we are on at least --
24473 -- a x386 processor. If the CPUID instruction is present, then we --
24474 -- have a later processor type. --
24475 -----------------------------------------------------------------------
24477 if Intel_CPU.Has_CPUID = False then
24479 -- No CPUID instruction, so we assume this is indeed a x386
24480 -- processor. We can still check if it has a FP co-processor.
24481 if Intel_CPU.Has_FPU then
24482 Ada.Text_IO.Put_Line
24483 ("x386-type processor with a FP co-processor");
24485 Ada.Text_IO.Put_Line
24486 ("x386-type processor without a FP co-processor");
24487 end if; -- check for FPU
24490 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
24493 end if; -- check for CPUID
24495 -----------------------------------------------------------------------
24496 -- If CPUID is supported, check if this is a true Intel processor, --
24497 -- if it is not, display a warning. --
24498 -----------------------------------------------------------------------
24500 if Intel_CPU.Vendor_ID /= Intel_CPU.Intel_Processor then
24501 Ada.Text_IO.Put_Line ("*** This is a Intel compatible processor");
24502 Ada.Text_IO.Put_Line ("*** Some information may be incorrect");
24503 end if; -- check if Intel
24505 ----------------------------------------------------------------------
24506 -- With the CPUID instruction present, we can assume at least a --
24507 -- x486 processor. If the CPUID support level is < 1 then we have --
24508 -- to leave it at that. --
24509 ----------------------------------------------------------------------
24511 if Intel_CPU.CPUID_Level < 1 then
24513 -- Ok, this is a x486 processor. we still can get the Vendor ID
24514 Ada.Text_IO.Put_Line ("x486-type processor");
24515 Ada.Text_IO.Put_Line ("Vendor ID is " & Intel_CPU.Vendor_ID);
24517 -- We can also check if there is a FPU present
24518 if Intel_CPU.Has_FPU then
24519 Ada.Text_IO.Put_Line ("Floating-Point support");
24521 Ada.Text_IO.Put_Line ("No Floating-Point support");
24522 end if; -- check for FPU
24525 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
24528 end if; -- check CPUID level
24530 ---------------------------------------------------------------------
24531 -- With a CPUID level of 1 we can use the processor signature to --
24532 -- determine it's exact type. --
24533 ---------------------------------------------------------------------
24535 Signature := Intel_CPU.Signature;
24537 ----------------------------------------------------------------------
24538 -- Ok, now we go into a lot of messy comparisons to get the --
24539 -- processor type. For clarity, no attememt to try to optimize the --
24540 -- comparisons has been made. Note that since Intel_CPU does not --
24541 -- support getting cache info, we cannot distinguish between P5 --
24542 -- and Celeron types yet. --
24543 ----------------------------------------------------------------------
24546 if Signature.Processor_Type = 2#00# and
24547 Signature.Family = 2#0100# and
24548 Signature.Model = 2#0100# then
24549 Type_Found := True;
24550 Ada.Text_IO.Put_Line ("x486SL processor");
24553 -- x486DX2 Write-Back
24554 if Signature.Processor_Type = 2#00# and
24555 Signature.Family = 2#0100# and
24556 Signature.Model = 2#0111# then
24557 Type_Found := True;
24558 Ada.Text_IO.Put_Line ("Write-Back Enhanced x486DX2 processor");
24562 if Signature.Processor_Type = 2#00# and
24563 Signature.Family = 2#0100# and
24564 Signature.Model = 2#1000# then
24565 Type_Found := True;
24566 Ada.Text_IO.Put_Line ("x486DX4 processor");
24569 -- x486DX4 Overdrive
24570 if Signature.Processor_Type = 2#01# and
24571 Signature.Family = 2#0100# and
24572 Signature.Model = 2#1000# then
24573 Type_Found := True;
24574 Ada.Text_IO.Put_Line ("x486DX4 OverDrive processor");
24577 -- Pentium (60, 66)
24578 if Signature.Processor_Type = 2#00# and
24579 Signature.Family = 2#0101# and
24580 Signature.Model = 2#0001# then
24581 Type_Found := True;
24582 Ada.Text_IO.Put_Line ("Pentium processor (60, 66)");
24585 -- Pentium (75, 90, 100, 120, 133, 150, 166, 200)
24586 if Signature.Processor_Type = 2#00# and
24587 Signature.Family = 2#0101# and
24588 Signature.Model = 2#0010# then
24589 Type_Found := True;
24590 Ada.Text_IO.Put_Line
24591 ("Pentium processor (75, 90, 100, 120, 133, 150, 166, 200)");
24594 -- Pentium OverDrive (60, 66)
24595 if Signature.Processor_Type = 2#01# and
24596 Signature.Family = 2#0101# and
24597 Signature.Model = 2#0001# then
24598 Type_Found := True;
24599 Ada.Text_IO.Put_Line ("Pentium OverDrive processor (60, 66)");
24602 -- Pentium OverDrive (75, 90, 100, 120, 133, 150, 166, 200)
24603 if Signature.Processor_Type = 2#01# and
24604 Signature.Family = 2#0101# and
24605 Signature.Model = 2#0010# then
24606 Type_Found := True;
24607 Ada.Text_IO.Put_Line
24608 ("Pentium OverDrive cpu (75, 90, 100, 120, 133, 150, 166, 200)");
24611 -- Pentium OverDrive processor for x486 processor-based systems
24612 if Signature.Processor_Type = 2#01# and
24613 Signature.Family = 2#0101# and
24614 Signature.Model = 2#0011# then
24615 Type_Found := True;
24616 Ada.Text_IO.Put_Line
24617 ("Pentium OverDrive processor for x486 processor-based systems");
24620 -- Pentium processor with MMX technology (166, 200)
24621 if Signature.Processor_Type = 2#00# and
24622 Signature.Family = 2#0101# and
24623 Signature.Model = 2#0100# then
24624 Type_Found := True;
24625 Ada.Text_IO.Put_Line
24626 ("Pentium processor with MMX technology (166, 200)");
24629 -- Pentium OverDrive with MMX for Pentium (75, 90, 100, 120, 133)
24630 if Signature.Processor_Type = 2#01# and
24631 Signature.Family = 2#0101# and
24632 Signature.Model = 2#0100# then
24633 Type_Found := True;
24634 Ada.Text_IO.Put_Line
24635 ("Pentium OverDrive processor with MMX " &
24636 "technology for Pentium processor (75, 90, 100, 120, 133)");
24639 -- Pentium Pro processor
24640 if Signature.Processor_Type = 2#00# and
24641 Signature.Family = 2#0110# and
24642 Signature.Model = 2#0001# then
24643 Type_Found := True;
24644 Ada.Text_IO.Put_Line ("Pentium Pro processor");
24647 -- Pentium II processor, model 3
24648 if Signature.Processor_Type = 2#00# and
24649 Signature.Family = 2#0110# and
24650 Signature.Model = 2#0011# then
24651 Type_Found := True;
24652 Ada.Text_IO.Put_Line ("Pentium II processor, model 3");
24655 -- Pentium II processor, model 5 or Celeron processor
24656 if Signature.Processor_Type = 2#00# and
24657 Signature.Family = 2#0110# and
24658 Signature.Model = 2#0101# then
24659 Type_Found := True;
24660 Ada.Text_IO.Put_Line
24661 ("Pentium II processor, model 5 or Celeron processor");
24664 -- Pentium Pro OverDrive processor
24665 if Signature.Processor_Type = 2#01# and
24666 Signature.Family = 2#0110# and
24667 Signature.Model = 2#0011# then
24668 Type_Found := True;
24669 Ada.Text_IO.Put_Line ("Pentium Pro OverDrive processor");
24672 -- If no type recognized, we have an unknown. Display what
24674 if Type_Found = False then
24675 Ada.Text_IO.Put_Line ("Unknown processor");
24678 -----------------------------------------
24679 -- Display processor stepping level. --
24680 -----------------------------------------
24682 Ada.Text_IO.Put_Line ("Stepping level:" & Signature.Stepping'Img);
24684 ---------------------------------
24685 -- Display vendor ID string. --
24686 ---------------------------------
24688 Ada.Text_IO.Put_Line ("Vendor ID: " & Intel_CPU.Vendor_ID);
24690 ------------------------------------
24691 -- Get the processors features. --
24692 ------------------------------------
24694 Features := Intel_CPU.Features;
24696 -----------------------------
24697 -- Check for a FPU unit. --
24698 -----------------------------
24700 if Features.FPU = True then
24701 Ada.Text_IO.Put_Line ("Floating-Point unit available");
24703 Ada.Text_IO.Put_Line ("no Floating-Point unit");
24704 end if; -- check for FPU
24706 --------------------------------
24707 -- List processor features. --
24708 --------------------------------
24710 Ada.Text_IO.Put_Line ("Supported features: ");
24712 -- Virtual Mode Extension
24713 if Features.VME = True then
24714 Ada.Text_IO.Put_Line (" VME - Virtual Mode Extension");
24717 -- Debugging Extension
24718 if Features.DE = True then
24719 Ada.Text_IO.Put_Line (" DE - Debugging Extension");
24722 -- Page Size Extension
24723 if Features.PSE = True then
24724 Ada.Text_IO.Put_Line (" PSE - Page Size Extension");
24727 -- Time Stamp Counter
24728 if Features.TSC = True then
24729 Ada.Text_IO.Put_Line (" TSC - Time Stamp Counter");
24732 -- Model Specific Registers
24733 if Features.MSR = True then
24734 Ada.Text_IO.Put_Line (" MSR - Model Specific Registers");
24737 -- Physical Address Extension
24738 if Features.PAE = True then
24739 Ada.Text_IO.Put_Line (" PAE - Physical Address Extension");
24742 -- Machine Check Extension
24743 if Features.MCE = True then
24744 Ada.Text_IO.Put_Line (" MCE - Machine Check Extension");
24747 -- CMPXCHG8 instruction supported
24748 if Features.CX8 = True then
24749 Ada.Text_IO.Put_Line (" CX8 - CMPXCHG8 instruction");
24752 -- on-chip APIC hardware support
24753 if Features.APIC = True then
24754 Ada.Text_IO.Put_Line (" APIC - on-chip APIC hardware support");
24757 -- Fast System Call
24758 if Features.SEP = True then
24759 Ada.Text_IO.Put_Line (" SEP - Fast System Call");
24762 -- Memory Type Range Registers
24763 if Features.MTRR = True then
24764 Ada.Text_IO.Put_Line (" MTTR - Memory Type Range Registers");
24767 -- Page Global Enable
24768 if Features.PGE = True then
24769 Ada.Text_IO.Put_Line (" PGE - Page Global Enable");
24772 -- Machine Check Architecture
24773 if Features.MCA = True then
24774 Ada.Text_IO.Put_Line (" MCA - Machine Check Architecture");
24777 -- Conditional Move Instruction Supported
24778 if Features.CMOV = True then
24779 Ada.Text_IO.Put_Line
24780 (" CMOV - Conditional Move Instruction Supported");
24783 -- Page Attribute Table
24784 if Features.PAT = True then
24785 Ada.Text_IO.Put_Line (" PAT - Page Attribute Table");
24788 -- 36-bit Page Size Extension
24789 if Features.PSE_36 = True then
24790 Ada.Text_IO.Put_Line (" PSE_36 - 36-bit Page Size Extension");
24793 -- MMX technology supported
24794 if Features.MMX = True then
24795 Ada.Text_IO.Put_Line (" MMX - MMX technology supported");
24798 -- Fast FP Save and Restore
24799 if Features.FXSR = True then
24800 Ada.Text_IO.Put_Line (" FXSR - Fast FP Save and Restore");
24803 ---------------------
24804 -- Program done. --
24805 ---------------------
24807 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
24812 Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
24818 @c ---------------------------------------------------------------------------
24819 @node Intel_CPU Package Specification
24820 @subsection @code{Intel_CPU} Package Specification
24821 @cindex Intel_CPU package specification
24823 @smallexample @c adanocomment
24824 -------------------------------------------------------------------------
24826 -- file: intel_cpu.ads --
24828 -- ********************************************* --
24829 -- * WARNING: for 32-bit Intel processors only * --
24830 -- ********************************************* --
24832 -- This package contains a number of subprograms that are useful in --
24833 -- determining the Intel x86 CPU (and the features it supports) on --
24834 -- which the program is running. --
24836 -- The package is based upon the information given in the Intel --
24837 -- Application Note AP-485: "Intel Processor Identification and the --
24838 -- CPUID Instruction" as of April 1998. This application note can be --
24839 -- found on www.intel.com. --
24841 -- It currently deals with 32-bit processors only, will not detect --
24842 -- features added after april 1998, and does not guarantee proper --
24843 -- results on Intel-compatible processors. --
24845 -- Cache info and x386 fpu type detection are not supported. --
24847 -- This package does not use any privileged instructions, so should --
24848 -- work on any OS running on a 32-bit Intel processor. --
24850 -------------------------------------------------------------------------
24852 with Interfaces; use Interfaces;
24853 -- for using unsigned types
24855 with System.Machine_Code; use System.Machine_Code;
24856 -- for using inline assembler code
24858 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
24859 -- for inserting control characters
24861 package Intel_CPU is
24863 ----------------------
24864 -- Processor bits --
24865 ----------------------
24867 subtype Num_Bits is Natural range 0 .. 31;
24868 -- the number of processor bits (32)
24870 --------------------------
24871 -- Processor register --
24872 --------------------------
24874 -- define a processor register type for easy access to
24875 -- the individual bits
24877 type Processor_Register is array (Num_Bits) of Boolean;
24878 pragma Pack (Processor_Register);
24879 for Processor_Register'Size use 32;
24881 -------------------------
24882 -- Unsigned register --
24883 -------------------------
24885 -- define a processor register type for easy access to
24886 -- the individual bytes
24888 type Unsigned_Register is
24896 for Unsigned_Register use
24898 L1 at 0 range 0 .. 7;
24899 H1 at 0 range 8 .. 15;
24900 L2 at 0 range 16 .. 23;
24901 H2 at 0 range 24 .. 31;
24904 for Unsigned_Register'Size use 32;
24906 ---------------------------------
24907 -- Intel processor vendor ID --
24908 ---------------------------------
24910 Intel_Processor : constant String (1 .. 12) := "GenuineIntel";
24911 -- indicates an Intel manufactured processor
24913 ------------------------------------
24914 -- Processor signature register --
24915 ------------------------------------
24917 -- a register type to hold the processor signature
24919 type Processor_Signature is
24921 Stepping : Natural range 0 .. 15;
24922 Model : Natural range 0 .. 15;
24923 Family : Natural range 0 .. 15;
24924 Processor_Type : Natural range 0 .. 3;
24925 Reserved : Natural range 0 .. 262143;
24928 for Processor_Signature use
24930 Stepping at 0 range 0 .. 3;
24931 Model at 0 range 4 .. 7;
24932 Family at 0 range 8 .. 11;
24933 Processor_Type at 0 range 12 .. 13;
24934 Reserved at 0 range 14 .. 31;
24937 for Processor_Signature'Size use 32;
24939 -----------------------------------
24940 -- Processor features register --
24941 -----------------------------------
24943 -- a processor register to hold the processor feature flags
24945 type Processor_Features is
24947 FPU : Boolean; -- floating point unit on chip
24948 VME : Boolean; -- virtual mode extension
24949 DE : Boolean; -- debugging extension
24950 PSE : Boolean; -- page size extension
24951 TSC : Boolean; -- time stamp counter
24952 MSR : Boolean; -- model specific registers
24953 PAE : Boolean; -- physical address extension
24954 MCE : Boolean; -- machine check extension
24955 CX8 : Boolean; -- cmpxchg8 instruction
24956 APIC : Boolean; -- on-chip apic hardware
24957 Res_1 : Boolean; -- reserved for extensions
24958 SEP : Boolean; -- fast system call
24959 MTRR : Boolean; -- memory type range registers
24960 PGE : Boolean; -- page global enable
24961 MCA : Boolean; -- machine check architecture
24962 CMOV : Boolean; -- conditional move supported
24963 PAT : Boolean; -- page attribute table
24964 PSE_36 : Boolean; -- 36-bit page size extension
24965 Res_2 : Natural range 0 .. 31; -- reserved for extensions
24966 MMX : Boolean; -- MMX technology supported
24967 FXSR : Boolean; -- fast FP save and restore
24968 Res_3 : Natural range 0 .. 127; -- reserved for extensions
24971 for Processor_Features use
24973 FPU at 0 range 0 .. 0;
24974 VME at 0 range 1 .. 1;
24975 DE at 0 range 2 .. 2;
24976 PSE at 0 range 3 .. 3;
24977 TSC at 0 range 4 .. 4;
24978 MSR at 0 range 5 .. 5;
24979 PAE at 0 range 6 .. 6;
24980 MCE at 0 range 7 .. 7;
24981 CX8 at 0 range 8 .. 8;
24982 APIC at 0 range 9 .. 9;
24983 Res_1 at 0 range 10 .. 10;
24984 SEP at 0 range 11 .. 11;
24985 MTRR at 0 range 12 .. 12;
24986 PGE at 0 range 13 .. 13;
24987 MCA at 0 range 14 .. 14;
24988 CMOV at 0 range 15 .. 15;
24989 PAT at 0 range 16 .. 16;
24990 PSE_36 at 0 range 17 .. 17;
24991 Res_2 at 0 range 18 .. 22;
24992 MMX at 0 range 23 .. 23;
24993 FXSR at 0 range 24 .. 24;
24994 Res_3 at 0 range 25 .. 31;
24997 for Processor_Features'Size use 32;
24999 -------------------
25001 -------------------
25003 function Has_FPU return Boolean;
25004 -- return True if a FPU is found
25005 -- use only if CPUID is not supported
25007 function Has_CPUID return Boolean;
25008 -- return True if the processor supports the CPUID instruction
25010 function CPUID_Level return Natural;
25011 -- return the CPUID support level (0, 1 or 2)
25012 -- can only be called if the CPUID instruction is supported
25014 function Vendor_ID return String;
25015 -- return the processor vendor identification string
25016 -- can only be called if the CPUID instruction is supported
25018 function Signature return Processor_Signature;
25019 -- return the processor signature
25020 -- can only be called if the CPUID instruction is supported
25022 function Features return Processor_Features;
25023 -- return the processors features
25024 -- can only be called if the CPUID instruction is supported
25028 ------------------------
25029 -- EFLAGS bit names --
25030 ------------------------
25032 ID_Flag : constant Num_Bits := 21;
25038 @c ---------------------------------------------------------------------------
25039 @node Intel_CPU Package Body
25040 @subsection @code{Intel_CPU} Package Body
25041 @cindex Intel_CPU package body
25043 @smallexample @c adanocomment
25044 package body Intel_CPU is
25046 ---------------------------
25047 -- Detect FPU presence --
25048 ---------------------------
25050 -- There is a FPU present if we can set values to the FPU Status
25051 -- and Control Words.
25053 function Has_FPU return Boolean is
25055 Register : Unsigned_16;
25056 -- processor register to store a word
25060 -- check if we can change the status word
25063 -- the assembler code
25064 "finit" & LF & HT & -- reset status word
25065 "movw $0x5A5A, %%ax" & LF & HT & -- set value status word
25066 "fnstsw %0" & LF & HT & -- save status word
25067 "movw %%ax, %0", -- store status word
25069 -- output stored in Register
25070 -- register must be a memory location
25071 Outputs => Unsigned_16'Asm_output ("=m", Register),
25073 -- tell compiler that we used eax
25076 -- if the status word is zero, there is no FPU
25077 if Register = 0 then
25078 return False; -- no status word
25079 end if; -- check status word value
25081 -- check if we can get the control word
25084 -- the assembler code
25085 "fnstcw %0", -- save the control word
25087 -- output into Register
25088 -- register must be a memory location
25089 Outputs => Unsigned_16'Asm_output ("=m", Register));
25091 -- check the relevant bits
25092 if (Register and 16#103F#) /= 16#003F# then
25093 return False; -- no control word
25094 end if; -- check control word value
25101 --------------------------------
25102 -- Detect CPUID instruction --
25103 --------------------------------
25105 -- The processor supports the CPUID instruction if it is possible
25106 -- to change the value of ID flag bit in the EFLAGS register.
25108 function Has_CPUID return Boolean is
25110 Original_Flags, Modified_Flags : Processor_Register;
25111 -- EFLAG contents before and after changing the ID flag
25115 -- try flipping the ID flag in the EFLAGS register
25118 -- the assembler code
25119 "pushfl" & LF & HT & -- push EFLAGS on stack
25120 "pop %%eax" & LF & HT & -- pop EFLAGS into eax
25121 "movl %%eax, %0" & LF & HT & -- save EFLAGS content
25122 "xor $0x200000, %%eax" & LF & HT & -- flip ID flag
25123 "push %%eax" & LF & HT & -- push EFLAGS on stack
25124 "popfl" & LF & HT & -- load EFLAGS register
25125 "pushfl" & LF & HT & -- push EFLAGS on stack
25126 "pop %1", -- save EFLAGS content
25128 -- output values, may be anything
25129 -- Original_Flags is %0
25130 -- Modified_Flags is %1
25132 (Processor_Register'Asm_output ("=g", Original_Flags),
25133 Processor_Register'Asm_output ("=g", Modified_Flags)),
25135 -- tell compiler eax is destroyed
25138 -- check if CPUID is supported
25139 if Original_Flags(ID_Flag) /= Modified_Flags(ID_Flag) then
25140 return True; -- ID flag was modified
25142 return False; -- ID flag unchanged
25143 end if; -- check for CPUID
25147 -------------------------------
25148 -- Get CPUID support level --
25149 -------------------------------
25151 function CPUID_Level return Natural is
25153 Level : Unsigned_32;
25154 -- returned support level
25158 -- execute CPUID, storing the results in the Level register
25161 -- the assembler code
25162 "cpuid", -- execute CPUID
25164 -- zero is stored in eax
25165 -- returning the support level in eax
25166 Inputs => Unsigned_32'Asm_input ("a", 0),
25168 -- eax is stored in Level
25169 Outputs => Unsigned_32'Asm_output ("=a", Level),
25171 -- tell compiler ebx, ecx and edx registers are destroyed
25172 Clobber => "ebx, ecx, edx");
25174 -- return the support level
25175 return Natural (Level);
25179 --------------------------------
25180 -- Get CPU Vendor ID String --
25181 --------------------------------
25183 -- The vendor ID string is returned in the ebx, ecx and edx register
25184 -- after executing the CPUID instruction with eax set to zero.
25185 -- In case of a true Intel processor the string returned is
25188 function Vendor_ID return String is
25190 Ebx, Ecx, Edx : Unsigned_Register;
25191 -- registers containing the vendor ID string
25193 Vendor_ID : String (1 .. 12);
25194 -- the vendor ID string
25198 -- execute CPUID, storing the results in the processor registers
25201 -- the assembler code
25202 "cpuid", -- execute CPUID
25204 -- zero stored in eax
25205 -- vendor ID string returned in ebx, ecx and edx
25206 Inputs => Unsigned_32'Asm_input ("a", 0),
25208 -- ebx is stored in Ebx
25209 -- ecx is stored in Ecx
25210 -- edx is stored in Edx
25211 Outputs => (Unsigned_Register'Asm_output ("=b", Ebx),
25212 Unsigned_Register'Asm_output ("=c", Ecx),
25213 Unsigned_Register'Asm_output ("=d", Edx)));
25215 -- now build the vendor ID string
25216 Vendor_ID( 1) := Character'Val (Ebx.L1);
25217 Vendor_ID( 2) := Character'Val (Ebx.H1);
25218 Vendor_ID( 3) := Character'Val (Ebx.L2);
25219 Vendor_ID( 4) := Character'Val (Ebx.H2);
25220 Vendor_ID( 5) := Character'Val (Edx.L1);
25221 Vendor_ID( 6) := Character'Val (Edx.H1);
25222 Vendor_ID( 7) := Character'Val (Edx.L2);
25223 Vendor_ID( 8) := Character'Val (Edx.H2);
25224 Vendor_ID( 9) := Character'Val (Ecx.L1);
25225 Vendor_ID(10) := Character'Val (Ecx.H1);
25226 Vendor_ID(11) := Character'Val (Ecx.L2);
25227 Vendor_ID(12) := Character'Val (Ecx.H2);
25234 -------------------------------
25235 -- Get processor signature --
25236 -------------------------------
25238 function Signature return Processor_Signature is
25240 Result : Processor_Signature;
25241 -- processor signature returned
25245 -- execute CPUID, storing the results in the Result variable
25248 -- the assembler code
25249 "cpuid", -- execute CPUID
25251 -- one is stored in eax
25252 -- processor signature returned in eax
25253 Inputs => Unsigned_32'Asm_input ("a", 1),
25255 -- eax is stored in Result
25256 Outputs => Processor_Signature'Asm_output ("=a", Result),
25258 -- tell compiler that ebx, ecx and edx are also destroyed
25259 Clobber => "ebx, ecx, edx");
25261 -- return processor signature
25266 ------------------------------
25267 -- Get processor features --
25268 ------------------------------
25270 function Features return Processor_Features is
25272 Result : Processor_Features;
25273 -- processor features returned
25277 -- execute CPUID, storing the results in the Result variable
25280 -- the assembler code
25281 "cpuid", -- execute CPUID
25283 -- one stored in eax
25284 -- processor features returned in edx
25285 Inputs => Unsigned_32'Asm_input ("a", 1),
25287 -- edx is stored in Result
25288 Outputs => Processor_Features'Asm_output ("=d", Result),
25290 -- tell compiler that ebx and ecx are also destroyed
25291 Clobber => "ebx, ecx");
25293 -- return processor signature
25300 @c END OF INLINE ASSEMBLER CHAPTER
25301 @c ===============================
25305 @c ***********************************
25306 @c * Compatibility and Porting Guide *
25307 @c ***********************************
25308 @node Compatibility and Porting Guide
25309 @appendix Compatibility and Porting Guide
25312 This chapter describes the compatibility issues that may arise between
25313 GNAT and other Ada 83 and Ada 95 compilation systems, and shows how GNAT
25314 can expedite porting
25315 applications developed in other Ada environments.
25318 * Compatibility with Ada 83::
25319 * Implementation-dependent characteristics::
25320 * Compatibility with DEC Ada 83::
25321 * Compatibility with Other Ada 95 Systems::
25322 * Representation Clauses::
25325 @node Compatibility with Ada 83
25326 @section Compatibility with Ada 83
25327 @cindex Compatibility (between Ada 83 and Ada 95)
25330 Ada 95 is designed to be highly upwards compatible with Ada 83. In
25331 particular, the design intention is that the difficulties associated
25332 with moving from Ada 83 to Ada 95 should be no greater than those
25333 that occur when moving from one Ada 83 system to another.
25335 However, there are a number of points at which there are minor
25336 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
25337 full details of these issues,
25338 and should be consulted for a complete treatment.
25340 following subsections treat the most likely issues to be encountered.
25343 * Legal Ada 83 programs that are illegal in Ada 95::
25344 * More deterministic semantics::
25345 * Changed semantics::
25346 * Other language compatibility issues::
25349 @node Legal Ada 83 programs that are illegal in Ada 95
25350 @subsection Legal Ada 83 programs that are illegal in Ada 95
25353 @item Character literals
25354 Some uses of character literals are ambiguous. Since Ada 95 has introduced
25355 @code{Wide_Character} as a new predefined character type, some uses of
25356 character literals that were legal in Ada 83 are illegal in Ada 95.
25358 @smallexample @c ada
25359 for Char in 'A' .. 'Z' loop ... end loop;
25362 The problem is that @code{'A'} and @code{'Z'} could be from either
25363 @code{Character} or @code{Wide_Character}. The simplest correction
25364 is to make the type explicit; e.g.:
25365 @smallexample @c ada
25366 for Char in Character range 'A' .. 'Z' loop ... end loop;
25369 @item New reserved words
25370 The identifiers @code{abstract}, @code{aliased}, @code{protected},
25371 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
25372 Existing Ada 83 code using any of these identifiers must be edited to
25373 use some alternative name.
25375 @item Freezing rules
25376 The rules in Ada 95 are slightly different with regard to the point at
25377 which entities are frozen, and representation pragmas and clauses are
25378 not permitted past the freeze point. This shows up most typically in
25379 the form of an error message complaining that a representation item
25380 appears too late, and the appropriate corrective action is to move
25381 the item nearer to the declaration of the entity to which it refers.
25383 A particular case is that representation pragmas
25386 extended DEC Ada 83 compatibility pragmas such as @code{Export_Procedure})
25388 cannot be applied to a subprogram body. If necessary, a separate subprogram
25389 declaration must be introduced to which the pragma can be applied.
25391 @item Optional bodies for library packages
25392 In Ada 83, a package that did not require a package body was nevertheless
25393 allowed to have one. This lead to certain surprises in compiling large
25394 systems (situations in which the body could be unexpectedly ignored by the
25395 binder). In Ada 95, if a package does not require a body then it is not
25396 permitted to have a body. To fix this problem, simply remove a redundant
25397 body if it is empty, or, if it is non-empty, introduce a dummy declaration
25398 into the spec that makes the body required. One approach is to add a private
25399 part to the package declaration (if necessary), and define a parameterless
25400 procedure called @code{Requires_Body}, which must then be given a dummy
25401 procedure body in the package body, which then becomes required.
25402 Another approach (assuming that this does not introduce elaboration
25403 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
25404 since one effect of this pragma is to require the presence of a package body.
25406 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
25407 In Ada 95, the exception @code{Numeric_Error} is a renaming of
25408 @code{Constraint_Error}.
25409 This means that it is illegal to have separate exception handlers for
25410 the two exceptions. The fix is simply to remove the handler for the
25411 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
25412 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
25414 @item Indefinite subtypes in generics
25415 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
25416 as the actual for a generic formal private type, but then the instantiation
25417 would be illegal if there were any instances of declarations of variables
25418 of this type in the generic body. In Ada 95, to avoid this clear violation
25419 of the methodological principle known as the ``contract model'',
25420 the generic declaration explicitly indicates whether
25421 or not such instantiations are permitted. If a generic formal parameter
25422 has explicit unknown discriminants, indicated by using @code{(<>)} after the
25423 type name, then it can be instantiated with indefinite types, but no
25424 stand-alone variables can be declared of this type. Any attempt to declare
25425 such a variable will result in an illegality at the time the generic is
25426 declared. If the @code{(<>)} notation is not used, then it is illegal
25427 to instantiate the generic with an indefinite type.
25428 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
25429 It will show up as a compile time error, and
25430 the fix is usually simply to add the @code{(<>)} to the generic declaration.
25433 @node More deterministic semantics
25434 @subsection More deterministic semantics
25438 Conversions from real types to integer types round away from 0. In Ada 83
25439 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
25440 implementation freedom was intended to support unbiased rounding in
25441 statistical applications, but in practice it interfered with portability.
25442 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
25443 is required. Numeric code may be affected by this change in semantics.
25444 Note, though, that this issue is no worse than already existed in Ada 83
25445 when porting code from one vendor to another.
25448 The Real-Time Annex introduces a set of policies that define the behavior of
25449 features that were implementation dependent in Ada 83, such as the order in
25450 which open select branches are executed.
25453 @node Changed semantics
25454 @subsection Changed semantics
25457 The worst kind of incompatibility is one where a program that is legal in
25458 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
25459 possible in Ada 83. Fortunately this is extremely rare, but the one
25460 situation that you should be alert to is the change in the predefined type
25461 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
25464 @item range of @code{Character}
25465 The range of @code{Standard.Character} is now the full 256 characters
25466 of Latin-1, whereas in most Ada 83 implementations it was restricted
25467 to 128 characters. Although some of the effects of
25468 this change will be manifest in compile-time rejection of legal
25469 Ada 83 programs it is possible for a working Ada 83 program to have
25470 a different effect in Ada 95, one that was not permitted in Ada 83.
25471 As an example, the expression
25472 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
25473 delivers @code{255} as its value.
25474 In general, you should look at the logic of any
25475 character-processing Ada 83 program and see whether it needs to be adapted
25476 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
25477 character handling package that may be relevant if code needs to be adapted
25478 to account for the additional Latin-1 elements.
25479 The desirable fix is to
25480 modify the program to accommodate the full character set, but in some cases
25481 it may be convenient to define a subtype or derived type of Character that
25482 covers only the restricted range.
25486 @node Other language compatibility issues
25487 @subsection Other language compatibility issues
25489 @item @option{-gnat83 switch}
25490 All implementations of GNAT provide a switch that causes GNAT to operate
25491 in Ada 83 mode. In this mode, some but not all compatibility problems
25492 of the type described above are handled automatically. For example, the
25493 new Ada 95 reserved words are treated simply as identifiers as in Ada 83.
25495 in practice, it is usually advisable to make the necessary modifications
25496 to the program to remove the need for using this switch.
25497 See @ref{Compiling Ada 83 Programs}.
25499 @item Support for removed Ada 83 pragmas and attributes
25500 A number of pragmas and attributes from Ada 83 have been removed from Ada 95,
25501 generally because they have been replaced by other mechanisms. Ada 95
25502 compilers are allowed, but not required, to implement these missing
25503 elements. In contrast with some other Ada 95 compilers, GNAT implements all
25504 such pragmas and attributes, eliminating this compatibility concern. These
25505 include @code{pragma Interface} and the floating point type attributes
25506 (@code{Emax}, @code{Mantissa}, etc.), among other items.
25510 @node Implementation-dependent characteristics
25511 @section Implementation-dependent characteristics
25513 Although the Ada language defines the semantics of each construct as
25514 precisely as practical, in some situations (for example for reasons of
25515 efficiency, or where the effect is heavily dependent on the host or target
25516 platform) the implementation is allowed some freedom. In porting Ada 83
25517 code to GNAT, you need to be aware of whether / how the existing code
25518 exercised such implementation dependencies. Such characteristics fall into
25519 several categories, and GNAT offers specific support in assisting the
25520 transition from certain Ada 83 compilers.
25523 * Implementation-defined pragmas::
25524 * Implementation-defined attributes::
25526 * Elaboration order::
25527 * Target-specific aspects::
25531 @node Implementation-defined pragmas
25532 @subsection Implementation-defined pragmas
25535 Ada compilers are allowed to supplement the language-defined pragmas, and
25536 these are a potential source of non-portability. All GNAT-defined pragmas
25537 are described in the GNAT Reference Manual, and these include several that
25538 are specifically intended to correspond to other vendors' Ada 83 pragmas.
25539 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
25541 compatibility with DEC Ada 83, GNAT supplies the pragmas
25542 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
25543 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
25544 and @code{Volatile}.
25545 Other relevant pragmas include @code{External} and @code{Link_With}.
25546 Some vendor-specific
25547 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
25549 avoiding compiler rejection of units that contain such pragmas; they are not
25550 relevant in a GNAT context and hence are not otherwise implemented.
25552 @node Implementation-defined attributes
25553 @subsection Implementation-defined attributes
25555 Analogous to pragmas, the set of attributes may be extended by an
25556 implementation. All GNAT-defined attributes are described in the
25557 @cite{GNAT Reference Manual}, and these include several that are specifically
25559 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
25560 the attribute @code{VADS_Size} may be useful. For compatibility with DEC
25561 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
25565 @subsection Libraries
25567 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
25568 code uses vendor-specific libraries then there are several ways to manage
25572 If the source code for the libraries (specifications and bodies) are
25573 available, then the libraries can be migrated in the same way as the
25576 If the source code for the specifications but not the bodies are
25577 available, then you can reimplement the bodies.
25579 Some new Ada 95 features obviate the need for library support. For
25580 example most Ada 83 vendors supplied a package for unsigned integers. The
25581 Ada 95 modular type feature is the preferred way to handle this need, so
25582 instead of migrating or reimplementing the unsigned integer package it may
25583 be preferable to retrofit the application using modular types.
25586 @node Elaboration order
25587 @subsection Elaboration order
25589 The implementation can choose any elaboration order consistent with the unit
25590 dependency relationship. This freedom means that some orders can result in
25591 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
25592 to invoke a subprogram its body has been elaborated, or to instantiate a
25593 generic before the generic body has been elaborated. By default GNAT
25594 attempts to choose a safe order (one that will not encounter access before
25595 elaboration problems) by implicitly inserting Elaborate_All pragmas where
25596 needed. However, this can lead to the creation of elaboration circularities
25597 and a resulting rejection of the program by gnatbind. This issue is
25598 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
25599 In brief, there are several
25600 ways to deal with this situation:
25604 Modify the program to eliminate the circularities, e.g. by moving
25605 elaboration-time code into explicitly-invoked procedures
25607 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
25608 @code{Elaborate} pragmas, and then inhibit the generation of implicit
25609 @code{Elaborate_All}
25610 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
25611 (by selectively suppressing elaboration checks via pragma
25612 @code{Suppress(Elaboration_Check)} when it is safe to do so).
25615 @node Target-specific aspects
25616 @subsection Target-specific aspects
25618 Low-level applications need to deal with machine addresses, data
25619 representations, interfacing with assembler code, and similar issues. If
25620 such an Ada 83 application is being ported to different target hardware (for
25621 example where the byte endianness has changed) then you will need to
25622 carefully examine the program logic; the porting effort will heavily depend
25623 on the robustness of the original design. Moreover, Ada 95 is sometimes
25624 incompatible with typical Ada 83 compiler practices regarding implicit
25625 packing, the meaning of the Size attribute, and the size of access values.
25626 GNAT's approach to these issues is described in @ref{Representation Clauses}.
25629 @node Compatibility with Other Ada 95 Systems
25630 @section Compatibility with Other Ada 95 Systems
25633 Providing that programs avoid the use of implementation dependent and
25634 implementation defined features of Ada 95, as documented in the Ada 95
25635 reference manual, there should be a high degree of portability between
25636 GNAT and other Ada 95 systems. The following are specific items which
25637 have proved troublesome in moving GNAT programs to other Ada 95
25638 compilers, but do not affect porting code to GNAT@.
25641 @item Ada 83 Pragmas and Attributes
25642 Ada 95 compilers are allowed, but not required, to implement the missing
25643 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
25644 GNAT implements all such pragmas and attributes, eliminating this as
25645 a compatibility concern, but some other Ada 95 compilers reject these
25646 pragmas and attributes.
25648 @item Special-needs Annexes
25649 GNAT implements the full set of special needs annexes. At the
25650 current time, it is the only Ada 95 compiler to do so. This means that
25651 programs making use of these features may not be portable to other Ada
25652 95 compilation systems.
25654 @item Representation Clauses
25655 Some other Ada 95 compilers implement only the minimal set of
25656 representation clauses required by the Ada 95 reference manual. GNAT goes
25657 far beyond this minimal set, as described in the next section.
25660 @node Representation Clauses
25661 @section Representation Clauses
25664 The Ada 83 reference manual was quite vague in describing both the minimal
25665 required implementation of representation clauses, and also their precise
25666 effects. The Ada 95 reference manual is much more explicit, but the minimal
25667 set of capabilities required in Ada 95 is quite limited.
25669 GNAT implements the full required set of capabilities described in the
25670 Ada 95 reference manual, but also goes much beyond this, and in particular
25671 an effort has been made to be compatible with existing Ada 83 usage to the
25672 greatest extent possible.
25674 A few cases exist in which Ada 83 compiler behavior is incompatible with
25675 requirements in the Ada 95 reference manual. These are instances of
25676 intentional or accidental dependence on specific implementation dependent
25677 characteristics of these Ada 83 compilers. The following is a list of
25678 the cases most likely to arise in existing legacy Ada 83 code.
25681 @item Implicit Packing
25682 Some Ada 83 compilers allowed a Size specification to cause implicit
25683 packing of an array or record. This could cause expensive implicit
25684 conversions for change of representation in the presence of derived
25685 types, and the Ada design intends to avoid this possibility.
25686 Subsequent AI's were issued to make it clear that such implicit
25687 change of representation in response to a Size clause is inadvisable,
25688 and this recommendation is represented explicitly in the Ada 95 RM
25689 as implementation advice that is followed by GNAT@.
25690 The problem will show up as an error
25691 message rejecting the size clause. The fix is simply to provide
25692 the explicit pragma @code{Pack}, or for more fine tuned control, provide
25693 a Component_Size clause.
25695 @item Meaning of Size Attribute
25696 The Size attribute in Ada 95 for discrete types is defined as being the
25697 minimal number of bits required to hold values of the type. For example,
25698 on a 32-bit machine, the size of Natural will typically be 31 and not
25699 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
25700 some 32 in this situation. This problem will usually show up as a compile
25701 time error, but not always. It is a good idea to check all uses of the
25702 'Size attribute when porting Ada 83 code. The GNAT specific attribute
25703 Object_Size can provide a useful way of duplicating the behavior of
25704 some Ada 83 compiler systems.
25706 @item Size of Access Types
25707 A common assumption in Ada 83 code is that an access type is in fact a pointer,
25708 and that therefore it will be the same size as a System.Address value. This
25709 assumption is true for GNAT in most cases with one exception. For the case of
25710 a pointer to an unconstrained array type (where the bounds may vary from one
25711 value of the access type to another), the default is to use a ``fat pointer'',
25712 which is represented as two separate pointers, one to the bounds, and one to
25713 the array. This representation has a number of advantages, including improved
25714 efficiency. However, it may cause some difficulties in porting existing Ada 83
25715 code which makes the assumption that, for example, pointers fit in 32 bits on
25716 a machine with 32-bit addressing.
25718 To get around this problem, GNAT also permits the use of ``thin pointers'' for
25719 access types in this case (where the designated type is an unconstrained array
25720 type). These thin pointers are indeed the same size as a System.Address value.
25721 To specify a thin pointer, use a size clause for the type, for example:
25723 @smallexample @c ada
25724 type X is access all String;
25725 for X'Size use Standard'Address_Size;
25729 which will cause the type X to be represented using a single pointer.
25730 When using this representation, the bounds are right behind the array.
25731 This representation is slightly less efficient, and does not allow quite
25732 such flexibility in the use of foreign pointers or in using the
25733 Unrestricted_Access attribute to create pointers to non-aliased objects.
25734 But for any standard portable use of the access type it will work in
25735 a functionally correct manner and allow porting of existing code.
25736 Note that another way of forcing a thin pointer representation
25737 is to use a component size clause for the element size in an array,
25738 or a record representation clause for an access field in a record.
25741 @node Compatibility with DEC Ada 83
25742 @section Compatibility with DEC Ada 83
25745 The VMS version of GNAT fully implements all the pragmas and attributes
25746 provided by DEC Ada 83, as well as providing the standard DEC Ada 83
25747 libraries, including Starlet. In addition, data layouts and parameter
25748 passing conventions are highly compatible. This means that porting
25749 existing DEC Ada 83 code to GNAT in VMS systems should be easier than
25750 most other porting efforts. The following are some of the most
25751 significant differences between GNAT and DEC Ada 83.
25754 @item Default floating-point representation
25755 In GNAT, the default floating-point format is IEEE, whereas in DEC Ada 83,
25756 it is VMS format. GNAT does implement the necessary pragmas
25757 (Long_Float, Float_Representation) for changing this default.
25760 The package System in GNAT exactly corresponds to the definition in the
25761 Ada 95 reference manual, which means that it excludes many of the
25762 DEC Ada 83 extensions. However, a separate package Aux_DEC is provided
25763 that contains the additional definitions, and a special pragma,
25764 Extend_System allows this package to be treated transparently as an
25765 extension of package System.
25768 The definitions provided by Aux_DEC are exactly compatible with those
25769 in the DEC Ada 83 version of System, with one exception.
25770 DEC Ada provides the following declarations:
25772 @smallexample @c ada
25773 TO_ADDRESS (INTEGER)
25774 TO_ADDRESS (UNSIGNED_LONGWORD)
25775 TO_ADDRESS (universal_integer)
25779 The version of TO_ADDRESS taking a universal integer argument is in fact
25780 an extension to Ada 83 not strictly compatible with the reference manual.
25781 In GNAT, we are constrained to be exactly compatible with the standard,
25782 and this means we cannot provide this capability. In DEC Ada 83, the
25783 point of this definition is to deal with a call like:
25785 @smallexample @c ada
25786 TO_ADDRESS (16#12777#);
25790 Normally, according to the Ada 83 standard, one would expect this to be
25791 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
25792 of TO_ADDRESS@. However, in DEC Ada 83, there is no ambiguity, since the
25793 definition using universal_integer takes precedence.
25795 In GNAT, since the version with universal_integer cannot be supplied, it is
25796 not possible to be 100% compatible. Since there are many programs using
25797 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
25798 to change the name of the function in the UNSIGNED_LONGWORD case, so the
25799 declarations provided in the GNAT version of AUX_Dec are:
25801 @smallexample @c ada
25802 function To_Address (X : Integer) return Address;
25803 pragma Pure_Function (To_Address);
25805 function To_Address_Long (X : Unsigned_Longword)
25807 pragma Pure_Function (To_Address_Long);
25811 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
25812 change the name to TO_ADDRESS_LONG@.
25814 @item Task_Id values
25815 The Task_Id values assigned will be different in the two systems, and GNAT
25816 does not provide a specified value for the Task_Id of the environment task,
25817 which in GNAT is treated like any other declared task.
25820 For full details on these and other less significant compatibility issues,
25821 see appendix E of the Digital publication entitled @cite{DEC Ada, Technical
25822 Overview and Comparison on DIGITAL Platforms}.
25824 For GNAT running on other than VMS systems, all the DEC Ada 83 pragmas and
25825 attributes are recognized, although only a subset of them can sensibly
25826 be implemented. The description of pragmas in this reference manual
25827 indicates whether or not they are applicable to non-VMS systems.
25832 @node Microsoft Windows Topics
25833 @appendix Microsoft Windows Topics
25839 This chapter describes topics that are specific to the Microsoft Windows
25840 platforms (NT, 2000, and XP Professional).
25843 * Using GNAT on Windows::
25844 * Using a network installation of GNAT::
25845 * CONSOLE and WINDOWS subsystems::
25846 * Temporary Files::
25847 * Mixed-Language Programming on Windows::
25848 * Windows Calling Conventions::
25849 * Introduction to Dynamic Link Libraries (DLLs)::
25850 * Using DLLs with GNAT::
25851 * Building DLLs with GNAT::
25852 * GNAT and Windows Resources::
25853 * Debugging a DLL::
25854 * GNAT and COM/DCOM Objects::
25857 @node Using GNAT on Windows
25858 @section Using GNAT on Windows
25861 One of the strengths of the GNAT technology is that its tool set
25862 (@code{gcc}, @code{gnatbind}, @code{gnatlink}, @code{gnatmake}, the
25863 @code{gdb} debugger, etc.) is used in the same way regardless of the
25866 On Windows this tool set is complemented by a number of Microsoft-specific
25867 tools that have been provided to facilitate interoperability with Windows
25868 when this is required. With these tools:
25873 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
25877 You can use any Dynamically Linked Library (DLL) in your Ada code (both
25878 relocatable and non-relocatable DLLs are supported).
25881 You can build Ada DLLs for use in other applications. These applications
25882 can be written in a language other than Ada (e.g., C, C++, etc). Again both
25883 relocatable and non-relocatable Ada DLLs are supported.
25886 You can include Windows resources in your Ada application.
25889 You can use or create COM/DCOM objects.
25893 Immediately below are listed all known general GNAT-for-Windows restrictions.
25894 Other restrictions about specific features like Windows Resources and DLLs
25895 are listed in separate sections below.
25900 It is not possible to use @code{GetLastError} and @code{SetLastError}
25901 when tasking, protected records, or exceptions are used. In these
25902 cases, in order to implement Ada semantics, the GNAT run-time system
25903 calls certain Win32 routines that set the last error variable to 0 upon
25904 success. It should be possible to use @code{GetLastError} and
25905 @code{SetLastError} when tasking, protected record, and exception
25906 features are not used, but it is not guaranteed to work.
25909 It is not possible to link against Microsoft libraries except for
25910 import libraries. The library must be built to be compatible with
25911 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
25912 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
25913 not be compatible with the GNAT runtime. Even if the library is
25914 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
25917 When the compilation environment is located on FAT32 drives, users may
25918 experience recompilations of the source files that have not changed if
25919 Daylight Saving Time (DST) state has changed since the last time files
25920 were compiled. NTFS drives do not have this problem.
25923 No components of the GNAT toolset use any entries in the Windows
25924 registry. The only entries that can be created are file associations and
25925 PATH settings, provided the user has chosen to create them at installation
25926 time, as well as some minimal book-keeping information needed to correctly
25927 uninstall or integrate different GNAT products.
25930 @node Using a network installation of GNAT
25931 @section Using a network installation of GNAT
25934 Make sure the system on which GNAT is installed is accessible from the
25935 current machine, i.e. the install location is shared over the network.
25936 Shared resources are accessed on Windows by means of UNC paths, which
25937 have the format @code{\\server\sharename\path}
25939 In order to use such a network installation, simply add the UNC path of the
25940 @file{bin} directory of your GNAT installation in front of your PATH. For
25941 example, if GNAT is installed in @file{\GNAT} directory of a share location
25942 called @file{c-drive} on a machine @file{LOKI}, the following command will
25945 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
25947 Be aware that every compilation using the network installation results in the
25948 transfer of large amounts of data across the network and will likely cause
25949 serious performance penalty.
25951 @node CONSOLE and WINDOWS subsystems
25952 @section CONSOLE and WINDOWS subsystems
25953 @cindex CONSOLE Subsystem
25954 @cindex WINDOWS Subsystem
25958 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
25959 (which is the default subsystem) will always create a console when
25960 launching the application. This is not something desirable when the
25961 application has a Windows GUI. To get rid of this console the
25962 application must be using the @code{WINDOWS} subsystem. To do so
25963 the @option{-mwindows} linker option must be specified.
25966 $ gnatmake winprog -largs -mwindows
25969 @node Temporary Files
25970 @section Temporary Files
25971 @cindex Temporary files
25974 It is possible to control where temporary files gets created by setting
25975 the TMP environment variable. The file will be created:
25978 @item Under the directory pointed to by the TMP environment variable if
25979 this directory exists.
25981 @item Under c:\temp, if the TMP environment variable is not set (or not
25982 pointing to a directory) and if this directory exists.
25984 @item Under the current working directory otherwise.
25988 This allows you to determine exactly where the temporary
25989 file will be created. This is particularly useful in networked
25990 environments where you may not have write access to some
25993 @node Mixed-Language Programming on Windows
25994 @section Mixed-Language Programming on Windows
25997 Developing pure Ada applications on Windows is no different than on
25998 other GNAT-supported platforms. However, when developing or porting an
25999 application that contains a mix of Ada and C/C++, the choice of your
26000 Windows C/C++ development environment conditions your overall
26001 interoperability strategy.
26003 If you use @code{gcc} to compile the non-Ada part of your application,
26004 there are no Windows-specific restrictions that affect the overall
26005 interoperability with your Ada code. If you plan to use
26006 Microsoft tools (e.g. Microsoft Visual C/C++), you should be aware of
26007 the following limitations:
26011 You cannot link your Ada code with an object or library generated with
26012 Microsoft tools if these use the @code{.tls} section (Thread Local
26013 Storage section) since the GNAT linker does not yet support this section.
26016 You cannot link your Ada code with an object or library generated with
26017 Microsoft tools if these use I/O routines other than those provided in
26018 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
26019 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
26020 libraries can cause a conflict with @code{msvcrt.dll} services. For
26021 instance Visual C++ I/O stream routines conflict with those in
26026 If you do want to use the Microsoft tools for your non-Ada code and hit one
26027 of the above limitations, you have two choices:
26031 Encapsulate your non Ada code in a DLL to be linked with your Ada
26032 application. In this case, use the Microsoft or whatever environment to
26033 build the DLL and use GNAT to build your executable
26034 (@pxref{Using DLLs with GNAT}).
26037 Or you can encapsulate your Ada code in a DLL to be linked with the
26038 other part of your application. In this case, use GNAT to build the DLL
26039 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
26040 environment to build your executable.
26043 @node Windows Calling Conventions
26044 @section Windows Calling Conventions
26049 * C Calling Convention::
26050 * Stdcall Calling Convention::
26051 * DLL Calling Convention::
26055 When a subprogram @code{F} (caller) calls a subprogram @code{G}
26056 (callee), there are several ways to push @code{G}'s parameters on the
26057 stack and there are several possible scenarios to clean up the stack
26058 upon @code{G}'s return. A calling convention is an agreed upon software
26059 protocol whereby the responsibilities between the caller (@code{F}) and
26060 the callee (@code{G}) are clearly defined. Several calling conventions
26061 are available for Windows:
26065 @code{C} (Microsoft defined)
26068 @code{Stdcall} (Microsoft defined)
26071 @code{DLL} (GNAT specific)
26074 @node C Calling Convention
26075 @subsection @code{C} Calling Convention
26078 This is the default calling convention used when interfacing to C/C++
26079 routines compiled with either @code{gcc} or Microsoft Visual C++.
26081 In the @code{C} calling convention subprogram parameters are pushed on the
26082 stack by the caller from right to left. The caller itself is in charge of
26083 cleaning up the stack after the call. In addition, the name of a routine
26084 with @code{C} calling convention is mangled by adding a leading underscore.
26086 The name to use on the Ada side when importing (or exporting) a routine
26087 with @code{C} calling convention is the name of the routine. For
26088 instance the C function:
26091 int get_val (long);
26095 should be imported from Ada as follows:
26097 @smallexample @c ada
26099 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26100 pragma Import (C, Get_Val, External_Name => "get_val");
26105 Note that in this particular case the @code{External_Name} parameter could
26106 have been omitted since, when missing, this parameter is taken to be the
26107 name of the Ada entity in lower case. When the @code{Link_Name} parameter
26108 is missing, as in the above example, this parameter is set to be the
26109 @code{External_Name} with a leading underscore.
26111 When importing a variable defined in C, you should always use the @code{C}
26112 calling convention unless the object containing the variable is part of a
26113 DLL (in which case you should use the @code{DLL} calling convention,
26114 @pxref{DLL Calling Convention}).
26116 @node Stdcall Calling Convention
26117 @subsection @code{Stdcall} Calling Convention
26120 This convention, which was the calling convention used for Pascal
26121 programs, is used by Microsoft for all the routines in the Win32 API for
26122 efficiency reasons. It must be used to import any routine for which this
26123 convention was specified.
26125 In the @code{Stdcall} calling convention subprogram parameters are pushed
26126 on the stack by the caller from right to left. The callee (and not the
26127 caller) is in charge of cleaning the stack on routine exit. In addition,
26128 the name of a routine with @code{Stdcall} calling convention is mangled by
26129 adding a leading underscore (as for the @code{C} calling convention) and a
26130 trailing @code{@@}@code{@i{nn}}, where @i{nn} is the overall size (in
26131 bytes) of the parameters passed to the routine.
26133 The name to use on the Ada side when importing a C routine with a
26134 @code{Stdcall} calling convention is the name of the C routine. The leading
26135 underscore and trailing @code{@@}@code{@i{nn}} are added automatically by
26136 the compiler. For instance the Win32 function:
26139 @b{APIENTRY} int get_val (long);
26143 should be imported from Ada as follows:
26145 @smallexample @c ada
26147 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26148 pragma Import (Stdcall, Get_Val);
26149 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
26154 As for the @code{C} calling convention, when the @code{External_Name}
26155 parameter is missing, it is taken to be the name of the Ada entity in lower
26156 case. If instead of writing the above import pragma you write:
26158 @smallexample @c ada
26160 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26161 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
26166 then the imported routine is @code{_retrieve_val@@4}. However, if instead
26167 of specifying the @code{External_Name} parameter you specify the
26168 @code{Link_Name} as in the following example:
26170 @smallexample @c ada
26172 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
26173 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
26178 then the imported routine is @code{retrieve_val@@4}, that is, there is no
26179 trailing underscore but the appropriate @code{@@}@code{@i{nn}} is always
26180 added at the end of the @code{Link_Name} by the compiler.
26183 Note, that in some special cases a DLL's entry point name lacks a trailing
26184 @code{@@}@code{@i{nn}} while the exported name generated for a call has it.
26185 The @code{gnatdll} tool, which creates the import library for the DLL, is able
26186 to handle those cases (see the description of the switches in
26187 @pxref{Using gnatdll} section).
26189 @node DLL Calling Convention
26190 @subsection @code{DLL} Calling Convention
26193 This convention, which is GNAT-specific, must be used when you want to
26194 import in Ada a variables defined in a DLL. For functions and procedures
26195 this convention is equivalent to the @code{Stdcall} convention. As an
26196 example, if a DLL contains a variable defined as:
26203 then, to access this variable from Ada you should write:
26205 @smallexample @c ada
26207 My_Var : Interfaces.C.int;
26208 pragma Import (DLL, My_Var);
26212 The remarks concerning the @code{External_Name} and @code{Link_Name}
26213 parameters given in the previous sections equally apply to the @code{DLL}
26214 calling convention.
26216 @node Introduction to Dynamic Link Libraries (DLLs)
26217 @section Introduction to Dynamic Link Libraries (DLLs)
26221 A Dynamically Linked Library (DLL) is a library that can be shared by
26222 several applications running under Windows. A DLL can contain any number of
26223 routines and variables.
26225 One advantage of DLLs is that you can change and enhance them without
26226 forcing all the applications that depend on them to be relinked or
26227 recompiled. However, you should be aware than all calls to DLL routines are
26228 slower since, as you will understand below, such calls are indirect.
26230 To illustrate the remainder of this section, suppose that an application
26231 wants to use the services of a DLL @file{API.dll}. To use the services
26232 provided by @file{API.dll} you must statically link against an import
26233 library which contains a jump table with an entry for each routine and
26234 variable exported by the DLL. In the Microsoft world this import library is
26235 called @file{API.lib}. When using GNAT this import library is called either
26236 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
26238 After you have statically linked your application with the import library
26239 and you run your application, here is what happens:
26243 Your application is loaded into memory.
26246 The DLL @file{API.dll} is mapped into the address space of your
26247 application. This means that:
26251 The DLL will use the stack of the calling thread.
26254 The DLL will use the virtual address space of the calling process.
26257 The DLL will allocate memory from the virtual address space of the calling
26261 Handles (pointers) can be safely exchanged between routines in the DLL
26262 routines and routines in the application using the DLL.
26266 The entries in the @file{libAPI.a} or @file{API.lib} jump table which is
26267 part of your application are initialized with the addresses of the routines
26268 and variables in @file{API.dll}.
26271 If present in @file{API.dll}, routines @code{DllMain} or
26272 @code{DllMainCRTStartup} are invoked. These routines typically contain
26273 the initialization code needed for the well-being of the routines and
26274 variables exported by the DLL.
26278 There is an additional point which is worth mentioning. In the Windows
26279 world there are two kind of DLLs: relocatable and non-relocatable
26280 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
26281 in the target application address space. If the addresses of two
26282 non-relocatable DLLs overlap and these happen to be used by the same
26283 application, a conflict will occur and the application will run
26284 incorrectly. Hence, when possible, it is always preferable to use and
26285 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
26286 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
26287 User's Guide) removes the debugging symbols from the DLL but the DLL can
26288 still be relocated.
26290 As a side note, an interesting difference between Microsoft DLLs and
26291 Unix shared libraries, is the fact that on most Unix systems all public
26292 routines are exported by default in a Unix shared library, while under
26293 Windows the exported routines must be listed explicitly in a definition
26294 file (@pxref{The Definition File}).
26296 @node Using DLLs with GNAT
26297 @section Using DLLs with GNAT
26300 * Creating an Ada Spec for the DLL Services::
26301 * Creating an Import Library::
26305 To use the services of a DLL, say @file{API.dll}, in your Ada application
26310 The Ada spec for the routines and/or variables you want to access in
26311 @file{API.dll}. If not available this Ada spec must be built from the C/C++
26312 header files provided with the DLL.
26315 The import library (@file{libAPI.a} or @file{API.lib}). As previously
26316 mentioned an import library is a statically linked library containing the
26317 import table which will be filled at load time to point to the actual
26318 @file{API.dll} routines. Sometimes you don't have an import library for the
26319 DLL you want to use. The following sections will explain how to build one.
26322 The actual DLL, @file{API.dll}.
26326 Once you have all the above, to compile an Ada application that uses the
26327 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
26328 you simply issue the command
26331 $ gnatmake my_ada_app -largs -lAPI
26335 The argument @option{-largs -lAPI} at the end of the @code{gnatmake} command
26336 tells the GNAT linker to look first for a library named @file{API.lib}
26337 (Microsoft-style name) and if not found for a library named @file{libAPI.a}
26338 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
26339 contains the following pragma
26341 @smallexample @c ada
26342 pragma Linker_Options ("-lAPI");
26346 you do not have to add @option{-largs -lAPI} at the end of the @code{gnatmake}
26349 If any one of the items above is missing you will have to create it
26350 yourself. The following sections explain how to do so using as an
26351 example a fictitious DLL called @file{API.dll}.
26353 @node Creating an Ada Spec for the DLL Services
26354 @subsection Creating an Ada Spec for the DLL Services
26357 A DLL typically comes with a C/C++ header file which provides the
26358 definitions of the routines and variables exported by the DLL. The Ada
26359 equivalent of this header file is a package spec that contains definitions
26360 for the imported entities. If the DLL you intend to use does not come with
26361 an Ada spec you have to generate one such spec yourself. For example if
26362 the header file of @file{API.dll} is a file @file{api.h} containing the
26363 following two definitions:
26375 then the equivalent Ada spec could be:
26377 @smallexample @c ada
26380 with Interfaces.C.Strings;
26385 function Get (Str : C.Strings.Chars_Ptr) return C.int;
26388 pragma Import (C, Get);
26389 pragma Import (DLL, Some_Var);
26396 Note that a variable is @strong{always imported with a DLL convention}. A
26397 function can have @code{C}, @code{Stdcall} or @code{DLL} convention. For
26398 subprograms, the @code{DLL} convention is a synonym of @code{Stdcall}
26399 (@pxref{Windows Calling Conventions}).
26401 @node Creating an Import Library
26402 @subsection Creating an Import Library
26403 @cindex Import library
26406 * The Definition File::
26407 * GNAT-Style Import Library::
26408 * Microsoft-Style Import Library::
26412 If a Microsoft-style import library @file{API.lib} or a GNAT-style
26413 import library @file{libAPI.a} is available with @file{API.dll} you
26414 can skip this section. Otherwise read on.
26416 @node The Definition File
26417 @subsubsection The Definition File
26418 @cindex Definition file
26422 As previously mentioned, and unlike Unix systems, the list of symbols
26423 that are exported from a DLL must be provided explicitly in Windows.
26424 The main goal of a definition file is precisely that: list the symbols
26425 exported by a DLL. A definition file (usually a file with a @code{.def}
26426 suffix) has the following structure:
26432 [DESCRIPTION @i{string}]
26442 @item LIBRARY @i{name}
26443 This section, which is optional, gives the name of the DLL.
26445 @item DESCRIPTION @i{string}
26446 This section, which is optional, gives a description string that will be
26447 embedded in the import library.
26450 This section gives the list of exported symbols (procedures, functions or
26451 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
26452 section of @file{API.def} looks like:
26466 Note that you must specify the correct suffix (@code{@@}@code{@i{nn}})
26467 (@pxref{Windows Calling Conventions}) for a Stdcall
26468 calling convention function in the exported symbols list.
26471 There can actually be other sections in a definition file, but these
26472 sections are not relevant to the discussion at hand.
26474 @node GNAT-Style Import Library
26475 @subsubsection GNAT-Style Import Library
26478 To create a static import library from @file{API.dll} with the GNAT tools
26479 you should proceed as follows:
26483 Create the definition file @file{API.def} (@pxref{The Definition File}).
26484 For that use the @code{dll2def} tool as follows:
26487 $ dll2def API.dll > API.def
26491 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
26492 to standard output the list of entry points in the DLL. Note that if
26493 some routines in the DLL have the @code{Stdcall} convention
26494 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@i{nn}
26495 suffix then you'll have to edit @file{api.def} to add it.
26498 Here are some hints to find the right @code{@@}@i{nn} suffix.
26502 If you have the Microsoft import library (.lib), it is possible to get
26503 the right symbols by using Microsoft @code{dumpbin} tool (see the
26504 corresponding Microsoft documentation for further details).
26507 $ dumpbin /exports api.lib
26511 If you have a message about a missing symbol at link time the compiler
26512 tells you what symbol is expected. You just have to go back to the
26513 definition file and add the right suffix.
26517 Build the import library @code{libAPI.a}, using @code{gnatdll}
26518 (@pxref{Using gnatdll}) as follows:
26521 $ gnatdll -e API.def -d API.dll
26525 @code{gnatdll} takes as input a definition file @file{API.def} and the
26526 name of the DLL containing the services listed in the definition file
26527 @file{API.dll}. The name of the static import library generated is
26528 computed from the name of the definition file as follows: if the
26529 definition file name is @i{xyz}@code{.def}, the import library name will
26530 be @code{lib}@i{xyz}@code{.a}. Note that in the previous example option
26531 @option{-e} could have been removed because the name of the definition
26532 file (before the ``@code{.def}'' suffix) is the same as the name of the
26533 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
26536 @node Microsoft-Style Import Library
26537 @subsubsection Microsoft-Style Import Library
26540 With GNAT you can either use a GNAT-style or Microsoft-style import
26541 library. A Microsoft import library is needed only if you plan to make an
26542 Ada DLL available to applications developed with Microsoft
26543 tools (@pxref{Mixed-Language Programming on Windows}).
26545 To create a Microsoft-style import library for @file{API.dll} you
26546 should proceed as follows:
26550 Create the definition file @file{API.def} from the DLL. For this use either
26551 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
26552 tool (see the corresponding Microsoft documentation for further details).
26555 Build the actual import library using Microsoft's @code{lib} utility:
26558 $ lib -machine:IX86 -def:API.def -out:API.lib
26562 If you use the above command the definition file @file{API.def} must
26563 contain a line giving the name of the DLL:
26570 See the Microsoft documentation for further details about the usage of
26574 @node Building DLLs with GNAT
26575 @section Building DLLs with GNAT
26576 @cindex DLLs, building
26579 * Limitations When Using Ada DLLs from Ada::
26580 * Exporting Ada Entities::
26581 * Ada DLLs and Elaboration::
26582 * Ada DLLs and Finalization::
26583 * Creating a Spec for Ada DLLs::
26584 * Creating the Definition File::
26589 This section explains how to build DLLs containing Ada code. These DLLs
26590 will be referred to as Ada DLLs in the remainder of this section.
26592 The steps required to build an Ada DLL that is to be used by Ada as well as
26593 non-Ada applications are as follows:
26597 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
26598 @code{Stdcall} calling convention to avoid any Ada name mangling for the
26599 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
26600 skip this step if you plan to use the Ada DLL only from Ada applications.
26603 Your Ada code must export an initialization routine which calls the routine
26604 @code{adainit} generated by @code{gnatbind} to perform the elaboration of
26605 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
26606 routine exported by the Ada DLL must be invoked by the clients of the DLL
26607 to initialize the DLL.
26610 When useful, the DLL should also export a finalization routine which calls
26611 routine @code{adafinal} generated by @code{gnatbind} to perform the
26612 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
26613 The finalization routine exported by the Ada DLL must be invoked by the
26614 clients of the DLL when the DLL services are no further needed.
26617 You must provide a spec for the services exported by the Ada DLL in each
26618 of the programming languages to which you plan to make the DLL available.
26621 You must provide a definition file listing the exported entities
26622 (@pxref{The Definition File}).
26625 Finally you must use @code{gnatdll} to produce the DLL and the import
26626 library (@pxref{Using gnatdll}).
26630 Note that a relocatable DLL stripped using the @code{strip} binutils
26631 tool will not be relocatable anymore. To build a DLL without debug
26632 information pass @code{-largs -s} to @code{gnatdll}.
26634 @node Limitations When Using Ada DLLs from Ada
26635 @subsection Limitations When Using Ada DLLs from Ada
26638 When using Ada DLLs from Ada applications there is a limitation users
26639 should be aware of. Because on Windows the GNAT run time is not in a DLL of
26640 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
26641 each Ada DLL includes the services of the GNAT run time that are necessary
26642 to the Ada code inside the DLL. As a result, when an Ada program uses an
26643 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
26644 one in the main program.
26646 It is therefore not possible to exchange GNAT run-time objects between the
26647 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
26648 handles (e.g. @code{Text_IO.File_Type}), tasks types, protected objects
26651 It is completely safe to exchange plain elementary, array or record types,
26652 Windows object handles, etc.
26654 @node Exporting Ada Entities
26655 @subsection Exporting Ada Entities
26656 @cindex Export table
26659 Building a DLL is a way to encapsulate a set of services usable from any
26660 application. As a result, the Ada entities exported by a DLL should be
26661 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
26662 any Ada name mangling. Please note that the @code{Stdcall} convention
26663 should only be used for subprograms, not for variables. As an example here
26664 is an Ada package @code{API}, spec and body, exporting two procedures, a
26665 function, and a variable:
26667 @smallexample @c ada
26670 with Interfaces.C; use Interfaces;
26672 Count : C.int := 0;
26673 function Factorial (Val : C.int) return C.int;
26675 procedure Initialize_API;
26676 procedure Finalize_API;
26677 -- Initialization & Finalization routines. More in the next section.
26679 pragma Export (C, Initialize_API);
26680 pragma Export (C, Finalize_API);
26681 pragma Export (C, Count);
26682 pragma Export (C, Factorial);
26688 @smallexample @c ada
26691 package body API is
26692 function Factorial (Val : C.int) return C.int is
26695 Count := Count + 1;
26696 for K in 1 .. Val loop
26702 procedure Initialize_API is
26704 pragma Import (C, Adainit);
26707 end Initialize_API;
26709 procedure Finalize_API is
26710 procedure Adafinal;
26711 pragma Import (C, Adafinal);
26721 If the Ada DLL you are building will only be used by Ada applications
26722 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
26723 convention. As an example, the previous package could be written as
26726 @smallexample @c ada
26730 Count : Integer := 0;
26731 function Factorial (Val : Integer) return Integer;
26733 procedure Initialize_API;
26734 procedure Finalize_API;
26735 -- Initialization and Finalization routines.
26741 @smallexample @c ada
26744 package body API is
26745 function Factorial (Val : Integer) return Integer is
26746 Fact : Integer := 1;
26748 Count := Count + 1;
26749 for K in 1 .. Val loop
26756 -- The remainder of this package body is unchanged.
26763 Note that if you do not export the Ada entities with a @code{C} or
26764 @code{Stdcall} convention you will have to provide the mangled Ada names
26765 in the definition file of the Ada DLL
26766 (@pxref{Creating the Definition File}).
26768 @node Ada DLLs and Elaboration
26769 @subsection Ada DLLs and Elaboration
26770 @cindex DLLs and elaboration
26773 The DLL that you are building contains your Ada code as well as all the
26774 routines in the Ada library that are needed by it. The first thing a
26775 user of your DLL must do is elaborate the Ada code
26776 (@pxref{Elaboration Order Handling in GNAT}).
26778 To achieve this you must export an initialization routine
26779 (@code{Initialize_API} in the previous example), which must be invoked
26780 before using any of the DLL services. This elaboration routine must call
26781 the Ada elaboration routine @code{adainit} generated by the GNAT binder
26782 (@pxref{Binding with Non-Ada Main Programs}). See the body of
26783 @code{Initialize_Api} for an example. Note that the GNAT binder is
26784 automatically invoked during the DLL build process by the @code{gnatdll}
26785 tool (@pxref{Using gnatdll}).
26787 When a DLL is loaded, Windows systematically invokes a routine called
26788 @code{DllMain}. It would therefore be possible to call @code{adainit}
26789 directly from @code{DllMain} without having to provide an explicit
26790 initialization routine. Unfortunately, it is not possible to call
26791 @code{adainit} from the @code{DllMain} if your program has library level
26792 tasks because access to the @code{DllMain} entry point is serialized by
26793 the system (that is, only a single thread can execute ``through'' it at a
26794 time), which means that the GNAT run time will deadlock waiting for the
26795 newly created task to complete its initialization.
26797 @node Ada DLLs and Finalization
26798 @subsection Ada DLLs and Finalization
26799 @cindex DLLs and finalization
26802 When the services of an Ada DLL are no longer needed, the client code should
26803 invoke the DLL finalization routine, if available. The DLL finalization
26804 routine is in charge of releasing all resources acquired by the DLL. In the
26805 case of the Ada code contained in the DLL, this is achieved by calling
26806 routine @code{adafinal} generated by the GNAT binder
26807 (@pxref{Binding with Non-Ada Main Programs}).
26808 See the body of @code{Finalize_Api} for an
26809 example. As already pointed out the GNAT binder is automatically invoked
26810 during the DLL build process by the @code{gnatdll} tool
26811 (@pxref{Using gnatdll}).
26813 @node Creating a Spec for Ada DLLs
26814 @subsection Creating a Spec for Ada DLLs
26817 To use the services exported by the Ada DLL from another programming
26818 language (e.g. C), you have to translate the specs of the exported Ada
26819 entities in that language. For instance in the case of @code{API.dll},
26820 the corresponding C header file could look like:
26825 extern int *_imp__count;
26826 #define count (*_imp__count)
26827 int factorial (int);
26833 It is important to understand that when building an Ada DLL to be used by
26834 other Ada applications, you need two different specs for the packages
26835 contained in the DLL: one for building the DLL and the other for using
26836 the DLL. This is because the @code{DLL} calling convention is needed to
26837 use a variable defined in a DLL, but when building the DLL, the variable
26838 must have either the @code{Ada} or @code{C} calling convention. As an
26839 example consider a DLL comprising the following package @code{API}:
26841 @smallexample @c ada
26845 Count : Integer := 0;
26847 -- Remainder of the package omitted.
26854 After producing a DLL containing package @code{API}, the spec that
26855 must be used to import @code{API.Count} from Ada code outside of the
26858 @smallexample @c ada
26863 pragma Import (DLL, Count);
26869 @node Creating the Definition File
26870 @subsection Creating the Definition File
26873 The definition file is the last file needed to build the DLL. It lists
26874 the exported symbols. As an example, the definition file for a DLL
26875 containing only package @code{API} (where all the entities are exported
26876 with a @code{C} calling convention) is:
26891 If the @code{C} calling convention is missing from package @code{API},
26892 then the definition file contains the mangled Ada names of the above
26893 entities, which in this case are:
26902 api__initialize_api
26907 @node Using gnatdll
26908 @subsection Using @code{gnatdll}
26912 * gnatdll Example::
26913 * gnatdll behind the Scenes::
26918 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
26919 and non-Ada sources that make up your DLL have been compiled.
26920 @code{gnatdll} is actually in charge of two distinct tasks: build the
26921 static import library for the DLL and the actual DLL. The form of the
26922 @code{gnatdll} command is
26926 $ gnatdll [@var{switches}] @var{list-of-files} [-largs @var{opts}]
26931 where @i{list-of-files} is a list of ALI and object files. The object
26932 file list must be the exact list of objects corresponding to the non-Ada
26933 sources whose services are to be included in the DLL. The ALI file list
26934 must be the exact list of ALI files for the corresponding Ada sources
26935 whose services are to be included in the DLL. If @i{list-of-files} is
26936 missing, only the static import library is generated.
26939 You may specify any of the following switches to @code{gnatdll}:
26942 @item -a[@var{address}]
26943 @cindex @option{-a} (@code{gnatdll})
26944 Build a non-relocatable DLL at @var{address}. If @var{address} is not
26945 specified the default address @var{0x11000000} will be used. By default,
26946 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
26947 advise the reader to build relocatable DLL.
26949 @item -b @var{address}
26950 @cindex @option{-b} (@code{gnatdll})
26951 Set the relocatable DLL base address. By default the address is
26954 @item -bargs @var{opts}
26955 @cindex @option{-bargs} (@code{gnatdll})
26956 Binder options. Pass @var{opts} to the binder.
26958 @item -d @var{dllfile}
26959 @cindex @option{-d} (@code{gnatdll})
26960 @var{dllfile} is the name of the DLL. This switch must be present for
26961 @code{gnatdll} to do anything. The name of the generated import library is
26962 obtained algorithmically from @var{dllfile} as shown in the following
26963 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
26964 @code{libxyz.a}. The name of the definition file to use (if not specified
26965 by option @option{-e}) is obtained algorithmically from @var{dllfile}
26966 as shown in the following example:
26967 if @var{dllfile} is @code{xyz.dll}, the definition
26968 file used is @code{xyz.def}.
26970 @item -e @var{deffile}
26971 @cindex @option{-e} (@code{gnatdll})
26972 @var{deffile} is the name of the definition file.
26975 @cindex @option{-g} (@code{gnatdll})
26976 Generate debugging information. This information is stored in the object
26977 file and copied from there to the final DLL file by the linker,
26978 where it can be read by the debugger. You must use the
26979 @option{-g} switch if you plan on using the debugger or the symbolic
26983 @cindex @option{-h} (@code{gnatdll})
26984 Help mode. Displays @code{gnatdll} switch usage information.
26987 @cindex @option{-I} (@code{gnatdll})
26988 Direct @code{gnatdll} to search the @var{dir} directory for source and
26989 object files needed to build the DLL.
26990 (@pxref{Search Paths and the Run-Time Library (RTL)}).
26993 @cindex @option{-k} (@code{gnatdll})
26994 Removes the @code{@@}@i{nn} suffix from the import library's exported
26995 names. You must specified this option if you want to use a
26996 @code{Stdcall} function in a DLL for which the @code{@@}@i{nn} suffix
26997 has been removed. This is the case for most of the Windows NT DLL for
26998 example. This option has no effect when @option{-n} option is specified.
27000 @item -l @var{file}
27001 @cindex @option{-l} (@code{gnatdll})
27002 The list of ALI and object files used to build the DLL are listed in
27003 @var{file}, instead of being given in the command line. Each line in
27004 @var{file} contains the name of an ALI or object file.
27007 @cindex @option{-n} (@code{gnatdll})
27008 No Import. Do not create the import library.
27011 @cindex @option{-q} (@code{gnatdll})
27012 Quiet mode. Do not display unnecessary messages.
27015 @cindex @option{-v} (@code{gnatdll})
27016 Verbose mode. Display extra information.
27018 @item -largs @var{opts}
27019 @cindex @option{-largs} (@code{gnatdll})
27020 Linker options. Pass @var{opts} to the linker.
27023 @node gnatdll Example
27024 @subsubsection @code{gnatdll} Example
27027 As an example the command to build a relocatable DLL from @file{api.adb}
27028 once @file{api.adb} has been compiled and @file{api.def} created is
27031 $ gnatdll -d api.dll api.ali
27035 The above command creates two files: @file{libapi.a} (the import
27036 library) and @file{api.dll} (the actual DLL). If you want to create
27037 only the DLL, just type:
27040 $ gnatdll -d api.dll -n api.ali
27044 Alternatively if you want to create just the import library, type:
27047 $ gnatdll -d api.dll
27050 @node gnatdll behind the Scenes
27051 @subsubsection @code{gnatdll} behind the Scenes
27054 This section details the steps involved in creating a DLL. @code{gnatdll}
27055 does these steps for you. Unless you are interested in understanding what
27056 goes on behind the scenes, you should skip this section.
27058 We use the previous example of a DLL containing the Ada package @code{API},
27059 to illustrate the steps necessary to build a DLL. The starting point is a
27060 set of objects that will make up the DLL and the corresponding ALI
27061 files. In the case of this example this means that @file{api.o} and
27062 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
27067 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
27068 the information necessary to generate relocation information for the
27074 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
27079 In addition to the base file, the @code{gnatlink} command generates an
27080 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
27081 asks @code{gnatlink} to generate the routines @code{DllMain} and
27082 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
27083 is loaded into memory.
27086 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
27087 export table (@file{api.exp}). The export table contains the relocation
27088 information in a form which can be used during the final link to ensure
27089 that the Windows loader is able to place the DLL anywhere in memory.
27093 $ dlltool --dllname api.dll --def api.def --base-file api.base \
27094 --output-exp api.exp
27099 @code{gnatdll} builds the base file using the new export table. Note that
27100 @code{gnatbind} must be called once again since the binder generated file
27101 has been deleted during the previous call to @code{gnatlink}.
27106 $ gnatlink api -o api.jnk api.exp -mdll
27107 -Wl,--base-file,api.base
27112 @code{gnatdll} builds the new export table using the new base file and
27113 generates the DLL import library @file{libAPI.a}.
27117 $ dlltool --dllname api.dll --def api.def --base-file api.base \
27118 --output-exp api.exp --output-lib libAPI.a
27123 Finally @code{gnatdll} builds the relocatable DLL using the final export
27129 $ gnatlink api api.exp -o api.dll -mdll
27134 @node Using dlltool
27135 @subsubsection Using @code{dlltool}
27138 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
27139 DLLs and static import libraries. This section summarizes the most
27140 common @code{dlltool} switches. The form of the @code{dlltool} command
27144 $ dlltool [@var{switches}]
27148 @code{dlltool} switches include:
27151 @item --base-file @var{basefile}
27152 @cindex @option{--base-file} (@command{dlltool})
27153 Read the base file @var{basefile} generated by the linker. This switch
27154 is used to create a relocatable DLL.
27156 @item --def @var{deffile}
27157 @cindex @option{--def} (@command{dlltool})
27158 Read the definition file.
27160 @item --dllname @var{name}
27161 @cindex @option{--dllname} (@command{dlltool})
27162 Gives the name of the DLL. This switch is used to embed the name of the
27163 DLL in the static import library generated by @code{dlltool} with switch
27164 @option{--output-lib}.
27167 @cindex @option{-k} (@command{dlltool})
27168 Kill @code{@@}@i{nn} from exported names
27169 (@pxref{Windows Calling Conventions}
27170 for a discussion about @code{Stdcall}-style symbols.
27173 @cindex @option{--help} (@command{dlltool})
27174 Prints the @code{dlltool} switches with a concise description.
27176 @item --output-exp @var{exportfile}
27177 @cindex @option{--output-exp} (@command{dlltool})
27178 Generate an export file @var{exportfile}. The export file contains the
27179 export table (list of symbols in the DLL) and is used to create the DLL.
27181 @item --output-lib @i{libfile}
27182 @cindex @option{--output-lib} (@command{dlltool})
27183 Generate a static import library @var{libfile}.
27186 @cindex @option{-v} (@command{dlltool})
27189 @item --as @i{assembler-name}
27190 @cindex @option{--as} (@command{dlltool})
27191 Use @i{assembler-name} as the assembler. The default is @code{as}.
27194 @node GNAT and Windows Resources
27195 @section GNAT and Windows Resources
27196 @cindex Resources, windows
27199 * Building Resources::
27200 * Compiling Resources::
27201 * Using Resources::
27205 Resources are an easy way to add Windows specific objects to your
27206 application. The objects that can be added as resources include:
27235 This section explains how to build, compile and use resources.
27237 @node Building Resources
27238 @subsection Building Resources
27239 @cindex Resources, building
27242 A resource file is an ASCII file. By convention resource files have an
27243 @file{.rc} extension.
27244 The easiest way to build a resource file is to use Microsoft tools
27245 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
27246 @code{dlgedit.exe} to build dialogs.
27247 It is always possible to build an @file{.rc} file yourself by writing a
27250 It is not our objective to explain how to write a resource file. A
27251 complete description of the resource script language can be found in the
27252 Microsoft documentation.
27254 @node Compiling Resources
27255 @subsection Compiling Resources
27258 @cindex Resources, compiling
27261 This section describes how to build a GNAT-compatible (COFF) object file
27262 containing the resources. This is done using the Resource Compiler
27263 @code{windres} as follows:
27266 $ windres -i myres.rc -o myres.o
27270 By default @code{windres} will run @code{gcc} to preprocess the @file{.rc}
27271 file. You can specify an alternate preprocessor (usually named
27272 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
27273 parameter. A list of all possible options may be obtained by entering
27274 the command @code{windres} @option{--help}.
27276 It is also possible to use the Microsoft resource compiler @code{rc.exe}
27277 to produce a @file{.res} file (binary resource file). See the
27278 corresponding Microsoft documentation for further details. In this case
27279 you need to use @code{windres} to translate the @file{.res} file to a
27280 GNAT-compatible object file as follows:
27283 $ windres -i myres.res -o myres.o
27286 @node Using Resources
27287 @subsection Using Resources
27288 @cindex Resources, using
27291 To include the resource file in your program just add the
27292 GNAT-compatible object file for the resource(s) to the linker
27293 arguments. With @code{gnatmake} this is done by using the @option{-largs}
27297 $ gnatmake myprog -largs myres.o
27300 @node Debugging a DLL
27301 @section Debugging a DLL
27302 @cindex DLL debugging
27305 * Program and DLL Both Built with GCC/GNAT::
27306 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
27310 Debugging a DLL is similar to debugging a standard program. But
27311 we have to deal with two different executable parts: the DLL and the
27312 program that uses it. We have the following four possibilities:
27316 The program and the DLL are built with @code{GCC/GNAT}.
27318 The program is built with foreign tools and the DLL is built with
27321 The program is built with @code{GCC/GNAT} and the DLL is built with
27327 In this section we address only cases one and two above.
27328 There is no point in trying to debug
27329 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
27330 information in it. To do so you must use a debugger compatible with the
27331 tools suite used to build the DLL.
27333 @node Program and DLL Both Built with GCC/GNAT
27334 @subsection Program and DLL Both Built with GCC/GNAT
27337 This is the simplest case. Both the DLL and the program have @code{GDB}
27338 compatible debugging information. It is then possible to break anywhere in
27339 the process. Let's suppose here that the main procedure is named
27340 @code{ada_main} and that in the DLL there is an entry point named
27344 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
27345 program must have been built with the debugging information (see GNAT -g
27346 switch). Here are the step-by-step instructions for debugging it:
27349 @item Launch @code{GDB} on the main program.
27355 @item Break on the main procedure and run the program.
27358 (gdb) break ada_main
27363 This step is required to be able to set a breakpoint inside the DLL. As long
27364 as the program is not run, the DLL is not loaded. This has the
27365 consequence that the DLL debugging information is also not loaded, so it is not
27366 possible to set a breakpoint in the DLL.
27368 @item Set a breakpoint inside the DLL
27371 (gdb) break ada_dll
27378 At this stage a breakpoint is set inside the DLL. From there on
27379 you can use the standard approach to debug the whole program
27380 (@pxref{Running and Debugging Ada Programs}).
27382 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
27383 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
27386 * Debugging the DLL Directly::
27387 * Attaching to a Running Process::
27391 In this case things are slightly more complex because it is not possible to
27392 start the main program and then break at the beginning to load the DLL and the
27393 associated DLL debugging information. It is not possible to break at the
27394 beginning of the program because there is no @code{GDB} debugging information,
27395 and therefore there is no direct way of getting initial control. This
27396 section addresses this issue by describing some methods that can be used
27397 to break somewhere in the DLL to debug it.
27400 First suppose that the main procedure is named @code{main} (this is for
27401 example some C code built with Microsoft Visual C) and that there is a
27402 DLL named @code{test.dll} containing an Ada entry point named
27406 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
27407 been built with debugging information (see GNAT -g option).
27409 @node Debugging the DLL Directly
27410 @subsubsection Debugging the DLL Directly
27414 Launch the debugger on the DLL.
27420 @item Set a breakpoint on a DLL subroutine.
27423 (gdb) break ada_dll
27427 Specify the executable file to @code{GDB}.
27430 (gdb) exec-file main.exe
27441 This will run the program until it reaches the breakpoint that has been
27442 set. From that point you can use the standard way to debug a program
27443 as described in (@pxref{Running and Debugging Ada Programs}).
27448 It is also possible to debug the DLL by attaching to a running process.
27450 @node Attaching to a Running Process
27451 @subsubsection Attaching to a Running Process
27452 @cindex DLL debugging, attach to process
27455 With @code{GDB} it is always possible to debug a running process by
27456 attaching to it. It is possible to debug a DLL this way. The limitation
27457 of this approach is that the DLL must run long enough to perform the
27458 attach operation. It may be useful for instance to insert a time wasting
27459 loop in the code of the DLL to meet this criterion.
27463 @item Launch the main program @file{main.exe}.
27469 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
27470 that the process PID for @file{main.exe} is 208.
27478 @item Attach to the running process to be debugged.
27484 @item Load the process debugging information.
27487 (gdb) symbol-file main.exe
27490 @item Break somewhere in the DLL.
27493 (gdb) break ada_dll
27496 @item Continue process execution.
27505 This last step will resume the process execution, and stop at
27506 the breakpoint we have set. From there you can use the standard
27507 approach to debug a program as described in
27508 (@pxref{Running and Debugging Ada Programs}).
27510 @node GNAT and COM/DCOM Objects
27511 @section GNAT and COM/DCOM Objects
27516 This section is temporarily left blank.
27521 @c **********************************
27522 @c * GNU Free Documentation License *
27523 @c **********************************
27525 @c GNU Free Documentation License
27527 @node Index,,GNU Free Documentation License, Top
27533 @c Put table of contents at end, otherwise it precedes the "title page" in
27534 @c the .txt version
27535 @c Edit the pdf file to move the contents to the beginning, after the title