Should --enable-debug set CFLAGS to -O0 instead of -O2?

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Should --enable-debug set CFLAGS to -O0 instead of -O2?
Date: 2025-08-01 03:16:58
Message-ID: CAEoWx2me6o8kHXx8CLdqymeeYkLD=kPKVi9wbx8_PTPO8cM_jw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I noticed that "./configure --enable-debug" will set "-O2" to CFLAGS. To
better debug/trace the code, I have to manually change "-O2" to "-O0" in
src/Makefile.global.

In the script "configure" line 5254 through 5259, I see this comment:

# C[XX]FLAGS are selected so:
# If the user specifies something in the environment, that is used.
# else: If the template file set something, that is used.
# else: If coverage was enabled, don't set anything.
# else: If the compiler is GCC, then we use -O2.
# else: If the compiler is something else, then we use -O, unless debugging.

The last line, "unless debugging", my first impression is that should apply
to GCC. However, the following code shows the GCC always use "-O2".

elif test "$GCC" = yes; then
CFLAGS="-O2"
else
# if the user selected debug mode, don't use -O
if test "$enable_debug" != yes; then
CFLAGS="-O"
fi
fi

For GCC, we need to use "-O0" for best debugging experience. Why here
always uses "-O2" for GCC regardless debugging?

If confirmed a bug, I'd like a submit a patch to fix that.

Thanks,
Chao Li (Evan)
============
HighGo Inc.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-08-01 03:30:03 Re: Should --enable-debug set CFLAGS to -O0 instead of -O2?
Previous Message Chao Li 2025-08-01 02:33:35 Re: Cannot login CommitFests due to cool-off period