目的:#define、#include、#undef
ac 文件中此构造的目的是什么?:
#define _TIMERC
#include "timer.h"
#undef _TIMERC
我知道用于防止多次包含头文件的防护措施。 但这似乎并不是正在发生的事情。
谢谢!
What would the purpose of this construct in a c file be?:
#define _TIMERC
#include "timer.h"
#undef _TIMERC
I am aware of the guard for preventing multiple inclusion of a header file. This doesn't appear to be whats happening though.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里有一个场景来说明...
假设timer.h 提供了一个宏tick_count(),它返回发生的定时器中断的数量。
一个模块 (rpm_reader.h) 使用定时器 A 进行间隔计时:
在另一个模块 (lap_time.h) 中使用定时器 C 进行间隔计时
rpm_reader 在调用 tick_count() 时会从定时器 A 返回滴答计数,并且 lap_time 将得到它是来自计时器 C 的计数。
(很抱歉回答我自己的问题,但提出问题帮助我得出了这一结论。)
Here's a scenario to illustrate...
Lets say that timer.h provides a macro tick_count() that returns the number of timer interrupts that occured.
One module (rpm_reader.h) using timer A for interval timing:
In another module (lap_time.h) is using timer C for its interval timing
The rpm_reader would return the tick count from timer A when it called tick_count() and lap_time would get its count from timer C.
(My apologies for answering my own question, but asking the question helped me come to this revelation.)
通常,库头文件会有多个选项,这些选项由宏定义启用和禁用。 这将启用这样的选项。
更典型的是,这些是通过配置构建系统以将(例如使用 gcc)-D_TIMERC 添加到编译器命令行来在全局范围内设置的。
Often times a library header file will have multiple options, that are enabled and disabled by macro defines. This will enable such an option.
More typically these are set at a global scope by configuring your build system to add (for eg with gcc) -D_TIMERC to the compilers command line.
我想知道是否可能是这样的:
在这种情况下,头文件旨在允许在每个 #include 之前建立具有不同定义的多个包含项。
如果在timer.h中有一个代码块(中断代码),用于微控制器中每个定时器的定时器A、B和C。 在某些情况下,一个模块需要定时器 A,而另一模块需要定时器 C。
I was wondering if it could be this:
The header file in this case is intended to allow multiple inclusions with different defines established before the each #include.
If in the timer.h there is a block of code (interrupt code) for timers A, B and C for each timer in the microcontroller. In some cases timer A is required in one module and timer C is required in another module.
我认为你的自我回答是正确的。 包含的标头中很可能存在条件内容,并且“调用”文件知道它想要包含哪一组特定的条件“内容”。
它不一定与多个包含有关 - 它可能只是特殊情况,具体取决于“调用”上下文。
我不太确定为什么有人会取消定义。 我想不出混合和匹配的情况,所以不确定为什么需要取消定义。
I think your self-answer is right. There is most likely conditional stuff in the included header and the "calling" file knows which specific set of conditional "stuff" it wants to include.
It does not necessarily have to do with multiple includes - it can just be special cases depending on the "calling" context.
I am not exactly sure why one would undefine though. I can't think of a case where I would mix and match so not sure why an undefine is necessary.
冒着陈述显而易见的风险,“timer.h”期望有 _TIMERC 而你的代码的其余部分没有。
在一般情况下显然不是一个好的做法,但在包含第三方代码时我也看到过类似的做法。 当 #defs 发生冲突时可能会变得令人讨厌......
At the risk of stating the obvious, "timer.h" expects to have _TIMERC and the rest of your code does not.
Clearly not good practice in the general case, but I have seen similar when including third party code. Can get nasty when you have #defs that clash...
作为记录,避免多次包含同一头文件的常见做法是将防护放在文件本身中,而不是依赖于某些外部定义... ^_^
标头以:
结尾:
当然, def 风格可能会有所不同。
因此,在第一次包含时,我们会跳过#ifndef(尚未定义)并设置宏。
在第二次包含时,如果有的话,我们只是跳到文件末尾,不包含任何内容。
For the record, common practice to avoid multiple includes of the same header file is to put the guard in the file itself, not to rely on some external define... ^_^
The headers start with:
and end with:
Of course, the def style can vary.
Thus, on first inclusion, we go past the #ifndef (not yet defined) and we set the macro.
On second inclusion, if any, we just jump to end of file, nothing is included.