Definicje i deklaracje (C++)
Programu Microsoft
Biblioteka DLL interfejsu odnosi się do wszystkich elementów (funkcji i danych), które są znane mają być wywiezione przez jakiś program w systemie; oznacza to, że wszystkie elementy, które zostały zgłoszone jako dllimport lub dllexport.Wszystkie deklaracje zawarte w interfejsie DLL należy określić albo dllimport lub dllexport atrybut.Jednakże, definicję należy określić tylko dllexport atrybut.Na przykład następujące definicji funkcji generuje błąd kompilatora:
__declspec( dllimport ) int func() { // Error; dllimport
// prohibited on definition.
return 1;
}
Kod ten jest również generuje błąd:
#define DllImport __declspec( dllimport )
__declspec( dllimport ) int i = 10; // Error; this is a
// definition.
Jednakże jest poprawna składnia:
__declspec( dllexport ) int i = 10; // Okay--export definition
Użycie dllexport pociąga za sobą definicji, podczas gdy dllimport oznacza deklarację.Należy użyć extern słowa kluczowego z dllexport do wymuszenia deklaracji; w przeciwnym razie jest implikowana definicji.W ten sposób poniższe przykłady są poprawne:
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
extern DllImport int k; // These are both correct and imply a
DllImport int j; // declaration.
Następujące przykłady wyjaśnienia poprzedzającego:
static __declspec( dllimport ) int l; // Error; not declared extern.
void func() {
static __declspec( dllimport ) int s; // Error; not declared
// extern.
__declspec( dllimport ) int m; // Okay; this is a
// declaration.
__declspec( dllexport ) int n; // Error; implies external
// definition in local scope.
extern __declspec( dllimport ) int i; // Okay; this is a
// declaration.
extern __declspec( dllexport ) int k; // Okay; extern implies
// declaration.
__declspec( dllexport ) int x = 5; // Error; implies external
// definition in local scope.
}