当前位置:网站首页>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
边栏推荐
- 2021-09-22
- Unity learning notes: personal learning project "crazy genius Edgar" error correction document
- Type de contenu « Application / X - www - form - urlencoded; Charset = utf - 8 'not supported
- Leetcode skimming ---367
- Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得
- 如何在游戏中制作一个血条
- Flink -- 内置函数(ALL)
- A detailed explanation of vector derivative and matrix derivative
- Numpy quick start (IV) -- random sampling and general functions
- CSDN, I'm coming!
猜你喜欢

Unity learning notes: online game pixel Adventure 1 learning process & error correction experience

Automatic derivation of introduction to deep learning (pytoch)

Ind kwf first week

QT:QSS自定义 QScrollBar实例

使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

Weight decay (pytorch)

ThreadLocal principle and usage scenario

MySql 怎么查出符合条件的最新的数据行?

The story of a 30-year-old tester struggling, even lying flat is extravagant

Iterator iterator enhances for loop
随机推荐
Wechat applet training 2
QT:QSS自定义 QMenuBar实例
Mysql--索引原理+如何使用
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
How does MySQL find the latest data row that meets the conditions?
Interviewer: what is the internal implementation of the list in redis?
Practical part: conversion of Oracle Database Standard Edition (SE) to Enterprise Edition (EE)
conda9.0+py2.7+tensorflow1.8.0
QT:QSS自定义 QTabWidget 和 QTabBar实例
QT:QSS自定义 QTreeView实例
Leetcode skimming ---704
Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
Set ArrayList nested map set loop traversal
Data captured
Jetson TX2 brush machine
Leetcode skimming ---44
分组函数之rollup、cube函数、grouping sets函数
大型电商项目-环境搭建
Unity小组工程实践项目《最强外卖员》策划案&纠错文档
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported