[Linux][C][gcc][tips] 在头文件中定义变量引发的讨论
概述
本人的原创文章,最先发表在github-Dramalife-note中。转载请注明出处。
Define variable(s) in header file referenced by multiple c files.
(CH:在 被多个c文件引用 的 头文件中定义变量)
If the variable is initialized, GCC will report an error.
(CH:如果这个变量被初始化, GCC会报错)
一般来说,如果需要,变量的声明一边放到头文件中,变量的定义不放到头文件中(尤其是被多个要被编译并链接到同一个可执行文件的源文件引用时)。
本文讨论的来源:
X:变量一定不能放到头文件中定义吗?
D:如果被多个C文件同时引用的话,gcc会报告重复定义变量的错误。
X:但是我在头文件中定义了一个变量,而且这个头文件被多个c文件引用,结果能编过!
D:??
本文源码地址1-github,
本文源码地址1-gitee镜像.
Reason_keywords : stack, heap, nm
总结
简单说明:
在 被多个源文件引用的 头文件 中定义变量,
如果这个变量没有初始化(例:int gb;),那么gcc编译时会把它放到common section,gcc在链接阶段允许有多个同名的common symbol,所以能编译成功;
如果初始化了这个变量(例:int gb = ;),gcc编译时会把变量放到data section或者bss section,gcc在链接时不允许data和bss段中的符号名重复,所以会报错-重复定义变量(multiple definition of `gb');
验证详情
0 NOTE
0.1 List files(验证时用到的文件)
验证时用到的文件(可在git获取)。
# ls
a_demo.out
config.mk
functions.c
functions.o
header.h
main.c
main.o
Makefile
README.md
source_pre.i
0.2 Manual page - nm (part)
关于nm(用于列出目标文件的符号)工具的说明。
NM(1)
"C" The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols
may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as
undefined references.
"B"
"b" The symbol is in the uninitialized data section (known as BSS).
"D"
"d" The symbol is in the initialized data section.
0.3 Macros
代码中用于方便演示而添加的两个宏。
//header.h
/*
* __INIT_THE_VAR__ : Initialize variable"gb" in this header file.(CH:)
* Define this macro(int gb = <x>;), GCC will report error:
* (CH:开启这个宏,头文件中会定义并初始化变量gb,此时GCC会报错)
* GCC Error - multiple definition of `gb'
* (CH:重复定义变量gb)
* __VAR_VAULE_ZERO__ : Initial vaule is 0.(CH:)
* Define this macro(int gb = 0;), GCC will place the var to BSS section, else DATA section.
* (CH:开启这个宏,头文件中会定义并初始化变量gb=0,GCC会把gb变量放到BSS段,初值不为0时放到DATA段)
*/
1. Compile(编译)
1.1 DEFINE(o), INIT( ), ZERO( )
Define (but not initialize) variable in header(CFLAGS += "-U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__")
(CH:在头文件中定义但不初始化变量)
make all-normal-create-obj CFLAGS+="-U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__"
CLEANING...
rm -rvf ./*.out ./*.i ./*.o
removed './source_pre.i'
removed './functions.o'
removed './main.o'
rm -rvf /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/build
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o -U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
BUILDING...
gcc -E /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c >> source_pre.i
FLAGS : -U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__ ;
SRCS:
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c
gcc -o a_demo.out /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
1.2 DEFINE(o), INIT(o), ZERO( )
Define & initialize variable in header(CFLAGS +=" -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__")
(CH:在头文件中定义并初始化变量为非零值)
make all-normal-create-obj CFLAGS+="-D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__"
CLEANING...
rm -rvf ./*.out ./*.i ./*.o
removed './a_demo.out'
removed './source_pre.i'
removed './functions.o'
removed './main.o'
rm -rvf /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/build
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
BUILDING...
gcc -E /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c >> source_pre.i
FLAGS : -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__ ;
SRCS:
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c
gcc -o a_demo.out /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o:(.data+0x0): multiple definition of `gb'
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:167: recipe for target 'all-normal-create-obj' failed
make: *** [all-normal-create-obj] Error 1
1.3 DEFINE(o), INIT(o), ZERO(o)
Define & initialize variable in header(CFLAGS +=" -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__")
(在头文件中定义并初始化变量为零)
# make all-normal-create-obj CFLAGS+="-D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__"
CLEANING...
rm -rvf ./*.out ./*.i ./*.o
removed './source_pre.i'
removed './functions.o'
removed './main.o'
rm -rvf /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/build
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__
gcc -c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c -o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__
BUILDING...
gcc -E /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c >> source_pre.i
FLAGS : -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__ ;
SRCS:
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.c /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.c
gcc -o a_demo.out /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o /home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/functions.o:(.bss+0x0): multiple definition of `gb'
/home/dramalife/note/70-gcc_gnu_compiler_collection/tips/Q_can_define_var_at_header/main.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:167: recipe for target 'all-normal-create-obj' failed
make: *** [all-normal-create-obj] Error 1
2 Symbols
2.1 DEFINE(o), INIT( ), ZERO( )
List symbol - define (but not initialize) variable in header(CFLAGS +=" -U__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__")
# nm main.o
000000000000000a r __func__.2252
U functionb
0000000000000004 C gb
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
U printf
# nm function.o
0000000000000010 r __func__.2252
0000000000000000 T functionb
0000000000000004 C gb
U _GLOBAL_OFFSET_TABLE_
U printf
2.2 DEFINE(o), INIT(o), ZERO( )
List symbol - define & initialize variable in header(CFLAGS +=" -D__INIT_THE_VAR__ -U__VAR_VAULE_ZERO__")
# nm main.o
000000000000000a r __func__.2252
U functionb
0000000000000000 D gb
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
U printf
# nm function.o
0000000000000010 r __func__.2252
0000000000000000 T functionb
0000000000000000 D gb
U _GLOBAL_OFFSET_TABLE_
U printf
2.3 DEFINE(o), INIT(o), ZERO(o)
List symbol - define & initialize variable in header(CFLAGS +=" -D__INIT_THE_VAR__ -D__VAR_VAULE_ZERO__")
# nm main.o
000000000000000a r __func__.2252
U functionb
0000000000000000 B gb
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
U printf
# nm function.o
0000000000000010 r __func__.2252
0000000000000000 T functionb
0000000000000000 B gb
U _GLOBAL_OFFSET_TABLE_
U printf
Summary(总结)
Define and/or init variable in header files referenced by multiple source files.
(CH:在 被多个源文件引用的 头文件 中 定义和初始化变量。)
变量定义时,如果不赋初值,GCC会把它放到COMMON段,COMMON类型的符号是未初始化的数据。链接时,gcc允许多个COMMON符号以相同的名称出现(在多个地方被定义)。
变量定义时,如果赋了初值,GCC会把它放到DATA或者BSS段,链接时,这两种类型的符号不允许重名。
| INIT | ZERO | Compile | Section of var |
|---|---|---|---|
| - | - | PASS | COMMON |
| v | - | ERR | DATA |
| v | v | ERR | BSS |
[Linux][C][gcc][tips] 在头文件中定义变量引发的讨论的更多相关文章
- 不要在.h文件中定义变量
今天在头文件.h中初始化了一个数组和函数,在编译的时候提示这个数组和函数重新定义了,检查后发现,犯了一个致命的错误,在头文件中定义变量... 以下引用别人的一篇说明,警示自己. C语言作为一种结构化的 ...
- c语言头文件中定义全局变量的问题
c语言头文件中定义全局变量的问题 (转http://www.cnblogs.com/Sorean/) 先说一下,全局变量只能定义在 函数里面,任意函数,其他函数在使用的时候用extern声明.千万不要 ...
- C语言头文件中定义全局变量导致重复定义错误
合作方升级SDK后,程序编译出现变量重复定义的错误,通过错误提示无法找到什么位置重复定义了,但确定是引入新SDK后才出现的错误,从SDK的头文件中查找,最终发现在头文件中定义了全局变量 我们的项目在多 ...
- C语言之在头文件中定义全局变量
通常情况下,都是在C文件中定义全局变量,在头文件中声明,但是,如果我们定义的全局变量需要被很多的C文件使用的话,那么将全局变量定义在头文件里面会方便很多,那到底是如何实现的? os_var.c文件内容 ...
- C++ vector 实现二维数组时, 在类的头文件中定义时遇到"应输入类型符"的问题?
见下,当我在类的声明文件中定义二维vector时,提示我应输入类型说明符; 但是相同的格式定义,在类中将二维vector修改为在源文件中定义就可以顺利通过,并顺利执行打印 打印结果如下: 望大神来解惑 ...
- 在C的头文件中定义的结构体,如何在cpp文件中引用
解决方案1:在cpp文件中放置.c,且在该文件中引用变量 解决方案2:在一个cpp文件中包含.c,但在另一个cpp文件中使用结构体变量 cpp文件1 cpp文件2 #include "dia ...
- 关于在.H文件中定义变量
KEIL中,在".H"文件定义变量. 如果该".H"文件同时被两个".C"文件调用,则会出现重复定义错误(*** ERROR L104: M ...
- linux下gcc默认搜索头文件及库文件的路径
一.头文件gcc 在编译时如何去寻找所需要的头文件:※所以header file的搜寻会从-I开始※然后找gcc的环境变量 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC ...
- 编写linux驱动所用到的头文件(转)
转自:http://blog.csdn.net/lufeiop02/article/details/6448497 关于linux驱动(应用)程序头文件使用 收藏 驱动程序: #include < ...
随机推荐
- spring基于@Value绑定属Bean性失
用spring注解@Value绑定属性失败 环境: eclipse Version: Luna Release (4.4.0) spring 4.0 Junit4 其他依赖包 描述: JsrDAO类, ...
- 吴裕雄--天生自然 PHP开发学习:PhpStorm的配置与安装
下载安装包
- 用手机应用追踪城市噪声污染——微软Azure助力解决城市问题
噪声无孔不入的城市地带(图片来自于网络) 2014年4月19日发行的<经济学人>杂志预言,到2030年,中国人口的70%(约10亿人)会在城市中居住.中国城镇化的高速发展一方面大大提高了 ...
- leetcode_315_逆序对问题
题目描述 本题来自于Leetcode的算法题库第315题,具体题目描述如下: 给定一个nums整数数组 ,按要求返回一个counts新数组 .数组 counts 有该性质: counts[i]的值是 ...
- 压力测试(六)-阿里云Linux服务器压测接口实战
1.SpringBoot 接口打包,并用jar包方式部署 简介:用jar包方式在控制台进行启动 打包 mvn package && java -jar target/gs-spring ...
- js笔记系列之--时间及时间戳
js入门系列之 时间及时间戳 时间及时间戳 时间及时间戳是js里面很常见的一个概念,在我们写前端页面的时候,经常会遇到需要获取当前时间的情况,所以,了解js中的时间概念非常重要.而时间戳是指格林威治时 ...
- npm注册源的配置
npm注册源的更换 在墙内久了,难免会碰到撞墙的时候,所幸国内也有众多 NPM 镜像可供选择,在大多数情况下我们可以使用国内的源(比如 淘宝 NPM 镜像)去替换官方的源以加快下载包的速度.不过呢,我 ...
- 基于 Redis 实现 CAS 操作
基于 Redis 实现 CAS 操作 Intro 在 .NET 里并发情况下我们可以使用 Interlocked.CompareExchange 来实现 CAS (Compare And Swap) ...
- 2019年最新老男孩高性能Web架构与自动化运维架构视频教程
课程目录L001-老男孩架构15期-Web架构之单机时代L002-老男孩架构15期-Web架构之集群时代L003-老男孩架构15期-Web架构之dnsL004-老男孩架构15期-Web架构之缓存体系L ...
- git版本回退问题记录
因为之前有个前端改了文件目录进行合并时候丢失掉些许代码,然后我在以前分支进行了代码层级的整理,项目如果想要启动还需还原回以前的版本,我进行了三次文件夹层级提交,所以我需要进行三次的版本回退. git命 ...