首页>Program>source

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

最新回答
  • 2021-1-9
    1 #

    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
    

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


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

  • 2021-1-9
    2 #

    如果使用 -frecord-gcc-switches进行编译 标记,则命令行编译器选项将以二进制形式写入注释部分.另请参阅文档。

  • 2021-1-9
    3 #

    另一个选项是-grecord-gcc-swtiches(注意,不是-f而是-g).根据gcc docs的介绍,它将标记放入矮小的调试信息中.从gcc 4.8开始,它似乎是默认启用的。

    我发现dwarfdump程序对于提取那些cflag是有用的.注意,字符串程序看不到它们.似乎矮信息已压缩。

  • 2021-1-9
    4 #

    这需要编译器支持.您没有提到正在使用什么编译器,但是由于您标记了问题 linux 我将假设您使用的是gcc -不会默认您要查询的功能(但是-frecord-gcc-switches是执行此操作的选项)。

    如果您想检查二进制文件,请 strings 该命令将向您显示文件中似乎是可读字符串的所有内容。

  • 2021-1-9
    5 #

    我非常怀疑是否有可能:

    int main()
    {
    }
    

    使用以下命令编译时:

    gcc -O3 -ffast-math -g main.c -o main
    

    在生成的对象中找不到任何参数:

    strings main | grep -O3
    (no output)
    

  • deployment:Android Studio未将更改部署到应用程序
  • xml:您如何在全局范围内将Jackson设置为忽略Spring中的未知属性?