2009-11-15 90 views
2

我想知道如何使用Unix從C程序生成彙編代碼。 我試了gcc:gcc -c file.c從linux中的C文件生成彙編代碼

我也首先使用cpp,然後嘗試as但我得到錯誤。

我想從3個不同的程序

建立一個彙編程序prog1.c的prog2.c prog.h

它是正確的做gcc -S prog1.c prog2.c prog.h? 似乎是不正確的。我不知道如果我有機會從他們每個人的彙編,然後鏈接它們

感謝

+0

您應該從需要編譯的文件列表中刪除prog.h。 – 2009-11-15 23:19:52

+0

但我需要包含標題,我該怎麼做? – Peter 2009-11-15 23:36:33

+0

頭文件應該包含在C文件中。頭文件包含重要的C語言語句,但它們並不構成完整的程序。他們的目的是幫助您避免重複複製大量的樣板文件。相反,你把它放在一個頭文件中,並將其包含在C文件中。 – 2009-11-16 05:02:32

回答

9

根據該手冊:

`-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

5

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. 
+0

並考慮使用標誌-fverbose-asm – mmagin 2009-11-15 22:23:28

5

如果你正在使用gcc(因爲它似乎)是GCC -S。

如果需要,不要忘記用-I指定包含路徑。

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

並且您將使用匯編程序指令獲取my_file.s。

2

objdump -d也很好地工作,並會給你整個二進制文件(exe或共享庫)的程序集清單。

這可能比使用編譯器生成的asm更清晰,因爲對同一源文件中的函數的調用可能顯示爲尚未解析到其最終位置。

使用-g構建代碼,還可以將-line和/或--source添加到objdump標誌。