Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does extern "C" have any effect in C?

Tags:

c++

c

extern-c

I just got some C code that uses extern "C" to declare external functions like this:

extern "C" void func();

Is this valid C? I'm getting an error at this line, but I'm not sure if it's because of this or something else.

like image 563
Jason Baker Avatar asked Apr 04 '09 20:04

Jason Baker


1 Answers

No, it's not valid C. It should only be used in C++ code to refer to functions defined in C code. The extern "C" should be surrounded in a ifdef __cplusplus/#endif block:

// For one function
#ifdef __cplusplus
extern "C"
#endif
void func();

// For more than one function
#ifdef __cplusplus
extern "C"
{
#endif

void func1();
void func2();

#ifdef __cplusplus
}
#endif
like image 186
Adam Rosenfield Avatar answered Oct 19 '22 23:10

Adam Rosenfield