Question

I cannot implicitly link DLL to C console application. I use Visual Studio 2008.

I created empty DLL project "Library" which contains only one file main.c:

__declspec(dllexport) int get_value()
{
    return 123;
}

I also created empty console project "CallingProgram" with file main.c:

__declspec(dllimport) int get_value();

void main()
{
    int result = get_value();
}

I added "Library.lib" to "Linker\Input\Additional Dependencies".

And still I have this error:

error LNK2019: unresolved external symbol __imp__get_value referenced in function _main

I tested created DLL with LoadLibrary/GetProcAddress - it works fine.

I checked Library.dll using DumpBin and it also looks good:

Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file library.dll

File Type: DLL

  Section contains the following exports for Library.dll

    00000000 characteristics
    5340072C time date stamp Sat Apr 05 17:37:48 2014
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00011109 get_value = @ILT+260(_get_value)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

Please help me understand what is missing!

Was it helpful?

Solution

1 0 00011109 get_value

The symbol does not have its normal decoration. It would normally be _get_value, all cdecl functions get a leading underscore. And using __declspec(dllexport) also provides the __imp_get_value export. It is a function pointer that optimizes the binding.

But that did not happen, you must have used a .def file in your library project. Which renames exported functions. Which is okay, but now your __declspec(dllimport) is incompatible, the DLL no longer exports the __imp_ function pointer. The linker complains because it cannot find it the import library.

Fix this either by removing the .def file from your library project (best) or by deleting the __declspec(dllimport) attribute from the declaration in your exe project. Writing a .h file that declares the exported functions in the DLL is also highly recommended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top