My observation: C++ Linker(Visual Studio specifically) takes __declspec(dllimport) too seriously, the linker looks up for definitions of any class/function declared with this directive, in the attached libraries ONLY. (Ignoring the definitions present locally). I want to give __declspec(dllimport) directive lesser priority so that the linker first considers the locally present definitions before looking elsewhere. is it possible to do so? Also is it a Windows Specific/ Visual Studio Specific Issue? if so is there a workaround?
I encountered this issue while trying to use the precompiled MathGL-v2.4.2 binaries on Visual Studio 2015, Windows.
Initial Builds gave many Linker Error 2019: unresolved external symbol __declspec(dllimport) _cdel mglGraph::mglGraph(void).... blah blah.
Further, I went through the Library code to find that Every Class and Struct definitions are of the form:
class MGL_EXPORT mglGraph {
// some variables and
// Full function definitions
// (also contains some inline functions)
};
Where MGL_EXPORT is defined as,
/* in dllexport.h the following is declared */
# ifdef mgl_EXPORTS
/* We are building this library */
# define MGL_EXPORT __declspec(dllexport)
# else
/* We are using this library */
# define MGL_EXPORT __declspec(dllimport)
# endif
also, I looked up the libmgl.dll Exported Symbols, those symbols that the linker is looking for don't exist(hence naturally Linker gives unresolved Symbol Error).
From all the Above, Conclusion: Linker Ignores any local Definition of class/function and Only Looks-up the Attached Libraries for Function Definitions, of the class/function declared with __declspec(dllimport) directive.
More proof: By removing the MGL_EXPORT from the definition of completely defined functions/classes the code compiles
Now the Question is: is this a platform specific problem? if yes is there a workaround? (as it is very cumbersome to carefully go through every class in the library to make sure that all the functions are completely defined before removing MGL_EXPORT directive).
"I want to give __declspec(dllimport) directive lesser priority so that the linker first considers the locally present definitions before looking elsewhere" This very elegantly solves the problem.