extern "C" isn't for compiling c inside cpp source. It's only to use ABI of C inside cpp source/header: mangling, calling conventions, exception handling... (thanks to Ajay Brahmakshatriya)
By mangling, I want to say the internal unique name of function used by compiler/linker. C mangling is really different to c++ mangling, and so not compatible. To find C function inside c++, you have to say to compiler/linker under which internal unique name the function is known.
extern "C" only switches which ABI has to be used, including mangling used to create internal unique name and how function has to be called, not to switches compilation mode.
If you really want to compile c code, you have to put your code inside a c source file and compile it separately. And use extern "C"to declare function in cpp environment, to allow c++ code to use it. BUT declaration of function has to be compatible with c++, and double Test( int nVar1, double f[nVar1] ) isn't.
function.c, compile with gcc -c:
double Test( int nVar1, double f[] ) {
return f[nVar1-1];
}
function.h, compatible c and c++:
#ifndef _FUNCTION_H
#define _FUNCTION_H
#ifdef __cplusplus
extern "C" {
#endif
double Test( int nVar1, double f[] );
#ifdef __cplusplus
}
#endif
#endif
main.cpp, compile with g++ -c:
#include "function.h"
int main() {
cout << "!!!Hello World!!!" << endl;
double f[2] = {1.0,2.0};
Test(2, f);
return 0;
}
And finally, link everything with g++ linker:
g++ function.o main.o -o my_program
See example here: