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

Re: OT: converting C to C++ : linkng problems



Gregory Seidman wrote:
On Tue, Dec 13, 2005 at 10:14:37AM -0600, Mike McCarty wrote:
} Joris Huizer wrote:
} >That makes sence, but still - it's exactly the same code as used in C } >and the C code compiles fine (except for some added casts as C++ doesn't } >like coercing void pointers to something else) } >if I do an objdump it seems those functions that are not found have been } >mangled by C++ } } C is not C++
} C is not a subset of C++
} They are different languages
} There is a subset of C++ which resembles C very closely

Actually, C is a proper subset of C++. In fact, the C++ standard is based
on the C standard. Some changes were made to the C standard (in 1999, as I
recall) to reconcile it with C++.

[snip]

C is not a proper subset of C++. The semantics of both programming
languages would have to change in order for that to be true. I give
below an example program which is valid both as a C program and
as a C++ program, but has different meanings when compiled by
compilers for the two languages.

What is true is that there is a subset of C which is also a subset
of C++. This subset is a "larger" subset of C than it is of C++.

Or, as I said, there is a subset of C++ which resembles C very much.

Some accomodations were indeed made in 1999, so that the Standard
Counterexample is now compiled the same in both lanuguages, this being

#include <stdio.h>

int	main(void) {
	printf("Compiled as C%s.\n",1//**/2
		? "++" : "");
	return 0;
}

Compiled as C++ it prints "Compiled as C++.", compiled
using the C89 Standard, it prints "Compiled as C.",
and compiled using the C99 Standard it prints
"Compiled as C++.".

But a C compiler is not allowed to set __cplusplus,
for example, so there are valid C programs which are
also valid C++ programs which do anything at all you
like differently when compiled by a C compiler or a
C++ compiler. For example:

#include <stdio.h>

#if __cplusplus

const char Message[] = "++";

#else

const char Message[] = "";

#endif

int	main(void) {
	printf("Compiled as C%s.\n",Message);
	return 0;
}

You can verify this for yourself, on your own machine,
if you have gcc and g++ both installed.

So there are programs which are valid C programs, and
also valid C++ programs, but WHICH MEAN DIFFERENT THINGS
TO THE DIFFERENT COMPILERS.

Neither of the languages is a proper subset of the other.

Mike
--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
You have found the bank of Larn.
I can explain it for you, but I can't understand it for you.
I speak only for myself, and I am unanimous in that!



Reply to: