DLL 隐式链接

wal*_*ruz 2 c dll dllimport dllexport

我无法将 DLL 隐式链接到 C 控制台应用程序。我使用 Visual Studio 2008。

我创建了空的 DLL 项目“Library”,其中仅包含一个文件 main.c:

__declspec(dllexport) int get_value()
{
    return 123;
}
Run Code Online (Sandbox Code Playgroud)

我还使用文件 main.c 创建了空控制台项目“CallingProgram”:

__declspec(dllimport) int get_value();

void main()
{
    int result = get_value();
}
Run Code Online (Sandbox Code Playgroud)

我将“Library.lib”添加到“Linker\Input\Additional Dependency”中。

我仍然有这个错误:

error LNK2019: unresolved external symbol __imp__get_value referenced in function _main
Run Code Online (Sandbox Code Playgroud)

我测试了使用 LoadLibrary/GetProcAddress 创建的 DLL - 它工作正常。

我使用 DumpBin 检查了 Library.dll,它看起来也不错:

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
Run Code Online (Sandbox Code Playgroud)

请帮助我了解缺少什么!

Han*_*ant 5

1 0 00011109 获取值

该符号没有其正常的装饰。通常_get_value,所有 cdecl 函数都有一个前导下划线。并且使用__declspec(dllexport)还提供__imp_get_value出口。它是一个优化绑定的函数指针。

但这并没有发生,您一定在库项目中使用了 .def 文件。它重命名导出的函数。没关系,但现在你的__declspec(dllimport)不兼容,DLL 不再导出__imp_函数指针。链接器抱怨因为它找不到导入库。

通过从库项目中删除 .def 文件(最好)或从 exe 项目的声明中删除 __declspec(dllimport) 属性来修复此问题。强烈建议编写一个 .h 文件来声明 DLL 中的导出函数。