How do you get assembler output from C/C++ source in gcc?

Doug T. picture Doug T. · Sep 26, 2008 · Viewed 361.6k times · Source

How does one do this?

If I want to analyze how something is getting compiled, how would I get the emitted assembly code?

Answer

Andrew Edgecombe picture Andrew Edgecombe · Sep 26, 2008

Use the -S option to gcc (or g++).

gcc -S helloworld.c

This will run the preprocessor (cpp) over helloworld.c, perform the initial compilation and then stop before the assembler is run.

By default this will output a file helloworld.s. The output file can be still be set by using the -o option.

gcc -S -o my_asm_output.s helloworld.c

Of course this only works if you have the original source. An alternative if you only have the resultant object file is to use objdump, by setting the --disassemble option (or -d for the abbreviated form).

objdump -S --disassemble helloworld > helloworld.dump

This option works best if debugging option is enabled for the object file (-g at compilation time) and the file hasn't been stripped.

Running file helloworld will give you some indication as to the level of detail that you will get by using objdump.