| |||
| Home > Compiler-specific Features > __declspec(dllexport) | |||
The __declspec(dllexport) attribute exports
the definition of a symbol through the dynamic symbol table when
building DLL libraries. On classes, it controls the visibility of
class impedimenta such as vtables, construction vtables and RTTI,
and sets the default visibility for member function and static data
members.
You can use __declspec(dllexport) on a
function, a class, or on individual members of a class.
When an inline function is marked __declspec(dllexport),
the function definition might be inlined, but an out-of-line instance
of the function is always generated and exported in the same way
as for a non-inline function.
When a class is marked __declspec(dllexport),
for example, class __declspec(dllexport) S { ... }; its
static data members and member functions are all exported. When
individual static data members and member functions are marked with __declspec(dllexport),
only those members are exported. vtables, construction vtable tables and
RTTI are also exported.
The following declaration is correct:
class __declspec(dllexport) S { ... };
The following declaration is incorrect:
__declspec(dllexport) class S { ... };
In conjunction with --export_all_vtbl,
you can use __declspec(notshared) to exempt a
class or structure from having its vtable, construction vtable table
and RTTI exported. --export_all_vtbl and __declspec(dllexport) are
typically not used together.
If you mark a class with __declspec(dllexport),
you cannot then mark individual members of that class with __declspec(dllexport).
If you mark a class with __declspec(dllexport),
ensure that all of the base classes of that class are marked __declspec(dllexport).
If you export a virtual function within a class, ensure that you either export all of the virtual functions in that class, or that you define them inline so that they are visible to the client.
The __declspec() required in a declaration
depends on whether or not the definition is in the same shared library.
/* This is the declaration for use in the same shared library as the */
/* definition */
__declspec(dllexport) extern int mymod_get_version(void);
/* Translation unit containing the definition */
__declspec(dllexport) extern int mymod_get_version(void)
{
return 42;
}
/* This is the declaration for use in a shared library that does not contain */
/* the definition */
__declspec(dllimport) extern int mymod_get_version(void);
As a result of the following macro, a translation unit that
does not have the definition in a defining link unit sees __declspec(dllexport).
/* mymod.h - interface to my module */ #ifdef BUILDING_MYMOD #define MYMOD_API __declspec(dllexport) #else /* not BUILDING_MYMOD */ #define MYMOD_API __declspec(dllimport) #endif MYMOD_API int mymod_get_version(void);