Weak symbol
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, a weak symbol is a symbol definition in an object file
Object file
An object file is a file containing relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker....

 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
Directive (programming)
In computer programming, the term directive is applied in a variety of ways that are similar to the term command. It is also used to describe some programming language constructs ....

, #pragma weak. However, the GNU Compiler Collection
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

 also supports a __attribute__((weak)) syntax as in the example below.

Example

Example written for the GNU Compiler Collection
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...



Source of libfoo.so :

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


weak_test.c:

#include

void fun(void);

void foo(void)
{
printf("Hello!\n");
}

int main
{
fun;
return 0;
}


Compile with :
gcc weak_test.c -o weak_test -lfoo

See also

  • Link editor
  • Program loader
  • Dynamic linking
  • Weak reference
    Weak reference
    In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector . An object referenced only by weak references is considered unreachable and so may be collected at any time...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK