I'm using visual studio 2010 to build a .dll. I wrote up a trial as:
// trialDLL.h
#ifndef TRIALDLL_H_
#define TRIALDLL_H_
// ... MyMathFuncs class definition omitted
#ifdef __cplusplus
extern "C"{
#endif
#ifdef TRIALDLL_EXPORT
#define TRIALDLL_API __declspec(dllexport)
#else
#define TRIALDLL_API __declspec(dllimport)
#endif
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset);
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath);
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs* myMath, double a, double b);
// some other similar stuff
#ifdef __cplusplus
}
#endif
#endif
And the triallDLL.cpp file:
// trialDLL.cpp
#include "trialDLL.h"
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset)
{
return new MyMathFuncs(offset);
}
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath)
{
delete myMath;
}
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs *myMath, double a, double b)
{
return myMath->Add(a, b);
}
// ... some other definitions
With these two files in the project, I added a property sheet to the project through visual studio 2010 property manager and added TRIALDLL_EXPORT
to user macros. After all these, the nice Intellisense gives me error for each function defined in the .cpp file and complains "error: a function declared 'dllimport' may not be defined". So it appears that Intellisense doesn't find TRIALDLL_EXPORT
defined. I thought it might make a difference if I actually build the project, but the result suggests the same error: "error C2491: 'new_MyMathFuncs' : definition of dllimport function not allowed". Then it is clear that the macro TRIALDLL_EXPORT
is still not defined in compiling time.
After failing to add macro through visual studio, I also tried putting code line: #define TRIALDLL_EXPORT
in trialDLL.cpp but it didn't help either. I wonder what's the proper way to do this? How do I inform the compiler that the micro is defined so that TRIALDLL_API
evaluates to dllexport
rather than dllimport
?
Also, if I can build the .dll successfully, is there any systematic way to test/verify the functionality of the .dll?
Thanks for any help in advance! (Although I know it's an issue here on stackoverflow to put appreciation in the question, I feel myself impolite not to do so. Forgive me for any inefficiency caused by these lines.)
"User macros" in VS property sheets have nothing to do with preprocessor macros. Put TRIALDLL_EXPORT
into the property sheet's section C/C++ > Preprocessor > Preprocessor Definitions
"User macros," which can only be defined in property sheets, allow you to create your own "variables" usable in Visual Studio properties, similar to the built-in ones $(TargetName)
, $(SolutionDir)
etc.
Put
#error Where is my macro?
in the #else
block of the header. Then experiment with the project settings or the #define
until you get it right. Did you perhaps only add the property sheet to one configuration? Did you put the #define
at the very top of the file? Do you have any PCH stuff that causes it to ignore your settings? And so on.
As said in this Microsoft article, you cannot apply the __declspec(dllimport)
keyword to implement a function. You should use it only in the declaration. For example:
// function declaration
void __declspec(dllimport) funcB();
// function definition
void funcB() {
//funcB code
}
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