首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WINAPI -编译时出错的LNK2019,LNK2001,LNK1120 .

WINAPI -编译时出错的LNK2019,LNK2001,LNK1120 .
EN

Stack Overflow用户
提问于 2013-05-04 12:26:37
回答 2查看 1.5K关注 0票数 3

非常新的WINAPI,所以要温柔。

我读过Jeffrey的“通过C/C++的Windows”一书,现在我正在尝试做一些他在书中删除的基本DLL内容。

在第19章,他做了一个简单的例子。我试着做了一个例子,但是在构建项目时,我一直遇到以下三个错误:

代码语言:javascript
复制
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第19.h章:

代码语言:javascript
复制
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft, int nRight);

DLLChapter19.cpp第19章:

代码语言:javascript
复制
//#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 :

代码语言:javascript
复制
//#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‘查看了DLLChap19.dll,它看起来很好,有两个导出的符号:

代码语言:javascript
复制
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

我找了又找,却找不到解决问题的办法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-04 12:31:21

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

我想你是在使用。在这种情况下,将导入库添加到可执行文件的项目配置中的“附加库依赖”设置中。

票数 2
EN

Stack Overflow用户

发布于 2014-12-09 14:41:10

“大卫·赫弗南”给出的答案对我有用。您需要将库路径添加到链接器路径中,在:{右键单击项目} ->属性->配置属性-> linker -> Input ->附加依赖项->编辑。

再次感谢“大卫·赫弗南”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16374289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档