I didn't know the answer to this, but it seemed like something interesting, so I gave it a try and got it to work. It looks like extern "C" only switches the calling convention to C-style, and does not declare functions as extern. The extern "C" is needed both when declaring the function for import and when exporting it.
I made a test app on FC4 with g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8).
First I made library.cpp:
Code:
//Shared Library
#include <iostream>
using namespace std;
extern "C"
{
extern void ImplFunc();
}
void LibFunc()
{
cout << "Inside LibFunc. v1" << endl;
ImplFunc();
}
I compiled this with the following command:
g++ -shared library.cpp -o library.so
Next I created a cpp for your implementation, main.cpp:
Code:
//Implementation
#include <iostream>
using namespace std;
extern void LibFunc();
extern "C"
{
void ImplFunc()
{
cout << "Implementation's ImplFunc called" << endl;
}
}
int main()
{
cout << "Calling LibFunc" << endl;
LibFunc();
cout << "LibFunc done" << endl;
}
I compiled it with this command:
g++ library.so main.cpp -o main
So I wouldn't have to install library.so somewhere, I changed my library path:
export LD_LIBRARY_PATH=.
When I run main, I get:
Code:
Calling LibFunc
Inside LibFunc. v1
Implementation's ImplFunc called
LibFunc done
To be sure that library.so isn't statically linked with main, I changed the "v1" to "v2" in library.cpp, and recompiled *just* library.so. Here is the new output from main:
Code:
Calling LibFunc
Inside LibFunc. v2
Implementation's ImplFunc called
LibFunc done