tags:

views:

408

answers:

10

Seems all compilers can deal with both c and c++,like gcc ,msvc...

Is it because these 2 languages are exactly very much alike?

+5  A: 

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).

Matthew Flaschen
Actually g++ and gcc do almost the same thing, but cc1 and cc1plus, which are the actual compilers, do very different things.
nategoose
"and can you can specify" grammar note.
JonoRR
+1  A: 

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.

Sid_M
It's not a superset.
GMan
What's it then?
tem
they share a common subset
jk
@tem A different language that originated from C but doesn't have much in common with it nowadays (a C++ compiler can't compile C99)
Samuel_xL
@Samuel_xL: a C++ compiler can't compile C89, either. At least, not without entering a special C89 mode which changes how the compiler treats certain things, in which case it's not a C++ compiler in that mode. That's what "C++ is not a superset" means.
Steve Jessop
In order for C++ to be a proper superset of C, every legal C program must also be a legal C++ program with identical semantics. That's obviously not the case, so C++ can't be a proper superset of C. So jk's comment is the most accurate; the two languages share a large common subset of language elements.
John Bode
Of course, the common subset is a powerful and useful language. Lua is written in it, for example. In practice, far fewer features of C are excluded from the common subset than features of C++. Of course, C99 does seem to have diverged further from the subset than C89/C90 did.
RBerteig
+4  A: 

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.

CashCow
i'd suspect plenty of embedded C compilers don't do C++
jk
+2  A: 

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

Chubsdad
On the other side, there's Comeau which compiles C++ to C.
Tony
A: 
  • 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.
birryree
I have to disagree with your final point, especially since the question asks about Microsoft's C++ compiler.
Matthew Flaschen
I was having trouble phrasing it in a way so as to say that if you had a compiler that strictly followed C++ standards (unlike VC++ which supports C89 and C++), then you would have compiler differences.
birryree
+2  A: 

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.

Clifford
"C++ is also highly inteoperable with C, C++ for example wholly includes C's standard library, and can link and C library." No, it's missing at least `stdbool.h`, `stdint.h`, and `inttypes.h`.
Matthew Flaschen
OK, it wholly includes C90's library (changed to clarify). In fact in practice it will include whatever C library is provided with the C/C++ suite in question. This does not change the fact that they are interoperable. stdbool.h is irellevant to C++, but the header normally has preprocessing code so that including it in C++ compilation is benign. If the C library provides stdint.h/inttypes they will work without problems with C++ compilation. And if they are not provided, being header only implementations, they can easily be provided.
Clifford
A: 

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

Sam Hoice
A: 

No, LCC compiler is for C only.

plan9assembler
+3  A: 

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++.

R..
A: 

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.

EJP