Weak references and strong references

Source: Internet
Author: User

If the target file strongly references the symbols of the external target file, but cannot be correctly resolved when it is linked to an executable file (look at p_51 for a strange name), the linker will report an error, this is for strong reference.

Correspondingly, there is also a weak reference (weak reference). When dealing with weak references, if the symbol is defined, the linker will determine the reference of the symbol, if this symbol is not defined, the linker will not report an error for this reference.

The process of handling strong references is almost the same as that of weak references. However, for undefined weak references, the linker does not regard it as an error. Generally, for undefined weak references, the default value of the linker is 0, or a special value, so that the program code can recognize it.

Weak references and symbols are mainly used in the database link process.
In GCC, the extension keyword _ attribute _ (weakref) is used to declare a weak reference to an external function. For example, the following code:

_ Attribute _ (weakref) void Foo ();

Int main ()
{
Foo ();
}

We can compile it into an executable file. GCC will not report a link error, but an error will occur when running this executable file. Because when the main function tries to call the foo function,
The address of the foo function is 0, so an Invalid Address Access error occurs. An example of improvement is:
_ Attrube _ (weakref) void Foo ();

Int main ()
{
If (FOO)
Foo ();

Return 0;
}

Weak symbols can be overwritten by user-defined strong symbols, so that the program can use the library functions of the custom version;
Library functions can be declared as weak references to some extension function modules. When we link the extension module with the program, the function module can be used normally; if we remove some functional modules,
The program can also be properly linked, but the corresponding functions are missing, which makes the program functions easier to crop and combine.

Appendix:
In Linux programming, if a program is designed to support a single thread or multi-thread mode, the weak reference method can be used to determine whether the current program is linked to the single-threaded glibc library or
Multi-threaded glibc library, so as to execute a single-threaded version of the program or multi-threaded version of the program. We can define a weak reference of the pthread_create function in the program, and then the program is dynamic at runtime.
Determine whether to link to the pthread library and decide whether to execute the multi-thread version or the single-thread version:

1 # include <stdio. h>
2 # include <pthread. h>
3
4 _ attribute _ (weak) int pthread_create (pthread_t *, const pthread_attr_t *, void * (*) (void *), void *);
5
6 int main (void)
7 {
8 If (pthread_create)
9 {
10 printf ("this is multi-thread version! \ N ");
11 // run the multi-thread version
12 // main_multi_thread ()
13}
14 else
15 {
16 printf ("this is single-thread version! \ N ");
17 // run the single-thread version
18 // main_single_thread ()
19}
20 return 0;
21}

GCC pthread. C-o PT

 

GCC pthread. C-lpthread-O PT

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.