Skip to content

Commit 94f2408

Browse files
committed
Add EXTLIB_SHARED_EXPORT and EXT_API macros to make compilation to shared lib possible
1 parent bfb1aac commit 94f2408

2 files changed

Lines changed: 209 additions & 130 deletions

File tree

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,48 @@ Features:
3232
3333
Configuration options:
3434
```c
35-
#define EXTLIB_NO_SHORTHANDS // Disable shorthands names, only prefixed one will be defined
36-
#define EXTLIB_NO_STD // Do not use libc functions
37-
#define EXTLIB_WASM // Enable when compiling for wasm target. Implies EXTLIB_NO_STD
38-
#define EXTLIB_THREADSAFE // Thread safe Context
39-
#define NDEBUG // Strips runtime assertions and replaces unreachables with compiler
40-
// intrinsics
35+
#define EXTLIB_NO_SHORTHANDS // Disable shorthands names, only prefixed one will be defined
36+
#define EXTLIB_NO_STD // Do not use libc functions
37+
#define EXTLIB_WASM // Enable when compiling for wasm target. Implies EXTLIB_NO_STD
38+
#define EXTLIB_THREADSAFE // Thread safe Context
39+
#define EXTLIB_SHARED_EXPORT // Mark all public API symbols for shared library use (see below)
40+
#define NDEBUG // Strips runtime assertions and replaces unreachables with compiler
41+
// intrinsics
4142
```
4243
44+
### Building as a shared library
45+
46+
Define `EXTLIB_SHARED_EXPORT` to enable the `EXT_API` visibility attribute on all public symbols,
47+
making it possible to compile extlib as a DLL or shared object.
48+
49+
The macro behaves differently depending on whether the translation unit is building the
50+
implementation or consuming it:
51+
52+
- **Implementation TU** (`EXTLIB_IMPL` defined): symbols are *exported*.
53+
- **Consumer TUs** (`EXTLIB_IMPL` not defined): symbols are *imported*.
54+
55+
On **Windows** this maps to `__declspec(dllexport)` / `__declspec(dllimport)`.
56+
On **GCC / Clang** both use `__attribute__((visibility("default")))`.
57+
On unrecognized compilers/platforms the attribute expands to nothing.
58+
59+
A typical CMake setup looks like:
60+
61+
```cmake
62+
# Shared library target — EXTLIB_IMPL builds the implementation and exports symbols
63+
add_library(extlib SHARED extlib_impl.c)
64+
target_compile_definitions(extlib PRIVATE EXTLIB_IMPL EXTLIB_SHARED_EXPORT)
65+
66+
# Consumer target — only EXTLIB_SHARED_EXPORT is defined so symbols are imported
67+
target_compile_definitions(my_app PRIVATE EXTLIB_SHARED_EXPORT)
68+
target_link_libraries(my_app extlib)
69+
```
70+
71+
Where `extlib_impl.c` contains exactly:
72+
```c
73+
#define EXTLIB_IMPL
74+
#include "extlib.h"
75+
```
76+
4377
## Examples
4478

4579
You can find a bunch of examples showcasing library features in `examples/`

0 commit comments

Comments
 (0)