问题描述
你能给我一个写一个自定义的gcc预处理器的例子吗?我的目标是用适当的CRC32计算值替换SID(foo)相似的宏。对于任何其他宏,我希望使用标准cpp预处理器。
看起来可以使用 -no-integrated来实现此目标-cpp -B
选项,但是我找不到任何简单的例子。
警告:危险和丑陋的黑客攻击。闭上你的眼睛你可以通过将'-no-integrated-cpp'和'-B'开关添加到gcc命令行来挂钩你自己的预处理器。 '-no-integrated-cpp'表示在它使用其内部搜索路径之前,gcc在其预处理器的'-B'路径中搜索。如果使用'-E'选项调用'cc1','cc1plus'或'cc1obj'程序(这些是C,C ++和Objective-c编译器),则可以识别预处理器的调用。当你看到这个选项时,你可以做你自己的预处理。如果没有'-E'选项,则将所有参数传递给原始程序。如果有这样的选项,你可以做自己的预处理,并将操纵的文件传递给原始编译器。
它看起来像这样:
> cat cc1
!!/ bin / sh
echo我自己的特殊预处理器 - $ @
/ usr / lib / gcc / i486-linux- gnu / 4.3 / cc1 $ @
exit $?
> chmod 755 cc1
> gcc -no-integrated-cpp -B $ PWD xc
我自己的特殊预处理器 - -E -quiet xc -mtune = generic -o /tmp/cc68tIbc.i
我自己的特殊预处理器 - fpreprocessed /tmp/cc68tIbc.i -quiet -dumpbase xc -mtune = generic -auxbase x -o /tmp/cc0WGHdh.s
这个例子调用原始的预处理器,但打印一个额外的消息和参数。您可以用您自己的预处理器替换脚本。
糟糕的黑客已经结束。你现在可以睁开眼睛。
Could you please give me an example of writing a custom gcc preprocessor?
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.
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.
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.
It looks like this:
> 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预处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!