You
can call a C function (possibly from a library) from C++ code, but
not the other way around! Also, if you call a C function from C++ code,
extern "C"
disables the
name mangling that a C++ compiler normally does.
That is why you will often see
header files like this to make it work with C and C++ compiler:
1 2 3 4 5 6 7 8 9
|
#ifdef __cplusplus
extern "C" {
#endif
int some_function_name(int param1, int param2);
#ifdef __cplusplus
}
#endif
| |
If you
must call a C++ method/class from C code, then I think the
only option is to write a "wrapper" function in
C++ that performs the actual call to the C++ method/class. This "wrapper" must be a simple function in global namespace (
no class or anything), so that it can be called from C code. I do
not think that there is any difference here between calling the C++ method/class from a "static" or "shared" library (or no library at all).
As for the actual
linking, a "static" library file (
.a
) is really just an archive containing a bunch of compiled object code files (
.o
). So, there is
no such thing as a "C" or "C++" library. There just are a bunch object code files (
.o
), each of which may have been compiled from C or C++ code, or maybe even from other languages...