How to use a class in DLL?

前端 未结 4 1474
抹茶落季
抹茶落季 2020-11-30 03:25

Can I put a class inside a DLL? The class i wrote is this:

    class SDLConsole
{
      public:
             SDLConsole();
             ~SDLConsole(){};
             


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 04:04

    If you want to expose the data in a class, the above solutions won't cut it. You have to slap a __declspec(dllexport) on the class itself in the DLL compilation, and a __declspec(dllimport) in the module that links to the DLL.

    A common technique is to do this (Microsoft wizards produce code like this):

    #ifdef EXPORT_API
    #define MY_API __declspec(dllexport)
    #else
    #define MY_API __declspec(dllimport)
    #endif
    
    class MY_API MyClass {
       ...
    };
    

    Then make sure EXPORT_API is defined in the DLL project, and make sure it isn't defined in the module that links to the DLL.

    If you create a new DLL project in Visual C++ from scratch, and check the check box "Export symbols", some sample code will be generated using this technique.

提交回复
热议问题