1- ## Export Macro Definitions
1+ # Export Macro Definitions
22
33A common approach for annotating project’s public interface is by defining a
44set of preprocessor macros that are used instead of adding annotations directly
@@ -7,38 +7,43 @@ to the source. This approach allows supporting visibility annotations
77DLL import/export annotations (` __declspec(dllimport) ` ,
88` __declspec(dllexport) ` ) when building for Windows.
99
10- For example, the ` PUBLIC_ABI ` macro defined below can be added to symbols in a
10+ For example, the ` PROJECT_EXPORT ` macro defined below can be added to symbols in a
1111project’s header files files that should be externally visible.
1212
1313``` cpp
14- #ifdef _WIN32
15- #ifdef EXPORTING_ABI
16- // When building the Windows DLL, the public API must be annotated for exported.
17- #define PUBLIC_ABI __ declspec(dllexport)
14+ #if defined(PROJECT_EXPORT_STATIC)
15+ /* We are building Project as a static library (no exports) * /
16+ # define PROJECT_EXPORT
1817#else
19- // When building clients of the Windows DLL, the public API must be annotated for import.
20- #define PUBLIC_ABI __ declspec(dllimport )
21- #endif
22- # elif defined(__cplusplus) && defined(__has_cpp_attribute) && \
23- __has_cpp_attribute (gnu::visibility) && defined( __ GNUC __ ) && !defined( __ clang __ )
24- // When supported, use the gnu::visibility C++ attribute to set visibility to default.
25- #define PUBLIC_API [[ gnu::visibility("default") ]]
26- #elif defined( __ has_attribute) && __ has_attribute(visibility )
27- // Otherwise, use the __ attribute __ to set visibility to default.
28- #define PUBLIC_ABI __ attribute __ ((visibility("default")))
29- #else
30- #error "Required visibility attribute are not supported by compiler!"
18+ /* We are building Project as a shared library * /
19+ # if defined( _ WIN32 )
20+ # if defined(Project_EXPORTS)
21+ /* We are building this library */
22+ # define PROJECT_EXPORT __ declspec(dllexport )
23+ # else
24+ /* We are using this library */
25+ # define PROJECT_EXPORT __ declspec(dllimport )
26+ # endif
27+ # else
28+ # define PROJECT_EXPORT __ visibility __ ((visibility("default")))
29+ # endif
3130#endif
3231```
3332
34- A Windows project using these macros must be built with `EXPORTING_ABI` defined
35- so that public symbols are annotated for export with `__declspec(dllexport)`.
36- When building the client , however, `EXPORTING_ABI` must not be defined so the
37- same set of public symbols will be properly annotated for import with
38- `__declspec(dllimport)`.
33+ A Windows project using these macros must be built with ` Project_EXPORTS `
34+ defined so that public symbols are annotated for export with
35+ ` __declspec(dllexport) ` . When building clients of the project , however,
36+ ` Project_EXPORTS ` must not be defined so the same set of public symbols will be
37+ properly annotated for import with ` __declspec(dllimport) ` .
3938
4039An ELF shared library or Mach-O dylib project will have all symbols publicly
4140visible by default so annotations are not strictly required. However, when a
4241project is built with default hidden symbol visibility using
4342` -fvisibility-default=hidden ` , individual symbols must be explicitly exported
4443using a visibility annotations.
44+
45+ ## CMake Support
46+ When building with CMake, the ` generate_export_header() ` function can be used
47+ to auto-generate a header with appropriate export macro definitions. See [ CMake
48+ documentation] ( https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html )
49+ for details.
0 commit comments