Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any circumstance under which valid C code will not compile properly using g++ [duplicate]

Possible Duplicate:
“C subset of C++” -> Where not ? examples?

I am aware that C is a subset of C++ (i.e. there does not exist valid C code that is not valid C++ code). My question is whether g++ is completely compatible with all C code. For example, will

g++ -o testing test.c

produce an identical binary to

gcc -o testing test.c

in all circumstances?

More specifically, if they do not always create identical binaries, is there any reason that that could be a problem? Is it safe to always use g++ if I'm not sure about the code?

like image 518
ewok Avatar asked Jul 26 '12 18:07

ewok


2 Answers

C is not a subset of C++.

Try:

foo.c

int main() {
    int class = 0;
    return 0;
}

Anyway have fun here: Where is C not a subset of C++?

like image 178
djechlin Avatar answered Sep 18 '22 21:09

djechlin


It's hard to figure out how to answer this:

  • C is not a complete subset of C++. There are several things in C that are not valid C++. Variable Length Arrays are one such thing. Implicit casts from void* is another.
  • What code g++ will accept depends on the flags passed to it. Is it compiled just by invoking g++ (which version?) Or with -ansi? -pedantic? How about std=<lang>?
  • and finally, whether the code is accepted is a completely different issue from "whether it produces an identical binary". Code which is accepted by both compilers might result in binaries which do the same thing, but which are nonetheless not identical.

Given all this ambiguity, it's impossible to give you a definitive answer.

like image 29
jalf Avatar answered Sep 20 '22 21:09

jalf