定义和声明 (C)
Microsoft 专用
DLL 接口引用已知由系统中的某程序导出的所有项(函数和数据);即所有被声明为 dllimport
或 dllexport
的项。 DLL 接口中包含的所有声明都必须指定 dllimport
或 dllexport
特性。 但是,定义仅可指定 dllexport
特性。 例如,以下函数定义产生了一个编译器错误:
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
DllImport int func() /* Error; dllimport prohibited in */
/* definition. */
{
return 1;
}
以下代码也会产生错误:
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
DllImport int i = 10; /* Error; this is a definition. */
但是,这是正确的语法:
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
DllExport int i = 10; /* Okay: this is an export definition. */
使用 dllexport
意味着定义,而使用 dllimport
则意味着声明。 必须使用带 extern
的 dllexport
关键字来强制进行声明;否则,会进行隐式定义。
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
extern DllImport int k; /* These are correct and imply */
Dllimport int j; /* a declaration. */
结束 Microsoft 专用
请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