当前位置:网站首页>Extern keyword
Extern keyword
2022-07-03 10:49:00 【Humboldt】
Catalog
1 Functional specifications
extern It can be placed in front of variables or functions , Used to identify variables or functions Definition In other documents , Prompt the compiler to look for definitions in other modules when encountering this variable and function . Such as adding extern, Declare only functions or variables in the header file , And in the .c File for the definition of functions .
2 Add before function or variable extern
2.1 introduces
A common way is to add extern, And then in .C First protect the header file in the file , Then define .
main.c file
1 #include <stdio.h>
2 #include "exter.h"
3 #include "exter2.h"
4 int main()
5 {
6 int num1 = 10;
7 int num2 = 20;
8 int num3 = add(num1, num2);
9 printf("sum = %d\n", num3); //sum = 30
10
11 num = 200;
12 printf("now num = %d\n", num); //now num = 200
13
14 multi(num,num2);
15
16 }
exter.h file
1 #ifndef EXTER_H
2 #define EXTER_H
3
4 #include <stdio.h>
5
6 extern int num;
7 extern int add(int a, int b);
8
9 #endif
exter.c file
1 #include "exter.h"
2
3 int num = 120;
4
5 int add(int a, int b)
6 {
7 printf("into exter.c and num = %d\n", num);
8
9 int c = a + b;
10 return c;
11 }
exter2.h
1 #ifndef EXTER2_H
2 #define EXTER2_H
3
4 extern int multi(int a, int b);
5
6 #endif
exter2.c
1 #include "exter2.h"
2 #include "exter.h"
3
4 int multi(int a, int b)
5 {
6 printf("exter2 now num = %d\n",num);
7 printf("a * b = %d\n",a * b); //a * b = 4000
8 }
Such as some above .h Through the file extern Keywords decorate functions or variables , Then the modified functions and variables are in main.c Use... In the document . Here's the variable num stay exter.h Declaration in the document , stay exter.c Assignment in file , stay main.c Revision in China , stay exter2.c Use in . Modified variable num The value of has changed , In the later use, the new value is used .
2.2 The build process
adopt gcc Compile check the compilation process and you will find , Pass in a file include Mode contains another file , In the preprocessing stage, the contents of the included header file will be included in the current file , So add extern It will be included once .gcc You can refer to gcc The compiling process and linking principle of the program
Here's how it works : gcc -E -o main.i main.c Later obtained main.i The form in the document .
837 # 2 "main.c" 2
838 # 1 "exter.h" 1
839
840
841
842
843
844 extern int num;
845 extern int add(int a, int b);
846 # 3 "main.c" 2
847 # 1 "exter2.h" 1
848
849
850
851 extern int multi(int a, int b);
852 # 4 "main.c" 2
853
854 int main()
855 {
856 int num1 = 10;
857 int num2 = 20;
858 int num3 = add(num1, num2);
859 printf("sum = %d\n", num3);
860
861 num = 200;
862 printf("now num = %d\n", num);
863
864 multi(num,num2);
865
866 }
The above is pretreated main.i file , It can be seen that it is equivalent to main The function is declared before num Variables and add function
1.2 Add before function or variable extern C
Pass the same gcc Display the results of the compilation process *.o It can be seen from the document that extern C With or without extern C The difference between .
Get the assembly file :gcc -c -o main.o main.c
View assembly file :nm main.o
[[email protected] extern]$ gcc -c -o main.o main.c
[[email protected] extern]$ nm main.o
U add
0000000000000000 T main
U multi
U num
U printf
边栏推荐
- [untitled] numpy learning
- 神经网络入门之模型选择(PyTorch)
- Windows security center open blank
- Numpy quick start (IV) -- random sampling and general functions
- 【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
- Introduction to deep learning linear algebra (pytorch)
- MySQL -- index principle + how to use
- Unity learning notes: online game pixel Adventure 1 learning process & error correction experience
- MAUI Developer Day in GCR
- Redis 笔记 01:入门篇
猜你喜欢
Unity小组工程实践项目《最强外卖员》策划案&纠错文档
8、 Transaction control language of MySQL
Set ArrayList nested map set loop traversal
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
[untitled]
Jupiter notebook changing font style and font size
Ut2012 learning notes
Redis 笔记 01:入门篇
深度学习入门之线性代数(PyTorch)
Hou Jie -- STL source code analysis notes
随机推荐
Win10系统下提示“系统组策略禁止安装此设备”的解决方案(家庭版无组策略)
MAUI Developer Day in GCR
UI interface design related knowledge (I)
Detailed cross validation and grid search -- sklearn implementation
神经网络入门之矩阵计算(Pytorch)
神经网络入门之模型选择(PyTorch)
Take you into the cloud native database industry, Amazon Aurora
QT:QSS自定义 QProgressBar实例
Leetcode skimming ---283
Leetcode skimming ---278
C语言项目:学生成绩系统
Traversal of map set
Unity小组工程实践项目《最强外卖员》策划案&纠错文档
How does MySQL find the latest data row that meets the conditions?
QT:QSS自定义QHeaderView实例
Nuget add reference error while installing packages
Jupiter notebook changing font style and font size
[untitled]
[untitled]
MySQL reports an error "expression 1 of select list is not in group by claim and contains nonaggre" solution