getting_started.qbk 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. [section:getting_started Getting started]
  8. Boost.DLL is a header only library. To start with the library you only need to include `<boost/dll.hpp>` header. After that you are free to import and export functions and variables. Importing code requires linking with `boost_filesystem` and `boost_system` libraries.
  9. If you want to load a library, just construct [classref boost::dll::shared_library] class with a path to the library as a parameter:
  10. ```
  11. boost::dll::shared_library lib("/test/boost/application/libtest_library.so");
  12. ```
  13. Now you can easily import symbols from that library using the `get` and `get_alias` member functions:
  14. ```
  15. int plugin_constant = lib.get<const int>("integer_variable");
  16. boost::function<int()> f = lib.get<int()>("function_returning_int");
  17. int& i = lib.get_alias<int>("alias_to_int_variable");
  18. ```
  19. In case of `boost::dll::shared_library` it is safe to use imported symbols only until `boost::dll::shared_library`
  20. instance is not destroyed.
  21. Query libraries using [classref boost::dll::library_info] and get symbol infos using [funcref boost::dll::symbol_location],
  22. [funcref boost::dll::this_line_location] and [funcref boost::dll::program_location].
  23. For importing a single function or variable you may use a following one liners:
  24. [import ../example/getting_started.cpp]
  25. [import ../example/getting_started_library.cpp]
  26. ```
  27. using namespace boost;
  28. // `extern "C"` - specifies C linkage: forces the compiler to export function/variable by a pretty (unmangled) C name.
  29. #define API extern "C" BOOST_SYMBOL_EXPORT
  30. ```
  31. [table:starting
  32. [[ Import (code that uses DLL/DSL): ] [ Export (DLL/DSL sources): ] [ Functions description: ]]
  33. [
  34. [ [getting_started_imports_cpp11_function] ]
  35. [ [getting_started_exports_cpp11_function] ]
  36. [ [funcref boost::dll::import import<T>(...)] ]
  37. ][
  38. [ [getting_started_imports_cpp_variable] ]
  39. [ [getting_started_exports_cpp_variable] ]
  40. [ [funcref boost::dll::import import<T>(...)] ]
  41. ][
  42. [ [getting_started_imports_alias] ]
  43. [ [getting_started_exports_alias] ]
  44. [
  45. [funcref boost::dll::import_alias import_alias<T>(...)]
  46. [macroref BOOST_DLL_ALIAS]
  47. ]
  48. ][/
  49. [ [getting_started_imports_c_function] ]
  50. [ [getting_started_exports_c_function] ]
  51. [ [funcref boost::dll::import import<T>(...) ] ]
  52. /][/
  53. [ [getting_started_imports_c_variable] ]
  54. [ [getting_started_exports_c_variable] ]
  55. [ [funcref boost::dll::import import<T>(...) ] ]
  56. /]]
  57. It is safe to use imported variable or function because the variables returned from [funcref boost::dll::import import<T>(...)] and [funcref boost::dll::import_alias import_alias<T>(...)] functions
  58. internally hold a reference to the shared library.
  59. [macroref BOOST_SYMBOL_EXPORT] is just a macro from Boost.Config that expands into the `__declspec(dllexport)` or `__attribute__((visibility("default")))`. You are free to use your own macro for exports.
  60. [note On Linux/POSIX/MacOS link with library "dl". "-fvisibility=hidden" flag is also recommended.]
  61. [endsect]