Does gcc define anything when -g is specified?

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.)

Leave a Comment