2

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

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
tem
  • 357
  • 1
  • 3
  • 8
  • 1
    C++ and C (all versions) share a common subset. This subset is fundamental enough one can compile both without the same effort as compiling two totally separate languages. (They are separate, no doubt, but not disjoint.) – GManNickG Oct 15 '10 at 08:37
  • Are gcc and g++ are dedicated for c and c++ respectively? – tem Oct 15 '10 at 08:38
  • 2
    Actually there is. You can't compile a C99 program with most C++ compilers. And g++ != gcc – KeatsPeeks Oct 15 '10 at 08:39
  • 3
    @Samuel_xL:GCC and G++ are in fact *more or less* the same thing. GCC will compile C++ and g++ will compile C code. The difference is merely in the default standard libraries; GCC does not link stdlibc++ automatically. – Clifford Oct 15 '10 at 08:57
  • @Clifford: It is actually cc1 and cc1plus that do the compiling (usually somewhere under /usr/lib/gcc ). gcc and g++ will both call both of these depending on extensions and options. – nategoose Oct 15 '10 at 14:05
  • 2
    GCC and G++ are drivers. cc1 and cc1plus are the compiler propers. – Johannes Schaub - litb Oct 15 '10 at 17:52
  • Indeed and the drivers gcc and g++ reamin as I said more or less the same. – Clifford Oct 15 '10 at 20:25
  • @Clifford, Johannes Schaub - litb; Good work on pointing these out. – Matt Joiner Nov 14 '10 at 01:43

10 Answers10

7

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

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
4

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
  • 30,981
  • 5
  • 61
  • 92
3

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

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
3

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
  • 88,407
  • 13
  • 85
  • 165
  • "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 Oct 15 '10 at 09:26
  • 2
    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 Oct 15 '10 at 20:36
  • you may enjoy this document: https://docs.google.com/document/pub?id=1aQntg-i_KhhzzAo0sD_LgY_5_HIUq9gOKO7yr4Or7Gs – Matt Joiner Nov 14 '10 at 01:46
3

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.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

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
  • 399
  • 1
  • 4
  • 1
    @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) – KeatsPeeks Oct 15 '10 at 08:37
  • 2
    @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 Oct 15 '10 at 10:32
  • 1
    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 Oct 15 '10 at 13:44
  • 1
    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 Oct 16 '10 at 08:12
1

No, LCC compiler is for C only.

plan9assembler
  • 2,862
  • 1
  • 24
  • 13
0
  • 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.
wkl
  • 77,184
  • 16
  • 165
  • 176
  • I have to disagree with your final point, especially since the question asks about Microsoft's C++ compiler. – Matthew Flaschen Oct 15 '10 at 08:41
  • 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. – wkl Oct 15 '10 at 08:46
0

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

Jonathan Adelson
  • 3,275
  • 5
  • 29
  • 39
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483