1
- ## Export Macro Definitions
1
+ # Export Macro Definitions
2
2
3
3
A common approach for annotating project’s public interface is by defining a
4
4
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
7
7
DLL import/export annotations (` __declspec(dllimport) ` ,
8
8
` __declspec(dllexport) ` ) when building for Windows.
9
9
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
11
11
project’s header files files that should be externally visible.
12
12
13
13
``` 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
18
17
#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
31
30
#endif
32
31
```
33
32
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) ` .
39
38
40
39
An ELF shared library or Mach-O dylib project will have all symbols publicly
41
40
visible by default so annotations are not strictly required. However, when a
42
41
project is built with default hidden symbol visibility using
43
42
` -fvisibility-default=hidden ` , individual symbols must be explicitly exported
44
43
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