头文件为什么可以包括自己? [英] Why could a header file be including itself?

查看:184
本文介绍了头文件为什么可以包括自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能想到在其中一个头文件包括本身的任何情况的?

Can anyone think of any scenario in which a header file includes itself?

我看到它在节目之一,这是包括在条件编译块,至少我找不到任何真正condition.But,我想有可能对此类情况的技术要求?

I saw it in one of the program and this inclusion is under conditional compilation block for which at least i could not find any true condition.But, I was thinking could there be any technical requirement for such scenario?

推荐答案

是的,如果你想赢得的国际混淆C $ C $ç比赛。这里是一个很好的例子(源文件名为 isaak.c

Yes, if you are trying to win the International Obfuscated C Code Contest. Here's a nice example (the source file is called isaak.c):

main(){}
#define P define
#P U ifdef
#P main Si
#U y
#undef y
#include "isaak.c"
Pb
#else
char*K="4499999;8   9+jW*':'TZhD m:*h.4-j'9(z7Q>r*:G#FS]mATIdMZY^HaKFZZ\
JyJw:X49@eJj1,Z'\\c^jGU@IXTF@9P2i:gAZx0pD*W3\\<ZZs1:.~Z8U:P<\\:ZOI0GBPZ7",*H
,S[5202],*B="oA9BA6iN`]`Ph>5F4::M6A69@6I{g[Za__]NPV``aV\177E4C5CG;4C<BEJG;\
?LGlSZ[Y_!oYi@uXPzLFyPOYP][]`RTaQo86564CAHCG4ES",*F,N;int Bk,V;Y
#endif
#P C K/16-2
(){char*H;F O=-263;for(H="$+---+|||";*++H;)*(F O=(*H+5&129)+1)= *H;F
#P W sprintf(
O= -132;}I/**/r(){if((N= *IO/**/O%(21 O -5)+81 O 16)==107)N+=
#undef I
*K++&15;*F++=N;return*K;}
#undef O
#P I K
#P O +
#U N
exit(N){F=WH=S,"%5060d")+385;while(Br(),++B,Kr())F+=(N=
*B++/26-1)?(")21["[N]-46)*N*4-22:-3194;while(*--K!=9){while(!(*++H+5&64));
F=(40-"(\206/"[((H-S)%130+45)/57]<<3)+H;*F++=*H++;*F=
*H==106?32:*H;Y();W WF-131,"%-3d",++Bk)+260,"%3d",V+=
*C?*C:"hijpqv"[*--C]-106);Pb();}for(H=S;*H||(int)_exit(0);H+=130)write(1,1+W
F+3,"%c%-73.73s\n",0,H),74);}
#endif
#undef U
#P U ifndef
#include <stdio.h>

在所有的严重性,头文件一般不应尝试直接包括本身,因为这通常只是一个糟糕的主意(你必须使用条件编译来控制递归,并得到毛茸茸的快)。如果头文件包括本身间接(通过中间的头文件),那么它可能是错误的,因为中间头文件将不能够从原始标题文件中使用的定义

In all seriousness, a header file should not normally try to include itself directly because that's generally just a bad idea (you'd have to use conditional compilation to control recursion, and that gets hairy fast). If a header file includes itself indirectly (through an intermediate header file) then it is likely an error because the intermediate header file will not be able to use the definitions from the original header file.

这就是说,在一个本领域技术人员的手,和一个非常特定的需要,这是可以做到的计算在preprocessor。这种计算可能需要生成编译时的结构,进行手动循环展开,或做其他各种preprocessor招数。在所有这些情况下,使用自包括或多包括精心设计。

That said, in the hands of a skilled practitioner, and a very specific need, it is possible to do computation in the preprocessor. Such computation may be necessary to generate compile-time constructs, perform manual loop unrolling, or do various other "preprocessor tricks". In all these cases, the use of self-includes or multiple includes is carefully designed.

这篇关于头文件为什么可以包括自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