Jump to content

How to get compiler flags from compile time to show up in runtime in C?

Gat Pelsinger

I want to show at runtime as a way of debugging with what compiler flags the program was compiled in. There are a few definitions which get defined and I can use conditional statements on them, but they don't gave that much information. For example if I want to know with how many optimizations my C program was compiled in, there is no definition which gets defined when a certain optimization level is used, only the __OPTIMIZE__ gets defined which only tells if the program was compiled with optimizations or not and not with how many levels of optimizations it was compiled with. Is there any other way? Makefile logging?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

Optimization level is an abstraction of a series of compiler options which could be used individually so I can see why there wouldn't be a special predefined macro for it; it might even change which exact flags are enabled depending on the compiler version. If you want to keep track of this (although I struggle to see a use case) you could manually define a preprocessor variable using the -D flag (in GCC):

gcc -O3 -Doptimization_level=3 [...]

then in your code:

#ifdef optimization_level
const char OPT_LEVEL = optimization_level
#else
const char OPT_LEVEL = 0
#endif

this way at runtime you could check the value of OPT_LEVEL to find out which optimization level was used... assuming you passed the right value. This can be automated with something like cmake. Alternatively you could define a string containing all compiler options.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

@Sauron I will try this, but what if I want to log all the compiler flags used?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, Gat Pelsinger said:

@Sauron I will try this, but what if I want to log all the compiler flags used?

as I said:

2 hours ago, Sauron said:

you could define a string containing all compiler options.

in gcc there is also this: https://stackoverflow.com/a/12112479

Quote

gcc has a -frecord-gcc-switches option for that:

   -frecord-gcc-switches
       This switch causes the command line that was used to invoke the compiler to
       be recorded into the object file that is being created.  This switch is only
       implemented on some targets and the exact format of the recording is target
       and binary file format dependent, but it usually takes the form of a section
       containing ASCII text.

Afterwards, the ELF executables will contain .GCC.command.line section with that information.

$ gcc -O2 -frecord-gcc-switches a.c
$ readelf -p .GCC.command.line a.out 

String dump of section '.GCC.command.line':
  [     0]  a.c
  [     4]  -mtune=generic
  [    13]  -march=x86-64
  [    21]  -O2
  [    25]  -frecord-gcc-switches

Of course, it won't work for executables compiled without that option.

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

@Sauron But they seem to use readelf, which only works in Linux and I don't know if there is a Windows equivalent, and second, how do make it read it in my program, and not out of it?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Gat Pelsinger said:

@Sauron But they seem to use readelf, which only works in Linux and I don't know if there is a Windows equivalent, and second, how do make it read it in my program, and not out of it?

You can read the ELF header of the executable at runtime https://stackoverflow.com/a/34960487

 

Honestly this whole thing looks like more work than it's worth. Why do you even need to know which optimization level was used at runtime?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×