Skip to content

Commit

Permalink
fix: update documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Olivier <martin.olivier@live.fr>
  • Loading branch information
martin-olivier committed Mar 2, 2025
1 parent 9a78d2d commit 9cfb643
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ vcpkg install dylib
```

```sh
conan install --requires=dylib/2.2.1
conan install --requires=dylib/3.0.0
```

### Using CMake Fetch
Expand All @@ -43,7 +43,7 @@ include(FetchContent)
FetchContent_Declare(
dylib
GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
GIT_TAG "v2.2.1"
GIT_TAG "v3.0.0"
)
FetchContent_MakeAvailable(dylib)
Expand All @@ -53,40 +53,46 @@ FetchContent_MakeAvailable(dylib)

### Constructor

The `dylib` class can load a dynamic library from the system library path
The `dylib::library` class can load a dynamic library from a relative or a full path:

```c++
// Load "foo" library from the system library path
// Load "foo" library from relative path "./plugins"

dylib lib("foo");
```
The `dylib` class can also load a dynamic library from a specific path
```c++
// Load "foo" library from relative path "./libs"
dylib lib("./libs", "foo");
dylib::library lib("./plugins/libfoo.so");

// Load "foo" library from full path "/usr/lib"
// Load "foo" library from full path "/lib"

dylib lib("/usr/lib", "foo");
dylib::library lib("/lib/libfoo.so");
```
The `dylib` class will automatically add the filename decorations of the current os to the library name, but you can disable that by setting `decorations` parameter to `dylib::no_filename_decorations`
The `dylib::library` class can add filename decorations to the library name.
You can use `dylib::decorations::os_default()` to add default OS decorations to the library name.
You can also use `dylib::decorations` to add custom decorations to the library name.
```c++
// No decorations added
// Windows -> "foo.lib"
// MacOS -> "foo.lib"
// Linux -> "foo.lib"
dylib::library lib("./foo.lib");
// Default OS decorations added
// Windows -> "foo.dll"
// MacOS -> "libfoo.dylib"
// Linux -> "libfoo.so"
dylib lib("foo");
dylib::library lib("./foo", dylib::decorations::os_default());
// Windows -> "foo.lib"
// MacOS -> "foo.lib"
// Linux -> "foo.lib"
// Custom OS decorations added
// Windows -> "foo.dll"
// MacOS -> "libfoo.so"
// Linux -> "libfoo.so"
dylib lib("foo.lib", dylib::no_filename_decorations);
auto custom_decorations = dylib::decorations(
DYLIB_WIN_OTHER("", "lib"), DYLIB_WIN_OTHER(".dll", ".so")
);
dylib::library lib("./foo", custom_decorations);
```

### Get a function or a variable
Expand All @@ -102,7 +108,7 @@ Get a global variable from the dynamic library currently loaded in the object
```c++
// Load "foo" dynamic library

dylib lib("foo");
dylib::library lib("./foo", dylib::decorations::os_default());

// Get the C function "adder" (get_function<T> will return T*)

Expand All @@ -125,6 +131,8 @@ double result = adder(pi, pi);
dylib lib("foo");
// Get the C++ functions "to_string" located in the namespace "tools"
// The format for the function arguments is the following:
// type [const] [volatile] [*|&|&&]
auto int_to_string = lib.get_function<std::string(int)>("tools::to_string(int)");
auto double_to_string = lib.get_function<std::string(double)>("tools::to_string(double)");
Expand All @@ -139,26 +147,20 @@ std::string pi_str = double_to_string(pi);

### Miscellaneous tools

`has_symbol`
Returns true if the symbol passed as parameter exists in the dynamic library, false otherwise
`get_symbol`
Get a symbol from the dynamic library currently loaded in the object
Get a C or C++ symbol from the dynamic library currently loaded in the object

`native_handle`
Returns the dynamic library handle

```c++
dylib lib("foo");

if (lib.has_symbol("GetModule") == false)
std::cerr << "symbol 'GetModule' not found in 'foo' lib" << std::endl;
dylib::native_handle_type handle = lib.native_handle();
dylib::native_symbol_type symbol = lib.get_symbol("GetModule");
dylib::native_symbol_type symbol = lib.get_symbol("pi_value");

assert(handle != nullptr && symbol != nullptr);
assert(symbol == dlsym(handle, "GetModule"));
assert(symbol == dlsym(handle, "pi_value"));
```
### Exceptions
Expand Down Expand Up @@ -207,17 +209,3 @@ ctest
If you have any question about the usage of the library, do not hesitate to open a [discussion](https://github.com/martin-olivier/dylib/discussions)

If you want to report a bug or provide a feature, do not hesitate to open an [issue](https://github.com/martin-olivier/dylib/issues) or submit a [pull request](https://github.com/martin-olivier/dylib/pulls)

## Contributing

Set the cmake flag `DYLIB_BUILD_TESTS` to `ON` to enable tests and make it easier for you to contribute

```sh
cmake . -B build -DDYLIB_BUILD_TESTS=ON
```

> Do not forget to sign your commits and use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) when providing a pull request
```sh
git commit -s -m "feat: ..."
```

0 comments on commit 9cfb643

Please sign in to comment.