How can I compile to assembly with gcc

2020-05-21 08:14发布

How do I compile to assembly instead of an executable with gcc. I know there is an -S flag, 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?

标签: c linux gcc x86
3条回答
再贱就再见
2楼-- · 2020-05-21 08:44

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

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

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

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

查看更多
干净又极端
3楼-- · 2020-05-21 09:05

Put it pretty much anywhere.

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

will store the generated assemby in output.asm.

查看更多
萌系小妹纸
4楼-- · 2020-05-21 09:06

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

For instance:

gcc -Wall -c test.c

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

gcc -Wall -o test test.c

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

gcc -Wall -S test.c

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

查看更多
登录 后发表回答