13

Is there a way to store the compile-time flags in the output binary when using clang?

For example after running:

clang -O3 -c main.c

The resulting main.o file should somewhere contain -O3.

gcc has -frecord-gcc-switches but I'm unable to find an equivalent for clang.

Community
  • 1
  • 1
donturner
  • 17,867
  • 8
  • 59
  • 81

1 Answers1

4

As ecatmur already has implied in the comments. This feature is currently not supported as documented in bug https://llvm.org/bugs/show_bug.cgi?id=16291 .

However as a work around while the feature is not available I would suggest having your build process define a macro inside the program using clang's -D argument. For example assuming you are invoking this from a bash script (adjust to whatever build tool you use):

CLANG_ARGS='-O3 -c main.c'
clang $CLANG_ARGS -D CLANG_ARGS="\"${CLANG_ARGS}\""

Then in your C or C++ programs you add something along the lines of:

const char clangArgs[] = CLANG_ARGS;

Which you can then retrieve using a debugger or some such or even could add some code to print it from your program when invoked with the -V or --version switch.

Vality
  • 6,577
  • 3
  • 27
  • 48