0

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.

kjhayes
  • 143
  • 7
  • 1
    Although you normally use `dllexport` in a DLL module, you can use it in any module. The function can then be used (imported) by other modules. So it is legal to use `dllexport` in an executable or static library. It might increase the size of the module though. – rveerd Sep 06 '21 at 14:57
  • Why are you defining `SHARED` in a static library? Did you check the exports of your final exe/dll? It probably has all those functions exported. – rustyx Sep 06 '21 at 15:56
  • 1
    It has no dramatic consequences, but is not completely ignored. The MSFT linker produces a .lib and .exp file when you build the final executable, even though you have no use for them. And you'll get less efficient code when the attributes are applied to data, the compiler assumes the data needs to be accessed through a pointer. – Hans Passant Sep 06 '21 at 16:05

0 Answers0