Serge Roussak
Serge Roussak

Reputation: 1807

Is there any difference, whether a struct declared in the 'extern "C"' block or not?

Let's say, I have the header file:

#ifdef __cplusplus
extern "C" {
#endif

struct S
{
  int i;
  double d;
  // etc
};

// Or start the 'extern "C"' block here?..

void f(A* a);

#ifdef __cplusplus
}
#endif

It should be used by C/C++ sources.

My question is: if I carry the struct A declaration out of the extern "C", whether this code will be compatible with a library which was built with the struct A declared as extern "C"?

That is, we built a library with the header as it is presented above, then we moved the extern "C" to the position marked by the corresponding comment line and, finally, we are trying to build a C++ application using the resulting header.

If all of this will be successful? In other words, if the extern "C" influence on a struct declaration?

Whether it depends what language (C/C++) the library is written on?

Upvotes: 1

Views: 224

Answers (1)

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14589

extern by itself declares external linkage, which is a default state for C++ functions.

Specification "C" gives C language linkage.

Only function names and variable names with external linkage have a language linkage, classes and class members are not affected. struct declaration is a class. If that file meant to be used with C code, there might be problem that older C standard do not support declaration in that format.

Specification "C" makes name conventions of externally linked entities compatible with said language, even while compatibility is implementation-dependent. E.g. for gcc your function would have C++ mangled name _Z3fooP1A, but with C it might be just _foo.

Upvotes: 2

Related Questions