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

后端 未结 17 1825
天命终不由人
天命终不由人 2020-11-22 02:43

How does one do this?

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

17条回答
  •  鱼传尺愫
    2020-11-22 03:10

    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.

提交回复
热议问题