Search code examples
c++cwindowsdllstatic-linking

LNK2019: unresolved external symbol -- Implicit DLL


There are many questions which are similar to this, but I have almost done everything to resolve the issue, but nothing seems to work

I have created a small simple Dll.

declarations:

void func_A()
void func_B()

There is some other stuff also, I have given it a C++ filename, but its basically C program only

I have written .def file

LIBRARY "myLib.dll"

EXPORTS
    func_A          @1
    func_B          @2

Dll is created successfully.

I have made sure that .def file is there in properties->linker->input->module definition file

I have also checked for exported functions using Dumpbin, all is good so far

Now In client code, I have written header like

 extern "C" __declspec(dllimport) void func_A();
 extern "C" __declspec(dllimport) void func_B();

which indeed satisfies the compiler,

Have copied .lib and .dll from DLL project to client project, Have kept .lib where .obj files are generated and have kept .dll where .exe will be generated.

But I get LNK2019: unresolved external symbol for dll function calls

I think I am sure that .lib is found by the linker, because if I remove Addition Library Dependencies I get .lib file not found error from linker.

As nothing was working, I have also tries using __declspec(dllexport) while creating a .dll, but to no avail


Solution

  • Now In client code, I have written header like

    Too late. You've already compiled the library as a C++ file with C++ names. What you do in the client code will not make any difference.

    The probable issue is your very first step, and that is compiling the DLL using these function names:

    void func_A();
    void func_B();
    

    If you compile this as a C++ file, those names will be mangled. If those names are mangled, then your DEF file has to have the mangled names exported, not the "clean" names.

    To make sure those names are not mangled, the proper way to do it would be as follows:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void func_A();
    void func_B();
    
    #ifdef __cplusplus
    }
    #endif
    

    The names are now not mangled when compiling as a C++ module, and the names in the DEF file now match the actual names of the functions you're exporting.