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 专用