请教下各位ti 的FAE: 最近在看项目组一个博士生写的代码时发现一个比较奇怪的现象,他定义了一个公共头文件,然后里边定义的很多 double类型的变量,而这个头文件在好几个.c文件中都被包含,按C语言规则全局变量不能重复定义吧,但在debug模式下,ccs居然编译通过了。这是为啥?有没高人可以解释下?
而切换到 release 模式,就报重复定义的错误了。
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
请教下各位ti 的FAE: 最近在看项目组一个博士生写的代码时发现一个比较奇怪的现象,他定义了一个公共头文件,然后里边定义的很多 double类型的变量,而这个头文件在好几个.c文件中都被包含,按C语言规则全局变量不能重复定义吧,但在debug模式下,ccs居然编译通过了。这是为啥?有没高人可以解释下?
而切换到 release 模式,就报重复定义的错误了。
默认debug模式是没有加优化选项的,没有从工程级来优化,编译器只对单个文件进行编译,其它文件的情况是不关心的。而release mode是从整个工程进行优化的。
• --opt_level=0 or -O0
– Performs control-flow-graph simplification
– Allocates variables to registers
– Performs loop rotation
– Eliminates unused code
– Simplifies expressions and statements
– Expands calls to functions declared inline
• --opt_level=1 or -O1
Performs all --opt_level=0 (-O0) optimizations, plus:
– Performs local copy/constant propagation
– Removes unused assignments
– Eliminates local common expressions
• --opt_level=2 or -O2
Performs all --opt_level=1 (-O1) optimizations, plus:
– Performs software pipelining (see Section 3.2)
– Performs loop optimizations
– Eliminates global common subexpressions
– Eliminates global unused assignments
– Converts array references in loops to incremented pointer form
– Performs loop unrolling
The optimizer uses --opt_level=2 (-O2) as the default if you use --opt_level (-O) without an optimization
level.
• --opt_level=3 or -O3
Performs all --opt_level=2 (-O2) optimizations, plus:
– Removes all functions that are never called
– Simplifies functions with return values that are never used
– Inlines calls to small functions
– Reorders function declarations; the called functions attributes are known when the caller is
optimized
– Propagates arguments into function bodies when all calls pass the same value in the same
argument position
– Identifies file-level variable characteristics
If you use --opt_level=3 (-O3), see Section 3.6 and Section 3.7 for more information.
那么我有一个疑问;在debug模式下,最后link的时候不会有问题? 各个文件里定义的同一个全局变量最终都指向同一个数据段的地址么? 否则,各个.c文件里的全局变量各有一个数据段的地址,那么我们一般用全局变量来进行模块间数据通讯的意义不就失去了?