Definition of Export in CC ++

zhaozj2021-02-16  103

C / C defines the DLL under Windows under Windows or the library of C , or the c may be used to use Import / Export. This article discusses a common use practice of a general import / export macro. As shown below, "common.h" defines a cross-platform IMPORT / EXPORT macro definition, "Something.h" defines the Import / Export definition of a class header file, "Something.cpp" implementation class and Export class, Use this class only with #include "something", because no macro Some_exports is defined, the default is import. "Common.h" includes: #ifdef WIN32 #ifdef __cplusplus #define DLL_EXPORT_C_DECL extern "C" __declspec (dllexport) #define DLL_IMPORT_C_DECL extern "C" __declspec (dllimport) #define DLL_EXPORT_DECL extern __declspec (dllexport) #define DLL_IMPORT_DECL extern __declspec ( dllimport) #define DLL_EXPORT_CLASS_DECL __declspec (dllexport) #define DLL_IMPORT_CLASS_DECL __declspec (dllimport) #else #define DLL_EXPORT_DECL __declspec (dllexport) #define DLL_IMPORT_DECL __declspec (dllimport) #endif #else #ifdef __cplusplus #define DLL_EXPORT_C_DECL extern "C" #define DLL_IMPORT_C_DECL extern "C" #define DLL_EXPORT_DECL extern #define DLL_IMPORT_DECL extern #define DLL_EXPORT_CLASS_DECL #define DLL_IMPORT_CLASS_DECL #else #define DLL_EXPORT_DECL extern #define DLL_IMPORT_DECL extern #endif # ENDIF

"Something.h" include: #include "common.h" #ifdef SOME_EXPORTS #define SOME_CLASS DLL_EXPORT_CLASS_DECL #define SOME_API DLL_EXPORT_DECL #else #define SOME_CLASS DLL_IMPORT_CLASS_DECL #define SOME_API DLL_IMPORT_DECL #endif

Class Some_class myclass {} Some_API void myfunc () {} "Something.cpp" implements myclass, header files include: #define some_exports #include "something" hometying.h "

Small chicks have always adopted the classes in this method Export DLL, such as the C of PLUGIN's implementation of C to implement Plugin. Of course, this is also the practice of OpenSource.

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

New Post(0)