How to print the GCC optimization options?

Viewed 48

I'm trying to compile my test program with various GCC optimizations options and I'd like to see all of them in the program output. Something like that (the __cpp_optimizations constant is invented for this question):

std::cout << __cpp_optimizations << std::endl;

Is it any way to do that?

1 Answers

Get the compiler options from a compiled executable? . Compile with -frecord-gcc-switches than read the command line options from the executable, in pseudocode:

buf = popen(std::string() + "readelf -p .GCC.command.line /proc/self/exe").read()
// or read elf yourself instead of popen
// tokenize buf, get all compiler options
// filter out only optimization options

Alternatively, you could instruct your build system to pass that information as a macro. For example, in CMake:

add_compile_options(-O3)
add_compile_definitions("CPP_OPTIMIZATIONS=\"-O3\"")
Related