Run preprocessor only but with only for certain statements

前端 未结 5 1081
-上瘾入骨i
-上瘾入骨i 2020-12-29 07:49

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.

In order to do this I first lo

相关标签:
5条回答
  • 2020-12-29 08:27

    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, this question has been discussed in SO as follows, please refer the below to get some ideas.

    1. Can gcc output C code after preprocessing?
    2. How do I see a C/C++ source file after preprocessing in Visual Studio?

    Hope it helps!


    Hi Mat,

    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

    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
    

    The <filename>.c.done will have what you need!

    #!/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
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-29 08:35

    There's no direct way to do that with the gcc preprocessor, though if you only include system headers, you might have some luck with gcc -E -nostdinc.

    However, you can comment out the #include directives, and other preprocessor directives you don't want processed, and run the code through the preprocessor (gcc -E or cpp) , that way only the macro you want expanded(the ones not commented out) gets expanded.

    0 讨论(0)
  • 2020-12-29 08:37

    gcc -E -nostdinc test.c produces

    # 1 "test.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "test.c"
    # 9 "test.c"
    int main( int argc, char* argv[] )
    {
    
    
        puts( "Hello, World!" );
    
        return 0;
    }
    

    and an error to stderr

    test.c:1:19: error: no include path in which to search for stdio.h
    

    You can easily filter out the # lines ... and re-add the includes.

    0 讨论(0)
  • 2020-12-29 08:45

    I know the question is old, but it does have an answer now. The "C Partial Preprocessor" does exactly this.

    http://www.muppetlabs.com/~breadbox/software/cppp.html

    For reference, if someone else still wonders (I did and found this page).

    0 讨论(0)
  • 2020-12-29 08:47

    One may use tools like unifdef, unifdefall — remove preprocessor conditionals from code. (Run a "light" preprocessor for GCC)

    0 讨论(0)
提交回复
热议问题