自定义 gcc 预处理器 [英] Custom gcc preprocessor

查看:26
本文介绍了自定义 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 预处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