tem
tem

Reputation: 367

Why there's no dedicated compiler for c or 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?

Upvotes: 3

Views: 1415

Answers (10)

user207421
user207421

Reputation: 310903

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.

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284806

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

Upvotes: 8

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215259

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

Upvotes: 3

plan9assembler
plan9assembler

Reputation: 2984

No, LCC compiler is for C only.

Upvotes: 1

Jonathan Adelson
Jonathan Adelson

Reputation: 3321

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

Upvotes: 0

Clifford
Clifford

Reputation: 93476

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.

Upvotes: 3

wkl
wkl

Reputation: 79921

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

Upvotes: 0

Sid_M
Sid_M

Reputation: 399

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.

Upvotes: 1

Chubsdad
Chubsdad

Reputation: 25497

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

Upvotes: 3

CashCow
CashCow

Reputation: 31435

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.

Upvotes: 4

Related Questions