Does the C++ code compile to assembly codes?
Solution 1
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 may actually 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 gcc
to generate assembly code (.s file) by using the -S
flag. Normally, you would never see the assembly.
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.
In fact, the earliest C++ "compilers" actually generated C source code and then compiled that.
You can have your C++ compiler generate object code, Java byte code, or even GWBASIC, should you be feeling masochistic.
Solution 2
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 -S
flag in your g++ compile options (as long as you are using g++ of course).
g++ -S -o file.s file.cpp
should do the trick.
Solution 3
It depends on the compiler. There are no real rules regarding what C++ compiles into, except at some point it should be able run on a computer. Most compilers have a switch to compile to assembly.
With gcc
you can add -S
to compile into a .asm
file.
For visual studio see: http://codegem.org/2008/10/generate-assembly-from-c-code-in-visual-studio
Related videos on Youtube
Ata
Updated on July 09, 2022Comments
-
Ata almost 2 years
Does C++ code compile to assembly code? If we have C++ code, will we be able to get assembly code?
-
Peter Cordes about 6 yearsre: the part about seeing the assembly: See How to remove “noise” from GCC/clang assembly output? for tips on making it nicer to read for humans. And also Matt Godbolt's CppCon2017 talk: “What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”
-
-
Y.H. about 13 yearsVery informative, just a question, how to compile C++ code to Java bytecode?
-
paxdiablo about 13 yearsWell, you would have to write a compiler to do that. Sorry, I didn't mean to imply there were compilers out there to do that, just that it was both possible and standards-compliant to write one.
-
MSalters about 13 yearsMSVC can compile to MSIL, which is conceptually similar to Java bytecodes.
-
Y.H. about 13 years@paxdliabo :-/ I thought it would be something to brag about as a C++ programmer! @MSalters both managed and unmanaged code is translated to MSIL?
-
SK-logic about 13 years@MSalters, MSIL is not conceptually similar to JVM - it allows unsafe pointer arithmetics, and there are mixed mode assemblies. But, it is certainly possible to compile C++ to JVM, with a significant performance penalty, of course.
-
FrankH. almost 11 yearsJust because you don't see gcc outputting assembly sourcefiles by default doesn't mean it's not created. Just run
gcc -v ...
and have a close look (yes that meansgcc
uses/creates temporary files, and one of them is the assembly source). To be precise: at leastgcc
will always create the assembly sourcefile (and invoke the assembler on it), but the default behaviour is to cleanup and remove those "intermediates". -
emery over 9 yearsYou know this really brings up an important question: would it be practical and useful to develop an open-source "translation" package where people who want to translate, say, Java into Ruby, or C++ into Java, could use a compiler to do it instead of hiring developers to re-write it manually. Does anything like that even exist?
-
paxdiablo over 9 years@emery, those things have been around for a long time, such as p2c for Pascal, f2c for Fortran, even the original CFRONT C++ compiler output C code.
-
old_timer about 6 yearsThe sane compiler authors compile to assembly, then call the assembler, gcc, msvc, etc. they do NOT compile directly to object code nor directly to a binary. So the correct statement is the vast majority compile to assembly language, which was the OP's question...
-
old_timer about 6 yearsthey will use an internal "language"/structure that is a binary, some can export that (llvm) easily and even support that as in input to a tool to generate assembly code. llvm added a beta feature to go to object rather than assembly language.
-
Xantium about 4 yearsYour link is dead.