Is there a way to store clang compile-time flags i

2019-04-04 06:11发布

问题:

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.

回答1:

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.



标签: c++ c gcc clang