简体   繁体   English

CPP - 自动删除 #ifdef 和 #ifndef

[英]CPP - Automatically remove #ifdef's and #ifndef's

I am using Geany IDE and I'm trying to get rid of all the #ifdef's and #ifndef's in my code.我正在使用 Geany IDE 并且我正在尝试摆脱代码中的所有#ifdef 和#ifndef There are more than 1000 occurrences, they are nested and it is impossible to do it manually.出现次数超过 1000 次,它们是嵌套的,无法手动完成。

Here is an example:这是一个例子:

#define EXPLAIN

#ifndef EXPLAIN
...
#endif
...
#ifdef EXPLAIN
...
#else
...
#endif

Somewhere the condition is #ifdef and somewhere it is #ifndef .某处条件是#ifdef某处是#ifndef You got the idea... Is there an easy way to get rid of them with all the corresponding code?你明白了......有没有一种简单的方法可以用所有相应的代码来摆脱它们?

I will download and install any other software if needed.如果需要,我将下载并安装任何其他软件。

A good solution is to run gcc using the -E flag which only runs the preprocessor.一个好的解决方案是使用仅运行预处理器的-E标志运行 gcc。 The following command:以下命令:

gcc -E -D EXPLAIN file.cpp -o file.i

Preprocesses file.cpp and outputs file.i which is file.cpp preprocessed with the EXPLAIN macro defined.预处理 file.cpp 并输出 file.i,这是使用定义的 EXPLAIN 宏预处理的 file.cpp。

Some useful flags if you want to produce an output file that is generally more readable and closer to what an input file would be expected to look like:如果您想生成一个 output 文件,一些有用的标志通常更具可读性并且更接近输入文件的预期外观:

  • -D NAME to define the macro NAME with definition 1. -D NAME用定义 1 定义宏NAME
  • -fdirectives-only to process directives but not macros. -fdirectives-only处理指令而不是宏。
  • -C to keep comments. -C保留评论。
  • -P to inhibit line markers. -P禁止行标记。

Even with these flags, #include s will be replaced with the files you are including preprocessed.即使使用这些标志, #include也会被您包含的预处理文件替换。 You can fix this by simply getting rid of the #include s, preprocessing the input file, and then adding the #include s in the output file.您可以通过简单地删除#include 、预处理输入文件,然后在 output 文件中添加#include来解决此问题。

Source: Using the GNU Compiler Collection (GCC): Preprocessor Options .来源:使用 GNU 编译器集合 (GCC):预处理器选项

You can use unifdef to achieve this.您可以使用unifdef来实现这一点。

The unifdef utility selectively processes conditional C preprocessor #if and #ifdef directives. unifdef实用程序选择性地处理有条件的 C 预处理器#if#ifdef指令。 It removes from a file both the directives and the additional text that they delimit, while otherwise leaving the file alone.它从文件中删除指令和它们分隔的附加文本,否则将保留文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM