Why there's no dedicated compiler for c or c++? - c++

Seems all compilers can deal with both c and c++,like gcc ,msvc...
Is it because these 2 languages are exactly very much alike?

Actually, GCC (GNU Compiler Collection) has two different front-ends, gcc and g++. To specify C++, you can also use a .cpp (or a few others) extension, or -x c++, when executing gcc. However, this requires extra options (like linking in the C++ standard library).
cl, Microsoft's C++ compiler, does not support modern C. However, it will compile C source files as a variant of C89, and you can specify this explicitly with /TC.
For both, you are correct that there's a lot of shared code, regardless of which front-end is used (GCC also has many more). However, the languages do have significant differences, which are discussed elsewhere (this question among others).

There is no dedicated compiler for C/C++ because there is no such language....
If you are going to write a C++ compiler then you will have to be able to compile C too, so you may as well also provide one.
There may still be some C compilers around that do not have a companion C++ compiler with them though.

No. That's not true. Look at Pelles which is a C only compiler.

The semantics of the core language constructs for C and C++ remain more or less identical, and C++ was designed to add structural elements to C rather than change or remove existing language features. Therefore if you go to the trouble to build a C++ compiler, having it also compile C is relatively trivial (at least for ISO C90). C99 diverges in some significant ways from C++, and some C++ compilers either do not support C99, or include C99 features as extensions in their C++ compiler.
C++ is also highly inteoperable with C, C++ for example wholly includes ISO C90's standard library, and can link any C library. C++ libraries can be given a C linkage compatible interface for use by C code (although that is often less straightforward than C++ calling C code).
Early C++ tools were not true compilers, but rather C++ translators, which generated C code for compilation by a native C compiler. Comeau C++ still takes this approach in order to support C++ on any target with a C compiler, which is useful in embedded environments where some targets are not well served by C++ tools.

TCC is an example of a C compiler which is not a C++ compiler. Actually compiling C++ is a huge pain; the only reason so many C compilers also support C++ is that there's a pretty big demand for C++.

C++ is a superset of C. I don't know if this is still true, but it at least used to be common for c++ compilers to convert code to C as a first step in compiling.
Edit:
I've always heard that it is a superset. Since GMan says no, I looked at Wikipedia which says, "C++ is often considered to be a superset of C, but this is not strictly true.[21] Most C code can easily be made to compile correctly in C++, but there are a few differences that cause some valid C code to be invalid in C++, or to behave differently in C++." (See http://en.wikipedia.org/wiki/C%2B%2B for details.) So I stand a bit corrected.
Edit 2:
I've read a bit further in the Wikipedia article. Sounds like this is more accurate: C++ started as C; C++ evolved by adding new features to C. At some point, C++ changed enough to no longer be a pure superset of C. Since then, C has also evolved, and now has some features that C++ doesn't. So they're closely related, but no longer wholly compatible with each other.

No, LCC compiler is for C only.

GCC has gcc and the g++ components which compile C and C++ code.
clang-llvm has a C front-end. There is an experimental, separate C++ front-end.
IBM's Visual Age is split into xlc and xlC compilers.
Portable C Compiler is C only.

Last time I used it, Labwindows/CVI suite by National Instruments was a C only compiler.

It isn't true, there are several, and there were plenty in the 1980s before C++ compiler products started to appear :-) However given a C++ compiler the marginal cost of producing a C compiler out of the same codebase is relatively small, and even going the other way isn't a major increment, at least compared tom starting from scratch with either.

Related

Will a C program compile in a C++ workspace?

