Do all C++ compilers generate C code?

前端 未结 5 2124
悲&欢浪女
悲&欢浪女 2020-12-11 04:25

Probably a pretty vague and broad question, but do all C++ compilers compile code into C first before compiling them into machine code?

相关标签:
5条回答
  • 2020-12-11 04:42

    No. This is a myth, based around the fact that a very early version of Stroustrup's work was implmented that way. C++ compilers generate machine code in almost exactly the same way that C compilers do.

    Today, the only C++ compiler that I am aware of that creates C code is Comeau. There may be one or two more for embedded targets, but it is certianly not a mainstream thing.

    0 讨论(0)
  • 2020-12-11 04:47

    Nope. GCC for example goes from C++ -> assembler. You can see this by using the -S option with g++.

    Actually, now that I think about it, I don't think any modern compiler goes to C before ASM.

    0 讨论(0)
  • 2020-12-11 04:49

    This is not defined by the standard. Certainly, compiling to C-source is a reasonable way to do it. It only requires the destination platform to have a C-compiler with a reasonable degree of compliance, so it is a highly portable way of doing things. The downside is speed. Probably compilation speed and perhaps also execution speed (due to loads of casts for e.g. virtual functions that prevents the compiler to optimise fully) will suffer. Not that long ago there was a company that had a very nice C++ compiler doing exactly that. Unfortunately, I do not remember the name of the company and a short google did not bring the name back. The owner of the company was an active participant in the ISO C++ committee and you could test your code directly on the homepage, which also had some quite decent ressources about C++. Edit: one of my fellow posters just reminded me. I was talking about Comeau, of course.

    0 讨论(0)
  • 2020-12-11 04:56

    No. C++ -> C was used only in the earliest phases of C++'s development and evolution. Most C++ compilers today compile directly to assembler or machine code. Borland C++ compiles directly to machine code, for example.

    0 讨论(0)
  • 2020-12-11 05:00

    Because C compilers are nearly ubiquitous and available on nearly every platform, a lot of (compiled) languages go through this phase in their development to bootstrap the process.

    In the early phases of language development to see if the language is feasible the easiest way to get a working compiler out is to build a compiler that converts your language to C then let the native C compiler build the actual binary.

    The trouble with this is that language specific constructs are lost and thus potential opportunities for optimization may be missed thus most languages in phase two get their own dedicated compiler front end that understands language specific constructs and can thus provide optimization strategies based on these constructs.

    C++ has gone through phase 1 and phase 2 over two decades ago. So it is easy to find a `front end' of a compiler that is dedicated to C++ and generates an intermediate format that is passed directly to a backed. But you can still find versions of C++ that are translated into C (as an intermediate format) before being compiled.

    0 讨论(0)
提交回复
热议问题