Is __declspec(dllexport) ignored if compiling a static library?

1.3k Views Asked by At

Currently I am taking an older static library I wrote, and trying to add functionality to allow it to be compiled as a .dll, (or .so). I've gone through and added a macro along the lines of:

#define DLLEXPORT
#ifdef SHARED
    #ifdef COMPILING
        #ifdef WIN32
           #undef DLLEXPORT
           #define DLLEXPORT __declspec(dllexport)
        #endif
    #else
        #ifdef WIN32    
           #undef DLLEXPORT
           #define DLLEXPORT __declspec(dllimport)
    #endif
#endif

And then gone through and added DLLEXPORT to all functions and classes that would need to be exported or imported.

But I am wondering if this is really necessary? Because when I compile the project using cmake add_library(LIB_NAME STATIC SRC_FILES) and define both SHARED and COMPILING, it still manages to make MinGW Makefiles which compile successfully to a static library.

So my question is, is this guaranteed by any standard? I've looked through all of the documentation I could find including Exporting From A DLL Using __declspec(dllexport) and The __declspec() keyword in Microsoft's docs. But no where can I find that it should be ignored.

Everything is working fine for now, so I'm mostly just asking out of curiosity, but I'm also somewhat worried that if this is undefined behavior, then I should fix it now before it causes harder to diagnose problems later.

0

There are 0 best solutions below