运行preprocessor只,但只有某些语句 [英] Run preprocessor only but with only for certain statements

查看:115
本文介绍了运行preprocessor只,但只有某些语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司拥有一批在程序中定义调试语句,我希望能够使源代码的副本没有这些语句。

I have a number of debug statements defined in a program, and I want to be able to make a copy of the source without these statements.

在为了做到这一点,我第一次看GCC的-E命令行参数,只运行preprocessor,但是这确实远远超过了我想要的,扩大了包括文件和添加的#line语句。

In order to do this I first looked at GCC's -E command line argument, which only runs the preprocessor, however this did far more than I wanted, expanding the included files and adding #line statements.

例如:

#include <stdio.h>

#ifdef DEBUG
    #define debug( s ) puts ( s );
#else
    #define debug( s )
#endif

int main( int argc, char* argv[] )
{
    debug( "Foo" )

    puts( "Hello, World!" );

    return 0;
}

我希望这被加工成:

I'd want this to be processed to:

#include <stdio.h>

int main( int argc, char* argv[] )
{


    puts( "Hello, World!" );

    return 0;
}

然后我可以整理那以类似的astyle,没有手工作业将需要得到正是我想要的。

I could then tidy that up with something like astyle and no manual work would be needed to get exactly what I want.

有没有指示我丢失了GCC或者是有能力这样做的?

Is there a directive I'm missing for GCC or is there a tool capable of doing this?

推荐答案

如果 -E 是没有帮助,那么请尝试使用 -fdump树-all ,如果你看不到你想要的,是不是可用式(或)不提供的,由的 GCC

If -E is not helping, then try using -fdump-tree-all and if you don't see what you want the that is not-available-in (or) not-provided-by GCC.

OTOH,这个问题已经在SO如下,请参考下面得到一些想法进行讨论。

OTOH, this question has been discussed in SO as follows, please refer the below to get some ideas.


  1. 能的gcc后C输出code preprocessing?

  2. C / C ++ preprocessing
  1. Can gcc output C code after preprocessing?
  2. C/C++ source file after preprocessing

希望它帮助!

我看到了@nos您的评论。但我有方便的一个这样的剧本,所以与大家分享。您可以尝试阅读我的答案类似的问题<一href=\"http://stackoverflow.com/questions/8742418/avoid-the-$p$pprocessing-of-includes/8742815#8742815\">here

I saw your comment to @nos. But I have one such script handy and so sharing it with you. You can try reading my answer for a similar question here

复制下面code在一个文件,说 convert.sh 。指定执行权限到该文件,搭配chmod + X convert.sh 键,如下运行它:

Copy the below code in a file, say convert.sh. Assign execute permission to that file, chmod +x convert.sh and run it as follows:

$./convert.sh <filename>.c
$cat filename.c.done

&LT;&名GT;!.c.done 将有你需要的东西。

#!/bin/bash

if [[ $# -ne 1 || ! -f $1 ]] ; then
    echo "Invalid args / Check file "
    exit 
fi

file_name=$1

grep '^\s*#\s*include' $file_name > /tmp/include.c
grep -Pv '^\s*#\s*include\b' $file_name > /tmp/code.c
gcc -E /tmp/code.c | grep -v ^# > /tmp/preprocessed.c
cat /tmp/include.c > $file_name.done
cat /tmp/preprocessed.c >> $file_name.done

希望这有助于!

这篇关于运行preprocessor只,但只有某些语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