【问题标题】:Generate assembler code from C file in linux在linux中从C文件生成汇编代码
【发布时间】:2009-11-15 22:10:43
【问题描述】:

我想知道如何使用 Unix 从 C 程序生成汇编代码。 我试过 gcc:gcc -c file.c

我也先使用cpp,然后尝试as,但出现错误。

我正在尝试从 3 个不同的程序构建一个汇编程序

prog1.c prog2.c prog.h

gcc -S prog1.c prog2.c prog.h 是否正确? 似乎这不正确。我不知道我是否必须从它们中的每个生成汇编器然后链接它们

谢谢

【问题讨论】:

  • 你应该从你必须编译的文件列表中删除 prog.h。
  • 但我需要包含标题,我该怎么做?
  • 头文件应该包含在 C 文件中。标头包含重要的 C 语言语句,但它们并不构成完整的程序。它们的目的是帮助您避免一遍又一遍地复制大量样板文件。相反,您将其放在头文件中并将其包含在您的 C 文件中。

标签: c unix gcc


【解决方案1】:

根据手册:

`-S'
     Stop after the stage of compilation proper; do not assemble.  The
     output is in the form of an assembler code file for each
     non-assembler input file specified.

     By default, the assembler file name for a source file is made by
     replacing the suffix `.c', `.i', etc., with `.s'.

     Input files that don't require compilation are ignored.

所以试试gcc -S file.c

【讨论】:

    【解决方案2】:

    来自man gcc

       -S     Stop  after the stage of compilation proper; do not
              assemble.  The output is an assembler code file for
              each non-assembler input file specified.
    
              By default, GCC makes the assembler file name for a
              source file by replacing  the  suffix  `.c',  `.i',
              etc., with `.s'.  Use -o to select another name.
    
              GCC ignores any input files that don't require com-
              pilation.
    

    【讨论】:

    • 并考虑使用标志 -fverbose-asm
    【解决方案3】:

    如果您使用的是 gcc(看起来),那就是 gcc -S。

    如果需要,不要忘记使用 -I 指定包含路径。

    gcc -I ../my_includes -S my_file.c
    

    您将获得带有汇编程序说明的 my_file.s。

    【讨论】:

      【解决方案4】:

      objdump -d 也非常好用,它会为您提供整个二进制文件(exe 或共享库)的程序集列表。

      这比使用编译器生成的 asm 更清晰,因为对同一源文件中的函数的调用可能会显示尚未解析到它们的最终位置。

      使用 -g 构建您的代码,您还可以将 --line 和/或 --source 添加到 objdump 标志。

      【讨论】:

        猜你喜欢
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 1970-01-01
        • 2021-07-18
        • 2017-07-19
        • 2011-02-17
        • 2011-10-08
        相关资源
        最近更新 更多