I saw in a C++ program that
dlfcn
library is used for dynamically linking to a shared object library chosen by the user of the C++ program during runtime, and for calling functions in the chosen shared object library viadlsym()
and other functions indlfcn
.Assume the user chooses a shared object library called
x.so
during runtime.x.so
was compiled from a cpp file with definitions of functions enclosed withinextern "C"
. A comment in the cpp file says that the usage ofextern "C"
is important but without further explanation, and I wonder why?Is it correct that there is only C++ code and no C code involved here? So is
extern "C"
not necessarily only used when mixing C and C++ code together?Does whether
dlfcn
library is statically or dynamically linked to the C++ program matters to the questions above?
- Now compare to a simpler case, where the shared object library is
known much earlier than runtime, and the author of the C++ program
specifies it in the C++ program without using
dlfcn
before compiling it, and then dynamically links the shared object library and the C++ program during runtime. In this case, is `extern "C" still necessary in the cpp file which was compiled into the shared object library?
Thanks.