Linux 如何使用 gcc 编译为程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8021874/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:03:39  来源:igfitidea点击:

How can I compile to assembly with gcc

clinuxgccx86

提问by MetallicPriest

How do I compile to assembly instead of an executable with gcc. I know there is an -Sflag, but where do I use it in the makefile. For example, if I use flags -O3 -o exe_name, where should I put the -S flag?

如何使用 gcc 编译为程序集而不是可执行文件。我知道有一个-S标志,但是我在 makefile 中的哪里使用它。例如,如果我使用标志-O3 -o exe_name,我应该在哪里放置 -S 标志?

采纳答案by Basile Starynkevitch

I suggest also using -fverbose-asmbecause then the generated assembler has some generated comments which "explains" the code. For example:

我建议也使用,-fverbose-asm因为生成的汇编器会生成一些“解释”代码的注释。例如:

gcc -S -fverbose-asm -O2 foo.c

would generate in foo.s(with some comments) the assembler code produced by compiling foo.c

将在foo.s(带有一些注释)中生成通过编译产生的汇编代码foo.c

And to understand what the GCC optimizations are doing one could even try -fdump-tree-all(but this produces hundredsof files!).

并且要了解 GCC 优化正在做什么,甚至可以尝试-fdump-tree-all(但这会产生数百个文件!)。

回答by Macmade

You can ask GCC to produce the assembly file, instead of an object file (or an executable).

您可以要求 GCC 生成程序集文件,而不是目标文件(或可执行文件)。

For instance:

例如:

gcc -Wall -c test.c

Will produce an object file from test.c (test.o).

将从 test.c (test.o) 产生一个目标文件。

gcc -Wall -o test test.c

Will produce an executable file named 'test' from test.c

将从 test.c 生成一个名为“test”的可执行文件

gcc -Wall -S test.c

Will produce an assembly file from test.c (test.s)

将从 test.c (test.s) 生成一个汇编文件

回答by Mat

Put it pretty much anywhere.

几乎可以放在任何地方。

gcc -O3 -S -o output.asm ...

will store the generated assemby in output.asm.

将生成的程序集存储在output.asm.