Writing .so libraries

I would like to create a program wherein I compile a user interface that has the ability to dynamically load libraries from a .so file. My abstraction class in my user interface program will be programmed to look for the library, but I don't know how to implement that in C++ code, or how to compile my libraries so that I can distribute them as updates without having to re-distribute the binary file.

If anybody has any insight on how to accomplish this, or where I can look for an up to date tutorial (I've seen a few, but they generally refer to C programming), I would appreciate it.
From an object design point of view, you need some abstract interface that the loadable modules implement. Each seperate implementation implements a concrete instance of this interface that does something and provides a factory/creator function that instantiates the class.

From a code partitioning point of view, you put the abstract interface into a common shared library. The app uses it and the pluggable modules use it.

The modules export a single function, the creator function that returns a pointer to the abstract class which is the local concrete instance you need to use.

You can either keep a registry of shared libraries or place them in the same directory. The app picks them up at runtime, loads and calls the creator and uses the returned class.
While this is helpful, I have the interface abstraction in place already, and I plan on plugging the .so files into specified directories.

My question was where I can look to learn how to compile each object (say Impl.h & Impl.cpp) into Impl.so, and from the code in my interface, how to point at the object that's contained in Impl.so.
Google for "building shared libraries"
Hi, I am currently writing an IRC bot that accepts plugins. I reduced the relevant code to a skeleton example. Note that this code uses C++11 for it std::shared_ptr class. If you don't have C++11 then you can use boost or failing that just a raw pointer.

Plugin library header: plugin.h (#include in main program and your plugin code).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#ifndef __PLUGIN_H__
#define __PLUGIN_H__

#include <memory>
#include <string>

#include <dlfcn.h>

class Plugin
{
public:
	virtual ~Plugin() {}

	virtual void func1() = 0;
	virtual void func2() = 0;
};

typedef std::shared_ptr<Plugin> PluginPtr;

/**
 * Deals with all the dynamic loading jiggery pokery.
 *
 * Usage:
 *
 * PluginLoader load;
 *
 * PluginPtr plugin = load("plugin_file_name.so");
 *
 * if(plugin)
 * {
 *     // Insert plugin into program
 * }
 *
 * @param file pathnam of plugin file to be loaded.
 */
class PluginLoader
{
public:
	PluginPtr operator()(const std::string& file);
};

#endif // __PLUGIN_H__ 


Plugin library source: plugin.cpp (compile as shared object and link with main program)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "plugin.h"

#include <stdexcept>

PluginPtr PluginLoader::operator()(const std::string& file)
{
	union { void* dl; void* dlsym; PluginPtr(*plugin)(); } get;

	if(!(get.dl = dlopen(file.c_str(), RTLD_NOW|RTLD_GLOBAL)))
	{
		throw std::runtime_error(dlerror());
	}

	// Obtain pointer to the Plugin creation factory function
	if(!(get.dlsym = dlsym(get.dl, "create_plugin")))
	{
		throw std::runtime_error(dlerror());
	}

	return get.plugin();
}


Main program code: main.cpp (compile as executable and link with -ldl and -lplugin [libplugin.so] plugin library.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "plugin.h"

#include <iostream>
#include <stdexcept>

int main()
{
	PluginLoader load;

	PluginPtr plugin;

	try
	{
		plugin = load("build/src/.libs/myplugin.so");

		if(plugin)
		{
			plugin->func1();
			plugin->func2();
		}
	}
	catch(std::runtime_error& e)
	{
		std::cout << e.what() << '\n';
	}

}


My plugin code: my_plugin.cpp (#include plugin.h - compile as shared object myplugin.so)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "plugin.h"

#include <iostream>

class MyPlugin
: public Plugin
{
public:

	virtual void func1()
	{
		std::cout << "func1()\n";
	}
	virtual void func2()
	{
		std::cout << "func2()\n";
	}
};

// Factory function MUST have C linkage
extern "C" PluginPtr create_plugin()
{
	return PluginPtr(new MyPlugin);
}
Last edited on
I plan on using this information, but I feel as if I may need some more direct assistance. Would you be willing to contact me at my listed email?
Topic archived. No new replies allowed.