2 Copyright 2014 Renato Tegon Forti, Antony Polukhin
3 Copyright 2015-2016 Antony Polukhin
4 Distributed under the Boost Software License, Version 1.0.
5 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 [template example_ref[path] '''<ulink url="https://github.com/apolukhin/Boost.DLL/blob/develop/example/'''[path]'''">example/'''[path]'''</ulink>''']
10 [def __to_top [link boost_dll.tutorial Back to the Top]]
14 This Tutorial is provided to give you an idea of how to create and use plugins.
16 [section Plugin basics]
18 The first thing to do when creating your own plugins is define the plugin interface. There is an example
19 of an abstract class that will be our plugin API:
21 [import ../example/tutorial_common/my_plugin_api.hpp]
24 Now let's make a DLL/DSO library that will holds implementation of plugin interface and exports it using the
25 `extern "C"` and [macroref BOOST_SYMBOL_EXPORT]:
27 [import ../example/tutorial1/my_plugin_sum.cpp]
28 [plugcpp_my_plugin_sum]
30 Simple application that loads plugin using the [funcref boost::dll::import]
31 and [enumref boost::dll::load_mode::type append_decorations]:
33 [import ../example/tutorial1/tutorial1.cpp]
34 [callplugcpp_tutorial1]
36 That application will output:
40 Constructing my_plugin_sum
41 plugin->calculate(1.5, 1.5) call: 3
42 Destructing my_plugin_sum ;o)
47 * [example_ref tutorial_common/my_plugin_api.hpp]
48 * [example_ref tutorial1/my_plugin_sum.cpp]
49 * [example_ref tutorial1/tutorial1.cpp]
57 [section Factory method in plugin]
59 In previous example we were importing from a plugin a single variable. Let's make a class
60 that uses our plugin API plugin and holds some state:
62 [import ../example/tutorial2/my_plugin_aggregator.cpp]
63 [plugcpp_my_plugin_aggregator]
65 As you may see, `my_namespace::create_plugin` is a factory method, that creates
66 instances of `my_namespace::my_plugin_aggregator`. We export that method with the name "create_plugin"
67 using [macroref BOOST_DLL_ALIAS].
69 [import ../example/tutorial2/tutorial2.cpp]
70 [callplugcpp_tutorial2]
72 In that application we have imported the factory method using [funcref boost::dll::import_alias].
74 [caution Be careful: `creator` variable holds a reference to the loaded shared library. If this
75 variable goes out of scope or will be reset, then the *DLL/DSO will be unloaded* and any attempt to
76 dereference the `plugin` variable will lead to *undefined behavior*. ]
78 Output of the application will be the following:
81 plugin->calculate(1.5, 1.5) call: 3
82 plugin->calculate(1.5, 1.5) second call: 6
83 Plugin Name: aggregator
88 * [example_ref tutorial2/my_plugin_aggregator.cpp]
89 * [example_ref tutorial2/tutorial2.cpp]
95 [section Searching for a symbol in multiple plugins]
97 Consider the situation: we have multiple plugins, but only some of them have symbols that we need.
98 Let's write a function that search list of plugins and attempts to find `"create_plugin"` method.
100 [import ../example/tutorial3/tutorial3.cpp]
101 [callplugcpp_tutorial3]
103 If we call that method for all our plugins we'll get the following output:
106 Loading plugin: "/test/libmy_plugin_aggregator.so"
107 Matching plugin name: aggregator
108 Loading plugin: "/test/libmy_plugin_sum.so"
109 Constructing my_plugin_sum
110 Destructing my_plugin_sum ;o)
115 * [example_ref tutorial3/tutorial3.cpp]
116 * [example_ref tutorial2/my_plugin_aggregator.cpp]
117 * [example_ref tutorial1/my_plugin_sum.cpp]
124 [section Linking plugin into the executable]
126 Linking plugin into the executable has the advantages of
128 * reducing common size of distribution
129 * simplification of installation of distribution
132 Let's start from creating a linkable in plugin. Such plugin will have a header,
133 common for plugin library itself and for the executable:
135 [import ../example/tutorial4/static_plugin.hpp]
136 [plugcpp_my_plugin_static]
138 Main trick here is the alias definition. When linking plugin into the executable, the alias *must*
139 be instantiated in one of the source files of the executable. Otherwise the linker will optimize
142 Here's how the implementation of the plugin looks like:
144 [import ../example/tutorial4/static_plugin.cpp]
145 [plugcpp_my_plugin_staic_impl]
147 Now if we make a static library from source file and link that static library with the following code,
148 we'll be able to import symbols from plugin:
150 [import ../example/tutorial4/load_self.cpp]
151 [plugcpp_my_plugin_load_self]
153 [note Flag '-rdynamic' must be used when linking the plugin into the executable on Linux OS.
154 Otherwise loading symbols from self *will fail*.]
156 Running the program will output the following:
160 Constructing my_plugin_static
162 Destructing my_plugin_static
165 [note If we want to make a traditional plugin that is located in a separate shared library, all we need to do is
166 remove the `#include "static_plugin.hpp"` line and replace `dll::program_location()` with plugin location and name.]
170 * [example_ref tutorial4/static_plugin.hpp]
171 * [example_ref tutorial4/static_plugin.cpp]
172 * [example_ref tutorial4/load_self.cpp]
178 [section Symbol shadowing problem (Linux)]
179 Let's make an executable, link a plugin into it and attempt to load all the existing plugins:
181 [import ../example/tutorial5/load_all.cpp]
182 [plugcpp_plugins_collector_def]
185 With the default flags you'll get a very strange output:
187 Loaded (0x180db60):"/libs/dll/test/libmy_plugin_aggregator.so"
188 Constructing my_plugin_static
189 Destructing my_plugin_static
195 Destructing my_plugin_sum ;o)
198 Why `my_plugin_static` was constructed while we were loading `my_plugin_aggregator`?
200 That's because function `create_plugin` from `libmy_plugin_aggregator.so` was shadowed by
201 the `create_plugin` function from other plugin. Dynamic linker thought that `create_plugin`
202 was already loaded and there is no need to load it again.
204 [warning Use "-fvisibility=hidden" flag (at least for plugins) while compiling for POSIX platforms.
205 This flag makes your code more portable ("-fvisibility=hidden" is the default behavior under Windows),
206 reduces size of the binaries and improves binary load time.
209 Now if we recompile your example with "-fvisibility=hidden" we'll get the following output:
211 Loaded (0x2406b60):"/libs/dll/test/libmy_plugin_aggregator.so"
212 Loaded (0x2407410):"/libs/dll/test/libgetting_started_library.so"
213 Constructing my_plugin_sum
217 (0x2406b60): aggregator
218 (0x7fd1cadce2c8): static
220 Destructing my_plugin_sum ;o)
225 * [example_ref tutorial5/load_all.cpp]
226 * [example_ref tutorial4/static_plugin.cpp]
227 * [example_ref tutorial2/my_plugin_aggregator.cpp]
228 * [example_ref tutorial1/my_plugin_sum.cpp]
234 [section Executing callbacks on library unload]
235 Boost.DLL provides no out of the box mechanism for catching library unloads. However such task could be easily implemented.
237 [import ../example/tutorial6/on_unload_lib.cpp]
238 All you need to do, is write a simple class that stores callbacks and calls them at destruction:
241 In the example above `my_namespace::on_unload` is a singleton structure that holds a vector of callbacks and
242 calls all the callbacks at destruction.
244 [import ../example/tutorial6/tutorial6.cpp]
245 Now we can load this library and provide a callback:
246 [callplugcpp_tutorial6]
248 If we run the example we'll get the following output:
251 Before library unload.
253 After library unload.
258 * [example_ref tutorial6/on_unload_lib.cpp]
259 * [example_ref tutorial6/tutorial6.cpp]
265 [section Querying libraries for symbols]
267 Situation when we do not know names of functions in plugin could occur. In that case querying library could
270 Imagine the situation: we have a project called 'Anna' that is capable of loading and using plugins that contain
271 functions with signature `void(const std::string&)`. We do not know function names, but wish to find out them somehow.
273 Solution would be pretty simple. Let's agree with plugin developers, that they can name functions as they like,
274 but all the plugin functions aliases must be located in section named 'Anna':
276 [import ../example/tutorial7/library1.cpp]
277 [plugcpp_tutorial7_library1]
279 [import ../example/tutorial7/library2.cpp]
280 [plugcpp_tutorial7_library2]
282 Now we can easily get those functions using the [classref boost::dll::library_info]:
284 [import ../example/tutorial7/tutorial7.cpp]
285 [callplugcpp_tutorial7]
287 If we run the example we'll get the following output:
290 Function 'print_hello' prints:
293 Function 'are_you_bored' prints:
296 Function 'howdy' prints:
297 How're you doing, User?
300 [note `BOOST_DLL_ALIAS` macro by default places all the aliases into the "boostdll" section. ]
304 * [example_ref tutorial7/library1.cpp]
305 * [example_ref tutorial7/library2.cpp]
306 * [example_ref tutorial7/tutorial7.cpp]
313 [section Advanced library reference counting]
315 As noted in documentation to the [funcref boost::dll::import]
316 variables and functions returned from those functions hold a reference to the shared library. However nested objects and
317 objects that are returned by `import*` functions do not hold a reference to the shared library. There's no way to solve
318 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
321 In this example we'll be importing function that constructs an instance of plugin and binding that
322 instance to shared_library.
324 First of all we need to define a new plugin api:
326 [import ../example/tutorial8/refcounting_api.hpp]
327 [plugcpp_my_plugin_refcounting_api]
329 This API does not differ much from the previous one. Only one abstract method was added.
331 Now let's define the plugin:
333 [import ../example/tutorial8/refcounting_plugin.hpp]
334 [plugcpp_my_plugin_refcounting_hpp]
337 [import ../example/tutorial8/refcounting_plugin.cpp]
338 [plugcpp_my_plugin_refcounting]
340 This plugin does not differ much from our previous examples except the additional method that calls
341 [funcref boost::dll::this_line_location] and `create()` function that returns a simple pointer instead of
344 Now lets make a function that binds a newly created instance of `my_refcounting_api` to a shared library:
345 [plugcpp_library_holding_deleter_api_bind]
347 In `bind` method we call `plugin->location()`. This call results in a call to
348 [funcref boost::dll::this_line_location] and returns the plugin location. Then a `shared_ptr` that holds a `shared_library`
349 is created using the `make_shared` call.
351 After that we construct a `boost::shared_ptr<my_refcounting_api>` with a `library_holding_deleter` that keeps an instance of the shared library.
353 [note Use `std::unique_ptr<my_refcounting_api>` instead of `my_refcounting_api*` in production code to avoid memory leaks when `plugin->location()`
354 throws or when some other class rises an exception.]
356 That's it, now we can get instance of a plugin:
357 [plugcpp_get_plugin_refcounting]
359 Here's how it `main()` function look like:
361 [import ../example/tutorial8/tutorial8.cpp]
362 [import ../example/tutorial8/tutorial8_static.cpp]
365 [[][Runtime plugin load][Plugin was linked in]]
366 [[Code][[callplugcpp_tutorial8]][[callplugcpp_tutorial8_static]]]
368 [[pre Plugin name: refcounting,
369 location: "/libs/dll/librefcounting_plugin.so"]]
370 [[pre Plugin name: refcounting,
371 location: "/tutorial8_static"]]
379 * [example_ref tutorial8/refcounting_api.hpp]
380 * [example_ref tutorial8/refcounting_plugin.hpp]
381 * [example_ref tutorial8/refcounting_plugin.cpp]
382 * [example_ref tutorial8/tutorial8.cpp]
383 * [example_ref tutorial8/tutorial8_static.cpp]
389 [section Importing a C function from Windows dll]
391 This a trivial example, but it has one tricky place. When you are importing a C function by *it's name*
392 you must use [funcref boost::dll::import], not [funcref boost::dll::import_alias]:
394 [import ../example/tutorial9/tutorial9.cpp]
395 [callplugcpp_tutorial9]
399 * [example_ref tutorial9/tutorial9.cpp]