locked
error LNK2019: unresolved external symbol to an exported function RRS feed

  • Question

  • I am getting this error when I am trying to link to the import library of my dll. The function is declared in the DLL as:

    MyClass.h

    extern "C" __declspec(dllexport) MyClass* GetMyClassInstance();

    I checked the exports from the dll and this is what I got:
    dumpbin /exports MyClass.dll
    ...
    4 000036A0 GetMyClassInstance

    dumpbin /exports MyClass.lib
    ...
    _GetMyClassInstance

    Don't understand why the import library has an (_). Now, in my main c++ program I am including the header and linking to its import library as follows:

    #include "MyClass.h"

    int _cdecl _tmain(...)
    {  
    MyClass* ptr = GetMyClassInstance();
    return 0;
    }

    Error: error LNK2019: unresolved external symbol _GetMyClassInstance@12 referenced in _wmain

    Any idea what is happening here ? I am using VS2010 Win32 Release configuration.




    • Edited by Shawn Dan Tuesday, August 14, 2012 12:00 AM
    Monday, August 13, 2012 11:05 PM

Answers

  • On 8/13/2012 8:01 PM, Shawn Dan wrote:

    I edited the question. I am only including the header file (i.e MyClass.h) where the exported function is declared. This header file is used by the dll implementation code as well.

    So you do after all specify __declspec(dllexport) when building an EXE that has no intention of exporting the function. You shouldn't.

    Also, _GetMyClassInstance looks like name mangling for __cdecl calling convention, while _GetMyClassInstance@12 looks like one for __stdcall calling convention (for a function taking 12 bytes worth of parameters - are you sure the code you show is exactly the same as the code you are building?) Have you changed the default calling convention in one project but not the other, by any chance? That would be Project | Properties | C/C++ | Advanced | Calling Convention


    Igor Tandetnik

    • Marked as answer by Shawn Dan Tuesday, August 14, 2012 4:55 PM
    Tuesday, August 14, 2012 12:12 AM

All replies

  • On 8/13/2012 7:05 PM, Shawn Dan wrote:

    I am getting this error when I am trying to link to the import library of my dll. The function is declared in the DLL as:

    extern "C" __declspec(dllexport) MyClass* GetMyClassInstance();

    Error: error LNK2019: unresolved external symbol _GetMyClassInstance@12 <mailto:_GetMyClassInstance@12> referenced in _wmain

    The function is marked extern "C" when the DLL is built, but not when the EXE is built. Look for discrepancies in the two declarations.


    Igor Tandetnik

    Monday, August 13, 2012 11:14 PM
  • What does that mean ? I am not calling the function using (_) neither am I re-declaring it in my main program.
    Monday, August 13, 2012 11:42 PM
  • On 8/13/2012 7:42 PM, Shawn Dan wrote:

    What does that mean ? I am not calling the function using (_) neither am I re-declaring it in my main program.

    The declaration in the DLL has __declspec(dllexport) in it. Are you saying you use the very same declaration in your EXE, dllexport and all?


    Igor Tandetnik

    Monday, August 13, 2012 11:46 PM
  • No, the declaration of that exported function is in the dll itself.

    All I want to do is call that exported function in my application. To do that I linked to its import library and called the function as shown in my question but it gives that error.
    Monday, August 13, 2012 11:49 PM
  • On 8/13/2012 7:49 PM, Shawn Dan wrote:

    No, the declaration of that exported function is in the dll itself.

    All I want to do is call that exported function in my application. To do that I linked to its import library and called the function as shown in my question but it gives that error.

    Before you could call the function, you had to declare it. Show what this declaration looks like.


    Igor Tandetnik

    Monday, August 13, 2012 11:55 PM
  • I edited the question. I am only including the header file (i.e MyClass.h) where the exported function is declared. This header file is used by the dll implementation code as well.

    Tuesday, August 14, 2012 12:01 AM
  • On 8/13/2012 8:01 PM, Shawn Dan wrote:

    I edited the question. I am only including the header file (i.e MyClass.h) where the exported function is declared. This header file is used by the dll implementation code as well.

    So you do after all specify __declspec(dllexport) when building an EXE that has no intention of exporting the function. You shouldn't.

    Also, _GetMyClassInstance looks like name mangling for __cdecl calling convention, while _GetMyClassInstance@12 looks like one for __stdcall calling convention (for a function taking 12 bytes worth of parameters - are you sure the code you show is exactly the same as the code you are building?) Have you changed the default calling convention in one project but not the other, by any chance? That would be Project | Properties | C/C++ | Advanced | Calling Convention


    Igor Tandetnik

    • Marked as answer by Shawn Dan Tuesday, August 14, 2012 4:55 PM
    Tuesday, August 14, 2012 12:12 AM