Linux 从编译的可执行文件中获取编译器选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12112338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the compiler options from a compiled executable?
提问by Mosby
It there a way to see what compiler and flags were used to create an executable file in *nix? I have an old version of my code compiled and I would like to see whether it was compiled with or without optimization. Google was not too helpful, but I'm not sure I am using the correct keywords.
有没有办法查看在 *nix 中使用什么编译器和标志来创建可执行文件?我编译了一个旧版本的代码,我想看看它是否经过优化编译。谷歌没有太大帮助,但我不确定我使用了正确的关键字。
采纳答案by Micha? Górny
gcc has a -frecord-gcc-switches
option for that:
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.
Afterwards, the ELF executables will contain .GCC.command.line
section with that information.
之后,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
Of course, it won't work for executables compiled without that option.
当然,它不适用于没有该选项编译的可执行文件。
For the simple case of optimizations, you couldtry using a debugger if the file was compiled with debug info. If you step through it a little, you may notice that some variables were 'optimized out'. That suggests that optimization took place.
对于简单的优化情况,如果文件是使用调试信息编译的,您可以尝试使用调试器。如果您稍微浏览一下,您可能会注意到某些变量已被“优化”。这表明发生了优化。
回答by mah
This is something that would require compiler support. You don't mention what compiler you are using but since you tagged your question linux
I will assume you are using gcc -- which does not default the feature you're asking about (but -frecord-gcc-switches is an option to perform this).
这是需要编译器支持的东西。您没有提到您使用的是什么编译器,但由于您标记了您的问题,linux
我假设您使用的是 gcc——它不会默认您所询问的功能(但 -frecord-gcc-switches 是执行此操作的一个选项)。
If you want to inspect your binary, the strings
command will show you everything that appears to be a readable character string within the file.
如果您想检查您的二进制文件,该strings
命令将向您显示文件中所有看起来是可读字符串的内容。
回答by user1202136
I highly doubt it is possible:
我非常怀疑这是可能的:
int main()
{
}
When compiled with:
编译时:
gcc -O3 -ffast-math -g main.c -o main
None of the parameters can be found in the generated object:
在生成的对象中找不到任何参数:
strings main | grep -O3
(no output)
回答by perreal
If you compile with the -frecord-gcc-switches
flag, then the command line compiler options will be written in the binary in the note section. See also the docs.
如果使用-frecord-gcc-switches
标志进行编译,则命令行编译器选项将写入注释部分的二进制文件中。另请参阅文档。
回答by Zyx 2000
If you still have the compiler (same version) you used, and it is only one flag you're unsure about, you can try compiling your code again, once with and once without the flag. Then you can compare the executables. Your old one should be identical, or very similar, to one of the new ones.
如果您仍然拥有您使用的编译器(相同版本),并且只有一个您不确定的标志,您可以尝试再次编译您的代码,一次使用该标志,一次不使用该标志。然后您可以比较可执行文件。您的旧的应该与新的其中之一相同或非常相似。
回答by Aliaksei Kandratsenka
Another option is -grecord-gcc-swtiches (note, not -f but -g). According to gcc docs it'll put flags into dwarf debug info. And looks like it's enabled by default since gcc 4.8.
另一个选项是 -grecord-gcc-swtices(注意,不是 -f 而是 -g)。根据 gcc 文档,它会将标志放入 dwarf 调试信息中。看起来它从 gcc 4.8 开始就默认启用了。
I've found dwarfdump program to be useful to extract those cflags. Note, strings program does not see them. Looks like dwarf info is compressed.
我发现 dwarfdump 程序对于提取这些 cflags 很有用。注意,字符串程序看不到它们。看起来矮人信息被压缩了。