Next: , Previous: Pragma Warnings, Up: Implementation Defined Pragmas


2.191 Pragma Weak_External

Syntax:

    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
      begin
        return key'Address /= System.Null_Address;
      end Present;
    end External_Module;