# Is there a way to know which options were used at compile time?

Take a look at the following command line:

gcc -o hello -Wall -D_BSD_SOURCE hello-world.c


Now, is there a way to know about these options by doing some processing on the 'hello' executable.

-

Sadly, no. But if you think about it before you create a binary, there are some ways. Here's another. With recent gcc, you can use -frecord-gcc-switches option which will add one section to the ELF file with the description you are seeking.

$gcc -frecord-gcc-switches -o hello -Wall -D_BSD_SOURCE hello-world.c$ readelf -p .GCC.command.line hello

String dump of section '.GCC.command.line':
[     0]  -D _BSD_SOURCE
[     f]  hello-world.c
[    1d]  -mtune=generic
[    2c]  -march=x86-64
[    3a]  -Wall
[    40]  -frecord-gcc-switches


As you can see it shows you all used options, not just those you provided explicitly.

-

Impossible, but compiler version might be available if you didn't strip the binary.

If you refer to the macro stuff, i.e #ifdef HAVE_XXX, it was recorded in the C/C++ source file. Take a look at GNU autogen tool chains.

-

No way, but if you use Makefile, there's a way:

TARGET=hello
CFLAGS=-Wall -D_BSD_SOURCE hello-world.c
all:
cc -o $(TARGET)$(CFLAGS) -DCFLAGS="\"\$(CFLAGS)\""

-