About inconsistent dll linkage

Solution 1:

The purpose of the preprocessor statements:

#ifdef _GUICTRLS 
   #define GUI_CTRLS_EXPORT __declspec(dllexport) 
#else 
   #define GUI_CTRLS_EXPORT  __declspec(dllimport) 
#endif 

is to make sure that the header file declares the class or function as __declspec(dllexport) in the .dll where it is defined, and as __declspec(dllimport) for any other .dll that might want to use it.

For this to work, _GUICTRLS must be defined when compiling the exporting .dll, and not defined for any other .dll. Generally you would expect _GUICTRLS to be defined in the project properties, under C/C++ -> Preprocessor -> Preprocessor Definitions.

The compiler error you are seeing usually happens because either _GUICTRLS is not defined for the project that is doing the export, or it is defined for multiple projects, usually resulting from cutting an pasting from one project to another. You will also see this if _GUICTRLS is defined in a header file that is included in multiple projects.

Solution 2:

There are multiple possibilities:

1) static AFX_EXTENSION_MODULE GuiCtrlsDLL = { NULL, NULL };

You use AFX_EXTENSION_MODULE. This means that you are implementing an MFC extension DLL. For such extension dlls you have to define the preprocessor _AFXEXT. Set this in the C++ compiler settings of your Visual C++ project

see:

How To Use _declspec(dllexport) in an MFC Extension DLL: http://support.microsoft.com/kb/128199

AFX_EXTENSION_MODULE Structure: http://msdn.microsoft.com/en-us/library/sxfyk0zk.aspx

TN033: DLL Version of MFC: http://msdn.microsoft.com/en-us/library/hw85e4bb.aspx

2) It is likely that you have a duplicated definiton/declaration.

Solution 3:

In addition to reading the warning message, pay attention to where it occurs if you have multiple projects as part of a workspace.

I wasted time looking for a problem in my DLL which was compiling and linking correctly. The workspace was also building the main application and my error was that I had inadvertently included a new (DLL) source file into the build file list of the application itself.

The main program requires the DLL header mynewdll.h to import things but does not require the source file mynewdll.cpp. (The code is brought in at run time with a DLL.) I have a habit of including header and code files into projects as a pair, and this is where I had gone wrong.

I would have detected the error much sooner if I had been alert and noticed that the DLL project linked with no errors and it was the main program that complained!

My DLL source code and project was error free and it was only the way I tried to build my executable that was faulty.