定義と宣言 (C++)
Microsoft 固有の仕様
DLL インターフェイスでは、システム内のプログラムによってエクスポートされることがわかっているすべての項目 (関数とデータ)、つまり、 dllimport
または dllexport
として宣言されたすべての項目が参照されます。 DLL インターフェイスに含まれるすべての宣言では、 dllimport
または dllexport
の属性を指定する必要があります。 しかし、定義では、dllexport
属性のみを指定する必要があります。 たとえば、次の関数定義はコンパイラ エラーになります。
__declspec( dllimport ) int func() { // Error; dllimport
// prohibited on definition.
return 1;
}
次のコードでも、エラーが生成します。
__declspec( dllimport ) int i = 10; // Error; this is a definition.
ただし、次に示す構文は、正しい構文です。
__declspec( dllexport ) int i = 10; // Okay--export definition
dllexport
の使用は定義を意味するのに対し、 dllimport
は宣言を意味します。 宣言を強制するには、extern
と共に dllexport
キーワードを使用する必要があります。このようにしない場合、暗黙の定義になります。 そのため、次の例は正しいコードになります。
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
extern DllExport int k; // These are both correct and imply a
DllImport int j; // declaration.
次の例に、上記の例をさらに明確に示します。
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.
}
Microsoft 固有の仕様はここまで
関連項目
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示