Reputation: 24414
If we want to write a module in C and have to compile it as C++ with g++
, is it OK to develop a piece of code in C++ without any own classes, only using "global / static functions" as in C? So, simply said, to code C in C++ (with only few system header changes etc.)
Upvotes: 6
Views: 4979
Reputation: 143
While most C source code will compile as C++ code without any changes, certain language differences prevent C++ from being a strict superset of C.
Valid in C but invalid in C++
int *j = malloc(sizeof(int) * 5);
Valid in both:
int *j = (int *) malloc(sizeof(int) * 5);
You can find the exhaustive differents list, here
Upvotes: 6
Reputation: 1
No, g++
and gcc
are not two different front-ends to the same compiler.
They are two different drivers for two different compilers (cc1plus
and cc1
) sharing the same middle-end and back-end code (withing GCC build-tree, inside gcc/libbackend.a
, a misnomer, since it contains both middle-end (the middle-end is the common part of GCC, common to C, C++, Ada, Fortran, Objective-C, ... and many targets machines; it is the biggest part of GCC and work on a common set of internal representations, notably Gimple) and back-end (the part of GCC transforming Gimple representations to assembly, using RTL -a target dependent internal representation).
But cc1 and cc1plus have different front-end code.
Upvotes: 0
Reputation: 79003
I don't see any reason for such a thing, g++ and gcc are just different frontends to the same compiler. So for all what concerns efficiency, byte compatability etc it should be no problem to mix .o
files that are produced by both.
C and C++ have many subtle differences that can cause you trouble, starting from things such as sizeof 'a'
beeing different but sizeof c
being the same (if c
is a char
), to bool being a type in one and a macro in the other, true
being of type bool
in C++ and int
in C, C not allowing static
declarations in a for
...
And even if it is so that C and C++ have a large intersection, if you restrict yourself to what is considered good coding practice in both communities you quickly find that the intersection is almost empty. This concerns pointer casts, allocation with malloc
or new
, compound initializers versus constructors, variable length arrays versus vector classes...
Just don't do it. All you need is to create a nice interface that is suitable for both.
Upvotes: 1
Reputation: 477710
You can "write C++" in many styles -- that's one of the fundamental strengths of the language. That includes a strictly procedural, flat programming style common to C programs. You'll still be writing C++, but the code should end up looking very familiar to any C programmer.
Strictly speaking, you will have to use the C++ headers <cstdio>
etc, and all your C library functions are in the std
namespace. Perhaps this is one of the few legitimate situations where you should use using namespace std;
! :-)
Upvotes: 3
Reputation: 208476
You will need to do a couple of things other than only use functions, in particular you should mark all your functions as extern "C"
to avoid name mangling and enforce C calling conventions (and incidentally block you from overloading). If you want to be able to compile it in C, you will have to qualify types with struct
when declaring variables (enum
for enumerations), or provide the appropriate typedefs...
Alternatively, you can add -x c
to the compiler options to tell g++ to compile the code as C (if you are not able to change the command line from g++
to gcc
, you might not be able to add compiler flags either...)
Upvotes: 4
Reputation: 186118
Yes. In fact, it's generally a good idea because C++ enforces stronger type-checking than C.
Upvotes: 7