Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between extern "C" and simply extern [duplicate]

Tags:

c++

c

extern-c

I have seen C/C++ code using extern "C" declared in function signatures and also while including a C header into a CPP file.

but some functions just declare extern before their signature(without the "C").

QN1:

are these both ways of defining functions have same effect or do they imply different things?

sorry if I am very silly but I am not able to find this difference through Google.

Eg:

extern int someFunction( void *ret_val);

extern "C" int someFunction( void *ret_val);

QN2:

if a function is declared with an extern in its signature, is it necessary for the corresponding header file to be included inside a extern "C" block?

As pointed by another user in comments, the marked duplicate does not fully satisfy the question here. I am editing so that in future others may not be mislead into a different question.

like image 986
Mohamed Iqzas Avatar asked Jan 20 '15 14:01

Mohamed Iqzas


2 Answers

extern "C" disables name mangling. It will allow your C++ code to call functions from library compiled by C compiler

like image 138
lowtech Avatar answered Oct 20 '22 04:10

lowtech


extern "C" int someFunction( void *ret_val);  

will make someFunction have C linkage.

like image 4
haccks Avatar answered Oct 20 '22 03:10

haccks