[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc can't compile, egcc can!??



Hello Liran!

On Tue, Jun 09, 1998 at 08:56:04PM +0300, Liran Zvibel wrote:
> Hi,
> 
> Why don't you compile the kernel with g++ ? (I know that egcs has some
> problems, but I think that g++ is fine.)
> As I wrote before, I have never found a real need to use gcc, since
> everything that is writtent in the old "clasic" C can be understood by
> g++, but not everything can be understood by gcc. 

This is simply not true, and dangerous in any way.

1) g++ produces a little overhead for exception handling by default. This is
a useful feature, but is not exploited by C programs. Note that the overhead
only is in excutable space, it shouldn't be noticed runtime.

2) g++ links with the standard c++ library by default, while gcc does not.
Don't know if this can produce problems, but I would expect so.

3) c++ is *not* completely backwards compatible. It is mostly, but there are
exceptions.

* In C, sizeof('a')=sizeof(int), but in C++, sizeof('a')=sizeof(char).
* A similar situation exists for enumerations.
* Look at this (constructed) example:

int f(int a, int b)
{
  return a //* pretty unlikely */ b
       ;    /* unrealistic: semicolon on seperate line to avoid syntax error */
}

So, what does this program in standard C (where // is *not* introducing a
comment in the rest of the line) and in C++. Think about it.

* In C, functions can be called without prior declaration. In C++, the
compiler will error out.
* In C, a function declared without specifying any arument types can take
any number of arguments of any type at all. In C++, no arguments are no
arguments.

* The following is C code, but not C++ code:
  void f(a,p,c) char *p; char c; { /* ... */ }
In C++, this has to be:
  void f(int a, char* p, char c) { /* ... */ }

* In C++, the following is invalid: const a = 7;
  In C, "int" is assumed as type of a.

* C allows definition of structs in obscure places:
  struct S { int x,y; } f();
  void g(struct S { int x,y; } y);
This is not allowed in C++.

* In C, you can assign integers to enumerations:
  enum Direction {up, down};
  Direction d=1;
This is not allowed in C++.

* In C++, many more keywords exist than in C (for example:
  friends, public, private, false, true, bool, this, template,
  throw, virtual, ...) If any of these keywords is used in a C program as
  identifiers (for example variable names), the C++ compiler will be confused
  and error out.

* In C, you can declare a global data object multiple times (if you don't
  initialize it), without using "extern". So this is allowed in C:
  int i; int i;

  This is not allowed in C++. In C++, you need exactly one declaration.

I stop here, there are more incompatabilities. You can find more detailed
information (including all facts and examples above) in Bjarne Stroustrups
EXCELLENT book: The C++ Programming Language, p.816 ff. Bjarne is the
main inventor of the C++ programming language, so he has to know ;)
Be sure to get the latest edition of the book.

Why did it work for you so long? There are three reasons:

* The programs you compiled were written in good C. Most of the examples
  above are bad and poorly written C, although valid. So, most of the
  exceptions above don't occure in reality too often.
* The compiler does not implement the C and C++ standard correctly.
  Especially the C++ compilers are far from the standard. This is because
  C++ is a new language, and the standard did change often in the last
  years. This is also because the programmers of the compiler didn't got so
  far in the standard yet. In some time, all C++ compilers will converge to
  the behaviour defined in the standard. For know, you'll be surprised what
  C++ code is accepted by the compiler (that is not valid in the standard),
  and how much of valid C++ code is not accepted by the compiler.
* You were lucky.

Uhm, this mail is way too long, but I hope you enjoyed reading it. It is
really an interesting topic, and although it would be desireable to have
full compatibility, it can't beachieved for technical reasons.

Fazit:
Please use gcc to compile C programs, and g++ to compile C++ programs.

(where g++ is really eg++, because it works better :)

Marcus

-- 
"Rhubarb is no Egyptian god."        Debian GNU/Linux        finger brinkmd@ 
Marcus Brinkmann                   http://www.debian.org    master.debian.org
Marcus.Brinkmann@ruhr-uni-bochum.de                        for public  PGP Key
http://homepage.ruhr-uni-bochum.de/Marcus.Brinkmann/       PGP Key ID 36E7CD09


--
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


Reply to: