Sdílet prostřednictvím


Definice a deklarace (C++)

Specifické pro produkty společnosti Microsoft

Rozhraní knihovny DLL se vztahuje na všechny položky (funkce a data), u kterých je známo, že mají být exportovány nějakou aplikací v systému. To je, všechny položky, které jsou deklarovány jako dllimport nebo jako dllexport.Všechny deklarace, které jsou součástí rozhraní DLL musí být atribut dllimport nebo dllexport.Definice však musí zadat pouze atribut dllexport.Následující definice funkce například vygeneruje chybu kompilátoru:

__declspec( dllimport ) int func() {   // Error; dllimport
                                    // prohibited on definition.
   return 1;
}

Tento kód také vygeneruje chybu:

#define DllImport   __declspec( dllimport )

__declspec( dllimport ) int i = 10;  // Error; this is a
                                     // definition.

Tato syntaxe je však správná:

__declspec( dllexport ) int i = 10;     // Okay--export definition

Použití dllexport zahrnuje definici, zatímco dllimport zahrnuje deklaraci.Klíčové slovo extern je nutné použít spolu s dllexport k vynucení deklarace, jinak je zahrnuto.Následující příklady jsou tedy správné:

#define DllImport   __declspec( dllimport )
#define DllExport   __declspec( dllexport )

extern DllImport int k; // These are both correct and imply a
DllImport int j;        // declaration.

Následující příklady objasňují předchozí:

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.
}

Viz také

Referenční dokumentace

dllexport, dllimport