在c++中剥离编译源代码的任何脚本

Any script to strip out the compiled source in C++

本文关键字:任何 脚本 源代码 编译 c++ 剥离      更新时间:2023-10-16

在我的c++文件中,我有

#ifdef DEBUG 
    then blah
#else 
    blooh. 

我想去掉预处理后没有编译的所有文本,这样如果DEBUG没有定义,那么所有形式的语句:

#ifdef DBUG 
    /* some debug code */ 
#endif

被从源文件中剥离。

编辑:下面是一个例子:

#include <iostream>
//#define DEBUG
int main(){
  #ifdef DEBUG
      cout << "In debugn";
  #endif
     cout << "hellon";
  return 0;
 }

运行脚本后,输出应为

#include <iostream>
//#define DEBUG
int main(){
  cout << "hellon";
 return 0;
}

仅仅运行预处理器还不够好吗?例如:g++ -E ?

用适当的定义运行编译器的预处理器。在Windows上,这将是cl /EP file,在Linux上是gcc -E。最有可能的是,您也必须传递您的定义,使用-DFoo .

我不知道你问题的答案,但谷歌知道:

    C的部分预处理器
  • cpp-partial——预处理器指令部分求值器

预处理器执行此操作。

您可以使用g++ -E somefile.cpp查看它生成的内容