当前位置:网站首页>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
边栏推荐
猜你喜欢
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
微信小程序获取登录用户信息、openid和access_token
smart-doc + torna生成接口文档
Record a strange bug: component copy after cache component jump
STM32 learning record 0007 - new project (based on register version)
Programming travel function
Wechat applet obtains login user information, openid and access_ token
1256: bouquet for algenon
DDT+Excel进行接口测试
Prediction of tomorrow's trading limit of Low Frequency Quantization
随机推荐
Restful WebService和gSoap WebService的本质区别
封装logging模块
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
Qt Designer怎样加入资源文件
关于stream流,浅记一下------
基于ibeacons三点定位(微信小程序)
微信小程序的订阅号开发(消息推送)
Chrome插件 之 Selenium IDE、XPath 安装
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
Windos中安装labellmg教程
Call wechat customer service applet
服务器日志分析工具(识别,提取,合并,统计异常信息)
生成随机高质量符合高斯分布的随机数
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
poi操作word模板替换数据并且导出word
Program compilation and debugging learning record
RobotFramework 之 项目框架
1256:献给阿尔吉侬的花束
程序编译调试学习记录
Business case | how to promote the activity of sports and health app users? It is enough to do these points well