c code in C++ project

Discussion in 'C++' started by Steven Woody, Jun 4, 2008.

  1. Steven Woody

    Steven Woody Guest

    Hi,

    I usually write c code in .c files and use c compiler to compile
    them. In this new project, my team member prefer to write c code
    in .cpp and use c++ compiler to make them. Which method is better?
    In particularly, the compiler is gnu gcc and g++, our OS is linux.

    Thanks.

    -
    narke
     
    Steven Woody, Jun 4, 2008
    #1
    1. Advertising

  2. Steven Woody

    Ian Collins Guest

    Steven Woody wrote:
    > Hi,
    >
    > I usually write c code in .c files and use c compiler to compile
    > them. In this new project, my team member prefer to write c code
    > in .cpp and use c++ compiler to make them. Which method is better?


    C and C++ are different languages. A lot of legal C will not compile as
    C++.

    Compile each languages with its correct compiler, otherwise you end up
    with what Alf said; all C++ code.

    --
    Ian Collins.
     
    Ian Collins, Jun 4, 2008
    #2
    1. Advertising

  3. On Jun 4, 6:21 am, Steven Woody <> wrote:
    > Hi,
    >
    > I usually write c code in .c files and use c compiler to compile
    > them.  In this new project, my team member prefer to write c code
    > in .cpp and use c++ compiler  to make them.  Which method is better?
    > In particularly, the compiler is gnu gcc and g++, our OS is linux.



    Call your C files ".c" and your C++ files ".cpp". Your IDE "Integrated
    Development Environment" should use the appropriate compiler for each.

    You should _not_ use a C++ compiler to compile C code, mostly because
    there's plenty of legal C code that won't compile with a C++ compiler.
    Consider the following:

    void Func(const i) /* Error implicit int */
    {
    int *parr[10];

    int try; /* Error keyword used as variable name */

    for (try = 0; try != 10; ++try)
    {
    parr[try] = malloc(sizeof(int));

    if (parr[try]) *(parr[try]) = val;
    /* Error implicit conv from void* to int* */
    }
    }

    You should REALLY dissuade your collegue from compiling C code as C++.
     
    Tomás Ó hÉilidhe, Jun 4, 2008
    #3
  4. Steven Woody

    Greg Herlihy Guest

    On Jun 3, 10:21 pm, Steven Woody <> wrote:
    >
    > I usually write c code in .c files and use c compiler to compile
    > them.  In this new project, my team member prefer to write c code
    > in .cpp and use c++ compiler  to make them.  Which method is better?
    > In particularly, the compiler is gnu gcc and g++, our OS is linux.


    One of C++'s design goals was to be a "better 'C' than 'C'" - so, in
    fact it does make sense to write in C++ - the kind of program that one
    might otherwise write in C. Or to put it another way: even programs
    that are not by nature object-oriented, can still take advantage of C+
    +'s other features. For example, a C++ program use inline functions,
    function templates or const variables in places where a C program
    would use a macro.

    Greg
     
    Greg Herlihy, Jun 4, 2008
    #4
  5. On Jun 4, 6:20 pm, Greg Herlihy <> wrote:

    > One of C++'s design goals was to be a "better 'C' than 'C'" - so, in
    > fact it does make sense to write in C++ - the kind of program that one
    > might otherwise write in C. Or to put it another way: even programs
    > that are not by nature object-oriented, can still take advantage of C+
    > +'s other features. For example, a C++ program use inline functions,
    > function templates or const variables in places where a C program
    > would use a macro.



    C++ took C and added more features to it, so it's better. The only
    problem with C++ is that its added complexity adds considerably to the
    complexity of its compiler. This gives rise to two issues:
    1) The unavailability of C++ compilers
    2) Bugs in C++ compilers

    If I'm programming something simple then I'll go for C so that it's
    more portable. If I'm programming for a micrcontroller, then I've no
    choice but to go for C because there isn't a C++ compiler available.

    If I'm programming a GUI application for Windows, Linux, Mac, etc.,
    then I'll go for C++.
     
    Tomás Ó hÉilidhe, Jun 4, 2008
    #5
    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. Claude seraphin
    Replies:
    12
    Views:
    2,539
    samundersingh1983
    Feb 15, 2014
  2. Ken Stealth
    Replies:
    2
    Views:
    4,373
    =?Utf-8?B?amRneChDb2xvbWJpYSk=?=
    Jan 31, 2004
  3. Brad
    Replies:
    2
    Views:
    590
    Kevin Spencer
    Apr 1, 2004
  4. Raterus
    Replies:
    2
    Views:
    862
    Joe Fallon
    Apr 28, 2004
  5. moondaddy
    Replies:
    4
    Views:
    756
    Steven Cheng[MSFT]
    Jul 27, 2004
Loading...

Share This Page