c++ - WINAPI - 编译时出现错误 LNK2019、LNK2001、LNK1120。

标签 c++ visual-studio

WINAPI 非常新,所以要温和。

我已经阅读了 Jeffrey Richter 的“Windows via C/C++”一书,现在我正在尝试做他在书中描述的一些基本 DLL 内容。

在第 19 章中,他举了一个简单的例子。我已经尝试制作示例,但是在构建项目时我不断遇到这三个错误:

Error   1   error LNK2019: unresolved external symbol __imp__Add referenced in function _wWinMain@16    
Error   2   error LNK2001: unresolved external symbol __imp__g_nResult
Error   3   error LNK1120: 2 unresolved externals

我有三个文件:

DLLChapter19.h :

#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft, int nRight);

DLLChapter19.cpp :

//#include <Windows.h> //apparently the complier says that I should use stdafx.h instead(?)
#include "stdafx.h"
#define MYLIBAPI extern "C" __declspec(dllexport)
#include "DLLChapter19.h"

int g_nResult;

int Add(int nLeft, int nRight) {
    g_nResult = nLeft + nRight;
    return(g_nResult);
}

然后(在另一个项目中,但在同一个解决方案中)。

DLLChapter19EXE.cpp :

//#include <Windows.h> //apparently the complier says that I should use stdafx.h instead?
#include "stdafx.h"
#include <strsafe.h>
#include <stdlib.h>

#include "C:\Users\Kristensen\Documents\Visual Studio 2012\Projects\DLLChapter19\DLLChapter19\DLLChapter19.h" 

int WINAPI _tWinMain(HINSTANCE , HINSTANCE , LPTSTR, int) {

    int nLeft = 10, nRight = 25;

    TCHAR sz[100];
    StringCchPrintf(sz, _countof(sz), TEXT("%d +%d =%d"),
        nLeft, nRight, Add(nLeft, nRight));
    MessageBox(NULL, sz, TEXT("Calculation"), MB_OK);

    StringCchPrintf(sz, _countof(sz),
        TEXT("The result from the last Add is: %d"), g_nResult);
    MessageBox(NULL, sz, TEXT("Last Result"), MB_OK);
    return(0);
}

为什么我会收到这三个错误?我通过“DUMPBIN -exports”查看了 DLLChapter19.dll,它看起来不错,带有 2 个导出符号:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\amd64>dumpbin -export
s DLLChapter19.dll
Microsoft (R) COFF/PE Dumper Version 11.00.60315.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file DLLChapter19.dll

File Type: DLL

  Section contains the following exports for DLLChapter19.dll

    00000000 characteristics
    5184F8EE time date stamp Sat May 04 14:02:54 2013
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 000110C3 Add
          2    1 00017128 g_nResult

  Summary

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

我搜索了又搜索,但找不到解决我的问题的方法。

最佳答案

这是编译可执行文件时的链接器错误。 DLL 没问题,但您没有告诉链接器如何链接到它。您需要将在构建 DLL 时创建的导入库(.lib 文件)传递给链接器。

我认为您使用的是 Visual Studio。在这种情况下,将导入库添加到可执行文件的项目配置中的附加库依赖项设置中。

关于c++ - WINAPI - 编译时出现错误 LNK2019、LNK2001、LNK1120。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374289/

相关文章:

c++ - 使用 SecureZeroMemory 新建/删除问题

visual-studio - 更改智能标签的颜色

visual-studio-2010 - Visual Studio 资源粘贴栏

visual-studio - 清理项目时删除附加文件

实例化自定义对象的 Javascript///<field type=?>

c++ - 加入std::variant <a,b,c>和std::variant <x,y,z>的类型

c++ - 递归删除二叉树中的每个节点

c++ - 如何在 C++ 上获取文件夹及其子文件夹名称和路径中的所有文件到字符串数组?

c++ - 无效指针参数,传递缓冲区

c# - 需要解决 Microsoft.QualityTools.Testing.Fakes