File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -56,12 +56,19 @@ On **Windows** this maps to `__declspec(dllexport)` / `__declspec(dllimport)`.
5656On **GCC / Clang** both use `__attribute__((visibility("default")))`.
5757On unrecognized compilers/platforms the attribute expands to nothing.
5858
59+ On GCC / Clang it is strongly recommended to compile the shared library with
60+ `-fvisibility=hidden`. Without it all symbols are public by default, defeating the purpose of
61+ `EXT_API` and leaking internal symbols into the DSO's export table. `EXT_API` then selectively
62+ restores visibility only for the symbols that are meant to be public.
63+
5964A typical CMake setup looks like:
6065
6166```cmake
62- # Shared library target — EXTLIB_IMPL builds the implementation and exports symbols
67+ # Shared library target — EXTLIB_IMPL builds the implementation and exports symbols.
68+ # -fvisibility=hidden ensures only EXT_API-marked symbols are exported on GCC/Clang.
6369add_library(extlib SHARED extlib_impl.c)
6470target_compile_definitions(extlib PRIVATE EXTLIB_IMPL EXTLIB_SHARED_EXPORT)
71+ target_compile_options(extlib PRIVATE $<$<C_COMPILER_ID:GNU,Clang,AppleClang>:-fvisibility=hidden>)
6572
6673# Consumer target — only EXTLIB_SHARED_EXPORT is defined so symbols are imported
6774target_compile_definitions(my_app PRIVATE EXTLIB_SHARED_EXPORT)
You can’t perform that action at this time.
0 commit comments