1

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.

  • 1
    Welcome to Stackoverflow. What are you considering to be an "attached library" verses "locally present"? – ThomasMcLeod Jan 16 '19 at 21:47
  • `__declspec(dllimport)` means the implementations are in a dll and you will be linking against an import library. – drescherjm Jan 16 '19 at 22:04
  • @ThomasMcLeod "attached Libraries" are the precompiled '.lib' files which I have manually attached as Additional Dependencies, "locally present definitions" are the function definitions that are present in the source file(i.e. in the .cpp/.h file) that I am compiling. For Example: – Sreevatsank Kadaveru Jan 17 '19 at 09:29
  • For Example: class FullyDefined_Class{ // is a fully defined Class, all functions have well defined definitions. ( present locally) int a, b, c; // some variables int Fully_Defined_Function(){ // Full function definitions printf("Example of a fully defined function\n"); return 0; } }; class __declspec(dllimport) ExternallyDefined_Class { // this is a externally defined class, one of its function definitions are not present here int a, b, c; int exteranlly_Defined Function(); }; – Sreevatsank Kadaveru Jan 17 '19 at 09:38
  • sounds like you don't have the linker configured to search for the .lib files. Are they located outside of your project tree? – ThomasMcLeod Jan 17 '19 at 20:21
  • @ThomasMcLeod the linker is configured, it links well with other functions – Sreevatsank Kadaveru Jan 20 '19 at 15:04
  • Other functions might be in other directories. If you're .lib is not in one of several directories that the Linker searches, then you must tell the linker where to look with a directory search argument. Usually, this is `-L` followed by a path. – ThomasMcLeod Jan 20 '19 at 20:21

0 Answers0