<small id='W9LqE'></small><noframes id='W9LqE'>

    <i id='W9LqE'><tr id='W9LqE'><dt id='W9LqE'><q id='W9LqE'><span id='W9LqE'><b id='W9LqE'><form id='W9LqE'><ins id='W9LqE'></ins><ul id='W9LqE'></ul><sub id='W9LqE'></sub></form><legend id='W9LqE'></legend><bdo id='W9LqE'><pre id='W9LqE'><center id='W9LqE'></center></pre></bdo></b><th id='W9LqE'></th></span></q></dt></tr></i><div id='W9LqE'><tfoot id='W9LqE'></tfoot><dl id='W9LqE'><fieldset id='W9LqE'></fieldset></dl></div>

    1. <legend id='W9LqE'><style id='W9LqE'><dir id='W9LqE'><q id='W9LqE'></q></dir></style></legend>
        <bdo id='W9LqE'></bdo><ul id='W9LqE'></ul>
    2. <tfoot id='W9LqE'></tfoot>

    3. 如何从 dll 导出 C++ 类?

      How to export a C++ class from a dll?(如何从 dll 导出 C++ 类?)
        <bdo id='16tXx'></bdo><ul id='16tXx'></ul>
      • <tfoot id='16tXx'></tfoot>

            <tbody id='16tXx'></tbody>

            <small id='16tXx'></small><noframes id='16tXx'>

            1. <legend id='16tXx'><style id='16tXx'><dir id='16tXx'><q id='16tXx'></q></dir></style></legend>
              • <i id='16tXx'><tr id='16tXx'><dt id='16tXx'><q id='16tXx'><span id='16tXx'><b id='16tXx'><form id='16tXx'><ins id='16tXx'></ins><ul id='16tXx'></ul><sub id='16tXx'></sub></form><legend id='16tXx'></legend><bdo id='16tXx'><pre id='16tXx'><center id='16tXx'></center></pre></bdo></b><th id='16tXx'></th></span></q></dt></tr></i><div id='16tXx'><tfoot id='16tXx'></tfoot><dl id='16tXx'><fieldset id='16tXx'></fieldset></dl></div>
              • 本文介绍了如何从 dll 导出 C++ 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个有两个重载函数的类.如何从 dll 导出它以及如何由其他 C++ 类使用它?我的班级看起来像这样:

                I have a class which has two overloaded functions. How do I export it from a dll and also how to use it by other C++ classes? My class looks like this:

                #define DECLDIREXP __declspec(dllexport) 
                
                #define DECLDIRIMP __declspec(dllimport)
                
                
                class DECLDIREXP xyz 
                
                {
                
                public: 
                          void printing();
                          void printing(int a);
                };  
                
                using namespace std; 
                
                void xyz::printing()
                {
                        cout<<"hello i donot take any argument";
                }
                
                
                void xyz::printing(int a)
                {
                        cout<<"hello i take "<< a <<"as argument";
                }
                

                推荐答案

                一个常见的方法是有一个单独的宏(我们称之为 EXPORT),它可以扩展为 dllimportdllexport 取决于是否设置了某种立即构建 DLL"定义,如下所示:

                A common approach is to have a single macro (let's call it EXPORT) which either expands to dllimport or dllexport depending on whether some sort of "building the DLL right now" define is set, like this:

                #ifdef MAKEDLL
                #  define EXPORT __declspec(dllexport)
                #else
                #  define EXPORT __declspec(dllimport)
                #endif
                
                class EXPORT xyz {
                  // ...
                };
                

                这个想法是在构建 DLL 时,将 MAKEDLL 添加到预处理器定义中.这样,所有代码都将被导出.链接到您的 DLL(并因此包含此头文件)的客户端根本不需要做任何事情.通过不定义MAKEDLL,它们将自动导入所有代码.

                The idea is that when building your DLL, you add MAKEDLL to the preprocessor definitions. That way, all the code will be exported. Clients who link against your DLL (and hence include this header file) don't need to do anything at all. By not defining MAKEDLL, they will automatically import all the code.

                这种方法的优点是正确使用宏的负担从许多(客户端)转移到了 DLL 的作者身上.

                The advantage of this approach is that the burden of getting the macros right is moved from the many (the clients) to just the author of the DLL.

                这样做的缺点是,当按原样使用上面的代码时,不再可能直接将代码编译到某个客户端模块中,因为不可能将 EXPORT 宏定义为空.为此,您需要进行另一项检查,如果为真,则将 EXPORT 定义为空.

                The disadvantage of this is that when using the code above as it is, it's no longer possible to just compile the code directly into some client module since it's not possible to define the EXPORT macro to nothing. To achieve that, you'd need to have another check which, if true, defines EXPORT to nothing.

                关于稍微不同的主题:在许多情况下,不可能(或不希望!)像这样导出一个完整的类.相反,您可能只想导出所需的符号.例如,在您的情况下,您可能只想导出两个公共方法.这样,所有私有/受保护成员都不会被导出:

                On a slightly different topic: in many cases, it's not possible (or desired!) to export a complete class like that. Instead, you may want to just export the symbols you need. For instance, in your case, you may want to just export the two public methods. That way, all the private/protected members won't be exported:

                class xyz
                {
                public: 
                    EXPORT void printing();
                    EXPORT void printing(int a);
                };
                

                这篇关于如何从 dll 导出 C++ 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                  <tfoot id='qF929'></tfoot>

                  <small id='qF929'></small><noframes id='qF929'>

                    <bdo id='qF929'></bdo><ul id='qF929'></ul>
                      <legend id='qF929'><style id='qF929'><dir id='qF929'><q id='qF929'></q></dir></style></legend>

                        • <i id='qF929'><tr id='qF929'><dt id='qF929'><q id='qF929'><span id='qF929'><b id='qF929'><form id='qF929'><ins id='qF929'></ins><ul id='qF929'></ul><sub id='qF929'></sub></form><legend id='qF929'></legend><bdo id='qF929'><pre id='qF929'><center id='qF929'></center></pre></bdo></b><th id='qF929'></th></span></q></dt></tr></i><div id='qF929'><tfoot id='qF929'></tfoot><dl id='qF929'><fieldset id='qF929'></fieldset></dl></div>
                            <tbody id='qF929'></tbody>