当前位置:网站首页>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
边栏推荐
- 快速安装mongodb
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
- Cdh6 based on CM management 3.2 cluster integration atlas 2 one
- Call wechat customer service applet
- visio安装报错 1:1935 2:{XXXXXXXX...
- 金蝶云星空API调用实践
- Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
- Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
- SPC简介
- 可否把模板的头文件和源文件分开编译
猜你喜欢

Promtail + Loki + Grafana 日志监控系统搭建

Wechat applet obtains login user information, openid and access_ token

Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design

scikit-learn构建模型的万能模板

SQL数据库

关于密匙传递的安全性和数字签名

Chrome插件 之 Selenium IDE、XPath 安装

linux安装mysql后修改密码

查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法

smart-doc + torna生成接口文档
随机推荐
按实际取,每三级分类汇总一次,看图知需求
网站_收藏
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
基础知识学习记录
RobotFramework 之 用例执行
理解虚基类、虚函数与纯虚函数的概念(转)
AtomicIntegerArray源码分析与感悟
Special test 05 · double integral [Li Yanfang's whole class]
websocket
Strange bug of cnpm
Nifi 快速安装及文件同步操作
DeepinV20安装Mariadb
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
室内外地图切换(室内基于ibeacons三点定位)
基於CM管理的CDH集群集成Phoenix
微信小程序的订阅号开发(消息推送)
Switch usage (wechat applet)
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Ptorch classical convolutional neural network lenet