Visual Studio C++: When should I be using __declspec(dllimport)?

前端 未结 1 715

I had a question about DLL building / linking in Visual Studio 2005 and later. Basically my understanding and experience is this:

To build a DLL, I specify the proje

相关标签:
1条回答
  • 2020-12-06 19:19

    Yes you would use __declspec(dllimport) and you generally have a macro that controls whether a source file either exports (if it's part of your DLL) or imports (if it's part of the using-executable) symbols.

    In your DLL you can set a manifest constant to the build settings of some sort, say 'BUILDING_MY_DLL' and then create the macro like this within your header file:

    #ifdef BUILDING_MY_DLL
    #define MY_DLL_EXPORT __declspec(dllexport)
    #else
    #define MY_DLL_EXPORT __declspec(dllimport)
    #endif
    

    and then decorate your exported functions like this:

    MY_DLL_EXPORT int func(int y);
    

    You can also export entire classes this way too:

    class MY_DLL_EXPORT InterestingClass
    {
    ...
    };
    
    0 讨论(0)
提交回复
热议问题