In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
I would like to share with you the example analysis of linker in C language. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
1 what is a linker
A typical linker integrates several target modules generated by a compiler or assembler into an entity called a load module or executable-an entity that can be executed directly by the operating system.
The linker usually treats the target module as a set of external objects. Each external object represents a part of the machine's memory and is identified by an external name. Therefore, every function and every external variable in the program is an external object if it is not declared as static. Some C compilers change the names of static functions and static variables as external objects. Because of the "name modification", they do not conflict with functions or variables with the same name in other source program files.
2 declare and define extern int a
The above code is not a definition of a, but rather an external integer variable.
Note: after the introduction, if the introduced location is outside the function, it is equivalent to defining a global variable at that location, which also follows the principle of local variable priority. If the introduced location is within a function, it is equivalent to a local variable. The scope is similar to the local variable defined in that place, and it is meaningless to discuss the declaration period here.
Int an Extrinsn int a
The above two statements can be in the same source file or in different source files of the program.
Note: each external variable can only be defined once. = = if multiple definitions of an external variable specify an initial value, for example:
Int a = 7
Appears in a source file, while
Int a = 9
When it appears in another source file, most systems refuse to accept the program. However, if an external variable is defined in multiple source files without specifying an initial value, then * * some systems will accept the program, while others will not. * * therefore, each external variable must be defined only once.
3 naming conflict 3.1 naming conflict
If the definition is included in two different source files
Int a
Then it either indicates a program error (if the linker system external variables are named repeatedly), or shares the same instance of an in two source files (regardless of whether the external variables in the two source files should be shared or not). The same process is done even if one of the definitions of an appears in the library file provided by the system.
3.2 static modifier static int a
After static modifies a, the scope of an is limited to one source file. For other source files, an is invisible and can no longer be referenced by extern. Of course, static also applies to functions. After using static, we can define a variable or function with the same name as the one that has been modified by static in other source files.
4 formal parameter, actual parameter, return value
If the function we are using is not declared but has been defined later, the default function return type is int, which can have extremely serious consequences.
If the function used is not defined before use or may be in another file, it is declared that the purpose of the function declaration is to tell the compiler the type of return value of the function.
Note: if a function does not have parameters of type float, short, or char, you can omit the description of the parameter type in the function declaration (note that the parameter type description cannot be omitted in the function definition). This approach relies on the caller's ability to provide the correct number and type of arguments. Here, "appropriate" does not mean "equivalent": parameters of type float are automatically converted to type double, and parameters of type short or char are automatically converted to type int.
Before the release of the ANSI C standard, there was often the following way to declare and define functions:
Int isvowel (); / / the way the function is declared int isvowel (c) char c; {return c = ='a';}
In fact, the above writing is equivalent to the following:
Int isvowel (int I) {char c; return caterpillars are available;}
Both of the above methods are supported in VS2019.
Look at the following example:
# includeint main () {int i; char c; for (I = 0; I)
< 5; i++) { scanf("%d", &c); printf("%d ", i); } printf("\n"); return 0;} 表面上,这个程序从标准输入设备读入5个数,在标准输出设备设备上写5个数: 0 1 2 3 4 实际上,这个程序并不一定得到上面的结果。例如,在某个编译器上,它的输出是(当然,在VS2019环境下程序会崩溃,因为非法修改了内存空间) 0 0 0 0 0 1 2 3 4 为什么呢?问题的关键在于,这里的c被声明为char类型,而不是int类型。如果程序要求scanf读入一个整数,应该传递给他一个指向整数的指针。而程序中scanf函数得到的却是一个指向字符的指针,scanf函数并不能分辨这种情况,它只是将这个指向字符的指针作为指向整数的指针而接受,并且在指针指向的位置存储一个整数。因为整数所占的存储空间要大于字符所占的存储空间,所以字符c附近的内存被覆盖。 字符c附近的内存中存储的内容是由编译器决定的,在本例中它所存放的是整数i的低端部分。因此,每次读入一个数值到c时,都会将i的低端部分覆盖为0,而i的高端部分本来就是0,相当于i每次被重新设置为0,循环将一直进行。当到达文件的结束位置后,scanf函数不再试图读入新的值到c。这时,i才可以正常的运行,最后终止循环。 5 检查外部类型 注意:保证一个特定类型的所有外部定义在每个目标模块中都有相同的类型,"相同的类型"也应该是严格意义上的相同。 例如,在一个文件中包含定义: char filename[] = "/etc/passwd"; 而在另一个文件中包含声明: extern char *filename; 在定义时,filename是一个字符数组的名称。尽管在一个语句中引用filename的值将得到指向该数组起始元素的指针,但是filename的类型是"字符数组",而不是字符指针。在第二个声明中,filename被确定为一个指针。这两种方式使用存储空间的方式是不同的,它们无法以一种合乎情理的方式共存。第一个例子字符数组filename的内存布局如下图所示:The second way, the memory layout of the character pointer filename is shown in the following figure:
The modification method is shown in the following figure:
Char filename [] = "/ etc/passwd"; extern char filename []
You can also modify it like this:
Char*filename = "/ etc/passwd"; extern char*filename; 6 header files
Note: each external object is declared in only one place, this declaration is usually in the header file, and all modules that need to use the external object should also be included in this header file. In particular, the module that defines the external object should also include this header file.
The above is all the content of this article "sample Analysis of Linker in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.