简体   繁体   English

WINAPI - 编译时出错LNK2019,LNK2001,LNK1120。

[英]WINAPI - error LNK2019, LNK2001, LNK1120 when compiling.

Very new to WINAPI so be gentle. 对WINAPI来说很新,所以要温柔。

I have read the "Windows via C/C++" book by Jeffrey Richter and now I'm trying to do some of the basic DLL stuff he descibes in the book. 我已经读过Jeffrey Richter的“Windows via C / C ++”一书,现在我正在尝试做一些他在书中描述的基本DLL内容。

In Chapter 19 he make a simple example. 在第19章中,他举了一个简单的例子。 I have tried to make the example, but I keep getting these three errors when building the project: 我试图制作这个例子,但是在构建项目时我一直遇到这三个错误:

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

I have three files: 我有三个文件:

DLLChapter19.h : 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 : 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);
}

And then (in another project, but in the same solution). 然后(在另一个项目中,但在相同的解决方案中)。

DLLChapter19EXE.cpp : 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);
}

Why am I getting these three errors? 为什么我会收到这三个错误? I have look at the DLLChapter19.dll via 'DUMPBIN -exports' and it looks fine, with the 2 exported symbols: 我通过'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

I have searched and searched but could not find the solution for my problem. 我搜索和搜索但找不到解决问题的方法。

This is a linker error when compiling the executable. 编译可执行文件时,这是链接器错误。 The DLL is fine, but you have not told the linker how to link to it. DLL很好,但你没有告诉链接器如何链接到它。 You need to pass to the linker the import library (the .lib file) that was created when you built your DLL. 您需要将构建DLL时创建的导入库(.lib文件)传递给链接器。

I take it you are using Visual Studio. 我认为你使用的是Visual Studio。 In which case add your import library to the Additional Library Dependencies setting in the project configuration for your executable. 在这种情况下,将导入库添加到可执行文件的项目配置中的Additional Library Dependencies设置中。

the answer given by "David Heffernan" worked for me. “David Heffernan”给出的答案为我工作。 You need to add your lib path to your linker path, at: {right click on your project} -> properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> Edit. 您需要将lib路径添加到链接器路径,位于:{右键单击项目} - >属性 - >配置属性 - >链接器 - >输入 - >其他依赖项 - >编辑。

thanks again "David Heffernan". 再次感谢“David Heffernan”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM