如何在大型 C 项目中摆脱 ifdef
我得到了一个用C编码的开源项目。它使用#ifdef 进行交叉编译。源代码中有很多 ifdef。我只想针对一个平台修改它。我想通过编译器的预处理器(Visual C++)运行它,但它会将预处理结果写入单个文件,这是我不需要的。有人知道一种预处理项目并保持其结构完整(所有文件完整)的方法吗?请不要使用 grep。
编辑:
我找到了一个潜在的解决方案(这些天你在互联网上找到的内容真是令人惊奇)。它是 boost.wave - 一个 C++ 预处理器库,可以做一些有趣的事情。我不知道结果会如何,但我会尝试一下。不过,这还不是最终答案,所以如果您有解决方案,我会很高兴听到。
I got my hands on a opensource project coded in C. It uses #ifdef's for cross-compiling. There are a lot of ifdef's all over the source code. I want just to modify it for one platform. I was thinking to run it through compiler's pre-processor (Visual C++) but it will write the preprocessed result to a single file, which I don't need. Anybody knows a way to pre-process a project leaving it's structure intact (all files intact)? No grep, please.
edit:
I found a potential solution (it's amazing what you can find on the internet these days). It's boost.wave - a C++ preprocessor library which can do some interesting stuff. I don't know how it will turn out, but I will give it a try. Still, it's not the final answer, so if you have a solution then I will be glad to hear it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您可以使用两种工具来半自动地完成此操作。
其中之一是
sunifdef
(unifdef
的儿子)。 AFAIK,不过,这不再被维护(它所基于的unifdef
也不再被维护)。另一个是
coan
,它正在积极维护中,并且正在sunifdef
的开发。另请参阅:是否有C 预处理器根据已定义/未定义的值消除
#ifdef
块?。碰巧的是,我仍在工作的主项目中使用
sunifdef
,从代码库中删除过时的代码(例如,自 1996 年左右以来不再支持的机器)。我遇到的唯一问题是,如果一行带有这样的括号:并且我们有
-UMACH_C
(因此不再支持机器 C),则输出行是:从技术上讲,这很好;这是正确的。最好在表达式中保留额外的、技术上多余的括号。
需要注意的是:虽然我可以回答这些在基于 Unix 的系统上的编译问题,但我个人还没有在 Windows 上检查过它们。
There are two tools I know of that you could use to do this semi-automatically.
One is
sunifdef
(son ofunifdef
). AFAIK, this is no longer being maintained, though (and neither isunifdef
on which it is based).The other is
coan
, which is being actively maintained, and is a development ofsunifdef
.See also: Is there a C pre-processor which eliminates
#ifdef
blocks based on values defined/undefined?.As it happens, I'm still using
sunifdef
on the main project at work where I'm eliminating archaic code (for example, machines not supported since about 1996) from the code base. The only gotcha I have with it is that if a line goes in with parentheses like this:and we have
-UMACH_C
(so machine C is no longer supported), the output line is:Technically, that's fine; it is correct. It is just preferable to keep the extra, technically redundant parentheses in the expression.
One caveat: although I can answer for these compiling on Unix-based systems, I've not personally checked them out on Windows.
注释掉/删除 #include 指令,对其进行预处理,恢复包含。
当然,您需要确保 #ifdef 使用的任何宏都可用。
Comment out/remove the #include directives, preprocess it, reinstate the includes.
You'll need to make sure any macros used by the #ifdef are available, of course.