Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Tutorial

Plugin basics
Factory method in plugin
Searching for a symbol in multiple plugins
Linking plugin into the executable
Symbol shadowing problem (Linux)
Executing callbacks on library unload
Querying libraries for symbols
Advanced library reference counting
Importing a C function from Windows dll

This Tutorial is provided to give you an idea of how to create and use plugins.

The first thing to do when creating your own plugins is define the plugin interface. There is an example of an abstract class that will be our plugin API:

#include <boost/config.hpp>
#include <string>

class BOOST_SYMBOL_VISIBLE my_plugin_api {
public:
   virtual std::string name() const = 0;
   virtual float calculate(float x, float y) = 0;

   virtual ~my_plugin_api() {}
};

Now let's make a DLL/DSO library that will holds implementation of plugin interface and exports it using the extern "C" and BOOST_SYMBOL_EXPORT:

#include <boost/config.hpp> // for BOOST_SYMBOL_EXPORT
#include "../tutorial_common/my_plugin_api.hpp"

namespace my_namespace {

class my_plugin_sum : public my_plugin_api {
public:
    my_plugin_sum() {
        std::cout << "Constructing my_plugin_sum" << std::endl;
    }

    std::string name() const {
        return "sum";
    }

    float calculate(float x, float y) {
        return x + y;
    }

    ~my_plugin_sum() {
        std::cout << "Destructing my_plugin_sum ;o)" << std::endl;
    }
};

// Exporting `my_namespace::plugin` variable with alias name `plugin`
// (Has the same effect as `BOOST_DLL_ALIAS(my_namespace::plugin, plugin)`)
extern "C" BOOST_SYMBOL_EXPORT my_plugin_sum plugin;
my_plugin_sum plugin;

} // namespace my_namespace

Simple application that loads plugin using the boost::dll::import and append_decorations:

#include <boost/dll/import.hpp> // for import_alias
#include <iostream>
#include "../tutorial_common/my_plugin_api.hpp"

namespace dll = boost::dll;

int main(int argc, char* argv[]) {

    boost::dll::fs::path lib_path(argv[1]);             // argv[1] contains path to directory with our plugin library
    boost::shared_ptr<my_plugin_api> plugin;            // variable to hold a pointer to plugin variable
    std::cout << "Loading the plugin" << std::endl;

    plugin = dll::import_symbol<my_plugin_api>(         // type of imported symbol is located between `<` and `>`
        lib_path / "my_plugin_sum",                     // path to the library and library name
        "plugin",                                       // name of the symbol to import
        dll::load_mode::append_decorations              // makes `libmy_plugin_sum.so` or `my_plugin_sum.dll` from `my_plugin_sum`
    );

    std::cout << "plugin->calculate(1.5, 1.5) call:  " << plugin->calculate(1.5, 1.5) << std::endl;
}

That application will output:

Loading the plugin
Constructing my_plugin_sum
plugin->calculate(1.5, 1.5) call:  3
Destructing my_plugin_sum ;o)

Full sources:

Back to the Top

In previous example we were importing from a plugin a single variable. Let's make a class that uses our plugin API plugin and holds some state:

#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS   
#include "../tutorial_common/my_plugin_api.hpp"

namespace my_namespace {

class my_plugin_aggregator : public my_plugin_api {
    float aggr_;
    my_plugin_aggregator() : aggr_(0) {}

public:
    std::string name() const {
        return "aggregator";
    }

    float calculate(float x, float y) {
        aggr_ += x + y;
        return aggr_;
    }

    // Factory method
    static boost::shared_ptr<my_plugin_aggregator> create() {
        return boost::shared_ptr<my_plugin_aggregator>(
            new my_plugin_aggregator()
        );
    }
};


BOOST_DLL_ALIAS(
    my_namespace::my_plugin_aggregator::create, // <-- this function is exported with...
    create_plugin                               // <-- ...this alias name
)

} // namespace my_namespace

