Building DLLs with GNAT (GNAT User’s Guide for Native Platforms)

#### 7.4.8.11 Building DLLs with GNAT ¶

This section explains how to build DLLs using the GNAT built-in DLL support. With the following procedure, it is straightforward to build and use DLLs with GNAT.

- Building object files. The first step is to build all objects files that are to be included into the DLL. This is done using the standard `gnatmake` tool.
- Building the DLL. To build the DLL, you must use the `gcc` `-shared` and `-shared-libgcc` switches. It’s quite simple to use this method:

      $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...

  It’s important to note that in this case all symbols found in the object files are automatically exported. You can restrict the set of symbols to export by passing to `gcc` a definition file (see The Definition File). For example:

      $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...

  If you use a definition file, you must export the elaboration procedures for every package that requires one. Elaboration procedures are named using the package name followed by “_E”.

- Preparing DLL to be used. For the DLL to be used by client programs, the bodies must be hidden from it and the `.ali` set with read-only attribute. This is very important because otherwise GNAT will recompile all packages and will not actually use the code in the DLL. For example:

      $ mkdir apilib
      $ copy *.ads *.ali api.dll apilib
      $ attrib +R apilib\\*.ali

At this point, you can use the DLL by directly linking against it. Note that you must use the GNAT shared runtime when using GNAT shared libraries. You do this with the `-shared` binder switch.

>     $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI