Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the extern “C” group C++ class (not header) here?

Tags:

c++

linkage

I was searching SVM libraries and encountered BudgetedSVM.

In the source code, I found an unusual usage, just like this:

#sample.h

#ifndef SAMPLE_H
#define SAMPLE_H

//no header included or namespace declared here

#ifdef __cplusplus
extern "C" {
#endif

//no header included or namespace declared too

class Sample: public Parent
{
public:
    Sample();
    ~Sample();

    type0 fun(type1 val1, type2 val2);
    ...
};

#ifdef __cplusplus
}
#endif

#endif // SAMPLE_H

As seen, no extra header or namespace is needed in the header, which all are in the cpp file.

Here are my thoughts:

  1. Why does extern "C", which usually is used for C interfaces, group the C++ class? Is the usage in this way good for something?

  2. Even if type0, type1 and type2 appeared, their own headers are not included here, but in the cpp file (e.g. sample.h). When I call the class Sample, however, I have to include these headers (e.g. type0.h, type1.h, type2.h), which seems inconvenient.

like image 204
foo Avatar asked Feb 14 '17 10:02

foo


People also ask

Where do you put extern C?

The extern must be applied to all declarations in all files. (Global const variables have internal linkage by default.) extern "C" specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.

Can you use extern C in C?

By declaring a function with extern "C" , it changes the linkage requirements so that the C++ compiler does not add the extra mangling information to the symbol. This pattern relies on the presence of the __cplusplus definition when using the C++ compiler. If you are using the C compiler, extern "C" is not used.

Is extern needed in header?

With variables, it is important to use the extern keyword (and no initializer) in the header file. Consequently, for symmetry with the (very few) global variables declared in headers, I use extern with the function too - even though it is strictly not necessary.

Does C need a header file?

The creation of header files are needed generally while writing large C programs so that the modules can share the function definitions, prototypes etc. Function and type declarations, global variables, structure declarations and in some cases, inline functions; definitions which need to be centralized in one file.


1 Answers

According to C++ Standard C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions.

So such grouping looks meaningless, unless type0, type1 or type2 are function pointers defined inline, in this case they will have C linkage.

In case type0, type1 or type2 are user defined types and their definitions are placed in separate headers it violates the good C/C++ style rule - all headers shall be self-contained and require no additional inclusions to compile.

To answer the main question ('Why?') - it looks like C language linkage was just blindly copy-pasted from another header (possibly pure C, where it makes sense). Or it maybe done deliberately - by "uniformity" reasons.

like image 160
Rost Avatar answered Sep 18 '22 19:09

Rost