Why would someone use c++ compiler on a C code?

2

2005

Would it suppress errors?

Is he trying to hide errors in his code?

He is using g++ on a *.c code!

Also what is so different between gcc & cc compiler - any advantages?
 
I

Ian Collins

2005 said:
Would it suppress errors?

Is he trying to hide errors in his code?

Probably the reverse, C++ and its compilers are more pedantic about a
number of issues, particularly type safety.

Do bear in ming there are sometimes obscure semantic differences between
the two languages, so you would be ill advised to try this at home
unless you are aware of the risks.
 
2

2005

Probably the reverse, C++ and its compilers are more pedantic about a
number of issues, particularly type safety.

Do bear in ming there are sometimes obscure semantic differences between
the two languages, so you would be ill advised to try this at home
unless you are aware of the risks.

I tried this code on "cc -c" - it gave numerous errors
But non when I did g++ on a C code

Is this a good practice to compile a C code with g++ compiler.
 
I

Ian Collins

*Please* don't quote signatures.
I tried this code on "cc -c" - it gave numerous errors
But non when I did g++ on a C code
Then the code is more than likely C like C++, not C.
Is this a good practice to compile a C code with g++ compiler.

Not unless you have a specific reason for doing so.
 
M

Martin Ambuhl

Don said:
A C++ compiler *IS*, by definition, a C compiler. PLUS some stuff.

The above is complete nonsense. It is unbelievable that someone who
knows so little can write so much with a presumed air of authority.
Until we see evidence that Don Bruder has learned something, it is safer
to ignore him. For example.
In general, unless you're trying to compile C++ code (in which case, a
"plain C" compiler just can't cope) it really doesn't much matter if you
use a "plain" C compiler, or a C++ compiler on a straight C source.

this is obvious bullshit.
 
P

Philip Potter

Don said:
A C++ compiler *IS*, by definition, a C compiler. PLUS some stuff. And
generally, it's even pickier than its "plain C" counterpart. So no, it's
not likely to suppress errors. If anything, it's more likely to complain
even MORE about errors than a "plain" C compiler would.

This is not true.

#include <stdlib.h>
#include <stdio.h>
int main(void) {
int *class = malloc(sizeof 'a');
for (*class = 0; *class<10; (*class)++) {
printf("C is not a subset of C++\n");
}
return 0;
}

The above program is valid C but invalid C++. There are at least three
problems:

* 'class' is not a valid identifier in C++
* 'void *' does not automatically convert to 'int *' in C++
* The character constant 'a' is of type int in C, but of type 'char' in
C++, and so the call to malloc may not allocate enough storage.

Although C++ is mostly a superset of C, these differences (especially
implicit void * conversion) mean that many C programs will not compile
as C++ without some modification.
 
P

Philip Potter

2005 said:
Would it suppress errors?

Is he trying to hide errors in his code?

Please repeat your question in the body of your message; don't just ask
it in the subject.

Others have suggested some solutions. The FAQ question 20.27 "Is C++ a
superset of C? What are the differences between C and C++? Can I use a
C++ compiler to compile C code?" will probably help you as well. The FAQ
also links to a statement on the subject from Bjarne Stroustrup, the
creator of C++. You can find the FAQ at http://c-faq.com/

Philip
 
I

Ian Collins

