用于从标头打印出宏值的单行代码
我有一个定义了大量宏的头文件,其中一些宏依赖于其他宏——但是,依赖关系都在这个头文件中解决。
我需要一行来打印该标头中定义的宏的值。
举个例子:
#define MACRO_A 0x60000000
#define MACRO_B MACRO_A + 0x00010000
//...
乍一看:
echo MACRO_B | ${CPREPROCESSOR} --include /path/to/header
...这几乎给了我我想要的东西:
# A number of lines that are not important
# ...
0x60000000 + 0x00010000
...但是,我试图防止它膨胀成一个巨大的序列“将它通过管道连接到这个,然后将它通过管道连接到那个” ……”。
我也尝试过这个:
echo 'main(){ printf( "0x%X", MACRO_B ); }' \
| ${CPREPROCESSOR} --include /path/to/header --include /usr/include/stdio.h
...但是它(gcc编译器)抱怨在标准输入上处理代码时需要-E,所以我最终不得不写入一个临时文件来编译/运行它。
有更好的办法吗?
-布莱恩
I have a header that defines a large number of macros, some of whom depend on other macros -- however, the dependencies are all resolved within this header.
I need a one-liner for printing out the value of a macro defined in that header.
As an example:
#define MACRO_A 0x60000000
#define MACRO_B MACRO_A + 0x00010000
//...
As a first blush:
echo MACRO_B | ${CPREPROCESSOR} --include /path/to/header
... which nearly gives me what I want:
# A number of lines that are not important
# ...
0x60000000 + 0x00010000
... however, I'm trying to keep this from ballooning into a huge sequence of "pipe it to this, then pipe it to that ...".
I've also tried this:
echo 'main(){ printf( "0x%X", MACRO_B ); }' \
| ${CPREPROCESSOR} --include /path/to/header --include /usr/include/stdio.h
... but it (the gcc compiler) complains that -E is required when processing code on standard input, so I end up having to write out to a temporary file to compile/run this.
Is there a better way?
-Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将在一行中完成。
(您误读了从 stdin 读取时 GCC 给出的错误。您需要
-E
或-x
(需要指定所需的语言))另外,它是
int main()
,或者,当您不关心这里的情况时,只需完全删除返回类型即可。并且您不需要指定stdio.h
的路径。所以稍微短一点:
will do it in one line.
(You misread the error GCC gives when reading from stdin. You need
-E
or-x
(needed to specify what language is expected))Also, it's
int main()
, or, when you don't care like here, just drop the return type entirely. And you don't need to specify the path forstdio.h
.So slightly shorter:
tail -n1
怎么样?像这样:What about
tail -n1
? Like this:人为地生成一个包含您的 MACRO_B 值的错误,然后编译代码怎么样?
How about artificially generating an error that contains your MACRO_B value in it, and then compiling the code?
我认为最简单的方法是编写一个小型 C 程序,包含标头,然后打印所需的输出。然后您可以在脚本、makefile 或其他文件中使用它。
I think the easiest way would be to write a small C program, include the header to that, and print the desired output. Then you can use it in your script, makefile or whatever.
印刷:
prints: