locked
Linking errors while linking a dll in c++ RRS feed

  • Question

  • Hi All,

    I am bit of a new to the C++ development in 'Windows platform' and is still in the process to get my hands free in the platform.
    I am using Microsoft Visual Studio 2005.

    Of late , I have been trying to generate a DLL and link it to an exe but in vain.
    The problem description is as follows:

    An exe was using a static lib (e.g. a.lib) while compiling and the the executable was compiling and linking fine and the exe was generated.
    I was trying to convert a.lib to a.dll and then link the executable exposing only "few" functions of a.lib.
    Now,
     The dll got generated,but when I am trying to link it, it is giving "error LNK2019: unresolved external symbol" to the functions which are "not" exposed but are still used in the exe.

     I have done the following steps to resolve it but the error still exists:
      1: Copied the a.lib and a.dll which were generated to the exe folder. (The DLL and the EXE and different projects)
      2: Added the full path along with the a.lib to Configuration Properties -> Linker -> Input -> Additional Dependencies.
      3: Right-click the "project" icon in solution explorer -> "Add" -> "Existing Item" and added the a.lib to it.
     
    Please let me know where I am going wrong or what am I missing to resolve the errors.

    Thanks in advance.

    Wednesday, December 14, 2011 1:33 PM

Answers

  • Multiply is defined inside the same class but not exported using __declspec(dllexport) ( i.e. it wont be exposed).
     
    So , we export the whole class and not a few functions inside the class. Right?
     
    And lets say,in the above case, if Multiply() is not exposed then there is no way we can use Multiply() outside the DLL ,in this case ,there is no way we can use Multiply() in the exe ?
    Yes, if Multiply() is not exported, how do you expect it to be used outside the DLL? What do you think the word export means (in this context)?
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shubh.das Wednesday, December 14, 2011 6:33 PM
    Wednesday, December 14, 2011 5:33 PM
  • Thanks David.
     
    I was actually under the impression that the functions which are 'not' exposed, they can be linked statically (lib file is generated) and the functions which are exposed will be linked dynamically(through DLL).
    The .lib file that is generated when you compile a DLL is called an import library. It does not contain any implementation code, but rather tells the program where to find the implementation at run time (in the DLL).
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shubh.das Wednesday, December 14, 2011 6:33 PM
    Wednesday, December 14, 2011 6:07 PM

All replies

  • Please let me know where I am going wrong or what am I missing to resolve the errors.
    Usually this is done most easily by making the .exe dependent on the .dll and using
     
    Project Properties->Configuration Properties->Linker->General->Link Library Dependencies->Yes.
     
    Have you tried that?
     

    David Wilkinson | Visual C++ MVP
    Wednesday, December 14, 2011 1:44 PM
  • Yes, I tried this option , but the problem still exists. :(

    I opened the a.lib file (using notepad ++) which got generated along with the a.dll , but looks like that contains only the mapping for the functions which are exposed in the DLL.

    But the "error LNK2019: unresolved external symbol" are to the functions which are "not" exposed by the DLL but are still used in the exe.

     

    Wednesday, December 14, 2011 1:54 PM
  • For Eg:

    I have a file wherein

     

     

    class MyMathFuncs

    {

     

    public:
     

     

    static __declspec(dllexport) double Add(double a, doubleb);
    static __declspec(dllexport) double Subtract(double a, double b);
    static __declspec(dllexport) double Divide(double a, double b);

     

    static double Multiply(double a, double b);

    };

    Now when I link the  MathFunc.dll to my exe,
    I get the following error:
    error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Multiply(double,double)"

    The function which is not exposed. So how can we resolve this?

    Wednesday, December 14, 2011 2:57 PM
  • Now when I link the MathFunc.dll to my exe,
    I get the following error:
    error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Multiply(double,double)"
     
    The function which is not exposed. So how can we resolve this?
    I guess I am confused. If Multiply() is not defined in (or exported from) the DLL, where is it defined?
     
    Normally, you will export the whole class, and put the __declspec(dllexport) on the whole class:
     
    class __declspec(dllexport) MyMathFuncs
    {
     
    };
     
    Also you need to arrange to be using __declspec(dllimport) when you use your header in the EXE project. This is normally done with an #ifdef:
     
    #ifdef LIBRARY_EXPORTS
    # define LIBRARY_API __declspec(dllexport)
    #else
    # define LIBRARY_API __declspec(dllimport)
    #endif
     
    But maybe I am not understanding your question.
     

    David Wilkinson | Visual C++ MVP
    Wednesday, December 14, 2011 3:40 PM
  • Now when I link the MathFunc.dll to my exe,
    I get the following error:
    error LNK2019: unresolved external symbol "public: static double __cdecl MathFuncs::MyMathFuncs::Multiply(double,double)"
     
    The function which is not exposed. So how can we resolve this?
    I guess I am confused. If Multiply() is not defined in (or exported from) the DLL, where is it defined?
     
    Normally, you will export the whole class, and put the __declspec(dllexport) on the whole class:
     
    class __declspec(dllexport) MyMathFuncs
    {
     
    };
     
    Also you need to arrange to be using __declspec(dllimport) when you use your header in the EXE project. This is normally done with an #ifdef:
     
    #ifdef LIBRARY_EXPORTS
    # define LIBRARY_API __declspec(dllexport)
    #else
    # define LIBRARY_API __declspec(dllimport)
    #endif
     
    But maybe I am not understanding your question.
     

    David Wilkinson | Visual C++ MVP

    Hi David,

    Multiply is defined inside the same class but not exported using __declspec(dllexport) ( i.e. it wont be exposed).

    So , we export the whole class and not a few functions inside the class. Right?


    And lets say,in the above case, if Multiply() is not exposed then there is no way we can use Multiply() outside the DLL ,in this case ,there is no way we can use Multiply() in the exe ?

    Please let me know if my understanding is correct .

    Wednesday, December 14, 2011 5:18 PM
  • Multiply is defined inside the same class but not exported using __declspec(dllexport) ( i.e. it wont be exposed).
     
    So , we export the whole class and not a few functions inside the class. Right?
     
    And lets say,in the above case, if Multiply() is not exposed then there is no way we can use Multiply() outside the DLL ,in this case ,there is no way we can use Multiply() in the exe ?
    Yes, if Multiply() is not exported, how do you expect it to be used outside the DLL? What do you think the word export means (in this context)?
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shubh.das Wednesday, December 14, 2011 6:33 PM
    Wednesday, December 14, 2011 5:33 PM
  • Thanks David.

    I was actually under the impression that the functions which are 'not' exposed, they can be linked statically (lib file is generated) and the functions which are exposed will be linked dynamically(through DLL).

    But looks like that is not what is happening actually.

    I hope I am correct.


    • Edited by shubh.das Wednesday, December 14, 2011 5:56 PM
    Wednesday, December 14, 2011 5:53 PM
  • Thanks David.
     
    I was actually under the impression that the functions which are 'not' exposed, they can be linked statically (lib file is generated) and the functions which are exposed will be linked dynamically(through DLL).
    The .lib file that is generated when you compile a DLL is called an import library. It does not contain any implementation code, but rather tells the program where to find the implementation at run time (in the DLL).
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shubh.das Wednesday, December 14, 2011 6:33 PM
    Wednesday, December 14, 2011 6:07 PM