Does gcc define anything when -g is specified?

前端 未结 3 1247
忘掉有多难
忘掉有多难 2020-12-14 05:25

Shortly, I want to know if gcc (or g++. I need it in C, but also am curious about c++) defines any special symbols if -g is enabled. Does it? I

相关标签:
3条回答
  • 2020-12-14 06:08

    As others have said, and as is apparent from the manual, GCC does not provide any intrinsic macros that indicate whether debug information will be generated (-g mode). What I want to add, is that this is on purpose.

    Long, long ago, the original authors of GCC (RMS, Kenner, probably a couple others) decided that enabling debugging information should not cause any changes whatsoever to the actual code, to reduce the risk of a bug mysteriously vanishing when you try to debug it. This is a bedrock design principle in GCC, and even today the developers go out of their way to maintain it.

    Defining a macro would violate this design principle, since it would permit the source code to change itself in response to -g.

    0 讨论(0)
  • 2020-12-14 06:16

    Running
    g++ -E -dD -xc++ /dev/null
    vs.
    g++ -E -dD -g -xc++ /dev/null
    shows me no extra defined symbols.

    That, combined with the lack of any documentation stating that symbols get defined, should be enough to safely say that nothing gets defined.

    0 讨论(0)
  • 2020-12-14 06:22

    You can see the list of all macros gcc/g++ defines for any combination of flags like that:

    $ g++ -E -dD -xc++ /dev/null
    

    For example:

    [max@truth ~]$ g++ -E -dD -xc++ /dev/null > a
    [max@truth ~]$ g++ -E -dD -xc++ -g -O3 /dev/null > b
    [max@truth ~]$ diff a b
    1a2
    > # 1 "/home/max//"
    173c174
    < #define __NO_INLINE__ 1
    ---
    > #define __OPTIMIZE__ 1
    

    Let's see whether -g defines anything:

    [max@truth ~]$ g++ -E -dD -xc++ -g /dev/null > c
    [max@truth ~]$ diff a c
    1a2
    > # 1 "/home/max//"
    

    Extra preprocessor directive, but no extra macros for -g flag.


    This makes sense: -g has no effect on code-gen, only on object-file metadata (debug symbols).

    -O0 is "debug mode" code-gen for consistent debugging, as opposed to -Og or higher. (This answer explains what that means in more detail for compilers like gcc and clang: store all variables back to their memory locations after every statement, and don't optimize across statements.)

    -O3 and -g are orthogonal (except that you get "optimized out" for some variables when debugging optimized code, and execution appears to jump around between source lines.)

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