用于从标头打印出宏值的单行代码

发布于 2024-12-03 15:41:46 字数 741 浏览 0 评论 0原文

我有一个定义了大量宏的头文件,其中一些宏依赖于其他宏——但是,依赖关系都在这个头文件中解决。

我需要一行来打印该标头中定义的宏的值。

举个例子:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彩虹直至黑白 2024-12-10 15:41:46
echo 'void main(){ printf( "0x%X", MACRO_B ); }' \
  | gcc -x c --include /path/to/header --include /usr/include/stdio.h - && ./a.out

将在一行中完成。

(您误读了从 stdin 读取时 GCC 给出的错误。您需要 -E-x (需要指定所需的语言))

另外,它是 int main(),或者,当您不关心这里的情况时,只需完全删除返回类型即可。并且您不需要指定 stdio.h 的路径。

所以稍微短一点:

echo 'main(){printf("0x%X",MACRO_B);}' \
  | gcc -xc --include /path/to/header --include stdio.h - && ./a.out
echo 'void main(){ printf( "0x%X", MACRO_B ); }' \
  | gcc -x c --include /path/to/header --include /usr/include/stdio.h - && ./a.out

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 for stdio.h.

So slightly shorter:

echo 'main(){printf("0x%X",MACRO_B);}' \
  | gcc -xc --include /path/to/header --include stdio.h - && ./a.out
梦境 2024-12-10 15:41:46

tail -n1 怎么样?像这样:

$ echo C_IRUSR | cpp --include /usr/include/cpio.h | tail -n 1
000400

What about tail -n1? Like this:

$ echo C_IRUSR | cpp --include /usr/include/cpio.h | tail -n 1
000400
玩心态 2024-12-10 15:41:46

人为地生成一个包含您的 MACRO_B 值的错误,然后编译代码怎么样?

How about artificially generating an error that contains your MACRO_B value in it, and then compiling the code?

勿忘心安 2024-12-10 15:41:46

我认为最简单的方法是编写一个小型 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.

暖阳 2024-12-10 15:41:46
echo '"EOF" EOF' | cpp --include /usr/include/stdio.h | grep EOF

印刷:

"EOF" (-1)
echo '"EOF" EOF' | cpp --include /usr/include/stdio.h | grep EOF

prints:

"EOF" (-1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文