Advertisement

dlsym failing. Reasons?

Started by April 10, 2005 07:12 AM
6 comments, last by python_regious 19 years, 6 months ago
Hi all. I seem to have a problem loading a shared object, or more specificially a couple of functions in the SO. The SO loads fine, however, whenever I try to get some function pointers with dlsym it fails. Now, it *should* work, the functions are certainly there, and the equivelent code works fine in windows. Now, just to be sure I'm not being a total twat and missing something obvious, here's the relevant code that loads the SO and attemepts to load the functions:

    dll_handle = dlopen( "./simulator.so", RTLD_LAZY );

    if( !dll_handle )
	{
		// failed to open the shared object/dll, so print an error and exit
		cout<<"Fatal Error: Failed to open the simulator shared object/dll.\n";
		cout<<"dlerror: "<<dlerror()<<endl;
		return 0;
	}

	// load the functions
	RetrieveSimulatorInterface = (PFNRETRIEVESIMULATORINTERFACE)dlsym( dll_handle, "RetrieveSimulatorInterface" );
	DestroySimulatorInterface = (PFNDESTROYSIMULATORINTERFACE)dlsym( dll_handle, "DestroySimulatorInterface" );

	if( !RetrieveSimulatorInterface ||
		!DestroySimulatorInterface )
	{
		cout<<"Error: Cannot load the Simulator Interface retrieval/destruction functions.\n";
		cout<<"dlerror: "<<dlerror()<<endl;
		dlclose( dll_handle );
		return 0;
	}


Now, I'm compiling the SO using the following command:
g++ <source files> -shared -o simulator.so
That correct? For completeness, the executable is compiled using:
g++ <source files> -l dl -o text_interface
Oh, and these are the functions, in the SO, that I'm wishing to get pointers to:

extern "C"
{
	bool RetrieveSimulatorInterface( ISimulatorManager **ptr, int version );
	void DestroySimulatorInterface( ISimulatorManager **ptr );
}


dlerror is reporting this on failure: "./text_interface: undefined symbol: DestroySimulatorInterface" Any ideas why this isn't working? Thanks all.
If at first you don't succeed, redefine success.
I have some news for you. Since you used RTLD_LAZY, the .so didn't load yet, so don't say it loads fine. Use another define to make dlopen actually load the object, and see what errors you get.

http://www.mildspring.com - developing android games

Advertisement
I think I may know your problem. You must compile the object files that make up your .so with the -fPIC flag, to ensure they are generated with position-indepedant code that can be found by dlsym().
I guess your problem is namemangling of functionname when
compiled with g++. Use 'nm' to look into the library
and search for the functions, if the names doesn't match
exactly than this is your problem.
Hmmm... Ok, well I added the -fPIC flag to the so compile, and for the hell of it change RTLD_LAZY to RTLD_NOW, didn't do a thing. [sad]
If at first you don't succeed, redefine success.
With this flag: RTLD_NOW you should be getting dlopen errors.
But you're saying nothing changed, so look into what lazarus07 said.
Run:
nm simulator.so and see if you get the function names you expect.
If not, than you are probably not including the header file you mentioned into the .cpp files in your shared object.
As mentioned before by lazarus07, C++ compiler mangles function names, all of the compilers do it differently (even depending on version of compiler), so your names could just have underscores around them, or some really weird characters.

http://www.mildspring.com - developing android games

Advertisement
Do:

objdump --dynamic-syms simulator.so

to make certain the the symbols that you think are there are indeed there.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Yeah, I know about name mangling, hence the 'extern "C"' around the functions.

I've fixed it anyway, as it happens I was just being a total twat. I was actually missing a file in compilation, which were the two exported functions I was trying to use *slaps forehead*. That nm is a really useful tool though, it's what made me find that out.

Thanks for your help all anyway.
If at first you don't succeed, redefine success.

This topic is closed to new replies.

Advertisement