what does this code mean?

hi, could someone please explain to me what this code below actually is doing to my program? thanks

extern "C" int foo(char* cstr);
extern "C" double bar(double a, double b);

extern "C"
{
int foo(char* cstr)
{
int v = 0;
// ...
return v;
}

double bar(double a, double b)
{
double v = 0;
// ...
return v;
}
}
The extern "C" tells the compiler that it shouldn't mangle the names of the functions.

C++ lets you overload functions, but linkers deal with symbols. So if file1.cpp calls foo(int) and foo(double) which are defined in file2.cpp, how does the linker know which call refers to which function? The answer is that the compiler "mangles" the names by combining the function names and their parameters. The linker sees these names and sorts it all out.

In C, the names are not mangled. Adding extern "C" tells the compiler that it shouldn't mangle the names. This way you can call a C function from code compiled with C++ or vice versa.
Topic archived. No new replies allowed.