Skip to content

Commit adffa75

Browse files
committed
PR feedback: update export macro documentation to match CMake
1 parent 2a82967 commit adffa75

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

Docs/ExportMacroDefinitions.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Export Macro Definitions
1+
# Export Macro Definitions
22

33
A common approach for annotating project’s public interface is by defining a
44
set of preprocessor macros that are used instead of adding annotations directly
@@ -7,38 +7,43 @@ to the source. This approach allows supporting visibility annotations
77
DLL 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
1111
project’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 Projet 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

4039
An ELF shared library or Mach-O dylib project will have all symbols publicly
4140
visible by default so annotations are not strictly required. However, when a
4241
project is built with default hidden symbol visibility using
4342
`-fvisibility-default=hidden`, individual symbols must be explicitly exported
4443
using a visibility annotations.
44+
45+
## CMake Support
46+
When building with CMake, the `generage_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

Comments
 (0)