Forcing symbol export with MSVC

后端 未结 5 533
我寻月下人不归
我寻月下人不归 2020-12-25 08:38

I have a application and several plugins in DLL files. The plugins use symbols from the application via a export library. The application links in several static libraries a

相关标签:
5条回答
  • 2020-12-25 09:16

    You probably want to look at __declspec(export/import)

    #ifdef DLL_EXPORTING
    #define WHDLL __declspec(dllexport)
    #else
    #define WHDLL __declspec(dllimport)
    #endif
    

    When linking static module into a dll it will only bring in the code that is used. I've never imported stuff from a static lib to simply re export it.
    Perhaps you just need to mark it as exportable in the dll when compiling the static lib.

    But that reminds me of putting std containers into exported classes and using some trickery in msvc to export the 'instance' of the specialised container. the template code is similar to your static code (in my thinking)

    for instance without the template you get warnings the template code is not exported to support the class - this is MSVC specific from my understanding

    template class DLL_EXPORTING std::auto_ptr<wxCursor>;
    class DLL_EXPORTING imageButton : public wxWindow
    {
        std::auto_ptr<wxCursor> m_Cursor;
    };
    
    0 讨论(0)
  • 2020-12-25 09:17

    The "Use Library Dependency Inputs" option does the trick in VS2005!

    This option can be found under Configuration Properties -> Linker -> General -> Use Library Dependency Inputs. Set to "true" to force linking in ALL symbols & code declared in every LIB specified as input to the project.

    You can do the following to get the symbol to export from the DLL: define LIB_EXPORTS in the library project and nothing in either the DLL project or the DLL client project.

    #ifdef LIB_EXPORTS
    #define DLLAPI __declspec(dllexport)
    #else
    #define DLLAPI __declspec(dllimport)
    #endif
    

    Turns out there is no need #include any headers from the LIB project when compiling the DLL project; just specify the LIB as a linker input. However, if you need to make use of the LIB code from within the DLL, you will need to #define DLLAPI as an empty macro; setting the symbol(s) to either dllexport or dllimport will generate an error or a warning, respectively.

    0 讨论(0)
  • 2020-12-25 09:24

    What I tried out to solve this was this:

    1. build a static library with function void afunction( int ).
    2. build a dll, linked to static lib, exporting afunction.
    3. build an exe, using the afunction symbol.

    How? Since the linker can be told to export functions using the __declspec(dllexport) directive, a dll needs no more than declare a to-be-exported symbol thusly.

    The lib has a header "afunction.h" and an accompanying cpp file containing the function body:

    // stat/afunction.h
    namespace static_lib { void afunction(int); }
    
    
    // stat/afunction.cpp
    #include "afunction.h"
    namespace static_lib { void afunction(int){ } }
    

    The dll has an include file "indirect.h", containing the declaration of the function to be exported. The dll has a link-time dependency to the static lib. (Linker options: Input/Additional Dependencies: "static_library.lib")

    // dll/indirect.h
    namespace static_lib {
      __declspec( dllexport ) void afunction(int);
    }
    

    The executable has only the indirectly included file:

    #include <dll/indirect.h>
    int main() { static_lib::afunction(1); }
    

    And guess what? It compiles, links and even runs!

    0 讨论(0)
  • 2020-12-25 09:33

    There is some discussion of this problem on MSDN which was pretty useful. As it turns out, /OPT:NOREF is not particularly helpful in this case. /INCLUDE can work but it can be hard to automatically figure out what needs to be /INCLUDEd. There's unfortunately no silver bullet.

    http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/2aa2e1b7-6677-4986-99cc-62f463c94ef3

    0 讨论(0)
  • 2020-12-25 09:38

    The /INCLUDE directive can be used to force the MSVC linker to include a symbol. Alternatively, /OPT:NOREF can be used to disable removal of unused symbols in general.

    A common approach is to create a single unused function that references all objects exported for your plugins. Then you only need a single /INCLUDE directive for that function.

    0 讨论(0)
提交回复
热议问题