Seeing expanded C macros

后端 未结 12 1059
清歌不尽
清歌不尽 2020-11-28 05:31

If I want to expand a C macro, what are some good ways to do that (besides tracing it manually)?

For instance, GTK_WIDGET_SET_FLAGS, it uses a macro that

12条回答
  •  感情败类
    2020-11-28 05:34

    GCC -save-temps

    The big advantage of this option over -E is that it is very easy to add it to any build script, without interfering much in the build itself.

    When you do:

    gcc -save-temps -c -o main.o main.c
    

    main.c

    #define INC 1
    
    int myfunc(int i) {
        return i + INC;
    }
    

    and now, besides the normal output main.o, the current working directory also contains the following files:

    • main.i is a contains the desired preprossessed file:

      # 1 "main.c"
      # 1 ""
      # 1 ""
      # 31 ""
      # 1 "/usr/include/stdc-predef.h" 1 3 4
      # 32 "" 2
      # 1 "main.c"
      
      
      int myfunc(int i) {
          return i + 1;
      }
      
    • main.s is a bonus, and contains the desired generated assembly:

          .file   "main.c"
          .text
          .globl  myfunc
          .type   myfunc, @function
      myfunc:
      .LFB0:
          .cfi_startproc
          pushq   %rbp
          .cfi_def_cfa_offset 16
          .cfi_offset 6, -16
          movq    %rsp, %rbp
          .cfi_def_cfa_register 6
          movl    %edi, -4(%rbp)
          movl    -4(%rbp), %eax
          addl    $1, %eax
          popq    %rbp
          .cfi_def_cfa 7, 8
          ret
          .cfi_endproc
      .LFE0:
          .size   myfunc, .-myfunc
          .ident  "GCC: (Ubuntu 8.3.0-6ubuntu1) 8.3.0"
          .section    .note.GNU-stack,"",@progbits
      

    If you want to do it for a large number of files, consider using instead:

     -save-temps=obj
    

    which saves the intermediate files to the same directory as the -o object output instead of the current working directory, thus avoiding potential basename conflicts.

    Another cool thing about this option is if you add -v:

    gcc -save-temps -c -o main.o -v main.c
    

    it actually shows the explicit files being used instead of ugly temporaries under /tmp, so it is easy to know exactly what is going on, which includes the preprocessing / compilation / assembly steps:

    /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu main.c -mtune=generic -march=x86-64 -fpch-preprocess -fstack-protector-strong -Wformat -Wformat-security -o main.i
    /usr/lib/gcc/x86_64-linux-gnu/8/cc1 -fpreprocessed main.i -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase-strip main.o -version -fstack-protector-strong -Wformat -Wformat-security -o main.s
    as -v --64 -o main.o main.s
    

    Tested in Ubuntu 19.04 amd64, GCC 8.3.0.

提交回复
热议问题