28

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

Mat
  • 202,337
  • 40
  • 393
  • 406
Ata
  • 12,126
  • 19
  • 63
  • 97
  • re: the part about seeing the assembly: See [How to remove “noise” from GCC/clang assembly output?](https://stackoverflow.com/questions/38552116/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”](https://youtu.be/bSkpMdDe4g4)** – Peter Cordes Mar 26 '18 at 23:33
  • Re: the compiler-construction aspect: [Does a compiler always produce an assembly code?](https://stackoverflow.com/q/14039843) – Peter Cordes May 29 '22 at 16:51

3 Answers3

37

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    Very informative, just a question, how to compile C++ code to Java bytecode? – Y.H. Jan 24 '11 at 07:37
  • 4
    Well, 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. – paxdiablo Jan 24 '11 at 08:03
  • MSVC can compile to MSIL, which is conceptually similar to Java bytecodes. – MSalters Jan 24 '11 at 10:00
  • @paxdliabo :-/ I thought it would be something to brag about as a C++ programmer! @MSalters both managed and unmanaged code is translated to MSIL? – Y.H. Jan 24 '11 at 23:41
  • @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. – SK-logic Mar 23 '11 at 21:25
  • 2
    Just 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 means `gcc` uses/creates _temporary files_, and one of them is the _assembly source_). To be precise: at least `gcc` will _always_ create the assembly sourcefile (and invoke the assembler on it), but the default behaviour is to _cleanup_ and remove those "intermediates". – FrankH. Jun 20 '13 at 09:41
  • You 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? – emery Dec 04 '14 at 20:26
  • 1
    @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. – paxdiablo Jan 07 '15 at 11:51
  • The 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 Mar 26 '18 at 19:12
  • they 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. – old_timer Mar 26 '18 at 19:14
24

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.

Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
6

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

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
stefan
  • 2,886
  • 21
  • 27