Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "extern C++" work?

I jumped into winnt.h and I found out the code as following:

extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];

I'd like to ask questions as following:

  1. how does extern "C++" work?
  2. is this portable among GCC, and Clang?
  3. can all templates be exported with this syntax?

With question 3, I mean that can I separate declearation and definition of the templates, and then generate a dynamic link for the template without actually give the implementation by using this trick?

like image 733
Adam Avatar asked Jun 15 '14 12:06

Adam


People also ask

How does extern work in C?

“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.

Why do we use extern C?

Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.

Is extern C needed?

the extern keyword is used to extend the visibility of variables/functions. Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit.

What is the use of extern C in C++?

The extern “C” keyword is used to make a function name in C++ have the C linkage. In this case the compiler does not mangle the function.


2 Answers

Well, extern "C++" won't work in C, of course (though some compilers might support it as an extension). So it only makes sense to use it in C++.

That's because in the case of multiple nested extern linkage specifiers, the innermost one takes effect. So if you have a header file surrounded with extern "C", you can use extern "C++" to temporarily break out of it and declare something with C++ linkage.

It makes the most sense when you want to provide a generally C interface for a C++ library, but you also want to provide C++ helper bits for people actually using it in C++. So you'd put #ifdef __cplusplus \ extern "C" { \ #endif around the header as a whole, and then you ifdef-in those bits with extern "C++" to revert to C++ linkage.

like image 184
Sneftel Avatar answered Sep 25 '22 17:09

Sneftel


  1. It works by forcing the compiler to use C++ linkage when the surrounding code uses C linkage by default (e.g., you include winnt.h in a C program).
  2. Yes, it should be portable.
  3. Yes they can. There is not much use for "extern "C++"" in C++ programs because the linkage is "C++" anyway. It makes sense to use "extern "C++"" only if there is a good chance that your C++ code will be included into a C code.
like image 23
Oleg Avatar answered Sep 25 '22 17:09

Oleg