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

#### 7.4.8.7 Using DLLs with GNAT ¶

To use the services of a DLL, say `API.dll`, in your Ada application you must have:

- The Ada spec for the routines and/or variables you want to access in `API.dll`. If not available, you must build this Ada spec from the C/C++ header files provided with the DLL.
- The import library (`libAPI.dll.a` or `API.lib`). As previously mentioned, an import library is a statically linked library containing the import table, which is filled at load time to point to the actual `API.dll` routines. Sometimes you don’t have an import library for the DLL you want to use. The following sections will explain how to build one. Note that this is optional.
- The actual DLL, `API.dll`.

Once you have all the above, to compile an Ada application that uses the services of `API.dll` and whose main subprogram is `My_Ada_App`, you simply issue the command

>     $ gnatmake my_ada_app -largs -lAPI

The argument `-largs -lAPI` at the end of the `gnatmake` command tells the GNAT linker to look for an import library. The linker will look for a library name in this specific order:

- `libAPI.dll.a`
- `API.dll.a`
- `libAPI.a`
- `API.lib`
- `libAPI.dll`
- `API.dll`

The first three are the GNU-style import libraries. The third is the Microsoft-style import libraries. The last two are the actual DLL names.

Note that if the Ada package spec for `API.dll` contains the following pragma

>     pragma Linker_Options ("-lAPI");

you do not have to add `-largs -lAPI` at the end of the `gnatmake` command.

If any one of the items above is missing, you will have to create it yourself. The following sections explain how to do so using as an example a fictitious DLL called `API.dll`.