a firstworks project
Rudiments
About Documentation Download Licensing News

Using the dynamiclib class

The dynamiclib class provides methods for loading dynamic libraries (shared objects/DLLs) at runtime and accessing their symbols.

This is useful when you want to load a library that might not be available on every system, or when you want to implement a plugin architecture.

The open() method loads the library. The getSymbol() method looks up a function or variable by name and returns a pointer to it. The close() method unloads the library.

The following example opens the math library (libm.so), looks up the ceil() and floor() functions by name, calls them, and closes the library.

#include <rudiments/dynamiclib.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	dynamiclib	dl;

	// open the math library
	if (!dl.open("libm.so",true,true)) {
		stdoutput.write("failed to open libm.so\n");
		return 1;
	}
	stdoutput.write("opened libm.so\n\n");


	// look up the ceil() function
	double (*ceilfn)(double)=
			(double (*)(double))dl.getSymbol("ceil");
	if (!ceilfn) {
		stdoutput.write("failed to find ceil()\n");
		dl.close();
		return 1;
	}

	// call ceil()
	stdoutput.printf("ceil(1.1) = %g\n",ceilfn(1.1));
	stdoutput.printf("ceil(1.9) = %g\n\n",ceilfn(1.9));


	// look up the floor() function
	double (*floorfn)(double)=
			(double (*)(double))dl.getSymbol("floor");
	if (!floorfn) {
		stdoutput.write("failed to find floor()\n");
		dl.close();
		return 1;
	}

	// call floor()
	stdoutput.printf("floor(1.1) = %g\n",floorfn(1.1));
	stdoutput.printf("floor(1.9) = %g\n\n",floorfn(1.9));


	// close the library
	dl.close();
	stdoutput.write("closed libm.so\n");
}
Copyright 2017 - David Muse - Contact