1

Is there any way to check the arguments used to compile?

Like:

gcc -std=c99 -ggdb3 source.c -o sate-enak

In source.c:

...
#ifdef (-ggdb3 variable is defined)
    do_some_function();
#else
    do_another_function();
#endif
...

With using this method, I can find out if the program is compiled for production or product.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bayuah
  • 251
  • 1
  • 5
  • 10

2 Answers2

3

With gcc, not to my knowledge but you can achieve the same goal with a macro:

In your Makefile:

CFLAGS_DEBUG = -ggdb3 -DDEBUG 
CFLAGS = -std=c99 $(CFLAGS_DEBUG)

then in your program:

#ifdef DEBUG     
    do_some_function();
#else
    do_another_function();
#endif
ouah
  • 142,963
  • 15
  • 272
  • 331
3

There isn't an easy way to spot the options used by the compiler. All else apart, most programs are built from many source files, and those source files may have been compiled with different sets of options.

Usually, if you want to know, you control it with a command-line #define:

gcc -DMODE=MODE_OPTIM -O3 …
gcc -DMODE=MODE_DEBUG -ggdb3 …

where you have a header that defines the meaning of MODE_OPTIM and MODE_DEBUG:

enum CompilationMode { MODE_OPTIM, MODE_DEBUG };

#ifndef MODE
#define MODE MODE_DEBUG
#endif

extern enum CompilationMode compiled_for;

And somewhere you define that:

enum CompilationMode compiled_for = MODE;

And then you can test compiled_for wherever you need to know which mode the program was built with.

Actually, this is runtime decision making. For compile time decision making, you replace the enum with:

#define MODE_OPTIM 0
#define MODE_DEBUG 1

and you can test:

#if MODE == MODE_DEBUG
    do_some_function();
#else
    do_another_function();
#endif
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Or just use the `NDEBUG` macro. (Unless you plan to not `#define` it in your release code either, of course.) – 5gon12eder Dec 23 '14 at 08:52
  • Absolutely not `NDEBUG`; that controls whether assertions are enabled or not, which is independent of whether it is production code or not — at least in my book. I might trigger whether `NDEBUG` is active based on the value of `MODE`; I would not do it the other way around (and thereby overload the meaning of `NDEBUG`). – Jonathan Leffler Dec 23 '14 at 08:53
  • Since the runtime decision wouldn't normally change for a given build, compile time would be preferable... unless you'd like the option to switch from debug to non-debug code paths by modifying a variable from your debugger. – Dmitri Dec 23 '14 at 08:54
  • @Dmitri: it depends on the context and overall goal and what is going to be different between the two functions. I've not normally seen different algorithms for production and debug builds. In my experience, that simply means that the production code is not going to be properly tested because the debug builds don't test it. But you can use either technique, or both in tandem (with suitable tweaks). – Jonathan Leffler Dec 23 '14 at 08:59
  • ..except that a test at runtime for a compile-time option will be either always true or always false, so there's no point in making it a runtime test (unless you might artificially alter the results). – Dmitri Dec 23 '14 at 09:03
  • @Dmitri: true, but you might want to select which algorithm is used…I think my answer covers more than the question requires, but it does cover what the question requires, albeit perhaps in the reverse order. – Jonathan Leffler Dec 23 '14 at 09:04