Using the #define macro to standardize class declarations

I am creating a C ++ DLL for one of my projects. I am trying to standardize a way to define a class. Therefore, instead of each spelling:

class __declspec(dllexport) ClassName 

I am creating a #define macro to facilitate this process:

 #define CLASS( cName ) class __declspec(dllexport) cName 

But when I use it, it causes the following error:

 Error: Expected a ';' 

I know that you can use the #define macro to define the entire creation of a class, but can it be used to define only the "class header"?

Thanks,

Keep in mind that I'm trying to do this because we are going to deal with hundreds of classes, so such “automation” would be most useful :)

EDIT:

Example:

 #define CLASS( nClass ) class __declspec(dllexport) nClass CLASS( APTest ) { // Here is the error of missing ';' public: APTest(); }; 
+4
source share
2 answers

Do not do that.

C ++ is already standardized!

If you ever expected other people to read your code, then just write it in regular C ++, and not in some kind of homecooked dialect that looks different. Get used to the correct C ++ syntax, this will make it easier to read other people's code in C ++.

One thing that makes sense is to simplify the __declspec part you can do as follows:

 #ifdef _WIN32 #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT #endif class DLLEXPORT APTest { // ... }; 

You really do not make your life easier by writing CLASS( APTest ) , and you complicate the understanding of others. Just say no.

+14
source

There is a better way than @Wakely. Do it like this:

 #ifdef MYLIB_DLL #ifndef MYLIB_IFACE #ifdef MYLIB_IFACE_EXPORT #define MYLIB_IFACE _declspec( dllexport ) #else // !MYLIB_IFACE_EXPORT #define MYLIB_IFACE _declspec( dllimport ) #endif // !MYLIB_IFACE_EXPORT #endif // !MYLIB_IFACE #else // !MYLIB_DLL #ifndef MYLIB_IFACE #define MYLIB_IFACE #endif // !MYLIB_IFACE 

Put such a block in the header that is used by each file in your DLL, and in the public header for your DLL.

Each character that should be exported from your DLL will be marked as follows:

 class MYLIB_IFACE MyClass { }; void MYLIB_IFACE myFunc(); 

Then in each .cpp file in your dll, the first line should be:

 #define MYLIB_IFACE_EXPORT 

If you do this, it will work perfectly on POSIX systems that do not use dllexport / dllimport. To create the DLL version of your library, you define MYLIB_DLL. (you can do this in compiler flags so that it can be controlled from your build system)

To create a static version of your library, do not define MYLIB_DLL.

@Update:

You can expand this to maintain GCC visibility as follows:

 #ifdef WIN32 #define KX_SYMBOL_EXPORT _declspec( dllexport ) #define KX_SYMBOL_IMPORT _declspec( dllimport ) #else // GCC #define KX_SYMBOL_EXPORT __attribute__(( visibility ("default"))) #define KX_SYMBOL_IMPORT #endif #ifdef KX_DLL #ifndef KX_IFACE #ifdef KX_IFACE_EXPORT #define KX_IFACE KX_SYMBOL_EXPORT #else // !KX_IFACE_EXPORT #define KX_IFACE KX_SYMBOL_IMPORT #endif // !KX_IFACE_EXPORT #endif // !KX_IFACE #else // !KX_DLL #ifndef KX_IFACE #define KX_IFACE #endif // !KX_IFACE #endif // !KX_DLL 

I remove the GCC bit in the first example for simplicity. But this is true. @Wakely is so right.

+1
source

Source: https://habr.com/ru/post/1481952/


All Articles