Possible Duplicate:
Why do we need extern “C”{ #include <foo.h> } in C++?
I have often seen programs coded like:
extern "C" bool doSomeWork() {
//
return true;
}
Why do we use an extern "C"
block? Can we replace this with something in C++? Is there any advantage to using extern "C"
?
I do see a link explaining this but why do we need to compile something in C when we already have C++?
extern "C" makes names not mangled.
It used when:
We need to use some C library in C++
extern "C" int foo(int);
We need export some C++ code to C
extern "C" int foo(int) { something; }
We need an ability to resolve symbol in shared library -- so we need to get rid mangling
extern "C" int foo(int) { something; }
///
typedef int (*foo_type)(int);
foo_type f = (foo_type)dlsym(handle,"foo")