【问题标题】:Using __declspec( dllexport )使用 __declspec(dllexport)
【发布时间】:2018-08-05 05:43:32
【问题描述】:

嗨,我对 dllexport 有点困惑。例如,当我在课堂上使用 __declspec( dllexport ) 时

 #define DllExport   __declspec( dllexport )  
class DllExport C {  
   int i;  
   virtual int func( void ) { return 1; }  
};  

我是将 C 类导出到 dll 文件还是从 dll 文件导出 C 类?

【问题讨论】:

标签: c++ dllexport


【解决方案1】:

在编译 DLL 时,您必须像以前一样编写 __declspec(dllexport)。这告诉编译器您希望将其导出。在包含的文件中使用您希望__declspec(dllimport) 的DLL 时。然后编译器知道这个函数和类在一个 DLL 文件中并且需要被导入。因为你不想改变你的头文件,你应该定义一个宏,例如BUILD_DLL.

    #ifdef BUILD_DLL
    #define DLL_PORTING __declspec(dllexport)
    #else
    #define DLL_PORTING __declspec(dllimport)
    #endif

现在你在 example.h 中编写:

    class DLL_PORTING example_class { … };

在您的 .exe 文件中只需包含您需要的头文件,一切都会正常工作。

【讨论】:

    猜你喜欢
    • 2016-03-25
    • 2023-01-26
    • 2015-02-24
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多