How To - Creating DLLs with GNAT


Building dlls with gnat 311 can be done as follows:
* Set up a .def file that contains the exported routine
* Create ada code for those routines. Realise that most things
  in windows use stdcall calling convention for calling routines in
  a dll, so if required use a pragma export(stdcall,...)
* In you code also create a routine called dllmain as follows:
    function DllMain (A, B, C : Integer) return Integer;
    pragma Export (StdCall, DllMain, "DllMain");
    function DllMain (A, B, C : Integer) return Integer is
    begin
       return 1;
    end DllMain;
  The routine is called when the dll initialises. Usually it is not
  save to call Ada code, becuase elaboration is not done at the moment the
  routine is called.
  Do not call the ada elaboration routine adainit from it, because it will
  hang when any tasking is involved in your code.
* Import the routine Adainit as follows:   
      procedure AdaInit;
      pragma Import (C, AdaInit);
  Call it one of the exported routines before any elaboration of the
  ada code is required.
  Usually it is best to add a routine in the dll interface for doing this.
  Note that does not matter to call this routine more then once.
  The next time you call it has no effect (see generated code from gnatbind)
After that you can build you dll with something like:

gnatmake -g -O2 gui_item_dispatch
gcc -c -I./ -g -O2 -I- d:\Embed\General\vif-items-vb.adb
gnatbind -n -x gui_item_dispatch.ali
gnatlink -g -mdll -o gui_item_dispatch.dll gui_item_dispatch.ali
   -Xlinker --base -file=gui_item_dispatch.base -Xlinker -s
dlltool --dllname gui_item_dispatch.dll
   --def d:\Embed\General\gui_item_dispatch.def
   --base-file gui_item_dispatch.base --output-exp gui_item_dispatch.exp
gnatlink -g -mdll -o gui_item_dispatch.dll gui_item_dispatch.ali
   -Xlinker --base-file=gui_item_dispatch.base
   gui_item_dispatch.exp -Xlinker -s
dlltool --dllname gui_item_dispatch.dll
   --def d:\Embed\General\gui_item_dispatch.def
   --base-file gui_item_dispatch.base --output-exp gui_item_dispatch.exp
gnatlink -g -mdll -o gui_item_dispatch.dll gui_item_dispatch.ali
   gui_item_dispatch.exp -Xlinker -s

This generates a relocatable dll without debug information.
If you want a dll with debug info then remove the 
-Xlinker -s" from the linker.
Alway use the -g switch on gnatlink, otherwise it will delete the
binder file. You do not want that because it is used multiple times.

Contributed by: Wiljan Derks
Contributed on: January 26, 1999
License: Public Domain
Back