annotate gcc/ada/doc/gnat_ugn/getting_started_with_gnat.rst @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 .. role:: switch(samp)
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 .. _Getting_Started_with_GNAT:
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 *************************
kono
parents:
diff changeset
6 Getting Started with GNAT
kono
parents:
diff changeset
7 *************************
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 This chapter describes how to use GNAT's command line interface to build
kono
parents:
diff changeset
10 executable Ada programs.
kono
parents:
diff changeset
11 On most platforms a visually oriented Integrated Development Environment
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
12 is also available, the GNAT Programming Studio (GNAT Studio).
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
13 GNAT Studio offers a graphical "look and feel", support for development in
111
kono
parents:
diff changeset
14 other programming languages, comprehensive browsing features, and
kono
parents:
diff changeset
15 many other capabilities.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
16 For information on GNAT Studio please refer to
111
kono
parents:
diff changeset
17 :title:`Using the GNAT Programming Studio`.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 .. _Running_GNAT:
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 Running GNAT
kono
parents:
diff changeset
23 ============
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 Three steps are needed to create an executable file from an Ada source
kono
parents:
diff changeset
26 file:
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 * The source file(s) must be compiled.
kono
parents:
diff changeset
29 * The file(s) must be bound using the GNAT binder.
kono
parents:
diff changeset
30 * All appropriate object files must be linked to produce an executable.
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 All three steps are most commonly handled by using the ``gnatmake``
kono
parents:
diff changeset
33 utility program that, given the name of the main program, automatically
kono
parents:
diff changeset
34 performs the necessary compilation, binding and linking steps.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 .. _Running_a_Simple_Ada_Program:
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 Running a Simple Ada Program
kono
parents:
diff changeset
39 ============================
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 Any text editor may be used to prepare an Ada program.
kono
parents:
diff changeset
42 (If Emacs is used, the optional Ada mode may be helpful in laying out the
kono
parents:
diff changeset
43 program.)
kono
parents:
diff changeset
44 The program text is a normal text file. We will assume in our initial
kono
parents:
diff changeset
45 example that you have used your editor to prepare the following
kono
parents:
diff changeset
46 standard format text file:
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 .. code-block:: ada
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
52 procedure Hello is
kono
parents:
diff changeset
53 begin
kono
parents:
diff changeset
54 Put_Line ("Hello WORLD!");
kono
parents:
diff changeset
55 end Hello;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 This file should be named :file:`hello.adb`.
kono
parents:
diff changeset
58 With the normal default file naming conventions, GNAT requires
kono
parents:
diff changeset
59 that each file
kono
parents:
diff changeset
60 contain a single compilation unit whose file name is the
kono
parents:
diff changeset
61 unit name,
kono
parents:
diff changeset
62 with periods replaced by hyphens; the
kono
parents:
diff changeset
63 extension is :file:`ads` for a
kono
parents:
diff changeset
64 spec and :file:`adb` for a body.
kono
parents:
diff changeset
65 You can override this default file naming convention by use of the
kono
parents:
diff changeset
66 special pragma ``Source_File_Name`` (for further information please
kono
parents:
diff changeset
67 see :ref:`Using_Other_File_Names`).
kono
parents:
diff changeset
68 Alternatively, if you want to rename your files according to this default
kono
parents:
diff changeset
69 convention, which is probably more convenient if you will be using GNAT
kono
parents:
diff changeset
70 for all your compilations, then the ``gnatchop`` utility
kono
parents:
diff changeset
71 can be used to generate correctly-named source files
kono
parents:
diff changeset
72 (see :ref:`Renaming_Files_with_gnatchop`).
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 You can compile the program using the following command (``$`` is used
kono
parents:
diff changeset
75 as the command prompt in the examples in this document):
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 .. code-block:: sh
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 $ gcc -c hello.adb
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 ``gcc`` is the command used to run the compiler. This compiler is
kono
parents:
diff changeset
83 capable of compiling programs in several languages, including Ada and
kono
parents:
diff changeset
84 C. It assumes that you have given it an Ada program if the file extension is
kono
parents:
diff changeset
85 either :file:`.ads` or :file:`.adb`, and it will then call
kono
parents:
diff changeset
86 the GNAT compiler to compile the specified file.
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 The :switch:`-c` switch is required. It tells ``gcc`` to only do a
kono
parents:
diff changeset
89 compilation. (For C programs, ``gcc`` can also do linking, but this
kono
parents:
diff changeset
90 capability is not used directly for Ada programs, so the :switch:`-c`
kono
parents:
diff changeset
91 switch must always be present.)
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 This compile command generates a file
kono
parents:
diff changeset
94 :file:`hello.o`, which is the object
kono
parents:
diff changeset
95 file corresponding to your Ada program. It also generates
kono
parents:
diff changeset
96 an 'Ada Library Information' file :file:`hello.ali`,
kono
parents:
diff changeset
97 which contains additional information used to check
kono
parents:
diff changeset
98 that an Ada program is consistent.
kono
parents:
diff changeset
99 To build an executable file,
kono
parents:
diff changeset
100 use ``gnatbind`` to bind the program
kono
parents:
diff changeset
101 and ``gnatlink`` to link it. The
kono
parents:
diff changeset
102 argument to both ``gnatbind`` and ``gnatlink`` is the name of the
kono
parents:
diff changeset
103 :file:`ALI` file, but the default extension of :file:`.ali` can
kono
parents:
diff changeset
104 be omitted. This means that in the most common case, the argument
kono
parents:
diff changeset
105 is simply the name of the main program:
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 .. code-block:: sh
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 $ gnatbind hello
kono
parents:
diff changeset
110 $ gnatlink hello
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 A simpler method of carrying out these steps is to use ``gnatmake``,
kono
parents:
diff changeset
113 a master program that invokes all the required
kono
parents:
diff changeset
114 compilation, binding and linking tools in the correct order. In particular,
kono
parents:
diff changeset
115 ``gnatmake`` automatically recompiles any sources that have been
kono
parents:
diff changeset
116 modified since they were last compiled, or sources that depend
kono
parents:
diff changeset
117 on such modified sources, so that 'version skew' is avoided.
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 .. index:: Version skew (avoided by ``gnatmake``)
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 .. code-block:: sh
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 $ gnatmake hello.adb
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 The result is an executable program called :file:`hello`, which can be
kono
parents:
diff changeset
126 run by entering:
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 .. code-block:: sh
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 $ hello
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 assuming that the current directory is on the search path
kono
parents:
diff changeset
133 for executable programs.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 and, if all has gone well, you will see::
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 Hello WORLD!
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 appear in response to this command.
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 .. _Running_a_Program_with_Multiple_Units:
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 Running a Program with Multiple Units
kono
parents:
diff changeset
144 =====================================
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 Consider a slightly more complicated example that has three files: a
kono
parents:
diff changeset
147 main program, and the spec and body of a package:
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 .. code-block:: ada
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 package Greetings is
kono
parents:
diff changeset
153 procedure Hello;
kono
parents:
diff changeset
154 procedure Goodbye;
kono
parents:
diff changeset
155 end Greetings;
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 with Ada.Text_IO; use Ada.Text_IO;
kono
parents:
diff changeset
158 package body Greetings is
kono
parents:
diff changeset
159 procedure Hello is
kono
parents:
diff changeset
160 begin
kono
parents:
diff changeset
161 Put_Line ("Hello WORLD!");
kono
parents:
diff changeset
162 end Hello;
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 procedure Goodbye is
kono
parents:
diff changeset
165 begin
kono
parents:
diff changeset
166 Put_Line ("Goodbye WORLD!");
kono
parents:
diff changeset
167 end Goodbye;
kono
parents:
diff changeset
168 end Greetings;
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 with Greetings;
kono
parents:
diff changeset
171 procedure Gmain is
kono
parents:
diff changeset
172 begin
kono
parents:
diff changeset
173 Greetings.Hello;
kono
parents:
diff changeset
174 Greetings.Goodbye;
kono
parents:
diff changeset
175 end Gmain;
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 Following the one-unit-per-file rule, place this program in the
kono
parents:
diff changeset
178 following three separate files:
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 *greetings.ads*
kono
parents:
diff changeset
183 spec of package ``Greetings``
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 *greetings.adb*
kono
parents:
diff changeset
187 body of package ``Greetings``
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 *gmain.adb*
kono
parents:
diff changeset
191 body of main program
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 To build an executable version of
kono
parents:
diff changeset
194 this program, we could use four separate steps to compile, bind, and link
kono
parents:
diff changeset
195 the program, as follows:
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 .. code-block:: sh
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 $ gcc -c gmain.adb
kono
parents:
diff changeset
200 $ gcc -c greetings.adb
kono
parents:
diff changeset
201 $ gnatbind gmain
kono
parents:
diff changeset
202 $ gnatlink gmain
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 Note that there is no required order of compilation when using GNAT.
kono
parents:
diff changeset
205 In particular it is perfectly fine to compile the main program first.
kono
parents:
diff changeset
206 Also, it is not necessary to compile package specs in the case where
kono
parents:
diff changeset
207 there is an accompanying body; you only need to compile the body. If you want
kono
parents:
diff changeset
208 to submit these files to the compiler for semantic checking and not code
kono
parents:
diff changeset
209 generation, then use the :switch:`-gnatc` switch:
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 .. code-block:: sh
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 $ gcc -c greetings.ads -gnatc
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 Although the compilation can be done in separate steps as in the
kono
parents:
diff changeset
216 above example, in practice it is almost always more convenient
kono
parents:
diff changeset
217 to use the ``gnatmake`` tool. All you need to know in this case
kono
parents:
diff changeset
218 is the name of the main program's source file. The effect of the above four
kono
parents:
diff changeset
219 commands can be achieved with a single one:
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 .. code-block:: sh
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 $ gnatmake gmain.adb
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 In the next section we discuss the advantages of using ``gnatmake`` in
kono
parents:
diff changeset
226 more detail.
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 .. _Using_the_gnatmake_Utility:
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 Using the ``gnatmake`` Utility
kono
parents:
diff changeset
231 ==============================
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 If you work on a program by compiling single components at a time using
kono
parents:
diff changeset
234 ``gcc``, you typically keep track of the units you modify. In order to
kono
parents:
diff changeset
235 build a consistent system, you compile not only these units, but also any
kono
parents:
diff changeset
236 units that depend on the units you have modified.
kono
parents:
diff changeset
237 For example, in the preceding case,
kono
parents:
diff changeset
238 if you edit :file:`gmain.adb`, you only need to recompile that file. But if
kono
parents:
diff changeset
239 you edit :file:`greetings.ads`, you must recompile both
kono
parents:
diff changeset
240 :file:`greetings.adb` and :file:`gmain.adb`, because both files contain
kono
parents:
diff changeset
241 units that depend on :file:`greetings.ads`.
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 ``gnatbind`` will warn you if you forget one of these compilation
kono
parents:
diff changeset
244 steps, so that it is impossible to generate an inconsistent program as a
kono
parents:
diff changeset
245 result of forgetting to do a compilation. Nevertheless it is tedious and
kono
parents:
diff changeset
246 error-prone to keep track of dependencies among units.
kono
parents:
diff changeset
247 One approach to handle the dependency-bookkeeping is to use a
kono
parents:
diff changeset
248 makefile. However, makefiles present maintenance problems of their own:
kono
parents:
diff changeset
249 if the dependencies change as you change the program, you must make
kono
parents:
diff changeset
250 sure that the makefile is kept up-to-date manually, which is also an
kono
parents:
diff changeset
251 error-prone process.
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 The ``gnatmake`` utility takes care of these details automatically.
kono
parents:
diff changeset
254 Invoke it using either one of the following forms:
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 .. code-block:: sh
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 $ gnatmake gmain.adb
kono
parents:
diff changeset
259 $ gnatmake gmain
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 The argument is the name of the file containing the main program;
kono
parents:
diff changeset
262 you may omit the extension. ``gnatmake``
kono
parents:
diff changeset
263 examines the environment, automatically recompiles any files that need
kono
parents:
diff changeset
264 recompiling, and binds and links the resulting set of object files,
kono
parents:
diff changeset
265 generating the executable file, :file:`gmain`.
kono
parents:
diff changeset
266 In a large program, it
kono
parents:
diff changeset
267 can be extremely helpful to use ``gnatmake``, because working out by hand
kono
parents:
diff changeset
268 what needs to be recompiled can be difficult.
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 Note that ``gnatmake`` takes into account all the Ada rules that
kono
parents:
diff changeset
271 establish dependencies among units. These include dependencies that result
kono
parents:
diff changeset
272 from inlining subprogram bodies, and from
kono
parents:
diff changeset
273 generic instantiation. Unlike some other
kono
parents:
diff changeset
274 Ada make tools, ``gnatmake`` does not rely on the dependencies that were
kono
parents:
diff changeset
275 found by the compiler on a previous compilation, which may possibly
kono
parents:
diff changeset
276 be wrong when sources change. ``gnatmake`` determines the exact set of
kono
parents:
diff changeset
277 dependencies from scratch each time it is run.