In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the repeated definition of function or global variable". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn how to understand the repeated definition of function or global variable.
Maybe some friends' first reaction is, it must be compiled but:
/ / Source: official account [programming Zhuji] / / author: Mr. Shouwang / / fun.c # include void fun () {printf ("programming Zhuji\ n");} / / main.c # include void func () {printf ("official account\ n");} int main (void) {func (); return 0;}
Compile:
$gcc-o main main.c fun.c / tmp/ccKeACRk.o: In function `fun': fun.c: (.text + 0x0): multiple definition of `fun' / tmp/cc4ezgqh.o:main.c: (.text + 0x0): first defined here collect2: error: ld returned 1 exit status
You can see that there is an error here because fun duplicates the definition.
But if you repeat the definition, you will get an error. Will you compile it? Not exactly!
Take a look at the following code:
/ / Source: official account [programming Zhuji] / / author: Mr. Watch / / var.c int num; void change () {num = 1023;} / / main.c # include void change (); int num = 1024; int main (void) {printf ("before:num is% d\ n", num); change (); printf ("after:num is% d\ n", num); return 0;}
Output result:
Before:num is 1024 after:num is 1023
As you can see from the results, although num is defined twice, it can still be compiled and run normally. And why is that?
Symbol
Before we explain what we're going to share today, let's take a brief look at what symbols are. As mentioned in "how hello programs become executable files," the final stage of ELF file generation goes through links, and the link phase is based on symbols. Each target file has a symbol table. The linking process is to "glue" different target files together through the symbols in the symbol table to form the final library or executable file. It is also easy to view the symbolic information of a target file:
/ / symbol.c # include int symbol = 1024; int func_symbol () {return 0;}
Compile:
$gcc smbol.c # compile $nm symbol.o # View symbol information 0000000000000000 T func_symbol 00000000000000 D symbol
You can view the symbol information through the nm command, and here are the symbols for our func_symbol function and the global variable symbol.
About the use of nm, it is also introduced in "several commands to know the Secrets of ELF Files".
In addition to the global symbols mentioned above, there is other symbolic information in the target file, but this is not the focus of this article.
Strong symbols and weak symbols
The compiler default function and initialized global variables are strong symbols and the uninitialized global variables are weak symbols for the C _ language. Of course, it can also be passed.
_ _ attribute__ ((weak))
To define a strong symbol as a weak symbol.
Use the following example to see which are strong symbols and which are weak symbols:
# include int weak; / / uninitialized global variable, weak symbol int strong = 1024; / / global variable initialized, strong symbol _ _ attribute__ ((weak)) int weak1 = 2222; / / use weak symbol int main (void) {printf ("programming Zhuji\ n"); return 0;}
Note that both strong and weak symbols here are for definitions.
Which one do you use when you have the same name?
For multiple definitions, that is, when the variable mentioned in the title has the same name, the linker has its own processing rules:
1. Strong symbols are not allowed to repeat
two。 There is a strong symbol and multiple weak symbols, using strong symbols
3. If there are multiple weak symbols, choose one at random.
On the first point, as you have seen in the initial example, the most common situation is that you repeatedly define variables, functions, and so on.
In the second point, there is also an example. In the example, although two num are defined, the uninitialized num in var.c is a weak symbol and the num in main.c is a strong symbol. In this case, the compilation is normal. It's just a num that ends up using strong symbols.
The third example is similar, when the num of main.c is not initialized, it can also be compiled. In this case, the misuse is enough. If it is a duplicate symbol, but the type is different, the problem is even greater, that is, the content of var.c is as follows:
/ / var.c double num; void change () {num = 1023;}
The num here becomes double, compile and run again, and you will find unexpected results:
Before:num is 1024 after:num is 0
Why is it 0 after modification? The reason is that the data storage format of the double type is different from that of the int type, and they both occupy different lengths of space. In this example, double takes 8 bytes and int 4 bytes.
In short, this is not the result we want, and the final consequences may be more serious than we thought and more difficult to find.
Thank you for your reading. the above is the content of "how to understand the repeated definition of function or global variable". After the study of this article, I believe you have a deeper understanding of how to understand the repeated definition of function or global variable. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.