As you may see, my_namespace::create_plugin is a factory method, that creates instances of my_namespace::my_plugin_aggregator. We export that method with the name "create_plugin" using BOOST_DLL_ALIAS.

#include <boost/dll/import.hpp> // for import_alias
#include <boost/function.hpp>
#include <iostream>
#include "../tutorial_common/my_plugin_api.hpp"

namespace dll = boost::dll;

int main(int argc, char* argv[]) {

    boost::dll::fs::path shared_library_path(argv[1]);                  // argv[1] contains path to directory with our plugin library
    shared_library_path /= "my_plugin_aggregator";
    typedef boost::shared_ptr<my_plugin_api> (pluginapi_create_t)();
    boost::function<pluginapi_create_t> creator;

    creator = boost::dll::import_alias<pluginapi_create_t>(             // type of imported symbol must be explicitly specified
        shared_library_path,                                            // path to library
        "create_plugin",                                                // symbol to import
        dll::load_mode::append_decorations                              // do append extensions and prefixes
    );

    boost::shared_ptr<my_plugin_api> plugin = creator();
    std::cout << "plugin->calculate(1.5, 1.5) call:  " << plugin->calculate(1.5, 1.5) << std::endl;
    std::cout << "plugin->calculate(1.5, 1.5) second call:  " << plugin->calculate(1.5, 1.5) << std::endl;
    std::cout << "Plugin Name:  " << plugin->name() << std::endl;
}

In that application we have imported the factory method using boost::dll::import_alias.

[Caution] Caution

Be careful: creator variable holds a reference to the loaded shared library. If this variable goes out of scope or will be reset, then the DLL/DSO will be unloaded and any attempt to dereference the plugin variable will lead to undefined behavior.

Output of the application will be the following:

plugin->calculate(1.5, 1.5) call:  3
plugin->calculate(1.5, 1.5) second call:  6
Plugin Name:  aggregator

Full sources:

Back to the Top

Consider the situation: we have multiple plugins, but only some of them have symbols that we need. Let's write a function that search list of plugins and attempts to find "create_plugin" method.

#include <boost/dll/import.hpp> // for import_alias
#include <boost/make_shared.hpp>
#include <boost/function.hpp>
#include <iostream>
#include "../tutorial_common/my_plugin_api.hpp"

namespace dll = boost::dll;

std::size_t search_for_symbols(const std::vector<boost::dll::fs::path>& plugins) {
    std::size_t plugins_found = 0;

    for (std::size_t i = 0; i < plugins.size(); ++i) {
        std::cout << "Loading plugin: " << plugins[i] << '\n';
        dll::shared_library lib(plugins[i], dll::load_mode::append_decorations);
        if (!lib.has("create_plugin")) {
            // no such symbol
            continue;
        }

        // library has symbol, importing...
        typedef boost::shared_ptr<my_plugin_api> (pluginapi_create_t)();
        boost::function<pluginapi_create_t> creator
            = dll::import_alias<pluginapi_create_t>(boost::move(lib), "create_plugin");

        std::cout << "Matching plugin name: " << creator()->name() << std::endl;
        ++ plugins_found;
    }

    return plugins_found;
}

If we call that method for all our plugins we'll get the following output:

Loading plugin: "/test/libmy_plugin_aggregator.so"
Matching plugin name: aggregator
Loading plugin: "/test/libmy_plugin_sum.so"
Constructing my_plugin_sum
Destructing my_plugin_sum ;o)

Full sources:

Back to the Top

Linking plugin into the executable has the advantages of

  • reducing common size of distribution
  • simplification of installation of distribution
  • faster plugin load

Let's start from creating a linkable in plugin. Such plugin will have a header, common for plugin library itself and for the executable:

#include <boost/dll/alias.hpp>                          // for BOOST_DLL_ALIAS
#include <boost/shared_ptr.hpp>
#include "../tutorial_common/my_plugin_api.hpp"

namespace my_namespace {
    boost::shared_ptr<my_plugin_api> create_plugin();   // Forward declaration
} // namespace my_namespace

