是否有一种方法来转义c预处理器(cpp)中的宏名称(标识符)?
我想附加一些web代码(html,css.)具有可读的宏名称。
条件css文件示例:
/*root*/
some rootcode
#if height==480
/* height 480 */
.page {
line-height:23px;
}
#elif height<480
/* height < 480 */
.page {
line-height:46px;
}
#endif对…的调用
cpp -P -D height=480 -oout.css css.ccss结果(在删除换行符后)
some rootcode
.page {
line-480:23px;
}但是"line-480“是错误的。
是否有一种方法可以在不更改宏名或不对其进行字符串化的情况下转义代码中的“高度”?
发布于 2011-12-01 18:51:17
我利用Luchian的想法来取消已使用的文件名的定义,并为这个问题找到了一个(几乎)通用的解决方案:
包括条件词开头的"define.file“和给定条件后的"undefine.file”。
因此问题被简化为两个宏名,它们必须保留: DEFINEFILE和UNDEFINEFILE。但是,这两个宏可以使用它们的哈希代码或随机名称进行加密,以避免在条件文本中使用这些名称。
"define.file":
#define height 480
#define os 1"undefine.file“
#undef height
#undef os"conditionalcss.ccss“
/*root*/
some rootcode
#include __DEFINEFILENAMEPATH__
#if height==480
#include __UNDEFINEFILENAMEPATH__
/* height 480 */
.page {
line-height:23px;
}
#include __DEFINEFILENAMEPATH__
#elif height<480
#include __UNDEFINEFILENAMEPATH__
/* height > 480 */
.page {
line-height:46px;
}
#include __DEFINEFILENAMEPATH__
#endif
#if os==1
#include __UNDEFINEFILENAMEPATH__
os is windows (if 1 refers to windows)
and height also undefined i hope
#endif最后,带有参数定义和取消定义文件的and调用:
cpp -P -D __DEFINEFILENAMEPATH__="\"define.file\"" -D __UNDEFINEFILENAMEPATH__="\"undefine.file\"" -oout.css css.ccss有了这个想法,restulting的"out.css“看起来就像:
some rootcode
.page {
line-height:23px;
}
os is windows (if 1 refers to windows)
and height also undefined i hope该解决方案只存在两个宏的缺点,而且由于多个导入,性能可能较差。
我希望它能帮助别人解决他们的问题。
格列茨·阿梦想斯
发布于 2011-12-01 09:57:25
你可以:
1)取消宏的定义:
#undef height2)使用标准的caps重命名宏:
#define HEIGHT3)在处理文件之前使用警卫:
#if height==480
#define HEIGHT_480
#undef height
#endif
#if height>480
#define HEIGHT_OVER_480
#undef height
#endif
/*root*/
some rootcode
#if HEIGHT_480
/* height 480 */
.page {
line-height:23px;
}
#elif HEIGHT_OVER_480
/* height < 480 */
.page {
line-height:46px;
}
#endif第一个在未定义之后丢失信息。如果宏的广泛使用,第二种方法是不切实际的。
第三种选择是海事组织的最佳选择。我见过在生产代码中使用这种东西是必要的。
https://stackoverflow.com/questions/8339205
复制相似问题