Definition of Export in CC ++

zhaozj2021-02-12  130

DLL under Windows, the library under Linux or C is called the library, which may be used to 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.h" Small chicken shooter has always adopted the class in this method Export DLL, such as Plugin's implementation of C implements PLUGIN. Of course, this is also the practice of OpenSource.

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

New Post(0)