BOOST_DLL_ALIAS(
    my_namespace::create_plugin,                        // <-- this function is exported with...
    create_plugin                                       // <-- ...this alias name
)

Main trick here is the alias definition. When linking plugin into the executable, the alias must be instantiated in one of the source files of the executable. Otherwise the linker will optimize away our plugin.

Here's how the implementation of the plugin looks like:

#include "static_plugin.hpp" // this is essential, BOOST_SYMBOL_ALIAS must be seen in this file

#include <boost/make_shared.hpp>
#include <iostream>

namespace my_namespace {

class my_plugin_static : public my_plugin_api {
public:
    my_plugin_static() {
        std::cout << "Constructing my_plugin_static" << std::endl;
    }

    std::string name() const {
        return "static";
    }

    float calculate(float x, float y) {
        return x - y;
    }

    ~my_plugin_static() {
        std::cout << "Destructing my_plugin_static" << std::endl;
    }
};

boost::shared_ptr<my_plugin_api> create_plugin() {
    return boost::make_shared<my_plugin_static>();
}

} // namespace my_namespace

Now if we make a static library from source file and link that static library with the following code, we'll be able to import symbols from plugin:

#include <boost/dll/shared_library.hpp>         // for shared_library
#include <boost/dll/runtime_symbol_info.hpp>    // for program_location()
#include "static_plugin.hpp"                    // without this headers some compilers may optimize out the `create_plugin` symbol
#include <boost/function.hpp>
#include <iostream>

namespace dll = boost::dll;

int main() {
    dll::shared_library self(dll::program_location());

    std::cout << "Call function" << std::endl;
    boost::function<boost::shared_ptr<my_plugin_api>()> creator
        = self.get_alias<boost::shared_ptr<my_plugin_api>()>("create_plugin");

    std::cout << "Computed Value: " << creator()->calculate(2, 2) << std::endl;
}

[Note] Note

Flag '-rdynamic' must be used when linking the plugin into the executable on Linux OS. Otherwise loading symbols from self will fail.

Running the program will output the following:

Call function
Constructing my_plugin_static
Computed Value: 0
Destructing my_plugin_static
[Note] Note

If we want to make a traditional plugin that is located in a separate shared library, all we need to do is remove the #include "static_plugin.hpp" line and replace dll::program_location() with plugin location and name.

Full sources:

Back to the Top

Let's make an executable, link a plugin into it and attempt to load all the existing plugins:

namespace dll = boost::dll;

class plugins_collector {
    // Name => plugin
    typedef boost::container::map<std::string, dll::shared_library> plugins_t;

    boost::dll::fs::path            plugins_directory_;
    plugins_t                       plugins_;

    // loads all plugins in plugins_directory_
    void load_all();

    // Gets `my_plugin_api` instance using "create_plugin" or "plugin" imports,
    // stores plugin with its name in the `plugins_` map.
    void insert_plugin(BOOST_RV_REF(dll::shared_library) lib);

public:
    plugins_collector(const boost::dll::fs::path& plugins_directory)
        : plugins_directory_(plugins_directory)
    {
        load_all();
    }

    void print_plugins() const;

    std::size_t count() const;
    // ...
};

int main(int argc, char* argv[]) {

    plugins_collector plugins(argv[1]);

    std::cout << "\n\nUnique plugins " << plugins.count() << ":\n";
    plugins.print_plugins();
    // ...

With the default flags you'll get a very strange output:

Loaded (0x180db60):"/libs/dll/test/libmy_plugin_aggregator.so"
Constructing my_plugin_static
Destructing my_plugin_static
...

Unique plugins 2:
(0x180db60): static
(0x180e3b0): sum
Destructing my_plugin_sum ;o)

Why my_plugin_static was constructed while we were loading my_plugin_aggregator?

That's because function create_plugin from libmy_plugin_aggregator.so was shadowed by the create_plugin function from other plugin. Dynamic linker thought that create_plugin was already loaded and there is no need to load it again.

