当前位置:网站首页>Principle and usage of extern
Principle and usage of extern
2022-07-06 17:53:00 【TABE_】
Here's the catalog title
extern It can be used to specify links ; It can also be placed in front of variables or functions , A function defined in a file or other variable , Prompt the compiler to look for its definition in other modules when it encounters this variable and function .
Say something reasonable. , It has the following two functions :
- And “C” Continuous use , Such as extern “C” void fun(); Tell the compiler to press C The rules to translate
- Global variable or function declaration , Its declared variables and functions can be found in other modules ( file ) Use in , Be careful , This is just a statement, not a definition , The specific definition should be completed in the specific module
And “C” Continuous use ( Link assignment )
stay CPP You can often see code in the following form in the file :
#ifdef __cplusplus
extern "C" {
#endif
/**** some declaration or so *****/
#ifdef __cplusplus
}
#endif /* end of __cplusplus */
that , What's the use of this way of writing ? actually , This is to make CPP Can and C Interface . The reason for this is , It is because of some differences between the two languages .
because CPP Support polymorphism , That is, functions with the same function name can complete different functions ,CPP It is usually through parameters to distinguish which function is called . At compile time ,CPP The compiler concatenates the parameter type with the function name , So after the program is compiled into the object file ,CPP The compiler can directly link multiple object files into one object file or executable file according to the symbol name in the object file . But in C In language , Because there is no concept of polymorphism ,C When compiling, the compiler will add an underscore in front of the function name , Do nothing ( At least that's what many compilers do ). For this reason , When CPP And C Mixed programming , There could be problems .
Suppose such a function is defined in a header file :
int foo(int a, int b);
The implementation of this function is located in a .c In file , meanwhile , stay .cpp The function is called in the file. . that , When CPP When the compiler compiles this function , It is possible to change the function name to _fooii, there ii Indicates that the first and second parameters of the function are integers . and C The compiler may compile the function name into _foo. in other words , stay CPP In the target file obtained by the compiler ,foo() The function consists of _fooii Symbols to reference , And in the C In the target file generated by the compiler ,foo() The function consists of _foo Referring to .
But when the linker works , It doesn't matter what language the upper level uses , It recognizes only symbols in the target file . therefore , The connector will be found in .cpp Called in foo() function , But it can't be found in other target files _fooii This symbol , Therefore, an error is prompted in the connection process .extern “C” {} This grammatical form is used to solve this problem .
Global variable or function declaration
for instance , We want to declare a long int runtime_minute Variables are used by multiple modules :
//time.h
#ifndef TIME_H
#define TIME_H
// First of all we have time.h Make the following statement :
extern long int runtime_minute;
#endif //TIME_H
Pass... In the header file extern Declared variables are not defined , So we need to time.cpp The definition of :
//time.cpp
#include "time.h"
// stay time.cpp Give in runtime_minute Specific definition of variables :
long int runtime_minute = 0;
At this time , If you want to time2.cpp Use this definition in time.cpp Medium runtime_minute Variable , Just include time.h This header file is enough :
//time2.cpp
#include "time.h"
#include <iostream>
// stay time.cpp Give in runtime_minute Specific definition of variables :
int main(){
runtime_minute++;
std::cout<<runtime_minute<<std::endl;
// Output is 1
return 0;
}
边栏推荐
- 酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
- Getting started with pytest ----- test case rules
- Compile and build, from the bottom to the top
- 2022年大厂Android面试题汇总(二)(含答案)
- Hongmeng introduction and development environment construction
- BearPi-HM_ Nano development board "flower protector" case
- 【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
- MySQL 8 sub database and table backup database shell script
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
- How to output special symbols in shell
猜你喜欢
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
[getting started with MySQL] fourth, explore operators in MySQL with Kiko
SAP UI5 框架的 manifest.json
How to use scroll bars to dynamically adjust parameters in opencv
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
8位MCU跑RTOS有没有意义?
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
传统家装有落差,VR全景家装让你体验新房落成效果
10 advanced concepts that must be understood in learning SQL
随机推荐
Grafana 9 正式发布,更易用,更酷炫了!
Vscode replaces commas, or specific characters with newlines
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
Distinguish between basic disk and dynamic disk RAID disk redundant array
Hongmeng introduction and development environment construction
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
FlutterWeb浏览器刷新后无法回退的解决方案
Unity particle special effects series - treasure chest of shining stars
【Android】Kotlin代码编写规范化文档
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Why should Li Shufu personally take charge of building mobile phones?
【ASM】字节码操作 ClassWriter 类介绍与使用
Getting started with pytest ----- allow generate report
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Getting started with pytest ----- test case rules
C# NanoFramework 点灯和按键 之 ESP32
Pyspark operator processing spatial data full parsing (5): how to use spatial operation interface in pyspark
Compile and build, from the bottom to the top
Awk command exercise
BearPi-HM_ Nano development board "flower protector" case