从编译的可执行文件中获取编译器选项?

有没有办法查看在*

nix中使用了哪些编译器和标志来创建可执行文件?我已经编译了一个旧版本的代码,我想看看它是经过优化还是未经优化而编译的。Google并没有太大的帮助,但是我不确定我使用的关键字是否正确。

回答:

gcc -frecord-gcc-switches为此提供了一个选项:

   -frecord-gcc-switches

This switch causes the command line that was used to invoke the compiler to

be recorded into the object file that is being created. This switch is only

implemented on some targets and the exact format of the recording is target

and binary file format dependent, but it usually takes the form of a section

containing ASCII text.

之后,ELF可执行文件将包含.GCC.command.line带有该信息的部分。

$ gcc -O2 -frecord-gcc-switches a.c

$ readelf -p .GCC.command.line a.out

String dump of section '.GCC.command.line':

[ 0] a.c

[ 4] -mtune=generic

[ 13] -march=x86-64

[ 21] -O2

[ 25] -frecord-gcc-switches

当然,如果没有该选项编译的可执行文件将无法使用。


对于简单的优化情况,如果文件是使用调试信息编译的,则 可以 尝试使用调试器。如果稍稍进行,可能会注意到一些变量已被“优化”。这表明发生了优化。

以上是 从编译的可执行文件中获取编译器选项? 的全部内容, 来源链接: utcz.com/qa/412566.html

回到顶部