[Warning] Warning

Use "-fvisibility=hidden" flag (at least for plugins) while compiling for POSIX platforms. This flag makes your code more portable ("-fvisibility=hidden" is the default behavior under Windows), reduces size of the binaries and improves binary load time.

Now if we recompile your example with "-fvisibility=hidden" we'll get the following output:

Loaded (0x2406b60):"/libs/dll/test/libmy_plugin_aggregator.so"
Loaded (0x2407410):"/libs/dll/test/libgetting_started_library.so"
Constructing my_plugin_sum
...

Unique plugins 3:
(0x2406b60): aggregator
(0x7fd1cadce2c8): static
(0x24073b0): sum
Destructing my_plugin_sum ;o)

Full sources:

Back to the Top

Boost.DLL provides no out of the box mechanism for catching library unloads. However such task could be easily implemented.

All you need to do, is write a simple class that stores callbacks and calls them at destruction:

#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS
#include <boost/function.hpp>
#include <vector>

namespace my_namespace {

struct on_unload {
    typedef boost::function<void()> callback_t;
    typedef on_unload this_type;

    ~on_unload() {
        for (std::size_t i = 0; i < callbacks_.size(); ++i) {
            callback_t& function = callbacks_[i];
            function(); // calling the callback
        }
    }

    // not thread safe
    static void add(const callback_t& function) {
        static this_type instance;
        instance.callbacks_.push_back(function);
    }

private:
    std::vector<callback_t> callbacks_;
    on_unload() {} // prohibit construction outside of the `add` function
};

// Exporting the static "add" function with name "on_unload"
BOOST_DLL_ALIAS(my_namespace::on_unload::add, on_unload)

} // namespace my_namespace

In the example above my_namespace::on_unload is a singleton structure that holds a vector of callbacks and calls all the callbacks at destruction.

Now we can load this library and provide a callback:

#include <boost/dll/import.hpp>
#include <boost/function.hpp>
#include <iostream>

typedef boost::function<void()> callback_t;

void print_unloaded() {
    std::cout << "unloaded" << std::endl;
}

int main(int argc, char* argv[]) {
    // argv[1] contains full path to our plugin library
    boost::dll::fs::path shared_library_path =  argv[1];

    // loading library and getting a function from it
    boost::function<void(const callback_t&)> on_unload
        = boost::dll::import_alias<void(const callback_t&)>(
            shared_library_path, "on_unload"
        );

    on_unload(&print_unloaded); // adding a callback
    std::cout << "Before library unload." << std::endl;

    // Releasing last reference to the library, so that it gets unloaded
    on_unload.clear();
    std::cout << "After library unload." << std::endl;
}

If we run the example we'll get the following output:

Before library unload.
unloaded
After library unload.

Full sources:

Back to the Top

Situation when we do not know names of functions in plugin could occur. In that case querying library could be useful.

Imagine the situation: we have a project called 'Anna' that is capable of loading and using plugins that contain functions with signature void(const std::string&). We do not know function names, but wish to find out them somehow.

Solution would be pretty simple. Let's agree with plugin developers, that they can name functions as they like, but all the plugin functions aliases must be located in section named 'Anna':

#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS_SECTIONED
#include <iostream>
#include <string>

void print(const std::string& s) {
    std::cout << "Hello, " << s << '!' << std::endl;
}

BOOST_DLL_ALIAS_SECTIONED(print, print_hello, Anna)

#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS_SECTIONED
#include <string>
#include <iostream>

void print_howdy(const std::string& s) {
    std::cout << "How're you doing, " << s << '?' << std::endl;
}

void print_bored(const std::string& s) {
    std::cout << "Are you bored, " << s << '?' << std::endl;
}

BOOST_DLL_ALIAS_SECTIONED(print_howdy, howdy, Anna)
BOOST_DLL_ALIAS_SECTIONED(print_bored, are_you_bored, Anna)

Now we can easily get those functions using the boost::dll::library_info:

