Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use extern "C" in simple words? [duplicate]

Tags:

c++

c

extern-c

Maybe I'm not understanding the differences between C and C++, but when and why do we need to use

extern "C" { 

? Apparently its a "linkage convention".

I read about it briefly and noticed that all the .h header files included with MSVS surround their code with it. What type of code exactly is "C code" and NOT "C++ code"? I thought C++ included all C code?

I'm guessing that this is not the case and that C++ is different and that standard features/functions exist in one or the other but not both (ie: printf is C and cout is C++), but that C++ is backwards compatible though the extern "C" declaration. Is this correct?

My next question depends on the answer to the first, but I'll ask it here anyway: Since MSVS header files that are written in C are surrounded by extern "C" { ... }, when would you ever need to use this yourself in your own code? If your code is C code and you are trying to compile it in a C++ compiler, shouldn't it work without problem because all the standard h files you include will already have the extern "C" thing in them with the C++ compiler?

Do you have to use this when compiling in C++ but linking to already built C libraries or something?

like image 563
Russel Avatar asked May 09 '10 06:05

Russel


People also ask

When should extern C be used?

You need to use extern "C" in C++ when declaring a function that was implemented/compiled in C. The use of extern "C" tells the compiler/linker to use the C naming and calling conventions, instead of the C++ name mangling and C++ calling conventions that would be used otherwise.

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

You need to use extern "C" in C++ when declaring a function that was implemented/compiled in C. The use of extern "C" tells the compiler/linker to use the C naming and calling conventions, instead of the C++ name mangling and C++ calling conventions that would be used otherwise. For functions provided by other libraries, you will almost never need to use extern "C", as well-written libraries will already have this in there for the public APIs that it exports to both C and C++. If, however, you write a library that you want to make available both in C and in C++, then you will have to conditionally put that in your headers.

As for whether all C code is C++ code... no, that is not correct. It is a popular myth that C++ is a "superset of C". While C++ certainly strives to be as compatible with C as possible, there are some incompatibilities. For example, bool is valid C++ but not valid C, while _Bool exists in C99, but is not available in C++.

As to whether you will ever need to use extern "C" with the system's ".h" files.... any well-designed implementation will have those in there for you, so that you do not need to use them. However, to be certain that they are provided, you should include the equivalent header file that begins with "c" and omits ".h". For example, if you include <ctype.h>, almost any reasonable system will have the extern "C" added; however, to be assured a C++-compatible header, you should instead include the header <cctype>.

You may also be interested in Mixing C and C++ from the C++ FAQ Lite.

like image 58
Michael Aaron Safyan Avatar answered Oct 02 '22 11:10

Michael Aaron Safyan


The other answers are correct, but a complete "boilerplate" example will probably help. The canonical method for including C code in C and/or C++ projects is as follows:

// // C_library.h //  #ifdef __cplusplus extern "C" { #endif  // // ... prototypes for C_library go here ... //  #ifdef __cplusplus } #endif 

-

// // C_library.c //  #include "C_library.h"  // // ... implementations for C_library go here ... // 

-

// // C++_code.cpp //  #include "C_library.h" #include "C++_code.h"  // // ... C++_code implementation here may call C functions in C_library.c ... // 

Note: the above also applies to calling C code from Objective-C++.

like image 23
Paul R Avatar answered Oct 02 '22 10:10

Paul R