C文件的部分预处理

Partial preprocessing of C files

我正在寻找允许对 C 源文件进行部分预处理的预处理器。我想做的是跳过某些或所有 #include 并在命令行(或单独的文件)上定义一些宏,然后预处理器应该只处理我指定的内容而忽略其余部分。

这是我想做的事的一个例子:

#define FOO  
#ifdef FOO  
/* FOO */  
#endif  
#ifdef BAR  
/* BAR */  
#endif  

应该翻译成

/* FOO */  
#ifdef BAR  
/* BAR */  
#endif

不久前,在我以前的工作中,我需要一个类似的预处理器,我想我在 Whosebug 上找到了一个 link 到一个独立的预处理器,但是在网上搜索一个小时后我放弃了。

您可能正在寻找 coan,它能够在给定命令行上提供的一组定义(或未定义)的情况下解释 #if#ifdef 指令。

  • coan 基于此相关问题中列出的 unifdef 实用程序:Partially processing a file with the preprocessor.

  • 另见 Is there a C preprocessor that eliminates #ifdefs but also evaluates preprocessor macros?,其中有一个调用示例。