1

How to detect the GCC flag -g issued on command line?

I'm trying some flags ono GCC to see if when we issue a flag -g at command line it's defined any macro to pre-processor.

An example is when we flag -fpie and -fPIE, both define the macros __pie__ and __PIE__.

So, I'm wondering if we flag -g or -g3, something similary will gone happen.

I did a few tests, but nothing came up on the cpp listing.

$ gcc -g3 -E main_debug_flags.c > main_debug_flags.LIST.c

The test code

It is quite simple:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

#ifndef DEBUG
    printf("DEBUG not defined!\n");
#else
    printf("DEBUG defined!\n");
#endif

#ifndef NDEBUG
    printf("NDEBUG not defined!\n");
#else
    printf("NDEBUG defined!\n");
#endif

    return 0;
}

Does anybody has any idea? Maybe, Any one has faced the same doubt!

The alternative is to define by hand the macro -DDEBUG to make sure the code will be compiled with some pre-processor checks.

The readings

I did a little digging into a few documents, but nothing came up!

  • GCC: The Complete Reference, by Arthur Griffith (McGraw-Hill/Osborne)
  • The Definitive Guide to GCC Second Edition, by William von Hagen (Apress, 2006)
  • Using the GNU Compiler Collection,For gcc version 13.0.0

I have compile a simple code with debug flag on and check the pre-processor output to see if something shows up. But nothing!

What I wanna do is detect with #ifdef the -g flag and include addition code to help DEBUG the application itself!

  • What's wrong with adding the `DEBUG` macro explicitly? That's basically what everyone else is doing (including Microsoft Visual Studio when creating C++ projects). – Some programmer dude Jun 01 '23 at 12:27
  • The `NDEBUG` preprocessor symbol already has meaning in the context of ``. On the one side that means "be careful not to get things mixed up", on the other "manually defining a preprocessor symbol to influence debug / no debug behavior is already done". – DevSolar Jun 01 '23 at 12:28
  • 2
    Also note that there are use-cases where one want to add debug information in a release build, which can't be done if the `-g` option added a preprocessor macro that was used. – Some programmer dude Jun 01 '23 at 12:29
  • Does this answer your question? [Get the compiler options from a compiled executable?](https://stackoverflow.com/questions/12112338/get-the-compiler-options-from-a-compiled-executable) – OrenIshShalom Jun 02 '23 at 05:18

1 Answers1

1

No, there is no build-in macros for debug info similar to gcc's __OPTIMIZE__ for optimization. There is simple rationale - debug information can be external or can be stripped from binary after compilation.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Yes, I'm aware of the strip debug-info option. But there is also a rational thought about MACRO. Most case scenarios we build extra blocks of code when compiling with debug flag. I have always a TRACE() macro who output a lot of information when running the code (FILE:LINE:FUNCTION: %s) to stderr or /dev/log. But thank you about the tip! It's shame the `-g` don't define a MACRO. – Willian Silva Jun 01 '23 at 13:28
  • 2
    @WillianSilva The point is that these demands -- having debugging information included in the binary, assertions being added to the binary, the binary generating trace output, ... -- are all independent from each other. You might want to enable all of them. You might want to only activate the tracing. You might want to leave the assertions active but none of the others. Having all of them enabled / disabled with the `-g` switch would be really cumbersome. That's why they aren't. – DevSolar Jun 01 '23 at 14:10
  • @WillianSilva functionality you describe doesn't rely on the presence of debugging info. Stacktracing readable output does though. – Swift - Friday Pie Jun 02 '23 at 09:20