Richard said:
/* Please convert this program to C++ using the
* following rules:

I tried to compile it as C first at got:

"/tmp/x.c", line 34: warning: no explicit type given
"/tmp/x.c", line 37: syntax error before or at: )
"/tmp/x.c", line 81: warning: implicit function declaration: toupper
"/tmp/x.c", line 86: warning: implicit function declaration: tolower
(Incidentally, this program also demonstrates that C99 isn't a superset of
C90.)
So after renaming identifiers, a C++ compiler gave similar errors:

"/tmp/x.c", line 33: Error: Cannot use const char[82] to initialize
char[81].
"/tmp/x.c", line 37: Error: Unexpected ")" -- Check for matching
parenthesis.
"/tmp/x.c", line 81: Error: The function "toupper" must have a prototype.
"/tmp/x.c", line 86: Error: The function "tolower" must have a prototype.

So what does this prove? C++ is closer to C99? C90 isn't C or C++?
 
I

Ian Collins

Richard said:
Ian Collins said:


A copyo, perhaps? It compiles just fine here, under the strictest set of
flags I can summon up.


Neither of these diagnostic messages is mandatory: implicit int is legal in
C...
Read them again "implicit function declaration". Your forgot <ctype.h>

Implicit int is illegal in both C99 (C) and C++.
....except, of course, when it isn't.


It demonstrates what we already know - i.e. that C++ compilers can't
compile C programs.

More topically it shows a C99 compiler can't compile a C90 program.
 
I

Ian Collins

Richard said:
Ian Collins said:


No, I didn't. I just mis-typed "implicit function declaration". :)
Implicit function declaration is, of course, legal in C90 (C).
Now where did I leave my Tardis?
 
K

Kelsey Bjarnason

[snips]

A C++ compiler *IS*, by definition, a C compiler.


#include <stdlib.h>

int main(void)
{
char *s = malloc(100);
if ( s ) free( s );
return 0;
}

gcc -Wall -ansi -pedantic test.c compiles clean. Note the use of the
proper C idiom of *not* casting the return of malloc, which is both
intentional and correct.


rename it to test.cpp, recompile with g++ and...

test.cpp: In function ‘int main()’:
test.cpp:5: error: invalid conversion from ‘void*’ to ‘char*’


Hmm. A trivial 8 line program - if you count braces and white space -
and it won't compile in C++.

Explain to us, then, how to get it to compile this perfectly valid C
program. No, not a modified version with casting in direct violation of
proper C programming practise, show us how to get a C++ compiler to
compile *that* program. If, as you assert, a C++ compiler is a C
compiler, the code should compile. It doesn't. It never will. It
cannot, as it is _C_ code, not _C++_ code and the two are not the same at
all.

Don't like that one? Try this:

#include <stdio.h>

int main(void)
{
int new = 3;
printf( "%d\n", new );
return 0;
}

Again, compiles clean with gcc. Save it as a C++ file and compile it
with g++ and...

test.cpp: In function ‘int main()’:
test.cpp:5: error: expected unqualified-id before ‘new’
test.cpp:6: error: expected type-specifier before ‘)’ token
test.cpp:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘int*’

Err what? argument 2 has type int *? No it doesn't, it has type int;
says so right there: int new = 3; No pointers at all.

You know, for a C compiler, g++ is pretty damn bad. Makes a fine C++
compiler, but it absolutely *blows* at compiling C code. If, as you say,
it is by definition also a C compiler, why does it suck so badly at
compiling C code?
 
C

Charlton Wilbur

> The above is complete nonsense. It is unbelievable that someone
> who knows so little can write so much with a presumed air of
> authority. Until we see evidence that Don Bruder has learned
> something, it is safer to ignore him. For example.

MMcL> Manners, manners, manners. Don Bruder is relatively new to
MMcL> the group and what he said is an easy mistake for a
MMcL> relatively well-informed person to make.

Only if your definition of "relatively well-informed" is so vague as
to be useless; someone who thinks C++ is a proper superset of C,
modulo keywords, isn't very well-informed about either language.

And calling it complete nonsense isn't rude; neither is pointing out
that people who write complete nonsense to this group are frequently
corrected but otherwise ignored.

Charlton
 
D

dj3vande

<OT>

On Apr 7, 9:56 pm, 2005 <[email protected]> wrote:

[Subject: Why would someone use c++ compiler on a C code?]
He is using g++ on a *.c code!

g++ will act as a C compiler when given the file 'foo.c', unless you
specifically ask it to do otherwise.
Any of the gcc or g++ manuals, a GCC-specific newsgroup, or possibly a
Linux programming newsgroup should be able to give you the details.

Also what is so different between gcc & cc compiler - any advantages?

gcc will always give you the GNU C compiler, unless the system was
deliberately set up to be confusing and/or broken.
cc will usually give you the system C compiler, which, where it isn't
GCC (on Linux systems and some others it is), can probably not
reasonably be expected to understand GCC-specific language extensions
and command-line arguments.


</OT>

dave
 
B

Ben Bacarisse

<OT>
On Apr 7, 9:56 pm, 2005 <[email protected]> wrote:

[Subject: Why would someone use c++ compiler on a C code?]
He is using g++ on a *.c code!

g++ will act as a C compiler when given the file 'foo.c', unless you
specifically ask it to do otherwise.
gcc will always give you the GNU C compiler, unless the system was
deliberately set up to be confusing and/or broken.

gcc will return the favour -- i.e. it will compile a C++ program if
the source is called, for example, x.cc.

I don't think you meant to imply otherwise, but some people might read
what you said as suggesting gcc is always a C compiler. I find it
helps always to use -std=c89 or -std=c99 etc. That way you get told
if you feed it a file that it takes to be in another language.
 
K

Kelsey Bjarnason

[snips]

Actually that idiom was retained, reluctantly, to avoid breaking reams
of legacy code, as committee members have informed us in previous posts
to this ng.

The committee has precisely zero authority in terms of defining this as
good practise or otherwise. What defines it as good practise or bad is
whether it works, and why.

Out there in the real world, there exist compilers which do *not* warn
you about failure to include appropriate headers, but which will - are
required to - warn about invalid assignments, such as from int to pointer.

If and when one's code can be reasonably certain of never, ever
encountering such a compiler in the code's entire lifetime, then and only
then will this cease to be a useful idiom.

Maybe this will come when C90 compilers are dead and buried, but given
the sickly snail's pace with which C99 has been adopted, I wouldn't count
on it happening tomorrow.
 
F

Flash Gordon

Malcolm McLean wrote, On 08/04/08 15:32:
Kelsey Bjarnason said:
[snips]

A C++ compiler *IS*, by definition, a C compiler.


#include <stdlib.h>

int main(void)
{
char *s = malloc(100);
if ( s ) free( s );
return 0;
}

gcc -Wall -ansi -pedantic test.c compiles clean. Note the use of the
proper C idiom of *not* casting the return of malloc, which is both
intentional and correct.
Actually that idiom was retained, reluctantly, to avoid breaking reams
of legacy code, as committee members have informed us in previous posts
to this ng.

I can't remember any such post. In fact, void* was *invented* by the
standards committee specifically to get rid of the need for casts in a
number of places http://www.lysator.liu.se/c/rat/c2.html#3-2-2-3
Note this quote from there...
| ... Adoption of this type was stimulated by the desire to specify
| function prototype arguments that either quietly convert arbitrary
| pointers...
One effect is that if used in new code, the program will no longer
compile under C++.

Well, seeing as new C code is C code rather than C++ this is not a problem.
 
K

Kenny McCormack

The above is complete nonsense. It is unbelievable that someone who
knows so little can write so much with a presumed air of authority.
Until we see evidence that Don Bruder has learned something, it is safer
to ignore him. For example.

It really pisses the regs off when intelligent people come on here and
express views not in accord with the party line. They are soon branded
"trolls" and most (but not all) leave soon after.
this is obvious bullshit.

Yup, Marty. Always the charmer, eh?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top