c code in C++ project

S

Steven Woody

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
 
I

Ian Collins

Steven said:
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.
 
T

Tomás Ó hÉilidhe

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

Greg Herlihy

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
 
T

Tomás Ó hÉilidhe

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top