将__LINE__包装为单引号

时间:2015-07-02 20:16:20

标签: gnu m4

如何在M4中包含LINE和FILE宏以显示' 4'或" 4"。我希望它能够输出到控制台。我正在使用C ++。

1 个答案:

答案 0 :(得分:1)

Not 100% percent sure if this is what you're after but this makes __file__ to output filename in quotes (on my mac os):

define(`_m4__file__',defn(`__file__'))
pushdef(`__file__',`"_m4__file__"')

define(`_m4__line__',defn(`__line__'))
pushdef(`__line__',`"_m4__line__"')

define(`echo',`$*')
echo(__file__,__line__)
echo(__file__,__line__)

output:

"m4.t","8"
"m4.t","9"

for single quotes use a bit more escaping:

define(`_m4__file__',defn(`__file__'))
pushdef(`__file__',`''`_m4__file__'`'')

define(`_m4__line__',defn(`__line__'))
pushdef(`__line__',`''`_m4__line__'`'')

define(`echo',`$*')
echo(__file__,__line__)
echo(__file__,__line__)

outputs:

'm4.t','8'
'm4.t','9'