I am trying to write a dll,this is how looks my header file:
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec 开发者_Python百科(dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
#endif /* _DLL_H_ */
In the .cpp file I include this header file,and I try declaring a dll import procedure this way:
DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello World from DLL!n", "Hi", MB_ICONINFORMATION);
}
But the compiler ( I have mingw32 on windows 7 64 bit) keeps giving me this error:
E:\Cpp\Sys64\main.cpp|7|error: function 'void HelloWorld()' definition is marked dllimport|
E:\Cpp\Sys64\main.cpp||In function 'void HelloWorld()':|
E:\Cpp\Sys64\main.cpp|7|warning: 'void HelloWorld()' redeclared without dllimport attribute: previous dllimport ignored|
||=== Build finished: 1 errors, 1 warnings ===|
And I don't understand why.
The declspec(dllimport)
generates entries in the module import table of the module. This import table is used to resolve the referneces to the symbols at link time. At load time these references are fixed by the loader.
The declspec(dllexport)
generates entries in the DLL export table of the DLL. Further you need to implement symbols (function, variables) that are declare with it.
Since you you implement the DLL, you must define BUILDING_DLL. This could be done with #define
but this should be better set in the project settings.
I had the exact same error before realizing that I didn't actually define BUILDING_DLL
.
Therefore, DLLIMPORT
was defined as __declspec (dllimport)
and not __declspec (dllexport)
as it was intended. After I defined the symbol, the problem was solved.
Since you're on MinGW, you need to pass the following:
-DBUILDING_DLL
as a compiler option, or simply add
#define BUILDING_DLL
at the top of your file. The former is better, only use the #define solution if you can't figure out how to pass the -DBUILDING_DLL
option to gcc.
精彩评论