[英]Why preprocessor doesn't expand type defined later in the code
My question is relatively simple and yet mystery to me.我的问题相对简单,但对我来说很神秘。
After peprocessing BLA_Str
isn't expanded to bla_Str
. peprocessing后
BLA_Str
没有扩展到bla_Str
。 This is the case:情况是这样的:
#include <stdio.h>
#include <stdint.h>
typedef BLA_Str blaInstance;
#define BLA_Str bla_Str
typedef struct
{
int bla;
}bla_Str;
void main(void){
// printf("%u\n\r",5);
}
The preprocessor just performs a single pass through the file, expanding macros as it goes, and adding macros to its list when it encounters the #define
.预处理器只对文件执行一次传递,在处理过程中扩展宏,并在遇到
#define
时将宏添加到其列表中。 When it gets to the line当它到达线路时
typedef BLA_Str blaInstance
it doesn't yet know about the BLA_Str
macro, so it leaves it unchanged in the output.它还不知道
BLA_Str
宏,所以它在输出中保持不变。
You shold generally put all the #define
lines at the beginning, so they'll affect everything in the rest of the file.您通常应该将所有
#define
行放在开头,因此它们会影响文件其余部分中的所有内容。
You can find a reasonable summary of how the C preprocessor works in The C Book .您可以在The C Book 中找到有关 C 预处理器如何工作的合理总结。 It explains:
它解释说:
There are two ways of defining macros, one of which looks like a function and one which does not.
有两种定义宏的方法,一种看起来像函数,另一种则不是。 Here is an example of each:
以下是每个示例:
#define FMAC(a,b) a here, then b #define NONFMAC some text here
Both definitions define a macro and some replacement text, which will be used to replace later occurrences of the macro name in the rest of the program.
这两个定义都定义了一个宏和一些替换文本,它们将用于替换程序其余部分中稍后出现的宏名称。
If a macro expands into another macro, this is handled with rescanning :如果一个宏扩展为另一个宏,则通过重新扫描来处理:
Once the processing described above has occurred, the replacement text plus the following tokens of the source file is rescanned, looking for more macro names to replace.
一旦发生上述处理,将重新扫描替换文本加上源文件的以下标记,寻找更多要替换的宏名称。 The one exception is that, within a macro's replacement text, the name of the macro itself is not expanded.
一个例外是,在宏的替换文本中,宏本身的名称没有扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.