Tre*_*key 92 c macros c-preprocessor ifndef
在我们的C代码库中,我看到每个宏都按以下方式定义:
#ifndef BEEPTRIM_PITCH_RATE_DEGPS
#define BEEPTRIM_PITCH_RATE_DEGPS 0.2f
#endif
#ifndef BEEPTRIM_ROLL_RATE_DEGPS
#define BEEPTRIM_ROLL_RATE_DEGPS 0.2f
#endif
#ifndef FORCETRIMRELEASE_HOLD_TIME_MS
#define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f
#endif
#ifndef TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS
#define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f
#endif
Run Code Online (Sandbox Code Playgroud)
这些定义检查的理由是什么,而不仅仅是定义宏?
#define BEEPTRIM_PITCH_RATE_DEGPS 0.2f
#define BEEPTRIM_ROLL_RATE_DEGPS 0.2f
#define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f
#define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f
Run Code Online (Sandbox Code Playgroud)
我无法在网络上的任何地方找到这种做法.
Bar*_*mar 138
这允许您在编译时覆盖宏:
gcc -DMACRONAME=value
Run Code Online (Sandbox Code Playgroud)
头文件中的定义用作默认值.
Enz*_*ber 51
正如我在评论中所说,想象一下这种情况:
foo.h中
#define FOO 4
Run Code Online (Sandbox Code Playgroud)
的defs.h
#ifndef FOO
#define FOO 6
#endif
#ifndef BAR
#define BAR 4
#endif
Run Code Online (Sandbox Code Playgroud)
bar.c
#include "foo.h"
#include "defs.h"
#include <stdio.h>
int main(void)
{
printf("%d%d", FOO, BAR);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
会打印44
.
但是,如果条件ifndef
不存在,结果将是MACRO重新定义的编译警告,它将打印出来64
.
$ gcc -o bar bar.c
In file included from bar.c:2:0:
defs.h:1:0: warning: "FOO" redefined [enabled by default]
#define FOO 6
^
In file included from bar.c:1:0:
foo.h:1:0: note: this is the location of the previous definition
#define FOO 4
^
Run Code Online (Sandbox Code Playgroud)
izo*_*ica 17
我不知道上下文,但这可以用来为用户提供覆盖这些宏定义设置的值的可用性.如果用户明确为这些宏中的任何一个定义了不同的值,则将使用它而不是此处使用的值.
例如,在g ++中,您可以-D
在编译期间使用该标志将值传递给宏.
任何C项目都驻留在多个源文件中.当处理单个源文件时,检查似乎(并且实际上)没有意义,但是在处理大型C项目时,最好在定义常量之前检查现有定义.这个想法很简单:你需要特定源文件中的常量,但它可能已经在另一个中定义了.
归档时间: |
|
查看次数: |
4721 次 |
最近记录: |