File: getting_started.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (76 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[/
    Copyright 2014 Renato Tegon Forti, Antony Polukhin.
    Copyright Antony Polukhin, 2015-2025.
    Distributed under the Boost Software License, Version 1.0.
    (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/]

[section:getting_started Getting started]

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.
`boost::dll::import*` functions require linking with `boost_filesystem` and `boost_system` libraries, along with some platform specific dynamic load libraries like `dl`. With CMake
that burden goes away as `find_package(Boost COMPONENTS dll)` provides a `Boost::dll` target that manages the dependencies to other libraries.

If you want to load a library, just construct [classref boost::dll::shared_library] class with a path to the library as a parameter:
```
boost::dll::shared_library lib("/test/boost/application/libtest_library.so");

```
Now you can easily import symbols from that library using the `get` and `get_alias` member functions:
```
int plugin_constant = lib.get<const int>("integer_variable");
auto function_ptr = lib.get<int()>("function_returning_int");
int& i = lib.get_alias<int>("alias_to_int_variable");
```
In case of `boost::dll::shared_library` it is safe to use imported symbols only until `boost::dll::shared_library`
instance is not destroyed.

Query libraries using [classref boost::dll::library_info] and get symbol infos using [funcref boost::dll::symbol_location],
[funcref boost::dll::this_line_location] and [funcref boost::dll::program_location].

For importing a single function or variable you may use a following one liners:
[import ../example/getting_started.cpp]
[import ../example/getting_started_library.cpp]

```
using namespace boost;

// `extern "C"` - specifies C linkage: forces the compiler to export function/variable by a pretty (unmangled) C name.
#define API extern "C" BOOST_SYMBOL_EXPORT
```
[table:starting
[[ Import (code that uses DLL/DSL): ] [ Export (DLL/DSL sources): ] [ Functions description: ]]
[
    [ [getting_started_imports_cpp11_function] ]
    [ [getting_started_exports_cpp11_function] ]
    [ [funcref boost::dll::import import<T>(...)] ]
][
    [ [getting_started_imports_cpp_variable] ]
    [ [getting_started_exports_cpp_variable] ]
    [ [funcref boost::dll::import import<T>(...)] ]
][
    [ [getting_started_imports_alias] ]
    [ [getting_started_exports_alias] ]
    [
        [funcref boost::dll::import_alias import_alias<T>(...)]

        [macroref BOOST_DLL_ALIAS]
    ]
][/
    [ [getting_started_imports_c_function] ]
    [ [getting_started_exports_c_function] ]
    [ [funcref boost::dll::import import<T>(...) ] ]
/][/
    [ [getting_started_imports_c_variable] ]
    [ [getting_started_exports_c_variable] ]
    [ [funcref boost::dll::import import<T>(...) ] ]
/]]

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
internally hold a reference to the shared library.

[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.

[note On Linux/POSIX/MacOS link with library "dl". "-fvisibility=hidden" flag is also recommended.]

[endsect]