#include <boost/dll/shared_library.hpp>
#include <boost/dll/library_info.hpp>
#include <iostream>

void load_and_execute(const boost::dll::fs::path libraries[], std::size_t libs_count) {
    const std::string username = "User";

    for (std::size_t i = 0; i < libs_count; ++i) {
        // Class `library_info` can extract information from a library
        boost::dll::library_info inf(libraries[i]);

        // Getting symbols exported from 'Anna' section
        std::vector<std::string> exports = inf.symbols("Anna");

        // Loading library and importing symbols from it
        boost::dll::shared_library lib(libraries[i]);
        for (std::size_t j = 0; j < exports.size(); ++j) {
            std::cout << "\nFunction '" << exports[j] << "' prints:\n\t";
            lib.get_alias<void(const std::string&)>(exports[j]) // importing function
                (username);                                     // calling function
        }
    }
}

If we run the example we'll get the following output:

Function 'print_hello' prints:
	Hello, User!

Function 'are_you_bored' prints:
	Are you bored, User?

Function 'howdy' prints:
	How're you doing, User?
[Note] Note

BOOST_DLL_ALIAS macro by default places all the aliases into the "boostdll" section.

Full sources:

Back to the Top

As noted in documentation to the boost::dll::import variables and functions returned from those functions hold a reference to the shared library. However nested objects and objects that are returned by import* functions do not hold a reference to the shared library. There's no way to solve this issue on the Boost.DLL library level, but you can take care of this problem by your own. Here's an example how this could be done.

In this example we'll be importing function that constructs an instance of plugin and binding that instance to shared_library.

First of all we need to define a new plugin api:

#include "../tutorial_common/my_plugin_api.hpp"
#include <boost/dll/config.hpp>

class my_refcounting_api: public my_plugin_api {
public:
    // Returns path to shared object that holds a plugin.
    // Must be instantiated in plugin.
    virtual boost::dll::fs::path location() const = 0;
};

This API does not differ much from the previous one. Only one abstract method was added.

Now let's define the plugin:

#include "refcounting_plugin.hpp"
#include <boost/dll/runtime_symbol_info.hpp> // for this_line_location()

namespace my_namespace {

class my_plugin_refcounting : public my_refcounting_api {
public:
    // Must be instantiated in plugin
    boost::dll::fs::path location() const {
        return boost::dll::this_line_location(); // location of this plugin
    }

    std::string name() const {
        return "refcounting";
    }

    // ...
};

} // namespace my_namespace

// Factory method. Returns *simple pointer*!
my_refcounting_api* create() {
    return new my_namespace::my_plugin_refcounting();
}

This plugin does not differ much from our previous examples except the additional method that calls boost::dll::this_line_location and create() function that returns a simple pointer instead of boost::shared_ptr.

Now lets make a function that binds a newly created instance of my_refcounting_api to a shared library:

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/dll/shared_library.hpp>

struct library_holding_deleter {
    boost::shared_ptr<boost::dll::shared_library> lib_;

    void operator()(my_refcounting_api* p) const {
        delete p;
    }
};

inline boost::shared_ptr<my_refcounting_api> bind(my_refcounting_api* plugin) {
    // getting location of the shared library that holds the plugin
    boost::dll::fs::path location = plugin->location();

    // `make_shared` is an efficient way to create a shared pointer
    boost::shared_ptr<boost::dll::shared_library> lib
        = boost::make_shared<boost::dll::shared_library>(location);

    library_holding_deleter deleter;
    deleter.lib_ = lib;

    return boost::shared_ptr<my_refcounting_api>(
        plugin, deleter
    );
}

In bind method we call plugin->location(). This call results in a call to boost::dll::this_line_location and returns the plugin location. Then a shared_ptr that holds a shared_library is created using the make_shared call.

After that we construct a boost::shared_ptr<my_refcounting_api> with a library_holding_deleter that keeps an instance of the shared library.

[Note] Note

