自定义 gcc 预处理器

编程入门 行业动态 更新时间:2024-10-22 02:40:57
本文介绍了自定义 gcc 预处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你能给我一个编写自定义 gcc 预处理器的例子吗?

Could you please give me an example of writing a custom gcc preprocessor?

我的目标是用适当的 CRC32 计算值替换 SID("foo") 类似的宏.对于任何其他宏,我想使用标准的 cpp 预处理器.

My goal is to replace SID("foo") alike macros with appropriate CRC32 computed values. For any other macro I'd like to use the standard cpp preprocessor.

看起来使用 -no-integrated-cpp -B 选项可以实现这个目标,但是我找不到任何简单的使用示例.

It looks like it's possible to achieve this goal using -no-integrated-cpp -B options, however I can't find any simple example of their usage.

推荐答案

警告:危险且丑陋的 hack.现在闭上眼睛 您可以通过在 gcc 命令行中添加-no-integrated-cpp"和-B"开关来挂钩自己的预处理器.'-no-integrated-cpp' 意味着 gcc 在使用其内部搜索路径之前会在 '-B' 路径中搜索其预处理器.如果使用-E"选项调用cc1"、cc1plus"或cc1obj"程序(这些是 C、C++ 和 Objective-c 编译器),则可以识别预处理器的调用.当您看到此选项时,您可以进行自己的预处理.当没有-E"选项时,将所有参数传递给原始程序.当有这样的选项时,您可以自己进行预处理,并将处理后的文件传递给原始编译器.

Warning: dangerous and ugly hack. Close your eyes now You can hook your own preprocessor by adding the '-no-integrated-cpp' and '-B' switches to the gcc command line. '-no-integrated-cpp' means that gcc does search in the '-B' path for its preprocessors before it uses its internal search path. The invocations of the preprocessor can be identified if the 'cc1', 'cc1plus' or 'cc1obj' programs (these are the C, C++ and Objective-c compilers) are invoked with the '-E' option. You can do your own preprocessing when you see this option. When there is no '-E' option pass all the parameters to the original programs. When there is such an option, you can do your own preprocessing, and pass the manipulated file to the original compiler.

看起来像这样:

> cat cc1 #!/bin/sh echo "My own special preprocessor -- $@" /usr/lib/gcc/i486-linux-gnu/4.3/cc1 $@ exit $? > chmod 755 cc1 > gcc -no-integrated-cpp -B$PWD x.c My own special preprocessor -- -E -quiet x.c -mtune=generic -o /tmp/cc68tIbc.i My own special preprocessor -- -fpreprocessed /tmp/cc68tIbc.i -quiet -dumpbase x.c -mtune=generic -auxbase x -o /tmp/cc0WGHdh.s

此示例调用原始预处理器,但打印附加消息和参数.您可以使用自己的预处理器替换脚本.

This example calls the original preprocessor, but prints an additional message and the parameters. You can replace the script by your own preprocessor.

糟糕的黑客攻击结束了.你现在可以睁开眼睛了.

The bad hack is over. You can open your eyes now.

更多推荐

自定义 gcc 预处理器

本文发布于:2023-11-14 00:39:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   gcc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!