The introduction of strong symbol and weak symbol in C language _c language

Source: Internet
Author: User

Previously in the extern "C" usage details have already mentioned the concept of symbols, it is a compiler for variables and functions of a token, the compiler for C and C + + code in the production of symbols when the rules are not the same, symbols in addition to their own name differences, there are strong symbols and weak symbols of the points

Let's look at a simple piece of code first.

Copy Code code as follows:

* TEST.C * *
void Hello ();
int main ()
{
Hello ();
return 0;
}

Obviously, this code is not able to link through, it will be an error undefined reference to Hello, said hello is undefined, because here we only declared the function hello, but did not define it. But we'll change the code a little bit as follows
Copy Code code as follows:

__ATTRIBUTE__ ((weak)) void Hello ();
int main ()
{
Hello ();
return 0;
}

Then you will find that compilation links can be passed, but the operation will be an error, because at this time we will declare hello for weak symbols, in the link when the weak symbol will be the linker as 0, the execution of an address of 0 function of course will be the error, the following code will not be an error, but it has no output
Copy Code code as follows:

__ATTRIBUTE__ ((weak)) void Hello ();
int main ()
{
if (hello)
Hello ();
return 0;
}

The compiler considers that the function and initialized global variables are strong symbols, uninitialized global variables are weak symbols, and the linker has the following rules for handling strong and weak symbols

1. Strong symbols with the same name are not allowed in different target files
2. If a symbol is a strong symbol in a target file, a weak symbol in the other target file, select the strong symbol
3. If a symbol is a weak symbol in all target files, choose to occupy the most space, such as the target file a double Global_var, file B has an int global_var,double occupy 8 bytes, greater than 4 bytes int, A and B after the link, Symbol Global accounts for 8 bytes

For this we can simply verify that there are the following two files

Copy Code code as follows:

/* 1.c * *
Char Global_var;
int main ()
{
return 0;
}

/* 2.C * *
int Global_var;

Global variable Global_var is not initialized in two files, so it is weak symbol, execute compile command gcc 1.c 2.c, readelf view symbol table Readelf-s a.out, we only output the last few lines in order to view the convenience

Copy Code code as follows:

Num:value Size Type Bind Vis Ndx Name
62:0000000000600818 4 OBJECT GLOBAL DEFAULT Global_var
63:0000000000400474 FUNC GLOBAL DEFAULT Main
64:0000000000400358 0 FUNC GLOBAL DEFAULT _init

Here the symbol Global_var occupies a size of 4, which means that the linker chooses to occupy a larger space of int Global_var, we modify it slightly to initialize the global variable in 1.c, as follows

Copy Code code as follows:

/* 1.c * *
char Global_var = 1;
int main ()
{
return 0;
}

/* 2.C * *
int Global_var;

At this time, the Global_var in 1.c is a strong symbol, 2.c Global_var is a weak symbol, the same compiled readelf view symbol table Readelf-s a.out as follows

Copy Code code as follows:

Num:value Size Type Bind Vis Ndx Name
62:0000000000600818 1 OBJECT GLOBAL DEFAULT Global_var
63:0000000000400474 FUNC GLOBAL DEFAULT Main
64:0000000000400358 0 FUNC GLOBAL DEFAULT _init

At this point the symbol Global_var occupies a size of 1, indicating that the linker selected a strong symbol

When writing code, you should try to avoid different types of symbols, otherwise it will cause very strange and imperceptible errors, in order to avoid the following measures can be taken:

1. The best policy: eliminate all global variables
2. China strategy: Declare global variables as static types and provide interfaces for access
3. The worst: Global variables must be initialized, even if initialized to 0
4. Prerequisite: Open gcc-fno-common option, it will prohibit the different types of symbols

Said so much, as if to say that should be used as strong symbols, what is the use of weak symbols, the so-called existence is reasonable, sometimes we even need to display the definition of weak symbols, which is very useful for library functions, such as the weak symbols in the library can be user-defined strong symbol coverage, thereby implementing a custom library version, Or in the use of certain extended functions, users can define a weak symbol, when linked to the function, the function module can be used normally, if the function module is removed, the program can also be normal links, just lack of some features, such as we can use the following code to determine whether the program links the Pthread library, To determine what action to perform

Copy Code code as follows:

* TEST.C * *
#include <stdio.h>
#include <pthread.h>

__ATTRIBUTE__ ((weak)) int pthread_create (
pthread_t*,
Const pthread_attr_t*,
void* (*) (void*),
void*);

int main ()
{
if (pthread_create)
{
printf ("This is Multi-thread version!\n");
}
Else
{
printf ("This is Single-thread version!\n");
}
return 0;
}

The results of the compilation run are as follows

Copy Code code as follows:

$ gcc test.c
$./a.out
This is Single-thread version!
$ gcc Test.c-lpthread
$ a.out
This is Multi-thread version!

Related Article

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.