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
You can export an entire C++ class by placing the __declspec(dllexport) before the class name, or you can export a single method by placing __declspec(dllexport) before the method name. Make class methods static and public.
The __declspec(dllexport) attribute exports the definition of a symbol through the dynamic symbol table when building DLL libraries.
__declspecThe extended attribute syntax simplifies and standardizes Microsoft-specific extensions to the C and C++ languages.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With