Export C ++ class in the DLL written in VC

xiaoxiao2021-03-05  26

How to guide the class in VC, this is a problem that is often asked. Here I explain this question with a simple example: first create a Win32 Dynamic-Link Library project using Wizard and define a simple C class CINDLL. Since this class is referenced by files outside the project, this class is required. Because only the DLL generated after the introduction has sufficient information to be correctly introduced in the process space when the connection and runtime are connected. There are two ways to extract classes, using __declspec (dllexport) definition and use definition files. Let's take a way to use __Declspec (dllexport): Class definitions can be changed to: class __declspec (dllexport) Cindll is OK. (Translator: You may not believe that there will be such simple, I don't believe it. :-) This generated project is correct when compiling but generates errors when using, because you can use __declspec (DLLEXPORT), and use this DLL project does not take this class, but to introduce this class) You need to change the class definition to class __declspec (DLLIMPORT) CINDLL when using. Using definition files can effectively avoid this problem, this method is to use macro definitions to generate different compile code in different places: Add as follows: #ifdef _classindll #define classindll_class_decl __declspec (dllexport) #ELSE #define CLASSINDLL_CLASS_DECL __declspec (dllimport) #endif #endif // ClassInDLL_H // the class __declspec (dllexport) CInDLL to class CLASSINDLL_CLASS_DECL CInDLL {CInDLL (); ...} #define _CLASSINDLL added to achieve CPP statement at the top of this class file. #Define _classindll cindll :: cindll () {} ... This can not be made without any changes when using this class. (Translator: This method can be used when not using the MFC, if you use the MFC to generate the MFC DLL, as long as you define the following definition, you can Class AFX_EXT_CLASS YOURCLASS)

转载请注明原文地址:https://www.9cbs.com/read-33134.html

New Post(0)