Create C ++ code in C style

If we want to write a module in C and need to compile it as C ++ with g++ , is it normal to develop a piece of C ++ code without any proper classes using only "global / static functions", as in C? So, just said to C code in C ++ (with slight changes in the system header, etc.)

+4
source share
6 answers

Yes. Actually, this is generally a good idea, because C ++ provides stronger type checking than C.

+7
source

You will need to do several things besides using only functions, in particular, you should mark all your functions as extern "C" in order to avoid name manipulation and apply C-calling conventions (and, by the way, block you from overloading). If you want to compile it in C, you will have to qualify the types with struct when declaring variables ( enum for enumerations) or provide the corresponding typedefs ...

Alternatively, you can add -xc to the compiler options to tell g ++ to compile the code like C (if you cannot change the command line from g++ to gcc , you may not be able to add compiler flags either ...)

+4
source

Although most C sources will compile as C ++ code without any changes, some language differences prevent C ++ from being a strict superset of C.

Valid in C but not valid in C ++

  • C ++ has new keywords (class, template, virtual, etc.), you should not use it in your C code if you intend to compile it using the C ++ compiler
  • C ++ has many restrictive types:

Valid in C but not valid in C ++

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

Valid in both:

 int *j = (int *) malloc(sizeof(int) * 5); 
  • Enumeration constants (enumeration values) are always of type int in C, while they are different types in C ++ and can have a size different from the value of int.
  • C allows you to declare struct, union, and enum types in function prototypes, while C ++ does not.

Behave differently in C and C ++

  • Character literals such as 'a' are of type int in C and type char in C ++
  • The static keyword is used in C to restrict a function or global variable to the scope of a file (internal binding). This is also true in C ++, although C ++ condemns this use in favor of anonymous namespaces (which are not available in C). In addition, C ++ implicitly treats any const global as a file region unless it is explicitly declared extern, unlike C, in which extern is the default value. Conversely, inline functions in C have the scope of the file, while they have an external connection by default in C ++.

You can find a list of comprehensive differences here.

+4
source

You can "write C ++" in many styles - this is one of the main strengths of the language. This includes a strictly procedural, flat programming style common to C programs. You will still be writing C ++, but the code should look very familiar to any C programmer.

Strictly speaking, you will need to use the C ++ <cstdio> , etc., and all of your C library functions are in the std . Perhaps this is one of the few legitimate situations where you should use using namespace std; ! :-)

+2
source

I see no reason for such a thing, g ++ and gcc are just different interfaces for the same compiler. Thus, for everyone regarding performance, byte compatibility, etc., there should be no problem mixing the .o files that are created by both.

C and C ++ have a lot of subtle differences that can cause you problems, starting with things like sizeof 'a' beeing different, but sizeof c is the same (if c is char ) so that bool is a type in one and the macro in another, true is of type bool in C ++ and int in C, C, not allowing static declarations in for ...

And even if it is that C and C ++ have a big intersection, if you limit yourself to what is considered good coding practice in both communities, you quickly find that the intersection is almost empty. This applies to casting pointers, highlighting with malloc or new , compound initializers and constructors, variable-length arrays and vector classes ...

Just don't do it. All you need to do is create a nice interface suitable for both.

+1
source

No, g++ and gcc are not two different fronts for the same compiler.

These are two different drivers for two different compilers ( cc1plus and cc1 ) that use the same middle and inner code (with the built-in GCC tree inside gcc/libbackend.a ), since it contains both the middle level (the middle end is a common part GCC, common to C, C ++, Ada, Fortran, Objective-C, ... and many target machines, is the largest part of GCC and works on a common set of internal views, in particular Gimple) and back-end (part of GCC transforms Gimple views into an assembly using RTL, an internal target-dependent view).

But cc1 and cc1plus have different interface codes.

0
source

Source: https://habr.com/ru/post/1380740/


All Articles