您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

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

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

其他 2022/1/1 18:14:37 有608人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