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

Discussion in 'C Programming' started by 2005, Apr 8, 2008.

  1. 2005

    2005 Guest

    Would it suppress errors?

    Is he trying to hide errors in his code?
     
    2005, Apr 8, 2008
    #1
    1. Advertising

  2. 2005

    2005 Guest

    On Apr 7, 9:56 pm, 2005 <> wrote:
    > 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?
     
    2005, Apr 8, 2008
    #2
    1. Advertising

  3. 2005

    Ian Collins Guest

    2005 wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2008
    #3
  4. 2005

    2005 Guest

    On Apr 7, 10:01 pm, Ian Collins <> wrote:
    > 2005 wrote:
    > > 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.
    >
    > --
    > Ian Collins.


    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.
     
    2005, Apr 8, 2008
    #4
  5. 2005

    Ian Collins Guest

    2005 wrote:
    > On Apr 7, 10:01 pm, Ian Collins <> wrote:
    >> 2005 wrote:
    >>> 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.
    >>

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

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2008
    #5
  6. Don Bruder wrote:
    > In article
    > <>,
    > 2005 <> wrote:
    >
    >> Would it suppress errors?
    >>
    >> Is he trying to hide errors in his code?

    >
    > 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.
     
    Martin Ambuhl, Apr 8, 2008
    #6
  7. Don Bruder wrote:
    > In article
    > <>,
    > 2005 <> wrote:
    >
    >> Would it suppress errors?
    >>
    >> Is he trying to hide errors in his code?

    >
    > 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.
     
    Philip Potter, Apr 8, 2008
    #7
  8. 2005 wrote:
    > 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
     
    Philip Potter, Apr 8, 2008
    #8
  9. 2005

    Ian Collins Guest

    Richard Heathfield wrote:
    >
    > /* 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++?

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2008
    #9
  10. 2005

    Ian Collins Guest

    Richard Heathfield wrote:
    > Ian Collins said:
    >
    >> Richard Heathfield wrote:
    >>> /* 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: )

    >
    > A copyo, perhaps? It compiles just fine here, under the strictest set of
    > flags I can summon up.
    >
    >> "/tmp/x.c", line 81: warning: implicit function declaration: toupper
    >> "/tmp/x.c", line 86: warning: implicit function declaration: tolower

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

    >>> (Incidentally, this program also demonstrates that C99 isn't a superset
    >>> of C90.)

    >
    > ....except, of course, when it isn't.
    >
    >> So what does this prove? C++ is closer to C99? C90 isn't C or C++?

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

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2008
    #10
  11. 2005

    Ian Collins Guest

    Richard Heathfield wrote:
    > Ian Collins said:
    >
    >> Richard Heathfield wrote:
    >>> Ian Collins said:
    >>>

    > <snip>
    >>>> "/tmp/x.c", line 81: warning: implicit function declaration: toupper
    >>>> "/tmp/x.c", line 86: warning: implicit function declaration: tolower
    >>> Neither of these diagnostic messages is mandatory: implicit int is legal
    >>> in C...
    >>>

    >> Read them again "implicit function declaration". Your forgot <ctype.h>

    >
    > 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?

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2008
    #11
  12. [snips]

    On Mon, 07 Apr 2008 22:41:49 -0700, Don Bruder wrote:

    > 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?
     
    Kelsey Bjarnason, Apr 8, 2008
    #12
  13. MMcL> "Martin Ambuhl" <> wrote in message
    MMcL> news:...

    >> Don Bruder wrote:


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


    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


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm


    --
    Charlton Wilbur
     
    Charlton Wilbur, Apr 8, 2008
    #13
  14. 2005

    Guest

    <OT>

    In article <>,
    2005 <> wrote:
    >On Apr 7, 9:56 pm, 2005 <> wrote:


    [Subject: Why would someone use c++ compiler on a C code?]

    >> Would it suppress errors?
    >>
    >> Is he trying to hide errors in his 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

    --
    Dave Vandervies dj3vande at eskimo dot com
    But, Greg, this thread just kicked out /classes/ because they were too big and
    clumsy and bloated, so how do you think we're likely to feel about *templates*?
    --Richard Heathfield in comp.lang.c
     
    , Apr 8, 2008
    #14
  15. lid writes:

    > <OT>
    > In article <>,
    > 2005 <> wrote:
    >>On Apr 7, 9:56 pm, 2005 <> wrote:

    >
    > [Subject: Why would someone use c++ compiler on a C code?]

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

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

    > </OT>


    --
    Ben.
     
    Ben Bacarisse, Apr 8, 2008
    #15
  16. [snips]

    On Tue, 08 Apr 2008 15:32:47 +0100, Malcolm McLean wrote:

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


    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.
     
    Kelsey Bjarnason, Apr 8, 2008
    #16
  17. 2005

    Flash Gordon Guest

    Malcolm McLean wrote, On 08/04/08 15:32:
    >
    > "Kelsey Bjarnason" <> wrote in message
    > news:...
    >> [snips]
    >>
    >> On Mon, 07 Apr 2008 22:41:49 -0700, Don Bruder wrote:
    >>
    >>> 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.
    --
    Flash Gordon
     
    Flash Gordon, Apr 8, 2008
    #17
  18. In article <>,
    Martin Ambuhl <> wrote:
    >Don Bruder wrote:
    >> In article
    >> <>,
    >> 2005 <> wrote:
    >>
    >>> Would it suppress errors?
    >>>
    >>> Is he trying to hide errors in his code?

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


    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.

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


    Yup, Marty. Always the charmer, eh?
     
    Kenny McCormack, Apr 8, 2008
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. It's me
    Replies:
    11
    Views:
    632
    It's me
    Dec 30, 2004
  2. RickMuller
    Replies:
    4
    Views:
    711
    Alexey Shamrin
    Mar 26, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    910
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,999
    Smokey Grindel
    Dec 2, 2006
  5. luser- -droog

    Would someone tell me if I'm on the wrong track?

    luser- -droog, May 10, 2011, in forum: C Programming
    Replies:
    14
    Views:
    659
    Tim Rentsch
    May 19, 2011
Loading...

Share This Page