Cartesius00
Cartesius00

Reputation: 24414

Write C++ code in C style

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

Answers (6)

IOException
IOException

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

  • C++ has new keywords (class,template,virtual and so on), you should n't use it it your C code if you intent to compile it by C++ compiler
  • C++ has much restrictive typecasting:

Valid in C but invalid in C++

int *j = malloc(sizeof(int) * 5);

Valid in both:

int *j = (int *) malloc(sizeof(int) * 5);
  • Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have size different from that of int.
  • C allows struct, union, and enum types to be declared in function prototypes, whereas C++ does not.

Behave differently in C and C++

  • Character literals such as 'a' are of type int in C and of type char in C++
  • The static keyword is used in C to restrict a function or global variable to file scope (internal linkage). This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const global as file scope unless it is explicitly declared extern, unlike C in which extern is the default. Conversely, inline functions in C are of file scope whereas they have external linkage by default in C++.

You can find the exhaustive differents list, here

Upvotes: 6

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

Jens Gustedt
Jens Gustedt

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

Kerrek SB
Kerrek SB

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

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

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

Yes. In fact, it's generally a good idea because C++ enforces stronger type-checking than C.

Upvotes: 7

Related Questions