当前位置:网站首页>Can global variables be defined in header files
Can global variables be defined in header files
2022-04-23 14:05:00 【JokerYourMemory】
I met a pit today , stay VS When compiling and running multiple files on , Global variables are defined in the header file a, The header file is implemented by the class CPP And where the main function is located CPP Include and declare extern int a. However, multiple definition errors are reported when linking . I watched it on the Internet , This article gives an answer : Can you define global variables in the header file ?. In summary , There are two reasons for reporting errors CPP, Each generates its own OBJ, So when looking for symbols , You can find that there is also a variable in each other a, So it's wrong . therefore , If you can guarantee that your header file is included only once , Then you can define other linked variables in it , If there is no guarantee , To quote the author “ Then you can just find a source file , Define the global variable into the source file , Then create a new header file , It's going on inside extern The declaration of this variable , Finally, in the source file that needs to use this global variable include Your new header file is OK 了 ”. If this method is written makefile It shouldn't be difficult , Historically common interfaces 、 Header files are put together , For this The header file of shared global variables is put here , Just write normally .
PS: The concept of definition , No addition extern Or initialization is regarded as definition . Statement only extern type-name a A kind of
版权声明
本文为[JokerYourMemory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401091186.html
边栏推荐
猜你喜欢
基于ibeacons签到系统
Un modèle universel pour la construction d'un modèle d'apprentissage scikit
visio安装报错 1:1935 2:{XXXXXXXX...
The latest development of fed digital currency
33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
Wechat applet obtains login user information, openid and access_ token
Special test 05 · double integral [Li Yanfang's whole class]
scikit-learn構建模型的萬能模板
Detailed tutorial on the use of setinterval timing function of wechat applet
趣谈网络协议
随机推荐
pthread_self()为何重复了
json反序列化匿名数组/对象
关于密匙传递的安全性和数字签名
容差分析相关的计算公式
mysql通过binlog文件恢复数据
Chrome插件 之 Selenium IDE、XPath 安装
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
Use of WiFi module based on wechat applet
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
DeepinV20安装Mariadb
读了一篇博客,重新理解闭包整理一下
Un modèle universel pour la construction d'un modèle d'apprentissage scikit
微信小程序setInterval定时函数使用详细教程
Check in system based on ibeacons
Pycharm连接远程服务器并实现远程调试
leetcode--380. O (1) time insertion, deletion and acquisition of random elements
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
PySide2
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
基于ibeacons三点定位(微信小程序)