I'm new to C/C++ and I have just downloaded the CodeLite IDE (any other suggestions for free ones would be welcome).
I intend to write a C program, but it only gives me an option to create a C++ workspace. Can I write my C in this workspace all the same?
It depends on your IDE, your compiler (which is invoked by IDEs to compile code, and usually [not always] not a part of the IDE), and the type of code you write.
Practically, quite a few C++ compilers have a "C mode" - for example, command line switches or other settings that may be configurable through your IDE - to compile as C. You'll need to read your compiler and IDE documentation. Bear in mind that some compilers, even in C mode, support C++ features that are not valid in C (and that the reverse is also true).
It also depends on what sort of C code you intend to write, and how well you know the features of C that are not part of C++, and vice versa.
But, yes, as a general rule you can write a C program in a C++ workspace. Just be aware of concerns like the above when doing so.
Also, don't be surprised when you proudly show some of your lovingly crafted code to a seasoned C or C++ developer, only to be informed you are writing a hybrid of C and C++. Unfortunately, writing a hybrid of C and C++ is considered poor form by both C and C++ developers - mostly because the code is likely to work with some compilers and fail with others. Too many compiler vendors intermix C and C++ in their documentation, and too much learning material (introductory texts, etc) does the same. If writing C code in a (suitably configured) C++ environment, there is a fair chance your compiler won't complain about your code doing that. This isn't a problem inherent to writing C in a C++ environment - too many vendors of C compilers support C++ features as an extension, and vice versa - but it is a phenomenon you are more likely to encounter if you write your C code in a C++ environment, unless you go out of your way to learn the differences between standard C and standard C++ independently of your compiler and compiler documentation.
I've lost track of the number of beginners who end up writing code that is some hybrid of C and C++ (e.g. using C++-specific features while believing they are writing C, or vice versa). Only to have their code break when they build it with a different compiler (or, worse, an update of their compiler).
If you actually intend to write code that includes a mix of C and C++ (by which I mean pure C functions interfacing with pure C++ functions, not a hybrid like I mentioned above), you will need to allow orchestration of the build process - particularly linking - to be controlled by C++ settings, even if compilation settings for particular files mean they are compiled as C or C++.
Yes its possible. You must use your C++ compiler when compiling main() (e.g., for static initialization)
Your C++ compiler should direct the linking process (e.g., so it can get its special libraries)
Your C and C++ compilers probably need to come from the same vendor and have compatible versions (e.g., so they have the same calling conventions
Non-trivial C programs will not compile as C++ code without modification — C is not a subset of C++. See Where is C not a subset of C++?
Set compiler options to treat your code as C.
For free compiler with IDE on linux check this one:
Code::Blocks and
for windows:
Microsoft Visual Studio Community
You can find extensive list of free C and C++ compilers here.

C code and C++ compiler

Does anyone know if there is any performance penalty if one compiles C code with C++ compiler?
I have a C like code and use MinGW C++ compiler. I'm using qmake to compile the project. If there is a performance gain if I switch the compiler to compile in C, I'll have to update the code, there are some incompatibility with the syntax and want to know if it worth it.
Thanks.
There should be very little, if any, performance difference, to the point that if there is a difference, it will be very nearly immeasurably small - assuming you're using a C and C++ compiler from the same vendor or collection. Using a C compiler from one vendor and a C++ compiler from another will likely show larger differences, but only due to the fact that different vendors implement different optimization strategies.
There are a small number of potential optimization opportunities that C++ calls for that C compilers may not natively support - but again, unless they're from different vendors, the difference will be meaningless, and many compilers implement similar optimizations in both C++ and C compiler frontends.
One exception to this is Microsoft's compiler - Microsoft never made a C compiler, to my knowledge.
Note: I am assuming that the code does not use C++-specific features like templates or classes.

Do all C++ compilers generate C code?

Probably a pretty vague and broad question, but do all C++ compilers compile code into C first before compiling them into machine code?
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.
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.
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.
No. This is a myth, based around the fact that a very early version of Stroustrup's work was implemented that way. C++ compilers generate machine code in almost exactly the same way that C compilers do.
As of this writing in 2010, the only C++ compiler that I was aware of that created C code was Comeau*. However, that compiler hasn't been heard from in over 5 years now (2022). There may be one or two more for embedded targets, but it is certainly not a mainstream thing.
* - There's a link to their old website on this WP page. I'd suggest not clicking that unless your computer has all its shots up to date
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.

Is C++ built on top of C?

Does C++ code gets converted to C before compilation ?
A few C++ compilers (the original cfront, Comeau C++) use C as an intermediate language during compilation. Most C++ compilers use other intermediate langauges (e.g. llvm).
Edit: Since there seems to be some misunderstanding about the history: "C with classes" started out using a preprocessor called "Cpre". At that time, it was seen strictly as a dialect of C, not a separate language in itself. In December 1983, people were starting to view it as a separate language, and the name C++ was invented. As it happens, development of cfront started in April 1983, so a reasonably usable version became available (to a select few) just about the same time as the name "C++" came into use. This appears to be mostly coincidence though.
As far as producing C as its output, that was really quite common on Unix. Just for example, the Berkeley Pascal compiler and at least a couple of Fortran compilers also produced C as their output.
There is, however, a huge difference between Cpre and Cfront. Although both produced C as their output, Cpre did virtually no syntax checking of its own -- it looked for a few specific things, and did a relatively mechanical translation on them. It wasn't until the C compiler looked at the result that real syntactical analysis was done. If your code contained a syntax error, it was almost certain that it wouldn't be caught until the C compiler parsed the output from Cpre.
Cfront, however, did full syntactical analysis of the source code itself, so (short of a bug in its code generator) you'd never see a syntax error from the C compiler. The C compiler was simply used as a code generator so nobody needed to rewrite CFront to accommodate different processors, object file formats, etc.
If you want to get into more detail, chapter 2 of The Design and Evolution of C++ is devoted almost entirely to the "C with Classes" time frame (and there are various other details about it spread throughout the book).
No, but like most myths there's a shred of truth to this. The original compiler for C with classes (which later became C++) was nicknamed CFront and did translate to C.
Not in most modern compilers.
The original C++ compiler was actually a preprocessor however. It generated C code, which was then compiled by a C compiler.
In the early days of C++ compilers, some did it that way. I haven't seen a C++ compiler implemented that way since the late 1980s however.
As others have answered. NO.
However if you want to use an OOP language like C#, and have your code compiled into C I recommend you take a look at Vala.
the title seems to ask is C++ a superset of C, i.e. can you just dump any c code in a c++ compiler and it will work? In which case, yes it is, sort of...
one major difference is that C automatically casts pointers for you, c++ does not, you need to cast the manually...
any one remember anything else?
thats all I remember from the horrible process of converting a massive C project to compile under c++ for some reason...

Is a cfront tool available for the new C++?

Is a cfront tool available for the new C++? For any other modern languages?
Comeau C/C++ is a C-generating C++ compiler, like cfront.
What do you mean by “cfront tool”? Cfront was the first C++ compiler. But since failure to add exception support, it has been discontinued. Modern C++ is way beyond the scope of Cfront.
For any other modern languages?
And what do you mean by that? If you mean whether other compilers exist that compile a high-level language into C code then the answer is yes, there are plenty. The Glasgow Haskell Compiler is just one out of many.
I believe a number of languages, at least in their infancy, just "compile" to C code, letting a good compiler (often gcc...) to generate optimized native code. I think GHC is (for some implementations?) such language, and there are some others I don't recall.
I know also lua2c, converting Lua code to C.
Bigloo
Bigloo enables full connections
between Scheme and C programs, between
Scheme and Java programs, and between
Scheme and C# programs.
Gambit-C
Gambit benchmarks