[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 < ...
随机推荐
- 如何有效地报告Bug
英文原文:Simon Tatham,编译:Dasn 引言 为公众写过软件的人,大概都收到过很拙劣的bug报告,例如: 在报告中说“不好用”: 所报告内容毫无意义: 在报告中用户没有提供足够的信息: 在 ...
- java实现树的一般操作
https://www.cnblogs.com/dawnyxl/p/9047437.html 树是数据结构中最基本的结构,今天的博客更新一下树的基本操作: 树的节点结构: package tree; ...
- 默认的Settings.xml文件(无修改过)-Maven
Tip: 当什么都不作修改时,默认是从Maven中央仓库进行下载,https://repo.maven.apache.org/maven2. 打开maven源码下的lib文件夹,找到maven-mod ...
- 最新中文版虚拟机VMware Workstation隆重上市
vmware虚拟机 在虚拟机软件中VMware Workstation算是一款非常强大较稳定的软件了,今天 VMware Workstation 10.0正式发布了,最让人欣喜的是该版本终于有了简体中 ...
- django中间件和auth模块
Django中间件 由django的生命周期图我们可以看出,django的中间件就类似于django的保安,请求一个相应时要先通过中间件才能到达django后端(url.views.template. ...
- Soldier and Badges
题目链接:https://vjudge.net/problem/CodeForces-546B AC代码: #include<iostream> #include<algorithm ...
- JavaScript中的document.fullscreenEnabled
本文主要讲述了: 什么是document.fullscreenEnabled 作用 兼容性 正文 什么是document.fullscreenEnabled document.fullscreenEn ...
- 关于struct stat
需要使用struct stat 类型时如果编译不过,修改Makefile: ##CFG_INC := -I$(MPI_DIR)/api/so/##CFG_INC += -I$(BASE_DIR)/pu ...
- 恭喜你,Get到一份 正则表达式 食用指南
先赞后看,养成习惯 前言 正则表达式 正则表达式: 定义一个搜索模式的字符串. 正则表达式可以用于搜索.编辑和操作文本. 正则对文本的分析或修改过程为:首先正则表达式应用的是文本字符串(text/st ...
- python基础知识的重新认识
昨天模拟书本上client和server交互的例子,代码明明是按照书上写的,可是就是出现了错误,像下面这样: # tcpserver from socket import * from time im ...