Previous: Pragma Warnings, Up: Implementation Defined Pragmas

Pragma Weak_External


     pragma Weak_External ([Entity =>] local_NAME);

local_NAME must refer to an object that is declared at the library level. This pragma specifies that the given entity should be marked as a weak symbol for the linker. It is equivalent to __attribute__((weak)) in GNU C and causes local_NAME to be emitted as a weak symbol instead of a regular symbol, that is to say a symbol that does not have to be resolved by the linker if used in conjunction with a pragma Import.

When a weak symbol is not resolved by the linker, its address is set to zero. This is useful in writing interfaces to external modules that may or may not be linked in the final executable, for example depending on configuration settings.

If a program references at run time an entity to which this pragma has been applied, and the corresponding symbol was not resolved at link time, then the execution of the program is erroneous. It is not erroneous to take the Address of such an entity, for example to guard potential references, as shown in the example below.

Some file formats do not support weak symbols so not all target machines support this pragma.

     --  Example of the use of pragma Weak_External
     package External_Module is
       key : Integer;
       pragma Import (C, key);
       pragma Weak_External (key);
       function Present return boolean;
     end External_Module;
     with System; use System;
     package body External_Module is
       function Present return boolean is
         return key'Address /= System.Null_Address;
       end Present;
     end External_Module;