Is there a flag or other reliable method to detect if a compiled C++ binary was compiled with optimizations?
I’m okay with compiler-specific solutions.
Edit: This is for a build deployment system, which could accidentally deploy binaries that were not built properly. A water-tight solution is unlikely, but it will save some pain (and money) if this can be detected some of the time.
The compiler will often be gcc, sometimes sun, and if there’s a MSVC solution, I don’t want to rule that out for the benefit of the community.
Recent versions of GCC have a way to report which flags were used to compile a binary (third bullet point).
There is a related command line switch (–fverbose-asm) that “only records the information in the assembler output file as comments, so the information never reaches the object file.” The –frecord-gcc-switches switch “causes the command line that was used to invoke the compiler to be recorded into the object file that is being created.”