C++ 代码是否编译为汇编代码?

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

Does the C++ code compile to assembly codes?

c++assemblycompilation

提问by Ata

Does C++ code compile to assembly code? If we have C++ code, will we be able to get assembly code?

C++ 代码是否编译为汇编代码?如果我们有 C++ 代码,我们能得到汇编代码吗?

回答by paxdiablo

The vast majority of C++ compilers will convert the C++ source into object files (machine code with enough control information to be linked into an executable). They mayactually generate assembly language as an interim step and even use a separate assembler for processing the assembler source, but you'll generally never see that. For example, you have to actually go out of your way to get gccto generate assembly code (.s file) by using the -Sflag. Normally, you would never see the assembly.

绝大多数 C++ 编译器会将 C++ 源代码转换为目标文件(具有足够控制信息以链接到可执行文件的机器代码)。它们实际上可能会生成汇编语言作为中间步骤,甚至使用单独的汇编程序来处理汇编程序源代码,但您通常不会看到这一点。例如,您必须实际gcc使用-S标志来生成汇编代码(.s 文件)。通常,您永远不会看到程序集。

But the C++ standard doesn't mandate the final form that's output from the compiler, just that the code has to behave in a certain way when you run it.

但是 C++ 标准并没有强制要求编译器输出的最终形式,只是代码在运行时必须以某种方式运行。

In fact, the earliest C++ "compilers" actually generated C source code and then compiled that.

事实上,最早的 C++“编译器”实际上是生成 C 源代码然后编译它。

You can have your C++ compiler generate object code, Java byte code, or even GWBASIC, should you be feeling masochistic.

如果您感到受虐,您可以让 C++ 编译器生成目标代码、Java 字节代码,甚至 GWBASIC。

回答by Antoine Pelisse

Your code has to be understood by the machine, and, as it is not interpreted nor running in a VM, it is first transformed in assembly. You can get this assembly code by using the -Sflag in your g++ compile options (as long as you are using g++ of course).

您的代码必须被机器理解,并且由于它既不在 VM 中解释也不在 VM 中运行,它首先在汇编中进行转换。您可以通过使用-Sg++ 编译选项中的标志来获取此汇编代码(当然,只要您使用的是 g++)。

g++ -S -o file.s file.cpp

should do the trick.

应该做的伎俩。

回答by stefan

It depends on the compiler. There are no real rules what c++ compiles into, except at some point it should be able run on a computer. Most compilers has a switch to compile to assembly.

这取决于编译器。c++ 编译成什么没有真正的规则,除了在某些时候它应该能够在计算机上运行。大多数编译器都有一个编译为汇编的开关。

With gcc you can add -S to compile into a .asm file.

使用 gcc,您可以添加 -S 以编译为 .asm 文件。

For visual studio see http://codegem.org/2008/10/generate-assembly-from-c-code-in-visual-studio

对于视觉工作室,请参阅http://codegem.org/2008/10/generate-assembly-from-c-code-in-visual-studio