#pragma weak

Description

The #pragma weak directive prevents the link editor from issuing error messages if it does not find a definition for a symbol, or if it encounters a symbol multiply-defined during linking.

Syntax

Read syntax diagramSkip visual syntax diagram>>-#--pragma--weak--identifier--+----------------+-------------><
                                '-=--identifier2-'

Notes

While this pragma is intended for use primarily with functions, it will also work for most data objects.

This pragma should not be used with uninitialized global data, or with shared library data objects that are exported to executables.

Two forms of #pragma weak can be specified in your program source.

#pragma weak identifier
This form of the pragma defines identifier as a weak global symbol.

If Identifier is defined in the same compilation unit as #pragma weak identifier, identifier is treated as a weak definition. If #pragma weak exists in a compilation unit that does not use or declare identifier, the pragma is accepted and ignored.

If identifier denotes a function with C++ linkage, identifier must be specified using the C++ mangled name of the function. Also, if the C++ function is a template function, you must explicitly instantiate the template function.

#pragma weak identifier=identifier2
This form of the pragma defines identifier as a weak global symbol. References to identifier will use the value of identifier2.

Identifier2 must not be a member function.

Identifier may or may not be declared in the same compilation unit as the #pragma weak, but must never be defined in the compilation unit.

If identifier is declared in the compilation unit, identifier's declaration must be compatible to that of identifier2. For example, if identifier2 is a function, identifier must have the same return and argument types as identifier2.

Identifier2 must be declared in the same compilation unit as #pragma weak.

If identifier2 denotes a function with C++ linkage, the names of identifier and identifier2 must be specified using the mangled names of the functions. If the C++ function is a template function, you must explicitly instantiate the template function.

The compiler will ignore #pragma weak and issue warning messages if:

The compiler will ignore #pragma weak and issue a severe error message if the weak identifier is defined.

Examples

  1. The following is an example of the #pragma weak identifier form of the pragma:
    // Begin Compilation Unit 1
    #include <stdio.h>
    extern int foo;
    #pragma weak foo
    
    int main()
    {
       int *ptr;
       ptr = &foo;
       if (ptr == 0)
          printf("foo has been assigned a value of 0\n");
       else
          printf("foo was already defined\n");
    }
    //End Compilation Unit 1
    
    // Begin Compilation Unit 2
    int foo = 1;
    // End Compilation Unit 2
    
           	
    If only Compilation Unit 1 is compiled to produce an executable, identifier foo will be defined and assigned the value 0. The output from execution will be the string: "foo has been assigned a value of 0."
  2. The following is an example of the #pragma weak identifier=identifier2 form of the pragma:
    //Begin Compilation Unit 
    extern "C" void printf(char *,...);
    
    void foo1(void)
    {
       printf("Just in function foo1()\n");
    }
    
    
    #pragma weak foo__Fv = foo1__Fv
    
    int main()
    {
       foo();
    }
    //End Compilation Unit

Related information