定义和声明 (C)Definitions and Declarations (C)

Microsoft 专用Microsoft Specific

DLL 接口引用已知由此系统中的某个程序导出的所有项(函数和数据);即,声明为 dllimport 或 dllexport 的所有项。The DLL interface refers to all items (functions and data) that are known to be exported by some program in the system; that is, all items that are declared as dllimport or dllexport. DLL 接口中包含的所有声明必须指定 dllimport 或 dllexport 特性。All declarations included in the DLL interface must specify either the dllimport or dllexport attribute. 但是,定义仅可指定 dllexport 特性。However, the definition can specify only the dllexport attribute. 例如,以下函数定义产生了一个编译器错误:For example, the following function definition generates a compiler error:

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

DllImport int func()    /* Error; dllimport prohibited in */  
                        /* definition. */  
{  
   return 1;  
}  

以下代码也会产生错误:This code also generates an error:

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

DllImport int i = 10;      /* Error; this is a definition. */  

但是,这是正确的语法:However, this is correct syntax:

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

DllExport int i = 10;      /* Okay: this is an export definition. */  

使用 dllexport 表示定义,而使用 dllimport 表示声明。The use of dllexport implies a definition, while dllimport implies a declaration. 必须使用带 externdllexport 关键字来强制进行声明;否则,会进行隐式定义。You must use the extern keyword with dllexport to force a declaration; otherwise, a definition is implied.

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

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

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

DLL 导入和导出函数DLL Import and Export Functions