Search code examples
c++functiondllexport

Export function to DLL without class


Is there a way to export only a function to DLL cos in tutorials they always export classes with something like:

 static __declspec(dllexport) double Add(double a, double b);

Inside a class the statement above does not cause any problem, but without a class it gives:

 dllexport/dllimport requires external linkage

Solution

  • The problem is the "static" qualifier. You need to remove it because it means the wrong thing in this context. Try just:

    __declspec(dllexport) double Add(double a, double b);
    

    That's what you need to have in your header file when compiling the DLL. Now to access the function from a program that uses the DLL, you need to have a header file with this:

    double Add(double a, double b);
    

    You can use the same header file for both purposes if you use #ifdefs:

    #ifndef MYDLL_EXPORT
      #define MYDLL_EXPORT
    #endif
    
    MYDLL_EXPORT double Add(double a, double b);