I wasted a lot of time today trying to understand why a certain code did not find a function (via the function name) in a shared object ( Linux equivalent of Windows DLLs ). The same code had no problem finding the function in the Windows DLL version. The function was a simple C function (although it was defined in a cpp file which contained som C++ class definitions and implementations)
If you encounter this problem, I’ll give you the short version of the story so you can carry on with your work without having to listen to boring war stories: When you compile a shared library with g++, and the C functions you export are defined within a cpp file that might also contain other C++ code, you have to wrap the exported C function definitions with an
extern “C” {
// … exported function definitions go here
}
otherwise the exported functions will get mangled by the compiler in the same way C++ methods do and the code that tries to find the function by its name will not find it.
Ok - boring war story comes here, continue only if you have nothing better to do:
After trying just about every relevant combination of g++ compiler flags, I eventually arrived at the conclusion that the name is either not exported at all or it is exported incorrectly. I had a suspicion that it might be due to g++ mangling the C function names. Searching the net I learned that the readelf utility can, among other things dump all the symbolic names exported by a shared library (yes, you already know this but I’m refreshing my Linux knowledge after a long break…). The function I was looking for was named “SetEventFunc” so I ran a :
readelf -s Sockets.o | grep SetEvent
sure enough - what was found was a function named: _Z12SetEventFuncPFvPKcE
no wonder the function was not found
I then wrapped the exported C functions in extern “C”, rebuilt the library and the next grep showed a clean SetEventFunc (as shown below)

I guess what threw me off was the fact that in Windows, the compiler (VS2005) doesn’t seem to mangle C style functions, even if they are declared in a C++ file, so the problem never came up in windows.
By the way - the screen shot above is my first screen shot on Linux, performed with the standard tools described here - I have to say that Linux in general and Ubuntu in particular are starting to grow on me. I’m writing this from a virtual VMWare Ubuntu desktop, but the more I use Ubuntu, the more I feel like making my Windows XP the VMWare guest OS instead of the VMWare hosting OS…