Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

gcc

clang

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.

like image 712
donturner Avatar asked Aug 02 '16 11:08

donturner


1 Answers

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.

like image 137
Vality Avatar answered Oct 11 '22 14:10

Vality