Weak symbol

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computing, a weak symbol is a symbol definition in an object file or dynamic library that may be overridden by other symbol definitions. Its value will be zero if no definition is found by the loader.

Weak symbols are not mentioned by C or C++ language standards; as such, inserting them into code is not portable. Some compilers can create a weak symbol with a special #pragma, #pragma weak. However, the GNU Compiler Collection also supports a __attribute__((weak)) syntax as in the example below.

[edit] Example

Example written for the GNU Compiler Collection

Source of libfoo.so :

 extern void foo(void) __attribute__((weak));
 void fun(void)
 {
   if (foo) foo();
 }

weak_test.c:

 #include <stdio.h>
 
 void fun(void);
 
 void foo(void)
 {
   printf("Hello!\n");
 }
 
 int main()
 {
   fun();
   return 0;
 }

Compile with :

gcc weak_test.c -o weak_test -lfoo

[edit] External References

[1]

[edit] See also




Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export