LLVM Interface Export Annotations#
Symbols that are part of LLVM’s public interface must be explicitly annotated to support shared library builds with hidden default symbol visibility. This document provides background and guidelines for annotating the codebase.
Annotation Macros#
The distinct DLL import and export annotations required for Windows DLLs typically lead developers to define a preprocessor macro for annotating exported symbols in header public files. The custom macro resolves to the export annotation when building the library and the import annotation when building the client.
We have defined the LLVM_ABI macro in llvm/Support/Compiler.h
for this purpose:
#if defined(LLVM_EXPORTS)
#define LLVM_ABI __declspec(dllexport)
#else
#define LLVM_ABI __declspec(dllimport)
#endif
Windows DLL symbol visibility requirements are approximated on ELF and Mach-O
shared library builds by setting default symbol visibility to hidden
(-fvisibility-default=hidden) when building with the following
configuration:
LLVM_BUILD_LLVM_DYLIB_VIS=On
For an ELF or Mach-O platform with this setting, the LLVM_ABI macro is
defined to override the default hidden symbol visibility:
#define LLVM_ABI __attribute__((visibility("default")))
In addition to LLVM_ABI, there are a few other macros for use in less
common cases described below.
Export macros are used to annotate symbols only within their intended shared library. This is necessary because of the way Windows handles import/export annotations.
For example, LLVM_ABI resolves to __declspec(dllexport) only when
building source that is part of the LLVM shared library (e.g. source under
llvm-project/llvm). If LLVM_ABI were incorrectly used to annotate a
symbol from a different LLVM project (such as Clang) it would always resolve to
__declspec(dllimport) and the symbol would not be properly exported.
How to Annotate Symbols#
Functions#
Exported function declarations in header files must be annotated with
LLVM_ABI.
#include "llvm/Support/Compiler.h"
LLVM_ABI void exported_function(int a, int b);
Global Variables#
Exported global variables must be annotated with LLVM_ABI at their
extern declarations.
#include "llvm/Support/Compiler.h"
LLVM_ABI extern int exported_global_variable;
Classes, Structs, and Unions#
Classes, structs, and unions can be annotated with LLVM_ABI at their
declaration, but this option is generally discouraged because it will
export every class member, vtable, and type information. Instead, LLVM_ABI
should be applied to individual class members that require export.
In the most common case, public and protected methods without a body in the
class declaration must be annotated with LLVM_ABI.
#include "llvm/Support/Compiler.h"
class ExampleClass {
public:
// Public methods defined externally must be annotated.
LLVM_ABI int sourceDefinedPublicMethod(int a, int b);