如何做到这一点呢?

如果我想分析某些东西是如何编译的,我该如何获得发出的程序集代码?


当前回答

最近我想知道a中每个函数的组装,我是这样做的:

gcc main.c    // 'main.c' source file
gdb a.exe     // 'gdb a.out' in Linux

广东发展银行:

disass main   // Note here 'main' is a function
              // Similarly, it can be done for other functions.

其他回答

最近我想知道a中每个函数的组装,我是这样做的:

gcc main.c    // 'main.c' source file
gdb a.exe     // 'gdb a.out' in Linux

广东发展银行:

disass main   // Note here 'main' is a function
              // Similarly, it can be done for other functions.

使用“-S”作为选项。它在终端中显示程序集输出。

我在答案中没有看到这种可能性,可能是因为这个问题来自2008年,但在2018年,你可以使用马特·戈德博尔特的在线网站https://godbolt.org

你也可以在本地克隆git并运行他的项目https://github.com/mattgodbolt/compiler-explorer

这将生成C代码+行号交织的汇编代码,以便更容易地查看哪些行生成了什么代码(-S -fverbose-asm -g -O2):

# Create assembler code:
g++ -S -fverbose-asm -g -O2 test.cc -o test.s

# Create asm interlaced with source lines:
as -alhnd test.s > test.lst

在第3页(PDF的第15页)中找到了它。

使用-S开关:

g++ -S main.cpp

或者也可以用gcc:

gcc -S main.c

还有这个。