如何仅适用于#IFDEF的GCC预处理标头,而不是#include和#defines

发布于 2025-02-13 15:13:29 字数 530 浏览 0 评论 0原文

我必须预处理标头,在此期间,我只想处理#ifdef的标题,#if已定义的部分,其他部分(例如宏扩展)和#include部分不应扩展或检查#include部分。如何得到这个?我需要将此输出文件传递给其他软件,例如CMOCK来创建模拟文件,然后将其用于编译。但是我面临的问题是,如果我使用GCC -E,它会生成一个文件,其中所有#CRUDE都会扩展,宏随#if定义而扩展。 任何帮助都将不胜感激

例如,

#include <stdint.h>   //i don't want to pre-process this line

#if defined (__MACRO_A__)  //i want to pre-process this line
    void funcA(void);
#else
    void funcB(void);
#endif

:我希望输出文件仅具有funcb原型。我尚未定义 macro_a ,因此,如果我只包含了funcb的预处理标题,但是所有#includes都在扩展,我不想要。

I have to preprocess a header, during this I want to only process the header for #ifdef, #if defined sections, other sections like Macro expansion and #include sections should not be expanded or checked. how to get this? I need to pass this output file to another software like cmock to create a mock file and then use it for compilation. but the problem I am facing is if I use gcc -E it generates a file where all #includes are expanded, Macros expanded along with #if defines.
any help would be appreciated

for example:

#include <stdint.h>   //i don't want to pre-process this line

#if defined (__MACRO_A__)  //i want to pre-process this line
    void funcA(void);
#else
    void funcB(void);
#endif

I want the output file to have only funcB prototype. I have not defined MACRO_A, so if I preprocess header only funcB is getting included, but all the #includes are getting expanded, which I don't want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小苏打饼 2025-02-20 15:13:29

但是所有的#includes都在扩展,我不想要。

因此,将它们从文件中删除。

grep -v '#include' inputfile.c | gcc -E -xc -

上面的正则可能会更好。

but all the #includes are getting expanded, which I don't want.

So remove them from the file.

grep -v '#include' inputfile.c | gcc -E -xc -

The regex above can possibly be better.

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