Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 1000
Shibli Avatar asked Feb 27 '12 00:02

Shibli


People also ask

How do I export a function in C++?

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.

What does __ Declspec Dllexport do?

The __declspec(dllexport) attribute exports the definition of a symbol through the dynamic symbol table when building DLL libraries.

What is __ Declspec in C++?

__declspecThe extended attribute syntax simplifies and standardizes Microsoft-specific extensions to the C and C++ languages.


1 Answers

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);
like image 164
David Grayson Avatar answered Nov 03 '22 02:11

David Grayson