Search code examples
librariesadasymbolselaboration

Ada library initialisation/elaboration and GPR directives : can't find elaboration symbol


I am trying to produce an Ada library for iOS. However, it is necessary to perform the Ada elaboration manually.

I know that the compiler can produce an init symbol, that can be later imported and used. However, with the following GPR definition, it is not produced (the nm command does not list it). The naming is supposed to be <libname>init with <libname> corresponding to the value defined in the GPR directive Library_Name

The GPR is defined in the following fashion (this one is windows/style -see DLL references-, but the problems also applies when producing for iOS on a Mac):

project adalib is
    for Languages use ("Ada");
    for Source_Dirs use (project'Project_Dir & "./src");
    for Library_Kind use "static"; --"static" on iOS will produce a .a file
    for Library_Name use project'Name; -- will produce "libadalib.a"
    for Library_Dir use project'Project_Dir & "./lib";
    for Library_Src_Dir use project'Project_Dir & "./includes";
    -- define your favorite compiler, builder, binder, linker options
end adalib;

I'm missing it : how to produce that symbol ?


Solution

  • I found the solution. My GPR was missing this simple directive:

        for Library_Interface use ("mypackage"); -- put whatever packages you want to expose, without .adb/.ads since we're talking about packages
    

    With the above directive, I can find the adalibinit symbol via nm command. When I import it in my ada code, I can also use it, see :

    package body mypackage is
        procedure Init_My_Lib
        is
           -- I want to call elaboration;
           pragma import (C, ada_elaboration, "adalibinit");
        begin
           ada_elaboration;
           -- further code
        end Init_My_Lib;
    -- rest of package
    

    So, the full GPR should be:

    project adalib is
        for Languages use ("Ada");
        for Source_Dirs use (project'Project_Dir & "./src");
        for Library_Kind use "static"; -- will produce a .a file
        for Library_Name use project'Name; -- will produce "libadalib.a"
    
        for Library_Interface use ("mypackage"); -- <=== THIS IS HERE
    
        for Library_Dir use project'Project_Dir & "./lib";
        for Library_Src_Dir use project'Project_Dir & "./includes";
        -- define your favorite compiler, builder, binder, linker options
    end adalib;