Forcing symbol export with MSVC

后端 未结 5 535
我寻月下人不归
我寻月下人不归 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: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 
    int main() { static_lib::afunction(1); }
    

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

提交回复
热议问题