Use std::unique_ptr<my_refcounting_api> instead of my_refcounting_api* in production code to avoid memory leaks when plugin->location() throws or when some other class rises an exception.

That's it, now we can get instance of a plugin:

#include <boost/dll/import.hpp>
#include <boost/function.hpp>
inline boost::shared_ptr<my_refcounting_api> get_plugin(
    boost::dll::fs::path path, const char* func_name)
{
    typedef my_refcounting_api*(func_t)();
    boost::function<func_t> creator = boost::dll::import_alias<func_t>(
        path,
        func_name,
        boost::dll::load_mode::append_decorations   // will be ignored for executable
    );

    // `plugin` does not hold a reference to shared library. If `creator` will go out of scope, 
    // then `plugin` can not be used.
    my_refcounting_api* plugin = creator();

    // Returned variable holds a reference to 
    // shared_library and it is safe to use it.
    return bind( plugin );

    // `creator` goes out of scope here and will be destroyed.
}

Here's how it main() function look like:

Runtime plugin load

Plugin was linked in

Code

#include <iostream>
#include "refcounting_api.hpp"

int main(int argc, char* argv[]) {

    boost::shared_ptr<my_refcounting_api> plugin = get_plugin(
        boost::dll::fs::path(argv[1]) / "refcounting_plugin",
        "create_refc_plugin"
    );

    std::cout << "Plugin name: " << plugin->name()
              << ", \nlocation: " << plugin->location()
              << std::endl;
}

#include <boost/dll/runtime_symbol_info.hpp> // program_location()
#include <iostream>
#include "refcounting_plugin.hpp"

int main() {
    boost::shared_ptr<my_refcounting_api> plugin = get_plugin(
        boost::dll::program_location(),
        "create_refc_plugin"
    );

    std::cout << "Plugin name: " << plugin->name()
              << ", \nlocation: " << plugin->location()
              << std::endl;
}

Output

Plugin name: refcounting,
location: "/libs/dll/librefcounting_plugin.so"
Plugin name: refcounting,
location: "/tutorial8_static"

Full sources:

Back to the Top

This a trivial example, but it has one tricky place. When you are importing a C function by it's name you must use boost::dll::import, not boost::dll::import_alias:

#include <boost/dll/import.hpp>         // for dll::import
#include <boost/dll/shared_library.hpp> // for dll::shared_library
#include <boost/function.hpp>
#include <iostream>
#include <windows.h>

namespace dll = boost::dll;

int main() {
    typedef HANDLE(__stdcall GetStdHandle_t)(DWORD );       // function signature with calling convention

    // OPTION #0, requires C++11 compatible compiler that understands GetStdHandle_t signature.

    auto get_std_handle = dll::import_symbol<GetStdHandle_t>(
        "Kernel32.dll",
        "GetStdHandle",
        boost::dll::load_mode::search_system_folders
    );
    std::cout << "0.0 GetStdHandle() returned " << get_std_handle(STD_OUTPUT_HANDLE) << std::endl;

    // You may put the `get_std_handle` into boost::function<>. But boost::function<Signature> can not compile with
    // Signature template parameter that contains calling conventions, so you'll have to remove the calling convention.
    boost::function<HANDLE(DWORD)> get_std_handle2 = get_std_handle;
    std::cout << "0.1 GetStdHandle() returned " << get_std_handle2(STD_OUTPUT_HANDLE) << std::endl;


    // OPTION #1, does not require C++11. But without C++11 dll::import<> can not handle calling conventions,
    // so you'll need to hand write the import.
    dll::shared_library lib("Kernel32.dll", dll::load_mode::search_system_folders);
    GetStdHandle_t& func = lib.get<GetStdHandle_t>("GetStdHandle");

    // Here `func` does not keep a reference to `lib`, you'll have to deal with that on your own.
    std::cout << "1.0 GetStdHandle() returned " << func(STD_OUTPUT_HANDLE) << std::endl;

    return 0;
}

Full sources:

Back to the Top


PrevUpHomeNext