tutorial.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. [/
  2. Copyright 2014 Renato Tegon Forti, Antony Polukhin
  3. Copyright 2015-2019 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)
  6. /]
  7. [template example_ref[path] '''<ulink url="https://github.com/apolukhin/Boost.DLL/blob/develop/example/'''[path]'''">example/'''[path]'''</ulink>''']
  8. [def __to_top [link boost_dll.tutorial Back to the Top]]
  9. [section Tutorial]
  10. This Tutorial is provided to give you an idea of how to create and use plugins.
  11. [section Plugin basics]
  12. The first thing to do when creating your own plugins is define the plugin interface. There is an example
  13. of an abstract class that will be our plugin API:
  14. [import ../example/tutorial_common/my_plugin_api.hpp]
  15. [plugapi]
  16. Now let's make a DLL/DSO library that will holds implementation of plugin interface and exports it using the
  17. `extern "C"` and [macroref BOOST_SYMBOL_EXPORT]:
  18. [import ../example/tutorial1/my_plugin_sum.cpp]
  19. [plugcpp_my_plugin_sum]
  20. Simple application that loads plugin using the [funcref boost::dll::import]
  21. and [enumref boost::dll::load_mode::type append_decorations]:
  22. [import ../example/tutorial1/tutorial1.cpp]
  23. [callplugcpp_tutorial1]
  24. That application will output:
  25. [pre
  26. Loading the plugin
  27. Constructing my_plugin_sum
  28. plugin->calculate(1.5, 1.5) call: 3
  29. Destructing my_plugin_sum ;o)
  30. ]
  31. [*Full sources:]
  32. * [example_ref tutorial_common/my_plugin_api.hpp]
  33. * [example_ref tutorial1/my_plugin_sum.cpp]
  34. * [example_ref tutorial1/tutorial1.cpp]
  35. __to_top
  36. [endsect]
  37. [section Factory method in plugin]
  38. In previous example we were importing from a plugin a single variable. Let's make a class
  39. that uses our plugin API plugin and holds some state:
  40. [import ../example/tutorial2/my_plugin_aggregator.cpp]
  41. [plugcpp_my_plugin_aggregator]
  42. As you may see, `my_namespace::create_plugin` is a factory method, that creates
  43. instances of `my_namespace::my_plugin_aggregator`. We export that method with the name "create_plugin"
  44. using [macroref BOOST_DLL_ALIAS].
  45. [import ../example/tutorial2/tutorial2.cpp]
  46. [callplugcpp_tutorial2]
  47. In that application we have imported the factory method using [funcref boost::dll::import_alias].
  48. [caution Be careful: `creator` variable holds a reference to the loaded shared library. If this
  49. variable goes out of scope or will be reset, then the *DLL/DSO will be unloaded* and any attempt to
  50. dereference the `plugin` variable will lead to *undefined behavior*. ]
  51. Output of the application will be the following:
  52. [pre
  53. plugin->calculate(1.5, 1.5) call: 3
  54. plugin->calculate(1.5, 1.5) second call: 6
  55. Plugin Name: aggregator
  56. ]
  57. [*Full sources:]
  58. * [example_ref tutorial2/my_plugin_aggregator.cpp]
  59. * [example_ref tutorial2/tutorial2.cpp]
  60. __to_top
  61. [endsect]
  62. [section Searching for a symbol in multiple plugins]
  63. Consider the situation: we have multiple plugins, but only some of them have symbols that we need.
  64. Let's write a function that search list of plugins and attempts to find `"create_plugin"` method.
  65. [import ../example/tutorial3/tutorial3.cpp]
  66. [callplugcpp_tutorial3]
  67. If we call that method for all our plugins we'll get the following output:
  68. [pre
  69. Loading plugin: "/test/libmy_plugin_aggregator.so"
  70. Matching plugin name: aggregator
  71. Loading plugin: "/test/libmy_plugin_sum.so"
  72. Constructing my_plugin_sum
  73. Destructing my_plugin_sum ;o)
  74. ]
  75. [*Full sources:]
  76. * [example_ref tutorial3/tutorial3.cpp]
  77. * [example_ref tutorial2/my_plugin_aggregator.cpp]
  78. * [example_ref tutorial1/my_plugin_sum.cpp]
  79. __to_top
  80. [endsect]
  81. [section Linking plugin into the executable]
  82. Linking plugin into the executable has the advantages of
  83. * reducing common size of distribution
  84. * simplification of installation of distribution
  85. * faster plugin load
  86. Let's start from creating a linkable in plugin. Such plugin will have a header,
  87. common for plugin library itself and for the executable:
  88. [import ../example/tutorial4/static_plugin.hpp]
  89. [plugcpp_my_plugin_static]
  90. Main trick here is the alias definition. When linking plugin into the executable, the alias *must*
  91. be instantiated in one of the source files of the executable. Otherwise the linker will optimize
  92. away our plugin.
  93. Here's how the implementation of the plugin looks like:
  94. [import ../example/tutorial4/static_plugin.cpp]
  95. [plugcpp_my_plugin_staic_impl]
  96. Now if we make a static library from source file and link that static library with the following code,
  97. we'll be able to import symbols from plugin:
  98. [import ../example/tutorial4/load_self.cpp]
  99. [plugcpp_my_plugin_load_self]
  100. [note Flag '-rdynamic' must be used when linking the plugin into the executable on Linux OS.
  101. Otherwise loading symbols from self *will fail*.]
  102. Running the program will output the following:
  103. [pre
  104. Call function
  105. Constructing my_plugin_static
  106. Computed Value: 0
  107. Destructing my_plugin_static
  108. ]
  109. [note If we want to make a traditional plugin that is located in a separate shared library, all we need to do is
  110. remove the `#include "static_plugin.hpp"` line and replace `dll::program_location()` with plugin location and name.]
  111. [*Full sources:]
  112. * [example_ref tutorial4/static_plugin.hpp]
  113. * [example_ref tutorial4/static_plugin.cpp]
  114. * [example_ref tutorial4/load_self.cpp]
  115. __to_top
  116. [endsect]
  117. [section Symbol shadowing problem (Linux)]
  118. Let's make an executable, link a plugin into it and attempt to load all the existing plugins:
  119. [import ../example/tutorial5/load_all.cpp]
  120. [plugcpp_plugins_collector_def]
  121. [plugcpp_load_all]
  122. With the default flags you'll get a very strange output:
  123. [pre
  124. Loaded (0x180db60):"/libs/dll/test/libmy_plugin_aggregator.so"
  125. Constructing my_plugin_static
  126. Destructing my_plugin_static
  127. ...
  128. Unique plugins 2:
  129. (0x180db60): static
  130. (0x180e3b0): sum
  131. Destructing my_plugin_sum ;o)
  132. ]
  133. Why `my_plugin_static` was constructed while we were loading `my_plugin_aggregator`?
  134. That's because function `create_plugin` from `libmy_plugin_aggregator.so` was shadowed by
  135. the `create_plugin` function from other plugin. Dynamic linker thought that `create_plugin`
  136. was already loaded and there is no need to load it again.
  137. [warning Use "-fvisibility=hidden" flag (at least for plugins) while compiling for POSIX platforms.
  138. This flag makes your code more portable ("-fvisibility=hidden" is the default behavior under Windows),
  139. reduces size of the binaries and improves binary load time.
  140. ]
  141. Now if we recompile your example with "-fvisibility=hidden" we'll get the following output:
  142. [pre
  143. Loaded (0x2406b60):"/libs/dll/test/libmy_plugin_aggregator.so"
  144. Loaded (0x2407410):"/libs/dll/test/libgetting_started_library.so"
  145. Constructing my_plugin_sum
  146. ...
  147. Unique plugins 3:
  148. (0x2406b60): aggregator
  149. (0x7fd1cadce2c8): static
  150. (0x24073b0): sum
  151. Destructing my_plugin_sum ;o)
  152. ]
  153. [*Full sources:]
  154. * [example_ref tutorial5/load_all.cpp]
  155. * [example_ref tutorial4/static_plugin.cpp]
  156. * [example_ref tutorial2/my_plugin_aggregator.cpp]
  157. * [example_ref tutorial1/my_plugin_sum.cpp]
  158. __to_top
  159. [endsect]
  160. [section Executing callbacks on library unload]
  161. Boost.DLL provides no out of the box mechanism for catching library unloads. However such task could be easily implemented.
  162. [import ../example/tutorial6/on_unload_lib.cpp]
  163. All you need to do, is write a simple class that stores callbacks and calls them at destruction:
  164. [plugcpp_on_unload]
  165. In the example above `my_namespace::on_unload` is a singleton structure that holds a vector of callbacks and
  166. calls all the callbacks at destruction.
  167. [import ../example/tutorial6/tutorial6.cpp]
  168. Now we can load this library and provide a callback:
  169. [callplugcpp_tutorial6]
  170. If we run the example we'll get the following output:
  171. [pre
  172. Before library unload.
  173. unloaded
  174. After library unload.
  175. ]
  176. [*Full sources:]
  177. * [example_ref tutorial6/on_unload_lib.cpp]
  178. * [example_ref tutorial6/tutorial6.cpp]
  179. __to_top
  180. [endsect]
  181. [section Querying libraries for symbols]
  182. Situation when we do not know names of functions in plugin could occur. In that case querying library could
  183. be useful.
  184. Imagine the situation: we have a project called 'Anna' that is capable of loading and using plugins that contain
  185. functions with signature `void(const std::string&)`. We do not know function names, but wish to find out them somehow.
  186. Solution would be pretty simple. Let's agree with plugin developers, that they can name functions as they like,
  187. but all the plugin functions aliases must be located in section named 'Anna':
  188. [import ../example/tutorial7/library1.cpp]
  189. [plugcpp_tutorial7_library1]
  190. [import ../example/tutorial7/library2.cpp]
  191. [plugcpp_tutorial7_library2]
  192. Now we can easily get those functions using the [classref boost::dll::library_info]:
  193. [import ../example/tutorial7/tutorial7.cpp]
  194. [callplugcpp_tutorial7]
  195. If we run the example we'll get the following output:
  196. [pre
  197. Function 'print_hello' prints:
  198. Hello, User!
  199. Function 'are_you_bored' prints:
  200. Are you bored, User?
  201. Function 'howdy' prints:
  202. How're you doing, User?
  203. ]
  204. [note `BOOST_DLL_ALIAS` macro by default places all the aliases into the "boostdll" section. ]
  205. [*Full sources:]
  206. * [example_ref tutorial7/library1.cpp]
  207. * [example_ref tutorial7/library2.cpp]
  208. * [example_ref tutorial7/tutorial7.cpp]
  209. __to_top
  210. [endsect]
  211. [section Advanced library reference counting]
  212. As noted in documentation to the [funcref boost::dll::import]
  213. variables and functions returned from those functions hold a reference to the shared library. However nested objects and
  214. objects that are returned by `import*` functions do not hold a reference to the shared library. There's no way to solve
  215. 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
  216. could be done.
  217. In this example we'll be importing function that constructs an instance of plugin and binding that
  218. instance to shared_library.
  219. First of all we need to define a new plugin api:
  220. [import ../example/tutorial8/refcounting_api.hpp]
  221. [plugcpp_my_plugin_refcounting_api]
  222. This API does not differ much from the previous one. Only one abstract method was added.
  223. Now let's define the plugin:
  224. [/
  225. [import ../example/tutorial8/refcounting_plugin.hpp]
  226. [plugcpp_my_plugin_refcounting_hpp]
  227. /]
  228. [import ../example/tutorial8/refcounting_plugin.cpp]
  229. [plugcpp_my_plugin_refcounting]
  230. This plugin does not differ much from our previous examples except the additional method that calls
  231. [funcref boost::dll::this_line_location] and `create()` function that returns a simple pointer instead of
  232. `boost::shared_ptr`.
  233. Now lets make a function that binds a newly created instance of `my_refcounting_api` to a shared library:
  234. [plugcpp_library_holding_deleter_api_bind]
  235. In `bind` method we call `plugin->location()`. This call results in a call to
  236. [funcref boost::dll::this_line_location] and returns the plugin location. Then a `shared_ptr` that holds a `shared_library`
  237. is created using the `make_shared` call.
  238. After that we construct a `boost::shared_ptr<my_refcounting_api>` with a `library_holding_deleter` that keeps an instance of the shared library.
  239. [note Use `std::unique_ptr<my_refcounting_api>` instead of `my_refcounting_api*` in production code to avoid memory leaks when `plugin->location()`
  240. throws or when some other class rises an exception.]
  241. That's it, now we can get instance of a plugin:
  242. [plugcpp_get_plugin_refcounting]
  243. Here's how it `main()` function look like:
  244. [import ../example/tutorial8/tutorial8.cpp]
  245. [import ../example/tutorial8/tutorial8_static.cpp]
  246. [table
  247. [[][Runtime plugin load][Plugin was linked in]]
  248. [[Code][[callplugcpp_tutorial8]][[callplugcpp_tutorial8_static]]]
  249. [[Output]
  250. [[pre Plugin name: refcounting,
  251. location: "/libs/dll/librefcounting_plugin.so"]]
  252. [[pre Plugin name: refcounting,
  253. location: "/tutorial8_static"]]
  254. ]
  255. ]
  256. [*Full sources:]
  257. * [example_ref tutorial8/refcounting_api.hpp]
  258. * [example_ref tutorial8/refcounting_plugin.hpp]
  259. * [example_ref tutorial8/refcounting_plugin.cpp]
  260. * [example_ref tutorial8/tutorial8.cpp]
  261. * [example_ref tutorial8/tutorial8_static.cpp]
  262. __to_top
  263. [endsect]
  264. [section Importing a C function from Windows dll]
  265. This a trivial example, but it has one tricky place. When you are importing a C function by *it's name*
  266. you must use [funcref boost::dll::import], not [funcref boost::dll::import_alias]:
  267. [import ../example/tutorial9/tutorial9.cpp]
  268. [callplugcpp_tutorial9]
  269. [*Full sources:]
  270. * [example_ref tutorial9/tutorial9.cpp]
  271. __to_top
  272. [endsect]
  273. [endsect]