C++ 错误 LNK2028、LNK2019 和 LNK1120。它们是什么意思,我该如何解决它们?

标签 c++ visual-studio-2010 visual-c++

我遇到了这三个错误,它们对我来说似乎没什么意义。如果我评论 UserInstruction1(P1, P2, P3);在控制台应用程序中,错误消失了。这两个项目都是/CLR 项目。

error LNK2028: unresolved token (0A000930) "void __cdecl UserInstruction1(double *,wchar_t *,wchar_t *)" (?UserInstruction1@@$$FYAXPANPA_W1@Z) referenced in function "int __cdecl wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)  

error LNK2019: unresolved external symbol "void __cdecl UserInstruction1(double *,wchar_t *,wchar_t *)" (?UserInstruction1@@$$FYAXPANPA_W1@Z) referenced in function "int __cdecl wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)

error LNK1120: 2 unresolved externals   C:\Workspace\Company.Pins\Bank\Source\Debug\Company.Pins.Bank.Win32Console.exe

//Console App.
#include "stdafx.h"
#include "UInstruction.h"



int _tmain(int argc, _TCHAR* argv[])
{
    auto P2 = (TCHAR *)"3 Barrowstead";
    TCHAR* P3;
    double* P1;
    P1[0] = 13;

    UserInstruction1(P1, P2, P3);
    return 0;
}

--

//UInstruction.h
#ifndef __UINSTRUCTION_H__
#include "stdafx.h"
#include "UInstruction.h"
#include "common.h"

#include <iostream>
#include <stdio.h>
#define PRES_NOCOMMAND_FOUND 2000


#define DllExport  __declspec(dllexport)

void ReconcileUHParameter(const double* lpNumeric, TCHAR* lpAlpha1, TCHAR* lpAlpha2);
extern void UserInstruction1(double* lpNumeric,     TCHAR* lpAlpha1, TCHAR* lpAlpha2);

#endif

--

//UInstruction.cpp
#include "stdafx.h"
#include "UInstruction.h"
#include "common.h"
#using "Company.Pins.Bank.Decryption.dll"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;


CPReSInterfaceApp theApp;
extern void UserInstruction1(
                    double* lpNumeric, 
                    TCHAR* lpAlpha1, TCHAR* lpAlpha2)
{
//logic goes here       
}

最佳答案

我在这里假设所有代码都位于一个项目 (Company.Pins.Bank.Win32Console) 中。如果是这样,您应该移动 <\iostream> 和 <\stdio.h> 包含(以及任何其他从不/很少更改为 stdafx.h 的 header :

//stdafx.h

#include <iostream>
#include <stdio.h>


//other headers that are widely used but never/seldom change...

#define DllExport  __declspec(dllexport)
#define DllImport  __declspec(dllimport)

//UInstruction.h

#pragma once //you are in VS 2010...

#include "common.h"

//ommited code for brevity...
void UserInstruction1(double* lpNumeric,     TCHAR* lpAlpha1, TCHAR* lpAlpha2);

//UInstruction.cpp
#include "stdafx.h"
#include "UInstruction.h"

//ommitted code for brevity...

void UserInstruction1( double* lpNumeric, 
                       TCHAR* lpAlpha1, TCHAR* lpAlpha2 )
{
   //logic goes here
}

如果 UserInstruction1 驻留在 Company.Pins.Bank.Win32Console 项目使用的 Dll 中:

确保在 stdafx.h 中为 dll 和控制台项目定义:

#define DllExport  __declspec(dllexport)
#define DllImport  __declspec(dllimport)

打开 DLL 项目的属性,转到“配置属性”->“C/C++”->“预处理器”并向“预处理器定义”添加一个预处理器符号(如果您没有)。 IE。我将其称为 MY_DLL。不要忘记在所有配置中定义它...

确保从 Dll 中导出函数

//UInstruction.h

#pragma once //you are in VS 2010...

#ifdef MY_DLL
    #define MY_DLL_EXPORTS  DllExport
#else
    #define MY_DLL_EXPORTS  DllImport
#endif //MY_DLL

#include "common.h"

#define PRES_NOCOMMAND_FOUND 2000

//ommited code for brevity...

void MY_DLL_EXPORTS UserInstruction1(double* lpNumeric,     TCHAR* lpAlpha1, TCHAR* lpAlpha2);

UInstruction 的cpp 文件与上面相同...

编辑:为了完整性...

//UInstruction.cpp
#include "stdafx.h"
#include "UInstruction.h"

//ommitted code for brevity...

//no extern needed...
void UserInstruction1( double* lpNumeric, 
                       TCHAR* lpAlpha1, TCHAR* lpAlpha2 )
{
   //logic goes here
}

不要忘记从 Company.Pins.Bank.Win32Console 的属性“公共(public)属性”->“框架和引用”中添加对 Company.Pins.Bank.Win32Console 项目的 Dll 项目的引用

关于C++ 错误 LNK2028、LNK2019 和 LNK1120。它们是什么意思,我该如何解决它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5678591/

相关文章:

c++ - 根据模板函数类型推断变量类型

c++ - 弃用的结构成员 C++

c# - 通过命令行仅构建 VS 安装项目

c# - Linq UNION 查询选择两个元素

database - Visual Studio 2010 自动化数据库部署

c++ - R6010 abort() 已被调用

C++ - OpenGL 在 glDrawElements() 上崩溃

c++ - 如何将字符串写入 .csv 文件?

c++ - 单击按钮调用 onPaint()

c++ - const 临时类型的最佳实践