定义和声明 (C)

Microsoft 专用

DLL 接口引用已知由系统中的某程序导出的所有项(函数和数据);即所有被声明为 dllimportdllexport 的项。 DLL 接口中包含的所有声明都必须指定 dllimportdllexport 特性。 但是,定义仅可指定 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 则意味着声明。 必须使用带 externdllexport 关键字来强制进行声明;否则,会进行隐式定义。

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

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

结束 Microsoft 专用

请参阅

DLL 导入和导出函数