annotate gcc/ada/doc/gnat_ugn/platform_specific_information.rst @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
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 .. -- Non-breaking space in running text
kono
parents:
diff changeset
4 -- E.g. Ada |nbsp| 95
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 .. |nbsp| unicode:: 0xA0
kono
parents:
diff changeset
7 :trim:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 .. _Platform_Specific_Information:
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 *****************************
kono
parents:
diff changeset
12 Platform-Specific Information
kono
parents:
diff changeset
13 *****************************
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 This appendix contains information relating to the implementation
kono
parents:
diff changeset
16 of run-time libraries on various platforms and also covers
kono
parents:
diff changeset
17 topics related to the GNAT implementation on Windows and Mac OS.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 .. _`Run_Time_Libraries`:
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 Run-Time Libraries
kono
parents:
diff changeset
22 ==================
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 .. index:: Tasking and threads libraries
kono
parents:
diff changeset
25 .. index:: Threads libraries and tasking
kono
parents:
diff changeset
26 .. index:: Run-time libraries (platform-specific information)
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 The GNAT run-time implementation may vary with respect to both the
kono
parents:
diff changeset
29 underlying threads library and the exception-handling scheme.
kono
parents:
diff changeset
30 For threads support, the default run-time will bind to the thread
kono
parents:
diff changeset
31 package of the underlying operating system.
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 For exception handling, either or both of two models are supplied:
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 .. index:: Zero-Cost Exceptions
kono
parents:
diff changeset
36 .. index:: ZCX (Zero-Cost Exceptions)
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 * **Zero-Cost Exceptions** ("ZCX"),
kono
parents:
diff changeset
39 which uses binder-generated tables that
kono
parents:
diff changeset
40 are interrogated at run time to locate a handler.
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 .. index:: setjmp/longjmp Exception Model
kono
parents:
diff changeset
43 .. index:: SJLJ (setjmp/longjmp Exception Model)
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 * **setjmp / longjmp** ('SJLJ'),
kono
parents:
diff changeset
46 which uses dynamically-set data to establish
kono
parents:
diff changeset
47 the set of handlers
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 Most programs should experience a substantial speed improvement by
kono
parents:
diff changeset
50 being compiled with a ZCX run-time.
kono
parents:
diff changeset
51 This is especially true for
kono
parents:
diff changeset
52 tasking applications or applications with many exception handlers.}
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 This section summarizes which combinations of threads and exception support
kono
parents:
diff changeset
55 are supplied on various GNAT platforms.
kono
parents:
diff changeset
56 It then shows how to select a particular library either
kono
parents:
diff changeset
57 permanently or temporarily,
kono
parents:
diff changeset
58 explains the properties of (and tradeoffs among) the various threads
kono
parents:
diff changeset
59 libraries, and provides some additional
kono
parents:
diff changeset
60 information about several specific platforms.
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 .. _Summary_of_Run-Time_Configurations:
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 Summary of Run-Time Configurations
kono
parents:
diff changeset
65 ----------------------------------
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 +-----------------+--------------+-------------------------+------------+
kono
parents:
diff changeset
68 | Platform | Run-Time | Tasking | Exceptions |
kono
parents:
diff changeset
69 +=================+==============+=========================+============+
kono
parents:
diff changeset
70 | GNU/Linux | rts-native | pthread library | ZCX |
kono
parents:
diff changeset
71 | | (default) | | |
kono
parents:
diff changeset
72 | +--------------+-------------------------+------------+
kono
parents:
diff changeset
73 | | rts-sjlj | pthread library | SJLJ |
kono
parents:
diff changeset
74 +-----------------+--------------+-------------------------+------------+
kono
parents:
diff changeset
75 | Windows | rts-native | native Win32 threads | ZCX |
kono
parents:
diff changeset
76 | | (default) | | |
kono
parents:
diff changeset
77 | +--------------+-------------------------+------------+
kono
parents:
diff changeset
78 | | rts-sjlj | native Win32 threads | SJLJ |
kono
parents:
diff changeset
79 +-----------------+--------------+-------------------------+------------+
kono
parents:
diff changeset
80 | Mac OS | rts-native | pthread library | ZCX |
kono
parents:
diff changeset
81 +-----------------+--------------+-------------------------+------------+
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 .. _Specifying_a_Run-Time_Library:
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 Specifying a Run-Time Library
kono
parents:
diff changeset
87 =============================
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 The :file:`adainclude` subdirectory containing the sources of the GNAT
kono
parents:
diff changeset
90 run-time library, and the :file:`adalib` subdirectory containing the
kono
parents:
diff changeset
91 :file:`ALI` files and the static and/or shared GNAT library, are located
kono
parents:
diff changeset
92 in the gcc target-dependent area:
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 ::
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 As indicated above, on some platforms several run-time libraries are supplied.
kono
parents:
diff changeset
100 These libraries are installed in the target dependent area and
kono
parents:
diff changeset
101 contain a complete source and binary subdirectory. The detailed description
kono
parents:
diff changeset
102 below explains the differences between the different libraries in terms of
kono
parents:
diff changeset
103 their thread support.
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 The default run-time library (when GNAT is installed) is *rts-native*.
kono
parents:
diff changeset
106 This default run-time is selected by the means of soft links.
kono
parents:
diff changeset
107 For example on x86-linux:
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 .. --
kono
parents:
diff changeset
110 -- $(target-dir)
kono
parents:
diff changeset
111 -- |
kono
parents:
diff changeset
112 -- +--- adainclude----------+
kono
parents:
diff changeset
113 -- | |
kono
parents:
diff changeset
114 -- +--- adalib-----------+ |
kono
parents:
diff changeset
115 -- | | |
kono
parents:
diff changeset
116 -- +--- rts-native | |
kono
parents:
diff changeset
117 -- | | | |
kono
parents:
diff changeset
118 -- | +--- adainclude <---+
kono
parents:
diff changeset
119 -- | | |
kono
parents:
diff changeset
120 -- | +--- adalib <----+
kono
parents:
diff changeset
121 -- |
kono
parents:
diff changeset
122 -- +--- rts-sjlj
kono
parents:
diff changeset
123 -- |
kono
parents:
diff changeset
124 -- +--- adainclude
kono
parents:
diff changeset
125 -- |
kono
parents:
diff changeset
126 -- +--- adalib
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 .. only:: html or latex
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 .. image:: rtlibrary-structure.png
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 .. only:: not (html or latex)
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 ::
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 $(target-dir)
kono
parents:
diff changeset
137 __/ / \ \___
kono
parents:
diff changeset
138 _______/ / \ \_________________
kono
parents:
diff changeset
139 / / \ \
kono
parents:
diff changeset
140 / / \ \
kono
parents:
diff changeset
141 ADAINCLUDE ADALIB rts-native rts-sjlj
kono
parents:
diff changeset
142 : : / \ / \
kono
parents:
diff changeset
143 : : / \ / \
kono
parents:
diff changeset
144 : : / \ / \
kono
parents:
diff changeset
145 : : / \ / \
kono
parents:
diff changeset
146 +-------------> adainclude adalib adainclude adalib
kono
parents:
diff changeset
147 : ^
kono
parents:
diff changeset
148 : :
kono
parents:
diff changeset
149 +---------------------+
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 Run-Time Library Directory Structure
kono
parents:
diff changeset
152 (Upper-case names and dotted/dashed arrows represent soft links)
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 If the *rts-sjlj* library is to be selected on a permanent basis,
kono
parents:
diff changeset
155 these soft links can be modified with the following commands:
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 ::
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 $ cd $target
kono
parents:
diff changeset
160 $ rm -f adainclude adalib
kono
parents:
diff changeset
161 $ ln -s rts-sjlj/adainclude adainclude
kono
parents:
diff changeset
162 $ ln -s rts-sjlj/adalib adalib
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 Alternatively, you can specify :file:`rts-sjlj/adainclude` in the file
kono
parents:
diff changeset
165 :file:`$target/ada_source_path` and :file:`rts-sjlj/adalib` in
kono
parents:
diff changeset
166 :file:`$target/ada_object_path`.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 .. index:: --RTS option
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 Selecting another run-time library temporarily can be
kono
parents:
diff changeset
171 achieved by using the :switch:`--RTS` switch, e.g., :switch:`--RTS=sjlj`
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 .. _Choosing_the_Scheduling_Policy:
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 .. index:: SCHED_FIFO scheduling policy
kono
parents:
diff changeset
177 .. index:: SCHED_RR scheduling policy
kono
parents:
diff changeset
178 .. index:: SCHED_OTHER scheduling policy
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 Choosing the Scheduling Policy
kono
parents:
diff changeset
181 ------------------------------
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 When using a POSIX threads implementation, you have a choice of several
kono
parents:
diff changeset
184 scheduling policies: ``SCHED_FIFO``, ``SCHED_RR`` and ``SCHED_OTHER``.
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 Typically, the default is ``SCHED_OTHER``, while using ``SCHED_FIFO``
kono
parents:
diff changeset
187 or ``SCHED_RR`` requires special (e.g., root) privileges.
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 .. index:: pragma Time_Slice
kono
parents:
diff changeset
190 .. index:: -T0 option
kono
parents:
diff changeset
191 .. index:: pragma Task_Dispatching_Policy
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 By default, GNAT uses the ``SCHED_OTHER`` policy. To specify
kono
parents:
diff changeset
195 ``SCHED_FIFO``,
kono
parents:
diff changeset
196 you can use one of the following:
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 * ``pragma Time_Slice (0.0)``
kono
parents:
diff changeset
199 * the corresponding binder option :switch:`-T0`
kono
parents:
diff changeset
200 * ``pragma Task_Dispatching_Policy (FIFO_Within_Priorities)``
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 To specify ``SCHED_RR``,
kono
parents:
diff changeset
204 you should use ``pragma Time_Slice`` with a
kono
parents:
diff changeset
205 value greater than 0.0, or else use the corresponding :switch:`-T`
kono
parents:
diff changeset
206 binder option.
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 To make sure a program is running as root, you can put something like
kono
parents:
diff changeset
210 this in a library package body in your application:
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 .. code-block:: ada
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 function geteuid return Integer;
kono
parents:
diff changeset
215 pragma Import (C, geteuid, "geteuid");
kono
parents:
diff changeset
216 Ignore : constant Boolean :=
kono
parents:
diff changeset
217 (if geteuid = 0 then True else raise Program_Error with "must be root");
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 It gets the effective user id, and if it's not 0 (i.e. root), it raises
kono
parents:
diff changeset
220 Program_Error.
kono
parents:
diff changeset
221
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
222 .. index:: Linux
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
223 .. index:: GNU/Linux
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
224
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
225 .. _GNU_Linux_Topics:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
226
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
227 GNU/Linux Topics
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
228 ================
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
229
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
230 This section describes topics that are specific to GNU/Linux platforms.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
231
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
232 .. _Required_packages_on_GNU_Linux:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
233
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
234 Required Packages on GNU/Linux
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
235 ------------------------------
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
236
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
237 GNAT requires the C library developer's package to be installed.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
238 The name of of that package depends on your GNU/Linux distribution:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
239
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
240 * RedHat, SUSE: ``glibc-devel``;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
241 * Debian, Ubuntu: ``libc6-dev`` (normally installed by default).
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
242
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
243 If using the 32-bit version of GNAT on a 64-bit version of GNU/Linux,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
244 you'll need the 32-bit version of the following packages:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
245
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
246 * RedHat, SUSE: ``glibc.i686``, ``glibc-devel.i686``, ``ncurses-libs.i686``
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
247 * Debian, Ubuntu: ``libc6:i386``, ``libc6-dev:i386``, ``lib32ncursesw5``
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
248
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
249 Other GNU/Linux distributions might be choosing a different name
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
250 for those packages.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
251
111
kono
parents:
diff changeset
252 .. index:: Windows
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 .. _Microsoft_Windows_Topics:
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 Microsoft Windows Topics
kono
parents:
diff changeset
257 ========================
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 This section describes topics that are specific to the Microsoft Windows
kono
parents:
diff changeset
260 platforms.
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 .. only:: PRO or GPL
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 .. rubric:: Installing from the Command Line
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 By default the GNAT installers display a GUI that prompts you to enter
kono
parents:
diff changeset
268 the installation path and similar information, and then guides you through the
kono
parents:
diff changeset
269 installation process. It is also possible to perform silent installations
kono
parents:
diff changeset
270 using the command-line interface.
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 In order to install one of the GNAT installers from the command
kono
parents:
diff changeset
273 line you should pass parameter :switch:`/S` (and, optionally,
kono
parents:
diff changeset
274 :switch:`/D=<directory>`) as command-line arguments.
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 .. only:: PRO
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 For example, for an unattended installation of
kono
parents:
diff changeset
279 GNAT 7.0.2 into the default directory
kono
parents:
diff changeset
280 ``C:\\GNATPRO\\7.0.2`` you would run:
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 ::
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 gnatpro-7.0.2-i686-pc-mingw32-bin.exe /S
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 To install into a custom directory, say, ``C:\\TOOLS\\GNATPRO\\7.0.2``:
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 ::
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 gnatpro-7.0.2-i686-pc-mingw32-bin /S /D=C:\TOOLS\GNATPRO\7.0.2
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 .. only:: GPL
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 For example, for an unattended installation of
kono
parents:
diff changeset
295 GNAT 2012 into ``C:\\GNAT\\2012``:
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 ::
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 gnat-gpl-2012-i686-pc-mingw32-bin /S /D=C:\GNAT\2012
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 .. only:: PRO or GPL
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 You can use the same syntax for all installers.
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 Note that unattended installations don't modify system path, nor create file
kono
parents:
diff changeset
306 associations, so such activities need to be done by hand.
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 .. _Using_GNAT_on_Windows:
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 Using GNAT on Windows
kono
parents:
diff changeset
313 ---------------------
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 One of the strengths of the GNAT technology is that its tool set
kono
parents:
diff changeset
316 (``gcc``, ``gnatbind``, ``gnatlink``, ``gnatmake``, the
kono
parents:
diff changeset
317 ``gdb`` debugger, etc.) is used in the same way regardless of the
kono
parents:
diff changeset
318 platform.
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 On Windows this tool set is complemented by a number of Microsoft-specific
kono
parents:
diff changeset
321 tools that have been provided to facilitate interoperability with Windows
kono
parents:
diff changeset
322 when this is required. With these tools:
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 * You can build applications using the ``CONSOLE`` or ``WINDOWS``
kono
parents:
diff changeset
326 subsystems.
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 * You can use any Dynamically Linked Library (DLL) in your Ada code (both
kono
parents:
diff changeset
329 relocatable and non-relocatable DLLs are supported).
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 * You can build Ada DLLs for use in other applications. These applications
kono
parents:
diff changeset
332 can be written in a language other than Ada (e.g., C, C++, etc). Again both
kono
parents:
diff changeset
333 relocatable and non-relocatable Ada DLLs are supported.
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 * You can include Windows resources in your Ada application.
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 * You can use or create COM/DCOM objects.
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 Immediately below are listed all known general GNAT-for-Windows restrictions.
kono
parents:
diff changeset
340 Other restrictions about specific features like Windows Resources and DLLs
kono
parents:
diff changeset
341 are listed in separate sections below.
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 * It is not possible to use ``GetLastError`` and ``SetLastError``
kono
parents:
diff changeset
345 when tasking, protected records, or exceptions are used. In these
kono
parents:
diff changeset
346 cases, in order to implement Ada semantics, the GNAT run-time system
kono
parents:
diff changeset
347 calls certain Win32 routines that set the last error variable to 0 upon
kono
parents:
diff changeset
348 success. It should be possible to use ``GetLastError`` and
kono
parents:
diff changeset
349 ``SetLastError`` when tasking, protected record, and exception
kono
parents:
diff changeset
350 features are not used, but it is not guaranteed to work.
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 * It is not possible to link against Microsoft C++ libraries except for
kono
parents:
diff changeset
353 import libraries. Interfacing must be done by the mean of DLLs.
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 * It is possible to link against Microsoft C libraries. Yet the preferred
kono
parents:
diff changeset
356 solution is to use C/C++ compiler that comes with GNAT, since it
kono
parents:
diff changeset
357 doesn't require having two different development environments and makes the
kono
parents:
diff changeset
358 inter-language debugging experience smoother.
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 * When the compilation environment is located on FAT32 drives, users may
kono
parents:
diff changeset
361 experience recompilations of the source files that have not changed if
kono
parents:
diff changeset
362 Daylight Saving Time (DST) state has changed since the last time files
kono
parents:
diff changeset
363 were compiled. NTFS drives do not have this problem.
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 * No components of the GNAT toolset use any entries in the Windows
kono
parents:
diff changeset
366 registry. The only entries that can be created are file associations and
kono
parents:
diff changeset
367 PATH settings, provided the user has chosen to create them at installation
kono
parents:
diff changeset
368 time, as well as some minimal book-keeping information needed to correctly
kono
parents:
diff changeset
369 uninstall or integrate different GNAT products.
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371
kono
parents:
diff changeset
372 .. _Using_a_network_installation_of_GNAT:
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 Using a network installation of GNAT
kono
parents:
diff changeset
375 ------------------------------------
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 Make sure the system on which GNAT is installed is accessible from the
kono
parents:
diff changeset
378 current machine, i.e., the install location is shared over the network.
kono
parents:
diff changeset
379 Shared resources are accessed on Windows by means of UNC paths, which
kono
parents:
diff changeset
380 have the format ``\\\\server\\sharename\\path``
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 In order to use such a network installation, simply add the UNC path of the
kono
parents:
diff changeset
383 :file:`bin` directory of your GNAT installation in front of your PATH. For
kono
parents:
diff changeset
384 example, if GNAT is installed in :file:`\\GNAT` directory of a share location
kono
parents:
diff changeset
385 called :file:`c-drive` on a machine :file:`LOKI`, the following command will
kono
parents:
diff changeset
386 make it available:
kono
parents:
diff changeset
387
kono
parents:
diff changeset
388 ::
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 $ path \\loki\c-drive\gnat\bin;%path%`
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 Be aware that every compilation using the network installation results in the
kono
parents:
diff changeset
393 transfer of large amounts of data across the network and will likely cause
kono
parents:
diff changeset
394 serious performance penalty.
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 .. _CONSOLE_and_WINDOWS_subsystems:
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 CONSOLE and WINDOWS subsystems
kono
parents:
diff changeset
399 ------------------------------
kono
parents:
diff changeset
400
kono
parents:
diff changeset
401 .. index:: CONSOLE Subsystem
kono
parents:
diff changeset
402 .. index:: WINDOWS Subsystem
kono
parents:
diff changeset
403 .. index:: -mwindows
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 There are two main subsystems under Windows. The ``CONSOLE`` subsystem
kono
parents:
diff changeset
406 (which is the default subsystem) will always create a console when
kono
parents:
diff changeset
407 launching the application. This is not something desirable when the
kono
parents:
diff changeset
408 application has a Windows GUI. To get rid of this console the
kono
parents:
diff changeset
409 application must be using the ``WINDOWS`` subsystem. To do so
kono
parents:
diff changeset
410 the :switch:`-mwindows` linker option must be specified.
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 ::
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 $ gnatmake winprog -largs -mwindows
kono
parents:
diff changeset
415
kono
parents:
diff changeset
416 .. _Temporary_Files:
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 Temporary Files
kono
parents:
diff changeset
419 ---------------
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 .. index:: Temporary files
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 It is possible to control where temporary files gets created by setting
kono
parents:
diff changeset
424 the :envvar:`TMP` environment variable. The file will be created:
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 * Under the directory pointed to by the :envvar:`TMP` environment variable if
kono
parents:
diff changeset
427 this directory exists.
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 * Under :file:`c:\\temp`, if the :envvar:`TMP` environment variable is not
kono
parents:
diff changeset
430 set (or not pointing to a directory) and if this directory exists.
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 * Under the current working directory otherwise.
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 This allows you to determine exactly where the temporary
kono
parents:
diff changeset
435 file will be created. This is particularly useful in networked
kono
parents:
diff changeset
436 environments where you may not have write access to some
kono
parents:
diff changeset
437 directories.
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 Disabling Command Line Argument Expansion
kono
parents:
diff changeset
440 -----------------------------------------
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 .. index:: Command Line Argument Expansion
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 By default, an executable compiled for the Windows platform will do
kono
parents:
diff changeset
445 the following postprocessing on the arguments passed on the command
kono
parents:
diff changeset
446 line:
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 * If the argument contains the characters ``*`` and/or ``?``, then
kono
parents:
diff changeset
449 file expansion will be attempted. For example, if the current directory
kono
parents:
diff changeset
450 contains :file:`a.txt` and :file:`b.txt`, then when calling::
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 $ my_ada_program *.txt
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 The following arguments will effectively be passed to the main program
kono
parents:
diff changeset
455 (for example when using ``Ada.Command_Line.Argument``)::
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 Ada.Command_Line.Argument (1) -> "a.txt"
kono
parents:
diff changeset
458 Ada.Command_Line.Argument (2) -> "b.txt"
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 * Filename expansion can be disabled for a given argument by using single
kono
parents:
diff changeset
461 quotes. Thus, calling::
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 $ my_ada_program '*.txt'
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 will result in::
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 Ada.Command_Line.Argument (1) -> "*.txt"
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 Note that if the program is launched from a shell such as Cygwin Bash
kono
parents:
diff changeset
470 then quote removal might be performed by the shell.
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 In some contexts it might be useful to disable this feature (for example if
kono
parents:
diff changeset
473 the program performs its own argument expansion). In order to do this, a C
kono
parents:
diff changeset
474 symbol needs to be defined and set to ``0``. You can do this by
kono
parents:
diff changeset
475 adding the following code fragment in one of your Ada units:
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 .. code-block:: ada
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 Do_Argv_Expansion : Integer := 0;
kono
parents:
diff changeset
480 pragma Export (C, Do_Argv_Expansion, "__gnat_do_argv_expansion");
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 The results of previous examples will be respectively::
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 Ada.Command_Line.Argument (1) -> "*.txt"
kono
parents:
diff changeset
485
kono
parents:
diff changeset
486 and::
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 Ada.Command_Line.Argument (1) -> "'*.txt'"
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 .. _Mixed-Language_Programming_on_Windows:
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 Mixed-Language Programming on Windows
kono
parents:
diff changeset
494 -------------------------------------
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 Developing pure Ada applications on Windows is no different than on
kono
parents:
diff changeset
497 other GNAT-supported platforms. However, when developing or porting an
kono
parents:
diff changeset
498 application that contains a mix of Ada and C/C++, the choice of your
kono
parents:
diff changeset
499 Windows C/C++ development environment conditions your overall
kono
parents:
diff changeset
500 interoperability strategy.
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 If you use ``gcc`` or Microsoft C to compile the non-Ada part of
kono
parents:
diff changeset
503 your application, there are no Windows-specific restrictions that
kono
parents:
diff changeset
504 affect the overall interoperability with your Ada code. If you do want
kono
parents:
diff changeset
505 to use the Microsoft tools for your C++ code, you have two choices:
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 * Encapsulate your C++ code in a DLL to be linked with your Ada
kono
parents:
diff changeset
508 application. In this case, use the Microsoft or whatever environment to
kono
parents:
diff changeset
509 build the DLL and use GNAT to build your executable
kono
parents:
diff changeset
510 (:ref:`Using_DLLs_with_GNAT`).
kono
parents:
diff changeset
511
kono
parents:
diff changeset
512 * Or you can encapsulate your Ada code in a DLL to be linked with the
kono
parents:
diff changeset
513 other part of your application. In this case, use GNAT to build the DLL
kono
parents:
diff changeset
514 (:ref:`Building_DLLs_with_GNAT_Project_files`) and use the Microsoft
kono
parents:
diff changeset
515 or whatever environment to build your executable.
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 In addition to the description about C main in
kono
parents:
diff changeset
518 :ref:`Mixed_Language_Programming` section, if the C main uses a
kono
parents:
diff changeset
519 stand-alone library it is required on x86-windows to
kono
parents:
diff changeset
520 setup the SEH context. For this the C main must looks like this:
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 .. code-block:: c
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 /* main.c */
kono
parents:
diff changeset
526 extern void adainit (void);
kono
parents:
diff changeset
527 extern void adafinal (void);
kono
parents:
diff changeset
528 extern void __gnat_initialize(void*);
kono
parents:
diff changeset
529 extern void call_to_ada (void);
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 int main (int argc, char *argv[])
kono
parents:
diff changeset
532 {
kono
parents:
diff changeset
533 int SEH [2];
kono
parents:
diff changeset
534
kono
parents:
diff changeset
535 /* Initialize the SEH context */
kono
parents:
diff changeset
536 __gnat_initialize (&SEH);
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 adainit();
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 /* Then call Ada services in the stand-alone library */
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 call_to_ada();
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 adafinal();
kono
parents:
diff changeset
545 }
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 Note that this is not needed on x86_64-windows where the Windows
kono
parents:
diff changeset
548 native SEH support is used.
kono
parents:
diff changeset
549
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 .. _Windows_Calling_Conventions:
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 Windows Calling Conventions
kono
parents:
diff changeset
554 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
555
kono
parents:
diff changeset
556 .. index:: Stdcall
kono
parents:
diff changeset
557 .. index:: APIENTRY
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 This section pertain only to Win32. On Win64 there is a single native
kono
parents:
diff changeset
560 calling convention. All convention specifiers are ignored on this
kono
parents:
diff changeset
561 platform.
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 When a subprogram ``F`` (caller) calls a subprogram ``G``
kono
parents:
diff changeset
564 (callee), there are several ways to push ``G``\ 's parameters on the
kono
parents:
diff changeset
565 stack and there are several possible scenarios to clean up the stack
kono
parents:
diff changeset
566 upon ``G``\ 's return. A calling convention is an agreed upon software
kono
parents:
diff changeset
567 protocol whereby the responsibilities between the caller (``F``) and
kono
parents:
diff changeset
568 the callee (``G``) are clearly defined. Several calling conventions
kono
parents:
diff changeset
569 are available for Windows:
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 * ``C`` (Microsoft defined)
kono
parents:
diff changeset
572
kono
parents:
diff changeset
573 * ``Stdcall`` (Microsoft defined)
kono
parents:
diff changeset
574
kono
parents:
diff changeset
575 * ``Win32`` (GNAT specific)
kono
parents:
diff changeset
576
kono
parents:
diff changeset
577 * ``DLL`` (GNAT specific)
kono
parents:
diff changeset
578
kono
parents:
diff changeset
579
kono
parents:
diff changeset
580 .. _C_Calling_Convention:
kono
parents:
diff changeset
581
kono
parents:
diff changeset
582 ``C`` Calling Convention
kono
parents:
diff changeset
583 """"""""""""""""""""""""
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 This is the default calling convention used when interfacing to C/C++
kono
parents:
diff changeset
586 routines compiled with either ``gcc`` or Microsoft Visual C++.
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 In the ``C`` calling convention subprogram parameters are pushed on the
kono
parents:
diff changeset
589 stack by the caller from right to left. The caller itself is in charge of
kono
parents:
diff changeset
590 cleaning up the stack after the call. In addition, the name of a routine
kono
parents:
diff changeset
591 with ``C`` calling convention is mangled by adding a leading underscore.
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 The name to use on the Ada side when importing (or exporting) a routine
kono
parents:
diff changeset
594 with ``C`` calling convention is the name of the routine. For
kono
parents:
diff changeset
595 instance the C function:
kono
parents:
diff changeset
596
kono
parents:
diff changeset
597 ::
kono
parents:
diff changeset
598
kono
parents:
diff changeset
599 int get_val (long);
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 should be imported from Ada as follows:
kono
parents:
diff changeset
602
kono
parents:
diff changeset
603 .. code-block:: ada
kono
parents:
diff changeset
604
kono
parents:
diff changeset
605 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
kono
parents:
diff changeset
606 pragma Import (C, Get_Val, External_Name => "get_val");
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 Note that in this particular case the ``External_Name`` parameter could
kono
parents:
diff changeset
609 have been omitted since, when missing, this parameter is taken to be the
kono
parents:
diff changeset
610 name of the Ada entity in lower case. When the ``Link_Name`` parameter
kono
parents:
diff changeset
611 is missing, as in the above example, this parameter is set to be the
kono
parents:
diff changeset
612 ``External_Name`` with a leading underscore.
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 When importing a variable defined in C, you should always use the ``C``
kono
parents:
diff changeset
615 calling convention unless the object containing the variable is part of a
kono
parents:
diff changeset
616 DLL (in which case you should use the ``Stdcall`` calling
kono
parents:
diff changeset
617 convention, :ref:`Stdcall_Calling_Convention`).
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619
kono
parents:
diff changeset
620 .. _Stdcall_Calling_Convention:
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 ``Stdcall`` Calling Convention
kono
parents:
diff changeset
623 """"""""""""""""""""""""""""""
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 This convention, which was the calling convention used for Pascal
kono
parents:
diff changeset
626 programs, is used by Microsoft for all the routines in the Win32 API for
kono
parents:
diff changeset
627 efficiency reasons. It must be used to import any routine for which this
kono
parents:
diff changeset
628 convention was specified.
kono
parents:
diff changeset
629
kono
parents:
diff changeset
630 In the ``Stdcall`` calling convention subprogram parameters are pushed
kono
parents:
diff changeset
631 on the stack by the caller from right to left. The callee (and not the
kono
parents:
diff changeset
632 caller) is in charge of cleaning the stack on routine exit. In addition,
kono
parents:
diff changeset
633 the name of a routine with ``Stdcall`` calling convention is mangled by
kono
parents:
diff changeset
634 adding a leading underscore (as for the ``C`` calling convention) and a
kono
parents:
diff changeset
635 trailing :samp:`@{nn}`, where ``nn`` is the overall size (in
kono
parents:
diff changeset
636 bytes) of the parameters passed to the routine.
kono
parents:
diff changeset
637
kono
parents:
diff changeset
638 The name to use on the Ada side when importing a C routine with a
kono
parents:
diff changeset
639 ``Stdcall`` calling convention is the name of the C routine. The leading
kono
parents:
diff changeset
640 underscore and trailing :samp:`@{nn}` are added automatically by
kono
parents:
diff changeset
641 the compiler. For instance the Win32 function:
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 ::
kono
parents:
diff changeset
644
kono
parents:
diff changeset
645 APIENTRY int get_val (long);
kono
parents:
diff changeset
646
kono
parents:
diff changeset
647 should be imported from Ada as follows:
kono
parents:
diff changeset
648
kono
parents:
diff changeset
649 .. code-block:: ada
kono
parents:
diff changeset
650
kono
parents:
diff changeset
651 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
kono
parents:
diff changeset
652 pragma Import (Stdcall, Get_Val);
kono
parents:
diff changeset
653 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@4"
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 As for the ``C`` calling convention, when the ``External_Name``
kono
parents:
diff changeset
656 parameter is missing, it is taken to be the name of the Ada entity in lower
kono
parents:
diff changeset
657 case. If instead of writing the above import pragma you write:
kono
parents:
diff changeset
658
kono
parents:
diff changeset
659 .. code-block:: ada
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
kono
parents:
diff changeset
662 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 then the imported routine is ``_retrieve_val@4``. However, if instead
kono
parents:
diff changeset
665 of specifying the ``External_Name`` parameter you specify the
kono
parents:
diff changeset
666 ``Link_Name`` as in the following example:
kono
parents:
diff changeset
667
kono
parents:
diff changeset
668 .. code-block:: ada
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
kono
parents:
diff changeset
671 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 then the imported routine is ``retrieve_val``, that is, there is no
kono
parents:
diff changeset
674 decoration at all. No leading underscore and no Stdcall suffix
kono
parents:
diff changeset
675 :samp:`@{nn}`.
kono
parents:
diff changeset
676
kono
parents:
diff changeset
677 This is especially important as in some special cases a DLL's entry
kono
parents:
diff changeset
678 point name lacks a trailing :samp:`@{nn}` while the exported
kono
parents:
diff changeset
679 name generated for a call has it.
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 It is also possible to import variables defined in a DLL by using an
kono
parents:
diff changeset
682 import pragma for a variable. As an example, if a DLL contains a
kono
parents:
diff changeset
683 variable defined as:
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 .. code-block:: c
kono
parents:
diff changeset
686
kono
parents:
diff changeset
687 int my_var;
kono
parents:
diff changeset
688
kono
parents:
diff changeset
689 then, to access this variable from Ada you should write:
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 .. code-block:: ada
kono
parents:
diff changeset
692
kono
parents:
diff changeset
693 My_Var : Interfaces.C.int;
kono
parents:
diff changeset
694 pragma Import (Stdcall, My_Var);
kono
parents:
diff changeset
695
kono
parents:
diff changeset
696 Note that to ease building cross-platform bindings this convention
kono
parents:
diff changeset
697 will be handled as a ``C`` calling convention on non-Windows platforms.
kono
parents:
diff changeset
698
kono
parents:
diff changeset
699
kono
parents:
diff changeset
700 .. _Win32_Calling_Convention:
kono
parents:
diff changeset
701
kono
parents:
diff changeset
702 ``Win32`` Calling Convention
kono
parents:
diff changeset
703 """"""""""""""""""""""""""""
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 This convention, which is GNAT-specific is fully equivalent to the
kono
parents:
diff changeset
706 ``Stdcall`` calling convention described above.
kono
parents:
diff changeset
707
kono
parents:
diff changeset
708
kono
parents:
diff changeset
709 .. _DLL_Calling_Convention:
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 ``DLL`` Calling Convention
kono
parents:
diff changeset
712 """"""""""""""""""""""""""
kono
parents:
diff changeset
713
kono
parents:
diff changeset
714 This convention, which is GNAT-specific is fully equivalent to the
kono
parents:
diff changeset
715 ``Stdcall`` calling convention described above.
kono
parents:
diff changeset
716
kono
parents:
diff changeset
717
kono
parents:
diff changeset
718 .. _Introduction_to_Dynamic_Link_Libraries_DLLs:
kono
parents:
diff changeset
719
kono
parents:
diff changeset
720 Introduction to Dynamic Link Libraries (DLLs)
kono
parents:
diff changeset
721 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
722
kono
parents:
diff changeset
723 .. index:: DLL
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 A Dynamically Linked Library (DLL) is a library that can be shared by
kono
parents:
diff changeset
726 several applications running under Windows. A DLL can contain any number of
kono
parents:
diff changeset
727 routines and variables.
kono
parents:
diff changeset
728
kono
parents:
diff changeset
729 One advantage of DLLs is that you can change and enhance them without
kono
parents:
diff changeset
730 forcing all the applications that depend on them to be relinked or
kono
parents:
diff changeset
731 recompiled. However, you should be aware than all calls to DLL routines are
kono
parents:
diff changeset
732 slower since, as you will understand below, such calls are indirect.
kono
parents:
diff changeset
733
kono
parents:
diff changeset
734 To illustrate the remainder of this section, suppose that an application
kono
parents:
diff changeset
735 wants to use the services of a DLL :file:`API.dll`. To use the services
kono
parents:
diff changeset
736 provided by :file:`API.dll` you must statically link against the DLL or
kono
parents:
diff changeset
737 an import library which contains a jump table with an entry for each
kono
parents:
diff changeset
738 routine and variable exported by the DLL. In the Microsoft world this
kono
parents:
diff changeset
739 import library is called :file:`API.lib`. When using GNAT this import
kono
parents:
diff changeset
740 library is called either :file:`libAPI.dll.a`, :file:`libapi.dll.a`,
kono
parents:
diff changeset
741 :file:`libAPI.a` or :file:`libapi.a` (names are case insensitive).
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 After you have linked your application with the DLL or the import library
kono
parents:
diff changeset
744 and you run your application, here is what happens:
kono
parents:
diff changeset
745
kono
parents:
diff changeset
746 * Your application is loaded into memory.
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 * The DLL :file:`API.dll` is mapped into the address space of your
kono
parents:
diff changeset
749 application. This means that:
kono
parents:
diff changeset
750
kono
parents:
diff changeset
751 - The DLL will use the stack of the calling thread.
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 - The DLL will use the virtual address space of the calling process.
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 - The DLL will allocate memory from the virtual address space of the calling
kono
parents:
diff changeset
756 process.
kono
parents:
diff changeset
757
kono
parents:
diff changeset
758 - Handles (pointers) can be safely exchanged between routines in the DLL
kono
parents:
diff changeset
759 routines and routines in the application using the DLL.
kono
parents:
diff changeset
760
kono
parents:
diff changeset
761 * The entries in the jump table (from the import library :file:`libAPI.dll.a`
kono
parents:
diff changeset
762 or :file:`API.lib` or automatically created when linking against a DLL)
kono
parents:
diff changeset
763 which is part of your application are initialized with the addresses
kono
parents:
diff changeset
764 of the routines and variables in :file:`API.dll`.
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 * If present in :file:`API.dll`, routines ``DllMain`` or
kono
parents:
diff changeset
767 ``DllMainCRTStartup`` are invoked. These routines typically contain
kono
parents:
diff changeset
768 the initialization code needed for the well-being of the routines and
kono
parents:
diff changeset
769 variables exported by the DLL.
kono
parents:
diff changeset
770
kono
parents:
diff changeset
771 There is an additional point which is worth mentioning. In the Windows
kono
parents:
diff changeset
772 world there are two kind of DLLs: relocatable and non-relocatable
kono
parents:
diff changeset
773 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
kono
parents:
diff changeset
774 in the target application address space. If the addresses of two
kono
parents:
diff changeset
775 non-relocatable DLLs overlap and these happen to be used by the same
kono
parents:
diff changeset
776 application, a conflict will occur and the application will run
kono
parents:
diff changeset
777 incorrectly. Hence, when possible, it is always preferable to use and
kono
parents:
diff changeset
778 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
kono
parents:
diff changeset
779 supported by GNAT. Note that the :switch:`-s` linker option (see GNU Linker
kono
parents:
diff changeset
780 User's Guide) removes the debugging symbols from the DLL but the DLL can
kono
parents:
diff changeset
781 still be relocated.
kono
parents:
diff changeset
782
kono
parents:
diff changeset
783 As a side note, an interesting difference between Microsoft DLLs and
kono
parents:
diff changeset
784 Unix shared libraries, is the fact that on most Unix systems all public
kono
parents:
diff changeset
785 routines are exported by default in a Unix shared library, while under
kono
parents:
diff changeset
786 Windows it is possible (but not required) to list exported routines in
kono
parents:
diff changeset
787 a definition file (see :ref:`The Definition File <The_Definition_File>`).
kono
parents:
diff changeset
788
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 .. _Using_DLLs_with_GNAT:
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 Using DLLs with GNAT
kono
parents:
diff changeset
793 ^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 To use the services of a DLL, say :file:`API.dll`, in your Ada application
kono
parents:
diff changeset
796 you must have:
kono
parents:
diff changeset
797
kono
parents:
diff changeset
798 * The Ada spec for the routines and/or variables you want to access in
kono
parents:
diff changeset
799 :file:`API.dll`. If not available this Ada spec must be built from the C/C++
kono
parents:
diff changeset
800 header files provided with the DLL.
kono
parents:
diff changeset
801
kono
parents:
diff changeset
802 * The import library (:file:`libAPI.dll.a` or :file:`API.lib`). As previously
kono
parents:
diff changeset
803 mentioned an import library is a statically linked library containing the
kono
parents:
diff changeset
804 import table which will be filled at load time to point to the actual
kono
parents:
diff changeset
805 :file:`API.dll` routines. Sometimes you don't have an import library for the
kono
parents:
diff changeset
806 DLL you want to use. The following sections will explain how to build
kono
parents:
diff changeset
807 one. Note that this is optional.
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 * The actual DLL, :file:`API.dll`.
kono
parents:
diff changeset
810
kono
parents:
diff changeset
811 Once you have all the above, to compile an Ada application that uses the
kono
parents:
diff changeset
812 services of :file:`API.dll` and whose main subprogram is ``My_Ada_App``,
kono
parents:
diff changeset
813 you simply issue the command
kono
parents:
diff changeset
814
kono
parents:
diff changeset
815 ::
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 $ gnatmake my_ada_app -largs -lAPI
kono
parents:
diff changeset
818
kono
parents:
diff changeset
819 The argument :switch:`-largs -lAPI` at the end of the ``gnatmake`` command
kono
parents:
diff changeset
820 tells the GNAT linker to look for an import library. The linker will
kono
parents:
diff changeset
821 look for a library name in this specific order:
kono
parents:
diff changeset
822
kono
parents:
diff changeset
823 * :file:`libAPI.dll.a`
kono
parents:
diff changeset
824 * :file:`API.dll.a`
kono
parents:
diff changeset
825 * :file:`libAPI.a`
kono
parents:
diff changeset
826 * :file:`API.lib`
kono
parents:
diff changeset
827 * :file:`libAPI.dll`
kono
parents:
diff changeset
828 * :file:`API.dll`
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 The first three are the GNU style import libraries. The third is the
kono
parents:
diff changeset
831 Microsoft style import libraries. The last two are the actual DLL names.
kono
parents:
diff changeset
832
kono
parents:
diff changeset
833 Note that if the Ada package spec for :file:`API.dll` contains the
kono
parents:
diff changeset
834 following pragma
kono
parents:
diff changeset
835
kono
parents:
diff changeset
836 .. code-block:: ada
kono
parents:
diff changeset
837
kono
parents:
diff changeset
838 pragma Linker_Options ("-lAPI");
kono
parents:
diff changeset
839
kono
parents:
diff changeset
840 you do not have to add :switch:`-largs -lAPI` at the end of the
kono
parents:
diff changeset
841 ``gnatmake`` command.
kono
parents:
diff changeset
842
kono
parents:
diff changeset
843 If any one of the items above is missing you will have to create it
kono
parents:
diff changeset
844 yourself. The following sections explain how to do so using as an
kono
parents:
diff changeset
845 example a fictitious DLL called :file:`API.dll`.
kono
parents:
diff changeset
846
kono
parents:
diff changeset
847
kono
parents:
diff changeset
848 .. _Creating_an_Ada_Spec_for_the_DLL_Services:
kono
parents:
diff changeset
849
kono
parents:
diff changeset
850 Creating an Ada Spec for the DLL Services
kono
parents:
diff changeset
851 """""""""""""""""""""""""""""""""""""""""
kono
parents:
diff changeset
852
kono
parents:
diff changeset
853 A DLL typically comes with a C/C++ header file which provides the
kono
parents:
diff changeset
854 definitions of the routines and variables exported by the DLL. The Ada
kono
parents:
diff changeset
855 equivalent of this header file is a package spec that contains definitions
kono
parents:
diff changeset
856 for the imported entities. If the DLL you intend to use does not come with
kono
parents:
diff changeset
857 an Ada spec you have to generate one such spec yourself. For example if
kono
parents:
diff changeset
858 the header file of :file:`API.dll` is a file :file:`api.h` containing the
kono
parents:
diff changeset
859 following two definitions:
kono
parents:
diff changeset
860
kono
parents:
diff changeset
861 .. code-block:: c
kono
parents:
diff changeset
862
kono
parents:
diff changeset
863 int some_var;
kono
parents:
diff changeset
864 int get (char *);
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 then the equivalent Ada spec could be:
kono
parents:
diff changeset
867
kono
parents:
diff changeset
868 .. code-block:: ada
kono
parents:
diff changeset
869
kono
parents:
diff changeset
870 with Interfaces.C.Strings;
kono
parents:
diff changeset
871 package API is
kono
parents:
diff changeset
872 use Interfaces;
kono
parents:
diff changeset
873
kono
parents:
diff changeset
874 Some_Var : C.int;
kono
parents:
diff changeset
875 function Get (Str : C.Strings.Chars_Ptr) return C.int;
kono
parents:
diff changeset
876
kono
parents:
diff changeset
877 private
kono
parents:
diff changeset
878 pragma Import (C, Get);
kono
parents:
diff changeset
879 pragma Import (DLL, Some_Var);
kono
parents:
diff changeset
880 end API;
kono
parents:
diff changeset
881
kono
parents:
diff changeset
882
kono
parents:
diff changeset
883 .. _Creating_an_Import_Library:
kono
parents:
diff changeset
884
kono
parents:
diff changeset
885 Creating an Import Library
kono
parents:
diff changeset
886 """"""""""""""""""""""""""
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 .. index:: Import library
kono
parents:
diff changeset
889
kono
parents:
diff changeset
890 If a Microsoft-style import library :file:`API.lib` or a GNAT-style
kono
parents:
diff changeset
891 import library :file:`libAPI.dll.a` or :file:`libAPI.a` is available
kono
parents:
diff changeset
892 with :file:`API.dll` you can skip this section. You can also skip this
kono
parents:
diff changeset
893 section if :file:`API.dll` or :file:`libAPI.dll` is built with GNU tools
kono
parents:
diff changeset
894 as in this case it is possible to link directly against the
kono
parents:
diff changeset
895 DLL. Otherwise read on.
kono
parents:
diff changeset
896
kono
parents:
diff changeset
897
kono
parents:
diff changeset
898 .. index:: Definition file
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 .. _The_Definition_File:
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 .. rubric:: The Definition File
kono
parents:
diff changeset
903
kono
parents:
diff changeset
904 As previously mentioned, and unlike Unix systems, the list of symbols
kono
parents:
diff changeset
905 that are exported from a DLL must be provided explicitly in Windows.
kono
parents:
diff changeset
906 The main goal of a definition file is precisely that: list the symbols
kono
parents:
diff changeset
907 exported by a DLL. A definition file (usually a file with a ``.def``
kono
parents:
diff changeset
908 suffix) has the following structure:
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 ::
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 [LIBRARY ``name``]
kono
parents:
diff changeset
913 [DESCRIPTION ``string``]
kono
parents:
diff changeset
914 EXPORTS
kono
parents:
diff changeset
915 ``symbol1``
kono
parents:
diff changeset
916 ``symbol2``
kono
parents:
diff changeset
917 ...
kono
parents:
diff changeset
918
kono
parents:
diff changeset
919 *LIBRARY name*
kono
parents:
diff changeset
920 This section, which is optional, gives the name of the DLL.
kono
parents:
diff changeset
921
kono
parents:
diff changeset
922
kono
parents:
diff changeset
923 *DESCRIPTION string*
kono
parents:
diff changeset
924 This section, which is optional, gives a description string that will be
kono
parents:
diff changeset
925 embedded in the import library.
kono
parents:
diff changeset
926
kono
parents:
diff changeset
927
kono
parents:
diff changeset
928 *EXPORTS*
kono
parents:
diff changeset
929 This section gives the list of exported symbols (procedures, functions or
kono
parents:
diff changeset
930 variables). For instance in the case of :file:`API.dll` the ``EXPORTS``
kono
parents:
diff changeset
931 section of :file:`API.def` looks like:
kono
parents:
diff changeset
932
kono
parents:
diff changeset
933 ::
kono
parents:
diff changeset
934
kono
parents:
diff changeset
935 EXPORTS
kono
parents:
diff changeset
936 some_var
kono
parents:
diff changeset
937 get
kono
parents:
diff changeset
938
kono
parents:
diff changeset
939 Note that you must specify the correct suffix (:samp:`@{nn}`)
kono
parents:
diff changeset
940 (see :ref:`Windows_Calling_Conventions`) for a Stdcall
kono
parents:
diff changeset
941 calling convention function in the exported symbols list.
kono
parents:
diff changeset
942
kono
parents:
diff changeset
943 There can actually be other sections in a definition file, but these
kono
parents:
diff changeset
944 sections are not relevant to the discussion at hand.
kono
parents:
diff changeset
945
kono
parents:
diff changeset
946
kono
parents:
diff changeset
947 .. _Create_Def_File_Automatically:
kono
parents:
diff changeset
948
kono
parents:
diff changeset
949 .. rubric:: Creating a Definition File Automatically
kono
parents:
diff changeset
950
kono
parents:
diff changeset
951 You can automatically create the definition file :file:`API.def`
kono
parents:
diff changeset
952 (see :ref:`The Definition File <The_Definition_File>`) from a DLL.
kono
parents:
diff changeset
953 For that use the ``dlltool`` program as follows:
kono
parents:
diff changeset
954
kono
parents:
diff changeset
955 ::
kono
parents:
diff changeset
956
kono
parents:
diff changeset
957 $ dlltool API.dll -z API.def --export-all-symbols
kono
parents:
diff changeset
958
kono
parents:
diff changeset
959 Note that if some routines in the DLL have the ``Stdcall`` convention
kono
parents:
diff changeset
960 (:ref:`Windows_Calling_Conventions`) with stripped :samp:`@{nn}`
kono
parents:
diff changeset
961 suffix then you'll have to edit :file:`api.def` to add it, and specify
kono
parents:
diff changeset
962 :switch:`-k` to ``gnatdll`` when creating the import library.
kono
parents:
diff changeset
963
kono
parents:
diff changeset
964 Here are some hints to find the right :samp:`@{nn}` suffix.
kono
parents:
diff changeset
965
kono
parents:
diff changeset
966 - If you have the Microsoft import library (.lib), it is possible to get
kono
parents:
diff changeset
967 the right symbols by using Microsoft ``dumpbin`` tool (see the
kono
parents:
diff changeset
968 corresponding Microsoft documentation for further details).
kono
parents:
diff changeset
969
kono
parents:
diff changeset
970 ::
kono
parents:
diff changeset
971
kono
parents:
diff changeset
972 $ dumpbin /exports api.lib
kono
parents:
diff changeset
973
kono
parents:
diff changeset
974 - If you have a message about a missing symbol at link time the compiler
kono
parents:
diff changeset
975 tells you what symbol is expected. You just have to go back to the
kono
parents:
diff changeset
976 definition file and add the right suffix.
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978
kono
parents:
diff changeset
979 .. _GNAT-Style_Import_Library:
kono
parents:
diff changeset
980
kono
parents:
diff changeset
981 .. rubric:: GNAT-Style Import Library
kono
parents:
diff changeset
982
kono
parents:
diff changeset
983 To create a static import library from :file:`API.dll` with the GNAT tools
kono
parents:
diff changeset
984 you should create the .def file, then use ``gnatdll`` tool
kono
parents:
diff changeset
985 (see :ref:`Using_gnatdll`) as follows:
kono
parents:
diff changeset
986
kono
parents:
diff changeset
987 ::
kono
parents:
diff changeset
988
kono
parents:
diff changeset
989 $ gnatdll -e API.def -d API.dll
kono
parents:
diff changeset
990
kono
parents:
diff changeset
991 ``gnatdll`` takes as input a definition file :file:`API.def` and the
kono
parents:
diff changeset
992 name of the DLL containing the services listed in the definition file
kono
parents:
diff changeset
993 :file:`API.dll`. The name of the static import library generated is
kono
parents:
diff changeset
994 computed from the name of the definition file as follows: if the
kono
parents:
diff changeset
995 definition file name is :file:`xyz.def`, the import library name will
kono
parents:
diff changeset
996 be :file:`libxyz.a`. Note that in the previous example option
kono
parents:
diff changeset
997 :switch:`-e` could have been removed because the name of the definition
kono
parents:
diff changeset
998 file (before the ``.def`` suffix) is the same as the name of the
kono
parents:
diff changeset
999 DLL (:ref:`Using_gnatdll` for more information about ``gnatdll``).
kono
parents:
diff changeset
1000
kono
parents:
diff changeset
1001
kono
parents:
diff changeset
1002 .. _MSVS-Style_Import_Library:
kono
parents:
diff changeset
1003
kono
parents:
diff changeset
1004 .. rubric:: Microsoft-Style Import Library
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 A Microsoft import library is needed only if you plan to make an
kono
parents:
diff changeset
1007 Ada DLL available to applications developed with Microsoft
kono
parents:
diff changeset
1008 tools (:ref:`Mixed-Language_Programming_on_Windows`).
kono
parents:
diff changeset
1009
kono
parents:
diff changeset
1010 To create a Microsoft-style import library for :file:`API.dll` you
kono
parents:
diff changeset
1011 should create the .def file, then build the actual import library using
kono
parents:
diff changeset
1012 Microsoft's ``lib`` utility:
kono
parents:
diff changeset
1013
kono
parents:
diff changeset
1014 ::
kono
parents:
diff changeset
1015
kono
parents:
diff changeset
1016 $ lib -machine:IX86 -def:API.def -out:API.lib
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 If you use the above command the definition file :file:`API.def` must
kono
parents:
diff changeset
1019 contain a line giving the name of the DLL:
kono
parents:
diff changeset
1020
kono
parents:
diff changeset
1021 ::
kono
parents:
diff changeset
1022
kono
parents:
diff changeset
1023 LIBRARY "API"
kono
parents:
diff changeset
1024
kono
parents:
diff changeset
1025 See the Microsoft documentation for further details about the usage of
kono
parents:
diff changeset
1026 ``lib``.
kono
parents:
diff changeset
1027
kono
parents:
diff changeset
1028
kono
parents:
diff changeset
1029 .. _Building_DLLs_with_GNAT_Project_files:
kono
parents:
diff changeset
1030
kono
parents:
diff changeset
1031 Building DLLs with GNAT Project files
kono
parents:
diff changeset
1032 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1033
kono
parents:
diff changeset
1034 .. index:: DLLs, building
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 There is nothing specific to Windows in the build process.
kono
parents:
diff changeset
1037 See the *Library Projects* section in the *GNAT Project Manager*
kono
parents:
diff changeset
1038 chapter of the *GPRbuild User's Guide*.
kono
parents:
diff changeset
1039
kono
parents:
diff changeset
1040 Due to a system limitation, it is not possible under Windows to create threads
kono
parents:
diff changeset
1041 when inside the ``DllMain`` routine which is used for auto-initialization
kono
parents:
diff changeset
1042 of shared libraries, so it is not possible to have library level tasks in SALs.
kono
parents:
diff changeset
1043
kono
parents:
diff changeset
1044
kono
parents:
diff changeset
1045 .. _Building_DLLs_with_GNAT:
kono
parents:
diff changeset
1046
kono
parents:
diff changeset
1047 Building DLLs with GNAT
kono
parents:
diff changeset
1048 ^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1049
kono
parents:
diff changeset
1050 .. index:: DLLs, building
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 This section explain how to build DLLs using the GNAT built-in DLL
kono
parents:
diff changeset
1053 support. With the following procedure it is straight forward to build
kono
parents:
diff changeset
1054 and use DLLs with GNAT.
kono
parents:
diff changeset
1055
kono
parents:
diff changeset
1056
kono
parents:
diff changeset
1057 * Building object files.
kono
parents:
diff changeset
1058 The first step is to build all objects files that are to be included
kono
parents:
diff changeset
1059 into the DLL. This is done by using the standard ``gnatmake`` tool.
kono
parents:
diff changeset
1060
kono
parents:
diff changeset
1061 * Building the DLL.
kono
parents:
diff changeset
1062 To build the DLL you must use the ``gcc`` :switch:`-shared` and
kono
parents:
diff changeset
1063 :switch:`-shared-libgcc` options. It is quite simple to use this method:
kono
parents:
diff changeset
1064
kono
parents:
diff changeset
1065 ::
kono
parents:
diff changeset
1066
kono
parents:
diff changeset
1067 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
kono
parents:
diff changeset
1068
kono
parents:
diff changeset
1069 It is important to note that in this case all symbols found in the
kono
parents:
diff changeset
1070 object files are automatically exported. It is possible to restrict
kono
parents:
diff changeset
1071 the set of symbols to export by passing to ``gcc`` a definition
kono
parents:
diff changeset
1072 file (see :ref:`The Definition File <The_Definition_File>`).
kono
parents:
diff changeset
1073 For example:
kono
parents:
diff changeset
1074
kono
parents:
diff changeset
1075 ::
kono
parents:
diff changeset
1076
kono
parents:
diff changeset
1077 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
kono
parents:
diff changeset
1078
kono
parents:
diff changeset
1079 If you use a definition file you must export the elaboration procedures
kono
parents:
diff changeset
1080 for every package that required one. Elaboration procedures are named
kono
parents:
diff changeset
1081 using the package name followed by "_E".
kono
parents:
diff changeset
1082
kono
parents:
diff changeset
1083 * Preparing DLL to be used.
kono
parents:
diff changeset
1084 For the DLL to be used by client programs the bodies must be hidden
kono
parents:
diff changeset
1085 from it and the .ali set with read-only attribute. This is very important
kono
parents:
diff changeset
1086 otherwise GNAT will recompile all packages and will not actually use
kono
parents:
diff changeset
1087 the code in the DLL. For example:
kono
parents:
diff changeset
1088
kono
parents:
diff changeset
1089 ::
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 $ mkdir apilib
kono
parents:
diff changeset
1092 $ copy *.ads *.ali api.dll apilib
kono
parents:
diff changeset
1093 $ attrib +R apilib\\*.ali
kono
parents:
diff changeset
1094
kono
parents:
diff changeset
1095 At this point it is possible to use the DLL by directly linking
kono
parents:
diff changeset
1096 against it. Note that you must use the GNAT shared runtime when using
kono
parents:
diff changeset
1097 GNAT shared libraries. This is achieved by using the :switch:`-shared` binder
kono
parents:
diff changeset
1098 option.
kono
parents:
diff changeset
1099
kono
parents:
diff changeset
1100 ::
kono
parents:
diff changeset
1101
kono
parents:
diff changeset
1102 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
kono
parents:
diff changeset
1103
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 .. _Building_DLLs_with_gnatdll:
kono
parents:
diff changeset
1106
kono
parents:
diff changeset
1107 Building DLLs with gnatdll
kono
parents:
diff changeset
1108 ^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1109
kono
parents:
diff changeset
1110 .. index:: DLLs, building
kono
parents:
diff changeset
1111
kono
parents:
diff changeset
1112 Note that it is preferred to use GNAT Project files
kono
parents:
diff changeset
1113 (:ref:`Building_DLLs_with_GNAT_Project_files`) or the built-in GNAT
kono
parents:
diff changeset
1114 DLL support (:ref:`Building_DLLs_with_GNAT`) or to build DLLs.
kono
parents:
diff changeset
1115
kono
parents:
diff changeset
1116 This section explains how to build DLLs containing Ada code using
kono
parents:
diff changeset
1117 ``gnatdll``. These DLLs will be referred to as Ada DLLs in the
kono
parents:
diff changeset
1118 remainder of this section.
kono
parents:
diff changeset
1119
kono
parents:
diff changeset
1120 The steps required to build an Ada DLL that is to be used by Ada as well as
kono
parents:
diff changeset
1121 non-Ada applications are as follows:
kono
parents:
diff changeset
1122
kono
parents:
diff changeset
1123 * You need to mark each Ada entity exported by the DLL with a ``C`` or
kono
parents:
diff changeset
1124 ``Stdcall`` calling convention to avoid any Ada name mangling for the
kono
parents:
diff changeset
1125 entities exported by the DLL
kono
parents:
diff changeset
1126 (see :ref:`Exporting Ada Entities <Exporting_Ada_Entities>`). You can
kono
parents:
diff changeset
1127 skip this step if you plan to use the Ada DLL only from Ada applications.
kono
parents:
diff changeset
1128
kono
parents:
diff changeset
1129 * Your Ada code must export an initialization routine which calls the routine
kono
parents:
diff changeset
1130 ``adainit`` generated by ``gnatbind`` to perform the elaboration of
kono
parents:
diff changeset
1131 the Ada code in the DLL (:ref:`Ada_DLLs_and_Elaboration`). The initialization
kono
parents:
diff changeset
1132 routine exported by the Ada DLL must be invoked by the clients of the DLL
kono
parents:
diff changeset
1133 to initialize the DLL.
kono
parents:
diff changeset
1134
kono
parents:
diff changeset
1135 * When useful, the DLL should also export a finalization routine which calls
kono
parents:
diff changeset
1136 routine ``adafinal`` generated by ``gnatbind`` to perform the
kono
parents:
diff changeset
1137 finalization of the Ada code in the DLL (:ref:`Ada_DLLs_and_Finalization`).
kono
parents:
diff changeset
1138 The finalization routine exported by the Ada DLL must be invoked by the
kono
parents:
diff changeset
1139 clients of the DLL when the DLL services are no further needed.
kono
parents:
diff changeset
1140
kono
parents:
diff changeset
1141 * You must provide a spec for the services exported by the Ada DLL in each
kono
parents:
diff changeset
1142 of the programming languages to which you plan to make the DLL available.
kono
parents:
diff changeset
1143
kono
parents:
diff changeset
1144 * You must provide a definition file listing the exported entities
kono
parents:
diff changeset
1145 (:ref:`The Definition File <The_Definition_File>`).
kono
parents:
diff changeset
1146
kono
parents:
diff changeset
1147 * Finally you must use ``gnatdll`` to produce the DLL and the import
kono
parents:
diff changeset
1148 library (:ref:`Using_gnatdll`).
kono
parents:
diff changeset
1149
kono
parents:
diff changeset
1150 Note that a relocatable DLL stripped using the ``strip``
kono
parents:
diff changeset
1151 binutils tool will not be relocatable anymore. To build a DLL without
kono
parents:
diff changeset
1152 debug information pass :switch:`-largs -s` to ``gnatdll``. This
kono
parents:
diff changeset
1153 restriction does not apply to a DLL built using a Library Project.
kono
parents:
diff changeset
1154 See the *Library Projects* section in the *GNAT Project Manager*
kono
parents:
diff changeset
1155 chapter of the *GPRbuild User's Guide*.
kono
parents:
diff changeset
1156
kono
parents:
diff changeset
1157
kono
parents:
diff changeset
1158 .. Limitations_When_Using_Ada_DLLs_from Ada:
kono
parents:
diff changeset
1159
kono
parents:
diff changeset
1160 Limitations When Using Ada DLLs from Ada
kono
parents:
diff changeset
1161 """"""""""""""""""""""""""""""""""""""""
kono
parents:
diff changeset
1162
kono
parents:
diff changeset
1163 When using Ada DLLs from Ada applications there is a limitation users
kono
parents:
diff changeset
1164 should be aware of. Because on Windows the GNAT run-time is not in a DLL of
kono
parents:
diff changeset
1165 its own, each Ada DLL includes a part of the GNAT run-time. Specifically,
kono
parents:
diff changeset
1166 each Ada DLL includes the services of the GNAT run-time that are necessary
kono
parents:
diff changeset
1167 to the Ada code inside the DLL. As a result, when an Ada program uses an
kono
parents:
diff changeset
1168 Ada DLL there are two independent GNAT run-times: one in the Ada DLL and
kono
parents:
diff changeset
1169 one in the main program.
kono
parents:
diff changeset
1170
kono
parents:
diff changeset
1171 It is therefore not possible to exchange GNAT run-time objects between the
kono
parents:
diff changeset
1172 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
kono
parents:
diff changeset
1173 handles (e.g., ``Text_IO.File_Type``), tasks types, protected objects
kono
parents:
diff changeset
1174 types, etc.
kono
parents:
diff changeset
1175
kono
parents:
diff changeset
1176 It is completely safe to exchange plain elementary, array or record types,
kono
parents:
diff changeset
1177 Windows object handles, etc.
kono
parents:
diff changeset
1178
kono
parents:
diff changeset
1179
kono
parents:
diff changeset
1180 .. _Exporting_Ada_Entities:
kono
parents:
diff changeset
1181
kono
parents:
diff changeset
1182 Exporting Ada Entities
kono
parents:
diff changeset
1183 """"""""""""""""""""""
kono
parents:
diff changeset
1184
kono
parents:
diff changeset
1185 .. index:: Export table
kono
parents:
diff changeset
1186
kono
parents:
diff changeset
1187 Building a DLL is a way to encapsulate a set of services usable from any
kono
parents:
diff changeset
1188 application. As a result, the Ada entities exported by a DLL should be
kono
parents:
diff changeset
1189 exported with the ``C`` or ``Stdcall`` calling conventions to avoid
kono
parents:
diff changeset
1190 any Ada name mangling. As an example here is an Ada package
kono
parents:
diff changeset
1191 ``API``, spec and body, exporting two procedures, a function, and a
kono
parents:
diff changeset
1192 variable:
kono
parents:
diff changeset
1193
kono
parents:
diff changeset
1194
kono
parents:
diff changeset
1195 .. code-block:: ada
kono
parents:
diff changeset
1196
kono
parents:
diff changeset
1197 with Interfaces.C; use Interfaces;
kono
parents:
diff changeset
1198 package API is
kono
parents:
diff changeset
1199 Count : C.int := 0;
kono
parents:
diff changeset
1200 function Factorial (Val : C.int) return C.int;
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 procedure Initialize_API;
kono
parents:
diff changeset
1203 procedure Finalize_API;
kono
parents:
diff changeset
1204 -- Initialization & Finalization routines. More in the next section.
kono
parents:
diff changeset
1205 private
kono
parents:
diff changeset
1206 pragma Export (C, Initialize_API);
kono
parents:
diff changeset
1207 pragma Export (C, Finalize_API);
kono
parents:
diff changeset
1208 pragma Export (C, Count);
kono
parents:
diff changeset
1209 pragma Export (C, Factorial);
kono
parents:
diff changeset
1210 end API;
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 .. code-block:: ada
kono
parents:
diff changeset
1213
kono
parents:
diff changeset
1214 package body API is
kono
parents:
diff changeset
1215 function Factorial (Val : C.int) return C.int is
kono
parents:
diff changeset
1216 Fact : C.int := 1;
kono
parents:
diff changeset
1217 begin
kono
parents:
diff changeset
1218 Count := Count + 1;
kono
parents:
diff changeset
1219 for K in 1 .. Val loop
kono
parents:
diff changeset
1220 Fact := Fact * K;
kono
parents:
diff changeset
1221 end loop;
kono
parents:
diff changeset
1222 return Fact;
kono
parents:
diff changeset
1223 end Factorial;
kono
parents:
diff changeset
1224
kono
parents:
diff changeset
1225 procedure Initialize_API is
kono
parents:
diff changeset
1226 procedure Adainit;
kono
parents:
diff changeset
1227 pragma Import (C, Adainit);
kono
parents:
diff changeset
1228 begin
kono
parents:
diff changeset
1229 Adainit;
kono
parents:
diff changeset
1230 end Initialize_API;
kono
parents:
diff changeset
1231
kono
parents:
diff changeset
1232 procedure Finalize_API is
kono
parents:
diff changeset
1233 procedure Adafinal;
kono
parents:
diff changeset
1234 pragma Import (C, Adafinal);
kono
parents:
diff changeset
1235 begin
kono
parents:
diff changeset
1236 Adafinal;
kono
parents:
diff changeset
1237 end Finalize_API;
kono
parents:
diff changeset
1238 end API;
kono
parents:
diff changeset
1239
kono
parents:
diff changeset
1240 If the Ada DLL you are building will only be used by Ada applications
kono
parents:
diff changeset
1241 you do not have to export Ada entities with a ``C`` or ``Stdcall``
kono
parents:
diff changeset
1242 convention. As an example, the previous package could be written as
kono
parents:
diff changeset
1243 follows:
kono
parents:
diff changeset
1244
kono
parents:
diff changeset
1245 .. code-block:: ada
kono
parents:
diff changeset
1246
kono
parents:
diff changeset
1247 package API is
kono
parents:
diff changeset
1248 Count : Integer := 0;
kono
parents:
diff changeset
1249 function Factorial (Val : Integer) return Integer;
kono
parents:
diff changeset
1250
kono
parents:
diff changeset
1251 procedure Initialize_API;
kono
parents:
diff changeset
1252 procedure Finalize_API;
kono
parents:
diff changeset
1253 -- Initialization and Finalization routines.
kono
parents:
diff changeset
1254 end API;
kono
parents:
diff changeset
1255
kono
parents:
diff changeset
1256 .. code-block:: ada
kono
parents:
diff changeset
1257
kono
parents:
diff changeset
1258 package body API is
kono
parents:
diff changeset
1259 function Factorial (Val : Integer) return Integer is
kono
parents:
diff changeset
1260 Fact : Integer := 1;
kono
parents:
diff changeset
1261 begin
kono
parents:
diff changeset
1262 Count := Count + 1;
kono
parents:
diff changeset
1263 for K in 1 .. Val loop
kono
parents:
diff changeset
1264 Fact := Fact * K;
kono
parents:
diff changeset
1265 end loop;
kono
parents:
diff changeset
1266 return Fact;
kono
parents:
diff changeset
1267 end Factorial;
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 ...
kono
parents:
diff changeset
1270 -- The remainder of this package body is unchanged.
kono
parents:
diff changeset
1271 end API;
kono
parents:
diff changeset
1272
kono
parents:
diff changeset
1273 Note that if you do not export the Ada entities with a ``C`` or
kono
parents:
diff changeset
1274 ``Stdcall`` convention you will have to provide the mangled Ada names
kono
parents:
diff changeset
1275 in the definition file of the Ada DLL
kono
parents:
diff changeset
1276 (:ref:`Creating_the_Definition_File`).
kono
parents:
diff changeset
1277
kono
parents:
diff changeset
1278
kono
parents:
diff changeset
1279 .. _Ada_DLLs_and_Elaboration:
kono
parents:
diff changeset
1280
kono
parents:
diff changeset
1281 Ada DLLs and Elaboration
kono
parents:
diff changeset
1282 """"""""""""""""""""""""
kono
parents:
diff changeset
1283
kono
parents:
diff changeset
1284 .. index:: DLLs and elaboration
kono
parents:
diff changeset
1285
kono
parents:
diff changeset
1286 The DLL that you are building contains your Ada code as well as all the
kono
parents:
diff changeset
1287 routines in the Ada library that are needed by it. The first thing a
kono
parents:
diff changeset
1288 user of your DLL must do is elaborate the Ada code
kono
parents:
diff changeset
1289 (:ref:`Elaboration_Order_Handling_in_GNAT`).
kono
parents:
diff changeset
1290
kono
parents:
diff changeset
1291 To achieve this you must export an initialization routine
kono
parents:
diff changeset
1292 (``Initialize_API`` in the previous example), which must be invoked
kono
parents:
diff changeset
1293 before using any of the DLL services. This elaboration routine must call
kono
parents:
diff changeset
1294 the Ada elaboration routine ``adainit`` generated by the GNAT binder
kono
parents:
diff changeset
1295 (:ref:`Binding_with_Non-Ada_Main_Programs`). See the body of
kono
parents:
diff changeset
1296 ``Initialize_Api`` for an example. Note that the GNAT binder is
kono
parents:
diff changeset
1297 automatically invoked during the DLL build process by the ``gnatdll``
kono
parents:
diff changeset
1298 tool (:ref:`Using_gnatdll`).
kono
parents:
diff changeset
1299
kono
parents:
diff changeset
1300 When a DLL is loaded, Windows systematically invokes a routine called
kono
parents:
diff changeset
1301 ``DllMain``. It would therefore be possible to call ``adainit``
kono
parents:
diff changeset
1302 directly from ``DllMain`` without having to provide an explicit
kono
parents:
diff changeset
1303 initialization routine. Unfortunately, it is not possible to call
kono
parents:
diff changeset
1304 ``adainit`` from the ``DllMain`` if your program has library level
kono
parents:
diff changeset
1305 tasks because access to the ``DllMain`` entry point is serialized by
kono
parents:
diff changeset
1306 the system (that is, only a single thread can execute 'through' it at a
kono
parents:
diff changeset
1307 time), which means that the GNAT run-time will deadlock waiting for the
kono
parents:
diff changeset
1308 newly created task to complete its initialization.
kono
parents:
diff changeset
1309
kono
parents:
diff changeset
1310
kono
parents:
diff changeset
1311 .. _Ada_DLLs_and_Finalization:
kono
parents:
diff changeset
1312
kono
parents:
diff changeset
1313 Ada DLLs and Finalization
kono
parents:
diff changeset
1314 ^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1315
kono
parents:
diff changeset
1316 .. index:: DLLs and finalization
kono
parents:
diff changeset
1317
kono
parents:
diff changeset
1318 When the services of an Ada DLL are no longer needed, the client code should
kono
parents:
diff changeset
1319 invoke the DLL finalization routine, if available. The DLL finalization
kono
parents:
diff changeset
1320 routine is in charge of releasing all resources acquired by the DLL. In the
kono
parents:
diff changeset
1321 case of the Ada code contained in the DLL, this is achieved by calling
kono
parents:
diff changeset
1322 routine ``adafinal`` generated by the GNAT binder
kono
parents:
diff changeset
1323 (:ref:`Binding_with_Non-Ada_Main_Programs`).
kono
parents:
diff changeset
1324 See the body of ``Finalize_Api`` for an
kono
parents:
diff changeset
1325 example. As already pointed out the GNAT binder is automatically invoked
kono
parents:
diff changeset
1326 during the DLL build process by the ``gnatdll`` tool
kono
parents:
diff changeset
1327 (:ref:`Using_gnatdll`).
kono
parents:
diff changeset
1328
kono
parents:
diff changeset
1329
kono
parents:
diff changeset
1330 .. _Creating_a_Spec_for_Ada_DLLs:
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 Creating a Spec for Ada DLLs
kono
parents:
diff changeset
1333 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1334
kono
parents:
diff changeset
1335 To use the services exported by the Ada DLL from another programming
kono
parents:
diff changeset
1336 language (e.g., C), you have to translate the specs of the exported Ada
kono
parents:
diff changeset
1337 entities in that language. For instance in the case of ``API.dll``,
kono
parents:
diff changeset
1338 the corresponding C header file could look like:
kono
parents:
diff changeset
1339
kono
parents:
diff changeset
1340 .. code-block:: c
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 extern int *_imp__count;
kono
parents:
diff changeset
1343 #define count (*_imp__count)
kono
parents:
diff changeset
1344 int factorial (int);
kono
parents:
diff changeset
1345
kono
parents:
diff changeset
1346 It is important to understand that when building an Ada DLL to be used by
kono
parents:
diff changeset
1347 other Ada applications, you need two different specs for the packages
kono
parents:
diff changeset
1348 contained in the DLL: one for building the DLL and the other for using
kono
parents:
diff changeset
1349 the DLL. This is because the ``DLL`` calling convention is needed to
kono
parents:
diff changeset
1350 use a variable defined in a DLL, but when building the DLL, the variable
kono
parents:
diff changeset
1351 must have either the ``Ada`` or ``C`` calling convention. As an
kono
parents:
diff changeset
1352 example consider a DLL comprising the following package ``API``:
kono
parents:
diff changeset
1353
kono
parents:
diff changeset
1354 .. code-block:: ada
kono
parents:
diff changeset
1355
kono
parents:
diff changeset
1356 package API is
kono
parents:
diff changeset
1357 Count : Integer := 0;
kono
parents:
diff changeset
1358 ...
kono
parents:
diff changeset
1359 -- Remainder of the package omitted.
kono
parents:
diff changeset
1360 end API;
kono
parents:
diff changeset
1361
kono
parents:
diff changeset
1362 After producing a DLL containing package ``API``, the spec that
kono
parents:
diff changeset
1363 must be used to import ``API.Count`` from Ada code outside of the
kono
parents:
diff changeset
1364 DLL is:
kono
parents:
diff changeset
1365
kono
parents:
diff changeset
1366 .. code-block:: ada
kono
parents:
diff changeset
1367
kono
parents:
diff changeset
1368 package API is
kono
parents:
diff changeset
1369 Count : Integer;
kono
parents:
diff changeset
1370 pragma Import (DLL, Count);
kono
parents:
diff changeset
1371 end API;
kono
parents:
diff changeset
1372
kono
parents:
diff changeset
1373
kono
parents:
diff changeset
1374 .. _Creating_the_Definition_File:
kono
parents:
diff changeset
1375
kono
parents:
diff changeset
1376 Creating the Definition File
kono
parents:
diff changeset
1377 """"""""""""""""""""""""""""
kono
parents:
diff changeset
1378
kono
parents:
diff changeset
1379 The definition file is the last file needed to build the DLL. It lists
kono
parents:
diff changeset
1380 the exported symbols. As an example, the definition file for a DLL
kono
parents:
diff changeset
1381 containing only package ``API`` (where all the entities are exported
kono
parents:
diff changeset
1382 with a ``C`` calling convention) is:
kono
parents:
diff changeset
1383
kono
parents:
diff changeset
1384 ::
kono
parents:
diff changeset
1385
kono
parents:
diff changeset
1386 EXPORTS
kono
parents:
diff changeset
1387 count
kono
parents:
diff changeset
1388 factorial
kono
parents:
diff changeset
1389 finalize_api
kono
parents:
diff changeset
1390 initialize_api
kono
parents:
diff changeset
1391
kono
parents:
diff changeset
1392 If the ``C`` calling convention is missing from package ``API``,
kono
parents:
diff changeset
1393 then the definition file contains the mangled Ada names of the above
kono
parents:
diff changeset
1394 entities, which in this case are:
kono
parents:
diff changeset
1395
kono
parents:
diff changeset
1396 ::
kono
parents:
diff changeset
1397
kono
parents:
diff changeset
1398 EXPORTS
kono
parents:
diff changeset
1399 api__count
kono
parents:
diff changeset
1400 api__factorial
kono
parents:
diff changeset
1401 api__finalize_api
kono
parents:
diff changeset
1402 api__initialize_api
kono
parents:
diff changeset
1403
kono
parents:
diff changeset
1404
kono
parents:
diff changeset
1405 .. _Using_gnatdll:
kono
parents:
diff changeset
1406
kono
parents:
diff changeset
1407 Using ``gnatdll``
kono
parents:
diff changeset
1408 """""""""""""""""
kono
parents:
diff changeset
1409
kono
parents:
diff changeset
1410 .. index:: gnatdll
kono
parents:
diff changeset
1411
kono
parents:
diff changeset
1412 ``gnatdll`` is a tool to automate the DLL build process once all the Ada
kono
parents:
diff changeset
1413 and non-Ada sources that make up your DLL have been compiled.
kono
parents:
diff changeset
1414 ``gnatdll`` is actually in charge of two distinct tasks: build the
kono
parents:
diff changeset
1415 static import library for the DLL and the actual DLL. The form of the
kono
parents:
diff changeset
1416 ``gnatdll`` command is
kono
parents:
diff changeset
1417
kono
parents:
diff changeset
1418 ::
kono
parents:
diff changeset
1419
kono
parents:
diff changeset
1420 $ gnatdll [ switches ] list-of-files [ -largs opts ]
kono
parents:
diff changeset
1421
kono
parents:
diff changeset
1422 where ``list-of-files`` is a list of ALI and object files. The object
kono
parents:
diff changeset
1423 file list must be the exact list of objects corresponding to the non-Ada
kono
parents:
diff changeset
1424 sources whose services are to be included in the DLL. The ALI file list
kono
parents:
diff changeset
1425 must be the exact list of ALI files for the corresponding Ada sources
kono
parents:
diff changeset
1426 whose services are to be included in the DLL. If ``list-of-files`` is
kono
parents:
diff changeset
1427 missing, only the static import library is generated.
kono
parents:
diff changeset
1428
kono
parents:
diff changeset
1429 You may specify any of the following switches to ``gnatdll``:
kono
parents:
diff changeset
1430
kono
parents:
diff changeset
1431
kono
parents:
diff changeset
1432 .. index:: -a (gnatdll)
kono
parents:
diff changeset
1433
kono
parents:
diff changeset
1434 :switch:`-a[{address}]`
kono
parents:
diff changeset
1435 Build a non-relocatable DLL at ``address``. If ``address`` is not
kono
parents:
diff changeset
1436 specified the default address ``0x11000000`` will be used. By default,
kono
parents:
diff changeset
1437 when this switch is missing, ``gnatdll`` builds relocatable DLL. We
kono
parents:
diff changeset
1438 advise the reader to build relocatable DLL.
kono
parents:
diff changeset
1439
kono
parents:
diff changeset
1440
kono
parents:
diff changeset
1441 .. index:: -b (gnatdll)
kono
parents:
diff changeset
1442
kono
parents:
diff changeset
1443 :switch:`-b {address}`
kono
parents:
diff changeset
1444 Set the relocatable DLL base address. By default the address is
kono
parents:
diff changeset
1445 ``0x11000000``.
kono
parents:
diff changeset
1446
kono
parents:
diff changeset
1447
kono
parents:
diff changeset
1448 .. index:: -bargs (gnatdll)
kono
parents:
diff changeset
1449
kono
parents:
diff changeset
1450 :switch:`-bargs {opts}`
kono
parents:
diff changeset
1451 Binder options. Pass ``opts`` to the binder.
kono
parents:
diff changeset
1452
kono
parents:
diff changeset
1453
kono
parents:
diff changeset
1454 .. index:: -d (gnatdll)
kono
parents:
diff changeset
1455
kono
parents:
diff changeset
1456 :switch:`-d {dllfile}`
kono
parents:
diff changeset
1457 ``dllfile`` is the name of the DLL. This switch must be present for
kono
parents:
diff changeset
1458 ``gnatdll`` to do anything. The name of the generated import library is
kono
parents:
diff changeset
1459 obtained algorithmically from ``dllfile`` as shown in the following
kono
parents:
diff changeset
1460 example: if ``dllfile`` is :file:`xyz.dll`, the import library name is
kono
parents:
diff changeset
1461 :file:`libxyz.dll.a`. The name of the definition file to use (if not specified
kono
parents:
diff changeset
1462 by option :switch:`-e`) is obtained algorithmically from ``dllfile``
kono
parents:
diff changeset
1463 as shown in the following example:
kono
parents:
diff changeset
1464 if ``dllfile`` is :file:`xyz.dll`, the definition
kono
parents:
diff changeset
1465 file used is :file:`xyz.def`.
kono
parents:
diff changeset
1466
kono
parents:
diff changeset
1467
kono
parents:
diff changeset
1468 .. index:: -e (gnatdll)
kono
parents:
diff changeset
1469
kono
parents:
diff changeset
1470 :switch:`-e {deffile}`
kono
parents:
diff changeset
1471 ``deffile`` is the name of the definition file.
kono
parents:
diff changeset
1472
kono
parents:
diff changeset
1473
kono
parents:
diff changeset
1474 .. index:: -g (gnatdll)
kono
parents:
diff changeset
1475
kono
parents:
diff changeset
1476 :switch:`-g`
kono
parents:
diff changeset
1477 Generate debugging information. This information is stored in the object
kono
parents:
diff changeset
1478 file and copied from there to the final DLL file by the linker,
kono
parents:
diff changeset
1479 where it can be read by the debugger. You must use the
kono
parents:
diff changeset
1480 :switch:`-g` switch if you plan on using the debugger or the symbolic
kono
parents:
diff changeset
1481 stack traceback.
kono
parents:
diff changeset
1482
kono
parents:
diff changeset
1483
kono
parents:
diff changeset
1484 .. index:: -h (gnatdll)
kono
parents:
diff changeset
1485
kono
parents:
diff changeset
1486 :switch:`-h`
kono
parents:
diff changeset
1487 Help mode. Displays ``gnatdll`` switch usage information.
kono
parents:
diff changeset
1488
kono
parents:
diff changeset
1489
kono
parents:
diff changeset
1490 .. index:: -I (gnatdll)
kono
parents:
diff changeset
1491
kono
parents:
diff changeset
1492 :switch:`-I{dir}`
kono
parents:
diff changeset
1493 Direct ``gnatdll`` to search the ``dir`` directory for source and
kono
parents:
diff changeset
1494 object files needed to build the DLL.
kono
parents:
diff changeset
1495 (:ref:`Search_Paths_and_the_Run-Time_Library_RTL`).
kono
parents:
diff changeset
1496
kono
parents:
diff changeset
1497
kono
parents:
diff changeset
1498 .. index:: -k (gnatdll)
kono
parents:
diff changeset
1499
kono
parents:
diff changeset
1500 :switch:`-k`
kono
parents:
diff changeset
1501 Removes the :samp:`@{nn}` suffix from the import library's exported
kono
parents:
diff changeset
1502 names, but keeps them for the link names. You must specify this
kono
parents:
diff changeset
1503 option if you want to use a ``Stdcall`` function in a DLL for which
kono
parents:
diff changeset
1504 the :samp:`@{nn}` suffix has been removed. This is the case for most
kono
parents:
diff changeset
1505 of the Windows NT DLL for example. This option has no effect when
kono
parents:
diff changeset
1506 :switch:`-n` option is specified.
kono
parents:
diff changeset
1507
kono
parents:
diff changeset
1508
kono
parents:
diff changeset
1509 .. index:: -l (gnatdll)
kono
parents:
diff changeset
1510
kono
parents:
diff changeset
1511 :switch:`-l {file}`
kono
parents:
diff changeset
1512 The list of ALI and object files used to build the DLL are listed in
kono
parents:
diff changeset
1513 ``file``, instead of being given in the command line. Each line in
kono
parents:
diff changeset
1514 ``file`` contains the name of an ALI or object file.
kono
parents:
diff changeset
1515
kono
parents:
diff changeset
1516
kono
parents:
diff changeset
1517 .. index:: -n (gnatdll)
kono
parents:
diff changeset
1518
kono
parents:
diff changeset
1519 :switch:`-n`
kono
parents:
diff changeset
1520 No Import. Do not create the import library.
kono
parents:
diff changeset
1521
kono
parents:
diff changeset
1522
kono
parents:
diff changeset
1523 .. index:: -q (gnatdll)
kono
parents:
diff changeset
1524
kono
parents:
diff changeset
1525 :switch:`-q`
kono
parents:
diff changeset
1526 Quiet mode. Do not display unnecessary messages.
kono
parents:
diff changeset
1527
kono
parents:
diff changeset
1528
kono
parents:
diff changeset
1529 .. index:: -v (gnatdll)
kono
parents:
diff changeset
1530
kono
parents:
diff changeset
1531 :switch:`-v`
kono
parents:
diff changeset
1532 Verbose mode. Display extra information.
kono
parents:
diff changeset
1533
kono
parents:
diff changeset
1534
kono
parents:
diff changeset
1535 .. index:: -largs (gnatdll)
kono
parents:
diff changeset
1536
kono
parents:
diff changeset
1537 :switch:`-largs {opts}`
kono
parents:
diff changeset
1538 Linker options. Pass ``opts`` to the linker.
kono
parents:
diff changeset
1539
kono
parents:
diff changeset
1540
kono
parents:
diff changeset
1541 .. rubric:: ``gnatdll`` Example
kono
parents:
diff changeset
1542
kono
parents:
diff changeset
1543 As an example the command to build a relocatable DLL from :file:`api.adb`
kono
parents:
diff changeset
1544 once :file:`api.adb` has been compiled and :file:`api.def` created is
kono
parents:
diff changeset
1545
kono
parents:
diff changeset
1546 ::
kono
parents:
diff changeset
1547
kono
parents:
diff changeset
1548 $ gnatdll -d api.dll api.ali
kono
parents:
diff changeset
1549
kono
parents:
diff changeset
1550 The above command creates two files: :file:`libapi.dll.a` (the import
kono
parents:
diff changeset
1551 library) and :file:`api.dll` (the actual DLL). If you want to create
kono
parents:
diff changeset
1552 only the DLL, just type:
kono
parents:
diff changeset
1553
kono
parents:
diff changeset
1554 ::
kono
parents:
diff changeset
1555
kono
parents:
diff changeset
1556 $ gnatdll -d api.dll -n api.ali
kono
parents:
diff changeset
1557
kono
parents:
diff changeset
1558 Alternatively if you want to create just the import library, type:
kono
parents:
diff changeset
1559
kono
parents:
diff changeset
1560 ::
kono
parents:
diff changeset
1561
kono
parents:
diff changeset
1562 $ gnatdll -d api.dll
kono
parents:
diff changeset
1563
kono
parents:
diff changeset
1564
kono
parents:
diff changeset
1565 .. rubric:: ``gnatdll`` behind the Scenes
kono
parents:
diff changeset
1566
kono
parents:
diff changeset
1567 This section details the steps involved in creating a DLL. ``gnatdll``
kono
parents:
diff changeset
1568 does these steps for you. Unless you are interested in understanding what
kono
parents:
diff changeset
1569 goes on behind the scenes, you should skip this section.
kono
parents:
diff changeset
1570
kono
parents:
diff changeset
1571 We use the previous example of a DLL containing the Ada package ``API``,
kono
parents:
diff changeset
1572 to illustrate the steps necessary to build a DLL. The starting point is a
kono
parents:
diff changeset
1573 set of objects that will make up the DLL and the corresponding ALI
kono
parents:
diff changeset
1574 files. In the case of this example this means that :file:`api.o` and
kono
parents:
diff changeset
1575 :file:`api.ali` are available. To build a relocatable DLL, ``gnatdll`` does
kono
parents:
diff changeset
1576 the following:
kono
parents:
diff changeset
1577
kono
parents:
diff changeset
1578 * ``gnatdll`` builds the base file (:file:`api.base`). A base file gives
kono
parents:
diff changeset
1579 the information necessary to generate relocation information for the
kono
parents:
diff changeset
1580 DLL.
kono
parents:
diff changeset
1581
kono
parents:
diff changeset
1582 ::
kono
parents:
diff changeset
1583
kono
parents:
diff changeset
1584 $ gnatbind -n api
kono
parents:
diff changeset
1585 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
kono
parents:
diff changeset
1586
kono
parents:
diff changeset
1587 In addition to the base file, the ``gnatlink`` command generates an
kono
parents:
diff changeset
1588 output file :file:`api.jnk` which can be discarded. The :switch:`-mdll` switch
kono
parents:
diff changeset
1589 asks ``gnatlink`` to generate the routines ``DllMain`` and
kono
parents:
diff changeset
1590 ``DllMainCRTStartup`` that are called by the Windows loader when the DLL
kono
parents:
diff changeset
1591 is loaded into memory.
kono
parents:
diff changeset
1592
kono
parents:
diff changeset
1593 * ``gnatdll`` uses ``dlltool`` (see :ref:`Using dlltool <Using_dlltool>`) to build the
kono
parents:
diff changeset
1594 export table (:file:`api.exp`). The export table contains the relocation
kono
parents:
diff changeset
1595 information in a form which can be used during the final link to ensure
kono
parents:
diff changeset
1596 that the Windows loader is able to place the DLL anywhere in memory.
kono
parents:
diff changeset
1597
kono
parents:
diff changeset
1598 ::
kono
parents:
diff changeset
1599
kono
parents:
diff changeset
1600 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
kono
parents:
diff changeset
1601 --output-exp api.exp
kono
parents:
diff changeset
1602
kono
parents:
diff changeset
1603 * ``gnatdll`` builds the base file using the new export table. Note that
kono
parents:
diff changeset
1604 ``gnatbind`` must be called once again since the binder generated file
kono
parents:
diff changeset
1605 has been deleted during the previous call to ``gnatlink``.
kono
parents:
diff changeset
1606
kono
parents:
diff changeset
1607 ::
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 $ gnatbind -n api
kono
parents:
diff changeset
1610 $ gnatlink api -o api.jnk api.exp -mdll
kono
parents:
diff changeset
1611 -Wl,--base-file,api.base
kono
parents:
diff changeset
1612
kono
parents:
diff changeset
1613
kono
parents:
diff changeset
1614 * ``gnatdll`` builds the new export table using the new base file and
kono
parents:
diff changeset
1615 generates the DLL import library :file:`libAPI.dll.a`.
kono
parents:
diff changeset
1616
kono
parents:
diff changeset
1617
kono
parents:
diff changeset
1618 ::
kono
parents:
diff changeset
1619
kono
parents:
diff changeset
1620 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
kono
parents:
diff changeset
1621 --output-exp api.exp --output-lib libAPI.a
kono
parents:
diff changeset
1622
kono
parents:
diff changeset
1623 * Finally ``gnatdll`` builds the relocatable DLL using the final export
kono
parents:
diff changeset
1624 table.
kono
parents:
diff changeset
1625
kono
parents:
diff changeset
1626 ::
kono
parents:
diff changeset
1627
kono
parents:
diff changeset
1628 $ gnatbind -n api
kono
parents:
diff changeset
1629 $ gnatlink api api.exp -o api.dll -mdll
kono
parents:
diff changeset
1630
kono
parents:
diff changeset
1631
kono
parents:
diff changeset
1632 .. _Using_dlltool:
kono
parents:
diff changeset
1633
kono
parents:
diff changeset
1634 .. rubric:: Using ``dlltool``
kono
parents:
diff changeset
1635
kono
parents:
diff changeset
1636 ``dlltool`` is the low-level tool used by ``gnatdll`` to build
kono
parents:
diff changeset
1637 DLLs and static import libraries. This section summarizes the most
kono
parents:
diff changeset
1638 common ``dlltool`` switches. The form of the ``dlltool`` command
kono
parents:
diff changeset
1639 is
kono
parents:
diff changeset
1640
kono
parents:
diff changeset
1641 ::
kono
parents:
diff changeset
1642
kono
parents:
diff changeset
1643 $ dlltool [`switches`]
kono
parents:
diff changeset
1644
kono
parents:
diff changeset
1645 ``dlltool`` switches include:
kono
parents:
diff changeset
1646
kono
parents:
diff changeset
1647
kono
parents:
diff changeset
1648 .. index:: --base-file (dlltool)
kono
parents:
diff changeset
1649
kono
parents:
diff changeset
1650 :switch:`--base-file {basefile}`
kono
parents:
diff changeset
1651 Read the base file ``basefile`` generated by the linker. This switch
kono
parents:
diff changeset
1652 is used to create a relocatable DLL.
kono
parents:
diff changeset
1653
kono
parents:
diff changeset
1654
kono
parents:
diff changeset
1655 .. index:: --def (dlltool)
kono
parents:
diff changeset
1656
kono
parents:
diff changeset
1657 :switch:`--def {deffile}`
kono
parents:
diff changeset
1658 Read the definition file.
kono
parents:
diff changeset
1659
kono
parents:
diff changeset
1660
kono
parents:
diff changeset
1661 .. index:: --dllname (dlltool)
kono
parents:
diff changeset
1662
kono
parents:
diff changeset
1663 :switch:`--dllname {name}`
kono
parents:
diff changeset
1664 Gives the name of the DLL. This switch is used to embed the name of the
kono
parents:
diff changeset
1665 DLL in the static import library generated by ``dlltool`` with switch
kono
parents:
diff changeset
1666 :switch:`--output-lib`.
kono
parents:
diff changeset
1667
kono
parents:
diff changeset
1668
kono
parents:
diff changeset
1669 .. index:: -k (dlltool)
kono
parents:
diff changeset
1670
kono
parents:
diff changeset
1671 :switch:`-k`
kono
parents:
diff changeset
1672 Kill :samp:`@{nn}` from exported names
kono
parents:
diff changeset
1673 (:ref:`Windows_Calling_Conventions`
kono
parents:
diff changeset
1674 for a discussion about ``Stdcall``-style symbols.
kono
parents:
diff changeset
1675
kono
parents:
diff changeset
1676
kono
parents:
diff changeset
1677 .. index:: --help (dlltool)
kono
parents:
diff changeset
1678
kono
parents:
diff changeset
1679 :switch:`--help`
kono
parents:
diff changeset
1680 Prints the ``dlltool`` switches with a concise description.
kono
parents:
diff changeset
1681
kono
parents:
diff changeset
1682
kono
parents:
diff changeset
1683 .. index:: --output-exp (dlltool)
kono
parents:
diff changeset
1684
kono
parents:
diff changeset
1685 :switch:`--output-exp {exportfile}`
kono
parents:
diff changeset
1686 Generate an export file ``exportfile``. The export file contains the
kono
parents:
diff changeset
1687 export table (list of symbols in the DLL) and is used to create the DLL.
kono
parents:
diff changeset
1688
kono
parents:
diff changeset
1689
kono
parents:
diff changeset
1690 .. index:: --output-lib (dlltool)
kono
parents:
diff changeset
1691
kono
parents:
diff changeset
1692 :switch:`--output-lib {libfile}`
kono
parents:
diff changeset
1693 Generate a static import library ``libfile``.
kono
parents:
diff changeset
1694
kono
parents:
diff changeset
1695
kono
parents:
diff changeset
1696 .. index:: -v (dlltool)
kono
parents:
diff changeset
1697
kono
parents:
diff changeset
1698 :switch:`-v`
kono
parents:
diff changeset
1699 Verbose mode.
kono
parents:
diff changeset
1700
kono
parents:
diff changeset
1701
kono
parents:
diff changeset
1702 .. index:: --as (dlltool)
kono
parents:
diff changeset
1703
kono
parents:
diff changeset
1704 :switch:`--as {assembler-name}`
kono
parents:
diff changeset
1705 Use ``assembler-name`` as the assembler. The default is ``as``.
kono
parents:
diff changeset
1706
kono
parents:
diff changeset
1707
kono
parents:
diff changeset
1708 .. _GNAT_and_Windows_Resources:
kono
parents:
diff changeset
1709
kono
parents:
diff changeset
1710 GNAT and Windows Resources
kono
parents:
diff changeset
1711 ^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1712
kono
parents:
diff changeset
1713 .. index:: Resources, windows
kono
parents:
diff changeset
1714
kono
parents:
diff changeset
1715 Resources are an easy way to add Windows specific objects to your
kono
parents:
diff changeset
1716 application. The objects that can be added as resources include:
kono
parents:
diff changeset
1717
kono
parents:
diff changeset
1718 * menus
kono
parents:
diff changeset
1719
kono
parents:
diff changeset
1720 * accelerators
kono
parents:
diff changeset
1721
kono
parents:
diff changeset
1722 * dialog boxes
kono
parents:
diff changeset
1723
kono
parents:
diff changeset
1724 * string tables
kono
parents:
diff changeset
1725
kono
parents:
diff changeset
1726 * bitmaps
kono
parents:
diff changeset
1727
kono
parents:
diff changeset
1728 * cursors
kono
parents:
diff changeset
1729
kono
parents:
diff changeset
1730 * icons
kono
parents:
diff changeset
1731
kono
parents:
diff changeset
1732 * fonts
kono
parents:
diff changeset
1733
kono
parents:
diff changeset
1734 * version information
kono
parents:
diff changeset
1735
kono
parents:
diff changeset
1736 For example, a version information resource can be defined as follow and
kono
parents:
diff changeset
1737 embedded into an executable or DLL:
kono
parents:
diff changeset
1738
kono
parents:
diff changeset
1739 A version information resource can be used to embed information into an
kono
parents:
diff changeset
1740 executable or a DLL. These information can be viewed using the file properties
kono
parents:
diff changeset
1741 from the Windows Explorer. Here is an example of a version information
kono
parents:
diff changeset
1742 resource:
kono
parents:
diff changeset
1743
kono
parents:
diff changeset
1744 ::
kono
parents:
diff changeset
1745
kono
parents:
diff changeset
1746 1 VERSIONINFO
kono
parents:
diff changeset
1747 FILEVERSION 1,0,0,0
kono
parents:
diff changeset
1748 PRODUCTVERSION 1,0,0,0
kono
parents:
diff changeset
1749 BEGIN
kono
parents:
diff changeset
1750 BLOCK "StringFileInfo"
kono
parents:
diff changeset
1751 BEGIN
kono
parents:
diff changeset
1752 BLOCK "080904E4"
kono
parents:
diff changeset
1753 BEGIN
kono
parents:
diff changeset
1754 VALUE "CompanyName", "My Company Name"
kono
parents:
diff changeset
1755 VALUE "FileDescription", "My application"
kono
parents:
diff changeset
1756 VALUE "FileVersion", "1.0"
kono
parents:
diff changeset
1757 VALUE "InternalName", "my_app"
kono
parents:
diff changeset
1758 VALUE "LegalCopyright", "My Name"
kono
parents:
diff changeset
1759 VALUE "OriginalFilename", "my_app.exe"
kono
parents:
diff changeset
1760 VALUE "ProductName", "My App"
kono
parents:
diff changeset
1761 VALUE "ProductVersion", "1.0"
kono
parents:
diff changeset
1762 END
kono
parents:
diff changeset
1763 END
kono
parents:
diff changeset
1764
kono
parents:
diff changeset
1765 BLOCK "VarFileInfo"
kono
parents:
diff changeset
1766 BEGIN
kono
parents:
diff changeset
1767 VALUE "Translation", 0x809, 1252
kono
parents:
diff changeset
1768 END
kono
parents:
diff changeset
1769 END
kono
parents:
diff changeset
1770
kono
parents:
diff changeset
1771 The value ``0809`` (langID) is for the U.K English language and
kono
parents:
diff changeset
1772 ``04E4`` (charsetID), which is equal to ``1252`` decimal, for
kono
parents:
diff changeset
1773 multilingual.
kono
parents:
diff changeset
1774
kono
parents:
diff changeset
1775 This section explains how to build, compile and use resources. Note that this
kono
parents:
diff changeset
1776 section does not cover all resource objects, for a complete description see
kono
parents:
diff changeset
1777 the corresponding Microsoft documentation.
kono
parents:
diff changeset
1778
kono
parents:
diff changeset
1779
kono
parents:
diff changeset
1780 .. _Building_Resources:
kono
parents:
diff changeset
1781
kono
parents:
diff changeset
1782 Building Resources
kono
parents:
diff changeset
1783 """"""""""""""""""
kono
parents:
diff changeset
1784
kono
parents:
diff changeset
1785 .. index:: Resources, building
kono
parents:
diff changeset
1786
kono
parents:
diff changeset
1787 A resource file is an ASCII file. By convention resource files have an
kono
parents:
diff changeset
1788 :file:`.rc` extension.
kono
parents:
diff changeset
1789 The easiest way to build a resource file is to use Microsoft tools
kono
parents:
diff changeset
1790 such as ``imagedit.exe`` to build bitmaps, icons and cursors and
kono
parents:
diff changeset
1791 ``dlgedit.exe`` to build dialogs.
kono
parents:
diff changeset
1792 It is always possible to build an :file:`.rc` file yourself by writing a
kono
parents:
diff changeset
1793 resource script.
kono
parents:
diff changeset
1794
kono
parents:
diff changeset
1795 It is not our objective to explain how to write a resource file. A
kono
parents:
diff changeset
1796 complete description of the resource script language can be found in the
kono
parents:
diff changeset
1797 Microsoft documentation.
kono
parents:
diff changeset
1798
kono
parents:
diff changeset
1799
kono
parents:
diff changeset
1800 .. _Compiling_Resources:
kono
parents:
diff changeset
1801
kono
parents:
diff changeset
1802 Compiling Resources
kono
parents:
diff changeset
1803 """""""""""""""""""
kono
parents:
diff changeset
1804
kono
parents:
diff changeset
1805 .. index:: rc
kono
parents:
diff changeset
1806 .. index:: windres
kono
parents:
diff changeset
1807 .. index:: Resources, compiling
kono
parents:
diff changeset
1808
kono
parents:
diff changeset
1809 This section describes how to build a GNAT-compatible (COFF) object file
kono
parents:
diff changeset
1810 containing the resources. This is done using the Resource Compiler
kono
parents:
diff changeset
1811 ``windres`` as follows:
kono
parents:
diff changeset
1812
kono
parents:
diff changeset
1813 ::
kono
parents:
diff changeset
1814
kono
parents:
diff changeset
1815 $ windres -i myres.rc -o myres.o
kono
parents:
diff changeset
1816
kono
parents:
diff changeset
1817 By default ``windres`` will run ``gcc`` to preprocess the :file:`.rc`
kono
parents:
diff changeset
1818 file. You can specify an alternate preprocessor (usually named
kono
parents:
diff changeset
1819 :file:`cpp.exe`) using the ``windres`` :switch:`--preprocessor`
kono
parents:
diff changeset
1820 parameter. A list of all possible options may be obtained by entering
kono
parents:
diff changeset
1821 the command ``windres`` :switch:`--help`.
kono
parents:
diff changeset
1822
kono
parents:
diff changeset
1823 It is also possible to use the Microsoft resource compiler ``rc.exe``
kono
parents:
diff changeset
1824 to produce a :file:`.res` file (binary resource file). See the
kono
parents:
diff changeset
1825 corresponding Microsoft documentation for further details. In this case
kono
parents:
diff changeset
1826 you need to use ``windres`` to translate the :file:`.res` file to a
kono
parents:
diff changeset
1827 GNAT-compatible object file as follows:
kono
parents:
diff changeset
1828
kono
parents:
diff changeset
1829 ::
kono
parents:
diff changeset
1830
kono
parents:
diff changeset
1831 $ windres -i myres.res -o myres.o
kono
parents:
diff changeset
1832
kono
parents:
diff changeset
1833
kono
parents:
diff changeset
1834 .. _Using_Resources:
kono
parents:
diff changeset
1835
kono
parents:
diff changeset
1836 Using Resources
kono
parents:
diff changeset
1837 """""""""""""""
kono
parents:
diff changeset
1838
kono
parents:
diff changeset
1839 .. index:: Resources, using
kono
parents:
diff changeset
1840
kono
parents:
diff changeset
1841 To include the resource file in your program just add the
kono
parents:
diff changeset
1842 GNAT-compatible object file for the resource(s) to the linker
kono
parents:
diff changeset
1843 arguments. With ``gnatmake`` this is done by using the :switch:`-largs`
kono
parents:
diff changeset
1844 option:
kono
parents:
diff changeset
1845
kono
parents:
diff changeset
1846 ::
kono
parents:
diff changeset
1847
kono
parents:
diff changeset
1848 $ gnatmake myprog -largs myres.o
kono
parents:
diff changeset
1849
kono
parents:
diff changeset
1850
kono
parents:
diff changeset
1851 .. _Using_GNAT_DLL_from_MSVS:
kono
parents:
diff changeset
1852
kono
parents:
diff changeset
1853 Using GNAT DLLs from Microsoft Visual Studio Applications
kono
parents:
diff changeset
1854 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1855
kono
parents:
diff changeset
1856 .. index:: Microsoft Visual Studio, use with GNAT DLLs
kono
parents:
diff changeset
1857
kono
parents:
diff changeset
1858 This section describes a common case of mixed GNAT/Microsoft Visual Studio
kono
parents:
diff changeset
1859 application development, where the main program is developed using MSVS, and
kono
parents:
diff changeset
1860 is linked with a DLL developed using GNAT. Such a mixed application should
kono
parents:
diff changeset
1861 be developed following the general guidelines outlined above; below is the
kono
parents:
diff changeset
1862 cookbook-style sequence of steps to follow:
kono
parents:
diff changeset
1863
kono
parents:
diff changeset
1864 1. First develop and build the GNAT shared library using a library project
kono
parents:
diff changeset
1865 (let's assume the project is :file:`mylib.gpr`, producing the library :file:`libmylib.dll`):
kono
parents:
diff changeset
1866
kono
parents:
diff changeset
1867 ::
kono
parents:
diff changeset
1868
kono
parents:
diff changeset
1869 $ gprbuild -p mylib.gpr
kono
parents:
diff changeset
1870
kono
parents:
diff changeset
1871 2. Produce a .def file for the symbols you need to interface with, either by
kono
parents:
diff changeset
1872 hand or automatically with possibly some manual adjustments
kono
parents:
diff changeset
1873 (see :ref:`Creating Definition File Automatically <Create_Def_File_Automatically>`):
kono
parents:
diff changeset
1874
kono
parents:
diff changeset
1875 ::
kono
parents:
diff changeset
1876
kono
parents:
diff changeset
1877 $ dlltool libmylib.dll -z libmylib.def --export-all-symbols
kono
parents:
diff changeset
1878
kono
parents:
diff changeset
1879 3. Make sure that MSVS command-line tools are accessible on the path.
kono
parents:
diff changeset
1880
kono
parents:
diff changeset
1881 4. Create the Microsoft-style import library (see :ref:`MSVS-Style Import Library <MSVS-Style_Import_Library>`):
kono
parents:
diff changeset
1882
kono
parents:
diff changeset
1883 ::
kono
parents:
diff changeset
1884
kono
parents:
diff changeset
1885 $ lib -machine:IX86 -def:libmylib.def -out:libmylib.lib
kono
parents:
diff changeset
1886
kono
parents:
diff changeset
1887 If you are using a 64-bit toolchain, the above becomes...
kono
parents:
diff changeset
1888
kono
parents:
diff changeset
1889 ::
kono
parents:
diff changeset
1890
kono
parents:
diff changeset
1891 $ lib -machine:X64 -def:libmylib.def -out:libmylib.lib
kono
parents:
diff changeset
1892
kono
parents:
diff changeset
1893 5. Build the C main
kono
parents:
diff changeset
1894
kono
parents:
diff changeset
1895 ::
kono
parents:
diff changeset
1896
kono
parents:
diff changeset
1897 $ cl /O2 /MD main.c libmylib.lib
kono
parents:
diff changeset
1898
kono
parents:
diff changeset
1899 6. Before running the executable, make sure you have set the PATH to the DLL,
kono
parents:
diff changeset
1900 or copy the DLL into into the directory containing the .exe.
kono
parents:
diff changeset
1901
kono
parents:
diff changeset
1902
kono
parents:
diff changeset
1903 .. _Debugging_a_DLL:
kono
parents:
diff changeset
1904
kono
parents:
diff changeset
1905 Debugging a DLL
kono
parents:
diff changeset
1906 ^^^^^^^^^^^^^^^
kono
parents:
diff changeset
1907
kono
parents:
diff changeset
1908 .. index:: DLL debugging
kono
parents:
diff changeset
1909
kono
parents:
diff changeset
1910 Debugging a DLL is similar to debugging a standard program. But
kono
parents:
diff changeset
1911 we have to deal with two different executable parts: the DLL and the
kono
parents:
diff changeset
1912 program that uses it. We have the following four possibilities:
kono
parents:
diff changeset
1913
kono
parents:
diff changeset
1914 * The program and the DLL are built with GCC/GNAT.
kono
parents:
diff changeset
1915 * The program is built with foreign tools and the DLL is built with
kono
parents:
diff changeset
1916 GCC/GNAT.
kono
parents:
diff changeset
1917 * The program is built with GCC/GNAT and the DLL is built with
kono
parents:
diff changeset
1918 foreign tools.
kono
parents:
diff changeset
1919
kono
parents:
diff changeset
1920 In this section we address only cases one and two above.
kono
parents:
diff changeset
1921 There is no point in trying to debug
kono
parents:
diff changeset
1922 a DLL with GNU/GDB, if there is no GDB-compatible debugging
kono
parents:
diff changeset
1923 information in it. To do so you must use a debugger compatible with the
kono
parents:
diff changeset
1924 tools suite used to build the DLL.
kono
parents:
diff changeset
1925
kono
parents:
diff changeset
1926 .. _Program_and_DLL_Both_Built_with_GCC/GNAT:
kono
parents:
diff changeset
1927
kono
parents:
diff changeset
1928 Program and DLL Both Built with GCC/GNAT
kono
parents:
diff changeset
1929 """"""""""""""""""""""""""""""""""""""""
kono
parents:
diff changeset
1930
kono
parents:
diff changeset
1931 This is the simplest case. Both the DLL and the program have ``GDB``
kono
parents:
diff changeset
1932 compatible debugging information. It is then possible to break anywhere in
kono
parents:
diff changeset
1933 the process. Let's suppose here that the main procedure is named
kono
parents:
diff changeset
1934 ``ada_main`` and that in the DLL there is an entry point named
kono
parents:
diff changeset
1935 ``ada_dll``.
kono
parents:
diff changeset
1936
kono
parents:
diff changeset
1937 The DLL (:ref:`Introduction_to_Dynamic_Link_Libraries_DLLs`) and
kono
parents:
diff changeset
1938 program must have been built with the debugging information (see GNAT -g
kono
parents:
diff changeset
1939 switch). Here are the step-by-step instructions for debugging it:
kono
parents:
diff changeset
1940
kono
parents:
diff changeset
1941 * Launch ``GDB`` on the main program.
kono
parents:
diff changeset
1942
kono
parents:
diff changeset
1943 ::
kono
parents:
diff changeset
1944
kono
parents:
diff changeset
1945 $ gdb -nw ada_main
kono
parents:
diff changeset
1946
kono
parents:
diff changeset
1947 * Start the program and stop at the beginning of the main procedure
kono
parents:
diff changeset
1948
kono
parents:
diff changeset
1949 ::
kono
parents:
diff changeset
1950
kono
parents:
diff changeset
1951 (gdb) start
kono
parents:
diff changeset
1952
kono
parents:
diff changeset
1953 This step is required to be able to set a breakpoint inside the DLL. As long
kono
parents:
diff changeset
1954 as the program is not run, the DLL is not loaded. This has the
kono
parents:
diff changeset
1955 consequence that the DLL debugging information is also not loaded, so it is not
kono
parents:
diff changeset
1956 possible to set a breakpoint in the DLL.
kono
parents:
diff changeset
1957
kono
parents:
diff changeset
1958 * Set a breakpoint inside the DLL
kono
parents:
diff changeset
1959
kono
parents:
diff changeset
1960 ::
kono
parents:
diff changeset
1961
kono
parents:
diff changeset
1962 (gdb) break ada_dll
kono
parents:
diff changeset
1963 (gdb) cont
kono
parents:
diff changeset
1964
kono
parents:
diff changeset
1965 At this stage a breakpoint is set inside the DLL. From there on
kono
parents:
diff changeset
1966 you can use the standard approach to debug the whole program
kono
parents:
diff changeset
1967 (:ref:`Running_and_Debugging_Ada_Programs`).
kono
parents:
diff changeset
1968
kono
parents:
diff changeset
1969
kono
parents:
diff changeset
1970 .. _Program_Built_with_Foreign_Tools_and_DLL_Built_with_GCC/GNAT:
kono
parents:
diff changeset
1971
kono
parents:
diff changeset
1972 Program Built with Foreign Tools and DLL Built with GCC/GNAT
kono
parents:
diff changeset
1973 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
kono
parents:
diff changeset
1974
kono
parents:
diff changeset
1975 In this case things are slightly more complex because it is not possible to
kono
parents:
diff changeset
1976 start the main program and then break at the beginning to load the DLL and the
kono
parents:
diff changeset
1977 associated DLL debugging information. It is not possible to break at the
kono
parents:
diff changeset
1978 beginning of the program because there is no ``GDB`` debugging information,
kono
parents:
diff changeset
1979 and therefore there is no direct way of getting initial control. This
kono
parents:
diff changeset
1980 section addresses this issue by describing some methods that can be used
kono
parents:
diff changeset
1981 to break somewhere in the DLL to debug it.
kono
parents:
diff changeset
1982
kono
parents:
diff changeset
1983 First suppose that the main procedure is named ``main`` (this is for
kono
parents:
diff changeset
1984 example some C code built with Microsoft Visual C) and that there is a
kono
parents:
diff changeset
1985 DLL named ``test.dll`` containing an Ada entry point named
kono
parents:
diff changeset
1986 ``ada_dll``.
kono
parents:
diff changeset
1987
kono
parents:
diff changeset
1988 The DLL (see :ref:`Introduction_to_Dynamic_Link_Libraries_DLLs`) must have
kono
parents:
diff changeset
1989 been built with debugging information (see the GNAT :switch:`-g` option).
kono
parents:
diff changeset
1990
kono
parents:
diff changeset
1991
kono
parents:
diff changeset
1992 .. rubric:: Debugging the DLL Directly
kono
parents:
diff changeset
1993
kono
parents:
diff changeset
1994 * Find out the executable starting address
kono
parents:
diff changeset
1995
kono
parents:
diff changeset
1996 ::
kono
parents:
diff changeset
1997
kono
parents:
diff changeset
1998 $ objdump --file-header main.exe
kono
parents:
diff changeset
1999
kono
parents:
diff changeset
2000 The starting address is reported on the last line. For example:
kono
parents:
diff changeset
2001
kono
parents:
diff changeset
2002 ::
kono
parents:
diff changeset
2003
kono
parents:
diff changeset
2004 main.exe: file format pei-i386
kono
parents:
diff changeset
2005 architecture: i386, flags 0x0000010a:
kono
parents:
diff changeset
2006 EXEC_P, HAS_DEBUG, D_PAGED
kono
parents:
diff changeset
2007 start address 0x00401010
kono
parents:
diff changeset
2008
kono
parents:
diff changeset
2009 * Launch the debugger on the executable.
kono
parents:
diff changeset
2010
kono
parents:
diff changeset
2011 ::
kono
parents:
diff changeset
2012
kono
parents:
diff changeset
2013 $ gdb main.exe
kono
parents:
diff changeset
2014
kono
parents:
diff changeset
2015 * Set a breakpoint at the starting address, and launch the program.
kono
parents:
diff changeset
2016
kono
parents:
diff changeset
2017 ::
kono
parents:
diff changeset
2018
kono
parents:
diff changeset
2019 $ (gdb) break *0x00401010
kono
parents:
diff changeset
2020 $ (gdb) run
kono
parents:
diff changeset
2021
kono
parents:
diff changeset
2022 The program will stop at the given address.
kono
parents:
diff changeset
2023
kono
parents:
diff changeset
2024 * Set a breakpoint on a DLL subroutine.
kono
parents:
diff changeset
2025
kono
parents:
diff changeset
2026 ::
kono
parents:
diff changeset
2027
kono
parents:
diff changeset
2028 (gdb) break ada_dll.adb:45
kono
parents:
diff changeset
2029
kono
parents:
diff changeset
2030 Or if you want to break using a symbol on the DLL, you need first to
kono
parents:
diff changeset
2031 select the Ada language (language used by the DLL).
kono
parents:
diff changeset
2032
kono
parents:
diff changeset
2033 ::
kono
parents:
diff changeset
2034
kono
parents:
diff changeset
2035 (gdb) set language ada
kono
parents:
diff changeset
2036 (gdb) break ada_dll
kono
parents:
diff changeset
2037
kono
parents:
diff changeset
2038 * Continue the program.
kono
parents:
diff changeset
2039
kono
parents:
diff changeset
2040 ::
kono
parents:
diff changeset
2041
kono
parents:
diff changeset
2042 (gdb) cont
kono
parents:
diff changeset
2043
kono
parents:
diff changeset
2044 This will run the program until it reaches the breakpoint that has been
kono
parents:
diff changeset
2045 set. From that point you can use the standard way to debug a program
kono
parents:
diff changeset
2046 as described in (:ref:`Running_and_Debugging_Ada_Programs`).
kono
parents:
diff changeset
2047
kono
parents:
diff changeset
2048 It is also possible to debug the DLL by attaching to a running process.
kono
parents:
diff changeset
2049
kono
parents:
diff changeset
2050
kono
parents:
diff changeset
2051 .. rubric:: Attaching to a Running Process
kono
parents:
diff changeset
2052
kono
parents:
diff changeset
2053 .. index:: DLL debugging, attach to process
kono
parents:
diff changeset
2054
kono
parents:
diff changeset
2055 With ``GDB`` it is always possible to debug a running process by
kono
parents:
diff changeset
2056 attaching to it. It is possible to debug a DLL this way. The limitation
kono
parents:
diff changeset
2057 of this approach is that the DLL must run long enough to perform the
kono
parents:
diff changeset
2058 attach operation. It may be useful for instance to insert a time wasting
kono
parents:
diff changeset
2059 loop in the code of the DLL to meet this criterion.
kono
parents:
diff changeset
2060
kono
parents:
diff changeset
2061 * Launch the main program :file:`main.exe`.
kono
parents:
diff changeset
2062
kono
parents:
diff changeset
2063 ::
kono
parents:
diff changeset
2064
kono
parents:
diff changeset
2065 $ main
kono
parents:
diff changeset
2066
kono
parents:
diff changeset
2067 * Use the Windows *Task Manager* to find the process ID. Let's say
kono
parents:
diff changeset
2068 that the process PID for :file:`main.exe` is 208.
kono
parents:
diff changeset
2069
kono
parents:
diff changeset
2070 * Launch gdb.
kono
parents:
diff changeset
2071
kono
parents:
diff changeset
2072 ::
kono
parents:
diff changeset
2073
kono
parents:
diff changeset
2074 $ gdb
kono
parents:
diff changeset
2075
kono
parents:
diff changeset
2076 * Attach to the running process to be debugged.
kono
parents:
diff changeset
2077
kono
parents:
diff changeset
2078 ::
kono
parents:
diff changeset
2079
kono
parents:
diff changeset
2080 (gdb) attach 208
kono
parents:
diff changeset
2081
kono
parents:
diff changeset
2082 * Load the process debugging information.
kono
parents:
diff changeset
2083
kono
parents:
diff changeset
2084 ::
kono
parents:
diff changeset
2085
kono
parents:
diff changeset
2086 (gdb) symbol-file main.exe
kono
parents:
diff changeset
2087
kono
parents:
diff changeset
2088 * Break somewhere in the DLL.
kono
parents:
diff changeset
2089
kono
parents:
diff changeset
2090 ::
kono
parents:
diff changeset
2091
kono
parents:
diff changeset
2092 (gdb) break ada_dll
kono
parents:
diff changeset
2093
kono
parents:
diff changeset
2094 * Continue process execution.
kono
parents:
diff changeset
2095
kono
parents:
diff changeset
2096 ::
kono
parents:
diff changeset
2097
kono
parents:
diff changeset
2098 (gdb) cont
kono
parents:
diff changeset
2099
kono
parents:
diff changeset
2100 This last step will resume the process execution, and stop at
kono
parents:
diff changeset
2101 the breakpoint we have set. From there you can use the standard
kono
parents:
diff changeset
2102 approach to debug a program as described in
kono
parents:
diff changeset
2103 :ref:`Running_and_Debugging_Ada_Programs`.
kono
parents:
diff changeset
2104
kono
parents:
diff changeset
2105
kono
parents:
diff changeset
2106 .. _Setting_Stack_Size_from_gnatlink:
kono
parents:
diff changeset
2107
kono
parents:
diff changeset
2108 Setting Stack Size from ``gnatlink``
kono
parents:
diff changeset
2109 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
2110
kono
parents:
diff changeset
2111 It is possible to specify the program stack size at link time. On modern
kono
parents:
diff changeset
2112 versions of Windows, starting with XP, this is mostly useful to set the size of
kono
parents:
diff changeset
2113 the main stack (environment task). The other task stacks are set with pragma
kono
parents:
diff changeset
2114 Storage_Size or with the *gnatbind -d* command.
kono
parents:
diff changeset
2115
kono
parents:
diff changeset
2116 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
kono
parents:
diff changeset
2117 reserve size of individual tasks, the link-time stack size applies to all
kono
parents:
diff changeset
2118 tasks, and pragma Storage_Size has no effect.
kono
parents:
diff changeset
2119 In particular, Stack Overflow checks are made against this
kono
parents:
diff changeset
2120 link-time specified size.
kono
parents:
diff changeset
2121
kono
parents:
diff changeset
2122 This setting can be done with ``gnatlink`` using either of the following:
kono
parents:
diff changeset
2123
kono
parents:
diff changeset
2124
kono
parents:
diff changeset
2125 * :switch:`-Xlinker` linker option
kono
parents:
diff changeset
2126
kono
parents:
diff changeset
2127 ::
kono
parents:
diff changeset
2128
kono
parents:
diff changeset
2129 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
kono
parents:
diff changeset
2130
kono
parents:
diff changeset
2131
kono
parents:
diff changeset
2132 This sets the stack reserve size to 0x10000 bytes and the stack commit
kono
parents:
diff changeset
2133 size to 0x1000 bytes.
kono
parents:
diff changeset
2134
kono
parents:
diff changeset
2135 * :switch:`-Wl` linker option
kono
parents:
diff changeset
2136
kono
parents:
diff changeset
2137 ::
kono
parents:
diff changeset
2138
kono
parents:
diff changeset
2139 $ gnatlink hello -Wl,--stack=0x1000000
kono
parents:
diff changeset
2140
kono
parents:
diff changeset
2141 This sets the stack reserve size to 0x1000000 bytes. Note that with
kono
parents:
diff changeset
2142 :switch:`-Wl` option it is not possible to set the stack commit size
kono
parents:
diff changeset
2143 because the comma is a separator for this option.
kono
parents:
diff changeset
2144
kono
parents:
diff changeset
2145
kono
parents:
diff changeset
2146 .. _Setting_Heap_Size_from_gnatlink:
kono
parents:
diff changeset
2147
kono
parents:
diff changeset
2148 Setting Heap Size from ``gnatlink``
kono
parents:
diff changeset
2149 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
kono
parents:
diff changeset
2150
kono
parents:
diff changeset
2151 Under Windows systems, it is possible to specify the program heap size from
kono
parents:
diff changeset
2152 ``gnatlink`` using either of the following:
kono
parents:
diff changeset
2153
kono
parents:
diff changeset
2154 * :switch:`-Xlinker` linker option
kono
parents:
diff changeset
2155
kono
parents:
diff changeset
2156 ::
kono
parents:
diff changeset
2157
kono
parents:
diff changeset
2158 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
kono
parents:
diff changeset
2159
kono
parents:
diff changeset
2160 This sets the heap reserve size to 0x10000 bytes and the heap commit
kono
parents:
diff changeset
2161 size to 0x1000 bytes.
kono
parents:
diff changeset
2162
kono
parents:
diff changeset
2163 * :switch:`-Wl` linker option
kono
parents:
diff changeset
2164
kono
parents:
diff changeset
2165 ::
kono
parents:
diff changeset
2166
kono
parents:
diff changeset
2167 $ gnatlink hello -Wl,--heap=0x1000000
kono
parents:
diff changeset
2168
kono
parents:
diff changeset
2169
kono
parents:
diff changeset
2170 This sets the heap reserve size to 0x1000000 bytes. Note that with
kono
parents:
diff changeset
2171 :switch:`-Wl` option it is not possible to set the heap commit size
kono
parents:
diff changeset
2172 because the comma is a separator for this option.
kono
parents:
diff changeset
2173
kono
parents:
diff changeset
2174
kono
parents:
diff changeset
2175 .. _Win32_Specific_Addons:
kono
parents:
diff changeset
2176
kono
parents:
diff changeset
2177 Windows Specific Add-Ons
kono
parents:
diff changeset
2178 -------------------------
kono
parents:
diff changeset
2179
kono
parents:
diff changeset
2180 This section describes the Windows specific add-ons.
kono
parents:
diff changeset
2181
kono
parents:
diff changeset
2182 .. _Win32Ada:
kono
parents:
diff changeset
2183
kono
parents:
diff changeset
2184 Win32Ada
kono
parents:
diff changeset
2185 ^^^^^^^^
kono
parents:
diff changeset
2186
kono
parents:
diff changeset
2187 Win32Ada is a binding for the Microsoft Win32 API. This binding can be
kono
parents:
diff changeset
2188 easily installed from the provided installer. To use the Win32Ada
kono
parents:
diff changeset
2189 binding you need to use a project file, and adding a single with_clause
kono
parents:
diff changeset
2190 will give you full access to the Win32Ada binding sources and ensure
kono
parents:
diff changeset
2191 that the proper libraries are passed to the linker.
kono
parents:
diff changeset
2192
kono
parents:
diff changeset
2193 .. code-block:: gpr
kono
parents:
diff changeset
2194
kono
parents:
diff changeset
2195 with "win32ada";
kono
parents:
diff changeset
2196 project P is
kono
parents:
diff changeset
2197 for Sources use ...;
kono
parents:
diff changeset
2198 end P;
kono
parents:
diff changeset
2199
kono
parents:
diff changeset
2200 To build the application you just need to call gprbuild for the
kono
parents:
diff changeset
2201 application's project, here p.gpr:
kono
parents:
diff changeset
2202
kono
parents:
diff changeset
2203 .. code-block:: sh
kono
parents:
diff changeset
2204
kono
parents:
diff changeset
2205 gprbuild p.gpr
kono
parents:
diff changeset
2206
kono
parents:
diff changeset
2207 .. _wPOSIX:
kono
parents:
diff changeset
2208
kono
parents:
diff changeset
2209 wPOSIX
kono
parents:
diff changeset
2210 ^^^^^^
kono
parents:
diff changeset
2211
kono
parents:
diff changeset
2212 wPOSIX is a minimal POSIX binding whose goal is to help with building
kono
parents:
diff changeset
2213 cross-platforms applications. This binding is not complete though, as
kono
parents:
diff changeset
2214 the Win32 API does not provide the necessary support for all POSIX APIs.
kono
parents:
diff changeset
2215
kono
parents:
diff changeset
2216 To use the wPOSIX binding you need to use a project file, and adding
kono
parents:
diff changeset
2217 a single with_clause will give you full access to the wPOSIX binding
kono
parents:
diff changeset
2218 sources and ensure that the proper libraries are passed to the linker.
kono
parents:
diff changeset
2219
kono
parents:
diff changeset
2220 .. code-block:: gpr
kono
parents:
diff changeset
2221
kono
parents:
diff changeset
2222 with "wposix";
kono
parents:
diff changeset
2223 project P is
kono
parents:
diff changeset
2224 for Sources use ...;
kono
parents:
diff changeset
2225 end P;
kono
parents:
diff changeset
2226
kono
parents:
diff changeset
2227 To build the application you just need to call gprbuild for the
kono
parents:
diff changeset
2228 application's project, here p.gpr:
kono
parents:
diff changeset
2229
kono
parents:
diff changeset
2230 .. code-block:: sh
kono
parents:
diff changeset
2231
kono
parents:
diff changeset
2232 gprbuild p.gpr
kono
parents:
diff changeset
2233
kono
parents:
diff changeset
2234
kono
parents:
diff changeset
2235 .. _Mac_OS_Topics:
kono
parents:
diff changeset
2236
kono
parents:
diff changeset
2237 Mac OS Topics
kono
parents:
diff changeset
2238 =============
kono
parents:
diff changeset
2239
kono
parents:
diff changeset
2240 .. index:: OS X
kono
parents:
diff changeset
2241
kono
parents:
diff changeset
2242 This section describes topics that are specific to Apple's OS X
kono
parents:
diff changeset
2243 platform.
kono
parents:
diff changeset
2244
kono
parents:
diff changeset
2245 Codesigning the Debugger
kono
parents:
diff changeset
2246 ------------------------
kono
parents:
diff changeset
2247
kono
parents:
diff changeset
2248 The Darwin Kernel requires the debugger to have special permissions
kono
parents:
diff changeset
2249 before it is allowed to control other processes. These permissions
kono
parents:
diff changeset
2250 are granted by codesigning the GDB executable. Without these
kono
parents:
diff changeset
2251 permissions, the debugger will report error messages such as::
kono
parents:
diff changeset
2252
kono
parents:
diff changeset
2253 Starting program: /x/y/foo
kono
parents:
diff changeset
2254 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
kono
parents:
diff changeset
2255 (please check gdb is codesigned - see taskgated(8))
kono
parents:
diff changeset
2256
kono
parents:
diff changeset
2257 Codesigning requires a certificate. The following procedure explains
kono
parents:
diff changeset
2258 how to create one:
kono
parents:
diff changeset
2259
kono
parents:
diff changeset
2260 * Start the Keychain Access application (in
kono
parents:
diff changeset
2261 /Applications/Utilities/Keychain Access.app)
kono
parents:
diff changeset
2262
kono
parents:
diff changeset
2263 * Select the Keychain Access -> Certificate Assistant ->
kono
parents:
diff changeset
2264 Create a Certificate... menu
kono
parents:
diff changeset
2265
kono
parents:
diff changeset
2266 * Then:
kono
parents:
diff changeset
2267
kono
parents:
diff changeset
2268 * Choose a name for the new certificate (this procedure will use
kono
parents:
diff changeset
2269 "gdb-cert" as an example)
kono
parents:
diff changeset
2270
kono
parents:
diff changeset
2271 * Set "Identity Type" to "Self Signed Root"
kono
parents:
diff changeset
2272
kono
parents:
diff changeset
2273 * Set "Certificate Type" to "Code Signing"
kono
parents:
diff changeset
2274
kono
parents:
diff changeset
2275 * Activate the "Let me override defaults" option
kono
parents:
diff changeset
2276
kono
parents:
diff changeset
2277
kono
parents:
diff changeset
2278 * Click several times on "Continue" until the "Specify a Location
kono
parents:
diff changeset
2279 For The Certificate" screen appears, then set "Keychain" to "System"
kono
parents:
diff changeset
2280
kono
parents:
diff changeset
2281 * Click on "Continue" until the certificate is created
kono
parents:
diff changeset
2282
kono
parents:
diff changeset
2283 * Finally, in the view, double-click on the new certificate,
kono
parents:
diff changeset
2284 and set "When using this certificate" to "Always Trust"
kono
parents:
diff changeset
2285
kono
parents:
diff changeset
2286 * Exit the Keychain Access application and restart the computer
kono
parents:
diff changeset
2287 (this is unfortunately required)
kono
parents:
diff changeset
2288
kono
parents:
diff changeset
2289
kono
parents:
diff changeset
2290 Once a certificate has been created, the debugger can be codesigned
kono
parents:
diff changeset
2291 as follow. In a Terminal, run the following command:
kono
parents:
diff changeset
2292
kono
parents:
diff changeset
2293 ::
kono
parents:
diff changeset
2294
kono
parents:
diff changeset
2295 $ codesign -f -s "gdb-cert" <gnat_install_prefix>/bin/gdb
kono
parents:
diff changeset
2296
kono
parents:
diff changeset
2297 where "gdb-cert" should be replaced by the actual certificate
kono
parents:
diff changeset
2298 name chosen above, and <gnat_install_prefix> should be replaced by
kono
parents:
diff changeset
2299 the location where you installed GNAT. Also, be sure that users are
kono
parents:
diff changeset
2300 in the Unix group ``_developer``.