Is there a better way to load a dll in C++?

前端 未结 7 1339
北海茫月
北海茫月 2020-12-13 16:46

Right now I do something like this and it seems messy if I end having a lot of functions I want to reference in my DLL. Is there a better and cleaner way of accessing the fu

7条回答
  •  攒了一身酷
    2020-12-13 17:03

    After building your .dll get the .lib file nearby and link your test application with it. Use functions as they are declared in .h

    There's a minor change you need to do in your header file:

    #ifdef EXPORTS_API
      #define MY_API_EXPORT __declspec (dllexport)
    #else
      #define MY_API_EXPORT __declspec (dllimport)
    #endif
    
    extern "C" {
        int MY_API_EXPORT Factorial(int n);
    
        // do the same for other functions
    }
    

    This way, when building your dll you define EXPORTS_API in your project settings and functions get exported, in the client application, no need to define anything.

提交回复
热议问题