Jump to content
  • Advertisement
Sign in to follow this  
methinks

dynamic linking on linux

This topic is 3996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi A while ago I wrote a small framework for dynamically loading plugins in windows, which I'm now trying to port to linux. I finally have my program compiling properly, but I get runtime errors when it tries to link the libraries. -Initially it couldn't find the library at all, until I added ./ in front of the library name -now I get an error "undefined symbol: __dso_handle" I'm pretty new to linux, so it's probably something stupid that I missed. Any help is appreciated. (This is not my full program, just a small test app I wrote to figure out how everything works.) The main program file (contained in main.cpp) is compiled with g++ -o pluginTest main.cpp -ldl and run with ./pluginTest
#include <iostream>
#include <dlfcn.h>

int main()
{
	std::cout<<"Starting Program"<<std::endl;
	void * handle;
	void (*libraryObject)();
	char * error;

	std::cout<<"Getting handle to library"<<std::endl;
	handle = dlopen("./extra.so.1.0",RTLD_NOW);

	if(!handle)
	{
		error = dlerror();
		std::cout<<"Error getting library handle: "<<error<<std::endl;
	}
	else
	{
		std::cout<<"Accessing library function"<<std::endl;
		libraryObject = (void(*)())dlsym(handle,"run");

		std::cout<<"Running library function"<<std::endl;
		libraryObject();

		std::cout<<"Unloading Library"<<std::endl;
		dlclose(handle);
	}
}

the "plugin" (contained in extra.cpp) is compiled with g++ -fPIC -c extra.cpp ld -shared -soname extra.so.1 -o extra.so.1.0 -lc extra.o
#include <iostream>

extern "C" void run() {std::cout<<"This is the plugin"<<std::endl;}

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure about the rest but using the -L. switch with gcc should tell it that the library resides in the same directory as the executable.

Share this post


Link to post
Share on other sites
I had a suspicion that you should use g++ to link the plugin instead of ld, because it turns on some funky C++-specific linking stuff by default. So to make sure I copied your sources and tried linking your way (got the same error you did) and then with g++. Linking with g++ fixes it.


devin@neo gamedev-help (0)% ld -shared -soname extra.so.1 -o extra.so.1.0 -lc extra.o
devin@neo gamedev-help (0)% ./pluginTest
Starting Program
Getting handle to library
Error getting library handle: ./extra.so.1.0: undefined symbol: __dso_handle
devin@neo gamedev-help (0)% g++ -shared -soname extra.so.1 -o extra.so.1.0 extra.o
g++: extra.so.1: No such file or directory
g++: unrecognized option '-soname'
devin@neo gamedev-help (1)% g++ -shared -o extra.so.1.0 extra.o
devin@neo gamedev-help (0)% ./pluginTest
Starting Program
Getting handle to library
Accessing library function
Running library function
This is the plugin
Unloading Library

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!