#pragma weak name1 [= name2] |
Use weak to define a weak global symbol. This pragma is used mainly in source files for building libraries. The linker does not warn you if it cannot resolve a weak symbol.
The weak pragma can specify symbols in one of two forms:
String form. The string must be the mangled name for a C++ variable or function. The behavior for an invalid mangled name reference is unpredictable. The back end may or may not produce an error for invalid mangled name references. Regardless of whether it produces an error, the behavior of the back end when invalid mangled names are used is unpredictable.
Identifier form. The identifier must be an unambiguous identifier for a C++ function that was previously declared in the compilation unit. The identifier form cannot be used for variables. The front end (ccfe) will produce an error message if it encounters an invalid identifier reference.
In the form #pragma weak name, the directive makes name a weak symbol. The linker will not complain if it does not find a symbol definition for name. It also does not complain about multiple weak definitions of the symbol. The linker simply takes the first one it encounters.
If another compilation unit has a strong definition for the function or variable, name will be linked to that. If there is no strong definition for name, the linker symbol will have a value of 0.
The following directive defines ping to be a weak symbol. No error messages are generated if the linker cannot find a definition for a symbol named ping.
#pragma weak ping |
In the form #pragma weak name1 = name2, the symbol name1 becomes a weak reference to name2. If name1 is not defined elsewhere, name1 will have the value name2. If name1 is defined elsewhere, the linker uses that definition and ignores the weak reference to name2. The following directive instructs the linker to resolve any references to bar if it is defined anywhere in the program, and to foo otherwise.
#pragma weak bar = foo |
In the identifier form, name2 must be declared and defined within the current compilation unit. For example:
extern void bar(int) {...} extern void _bar(int); #pragma weak _bar=bar |
When you use the string form, the symbol does not need to be previously declared. If both _bar and bar in the following example are extern "C", the functions do not need to be declared. However, bar must be defined in the same object.
extern "C" void bar(int) {...} #pragma weak "_bar" = "bar" |
When you use the identifier form, there must be exactly one function with the specified name in scope at the pragma location. Attempting to use the identifier form of #pragma weak with an overloaded function is an error. For example:
int bar(int); float bar(float); #pragma weak bar // error, ambiguous function name |
To avoid the error, use the string form, as shown in the following example.
int bar(int); float bar(float); #pragma weak "__1cDbar6Fi_i_" // make float bar(int) weak |
See the Solaris Linker and Libraries Guide for more information.