Do the `-g` and `-Ox` compilation flags conflict with each other

Consider commonly used compilers (gfortran, ifort, etc), and suppose that x is 0, 1, …, or nothing.

Do the -g and -Ox compilation flags conflict with each other? Will -g turn off some optimization that is intended to be turned on by -Ox? Do -g -Ox and -Ox -g behave the same?

I asked ChatGPT, which said that they do not conflict in general, but I choose to trust the human experts here. Thank you.

No, you can include debug information without influencing optimisation. If you build with CMake you’d typically do this with the RelWithDebInfo build type. If you want to generate stacktraces e.g. along with error messages then you might want to use -g in release builds as well.

1 Like

Thank you @plevold for the response.

It happened to me just now (after asking the question) that the executable generated by compiler -Ofast encountered a SIGSEV, but it finished without any error when compiled with compiler -g -Ofast. Sorry that I do not have a minimal working example yet.

Did a similar thing happen to anyone here before?

It’s not uncommon to have a code that crashes or not depending on the compilation options, including -g. The worst case for debugging is when it crashes without -g but doesn’t with -g :frowning:

Sorry for the wrong information. There turns out an error in my Makefile, so compilation flags were not appended as intended. The claim above is wrong.

However, according to @PierU , similar things did happen somewhere else.