visual studio 2010 - c++错误LNK2028, LNK2019和LNK1120.它们是什么意思,我

visual studio 2010 - C++ errors LNK2028, LNK2019 and LNK1120. What do they mean and how do I solve them?

本文关键字:LNK1120 是什么 意思 LNK2019 2010 c++ 错误 visual LNK2028 studio      更新时间:2023-10-16

我得到了这三个错误,它们似乎对我没有什么意义。如果我注释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:WorkspaceCompany.PinsBankSourceDebugCompany.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> include(以及从不/很少更改的头文件的任何其他include)移动到stdafx.h:

//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使用的Dll中。Win32Console项目:

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

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

打开DLL项目的属性,转到"配置属性"->"C/c++"->"预处理器",并在"预处理器定义"中添加预处理器符号(如果没有的话)。也就是说,我把它命名为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中添加对Dll项目的引用。Win32Console项目从Company.Pins.Bank的属性。Win32Console "Common Properties" -> "Framework and References"

您正在尝试在使用/clr选项编译的项目中使用函数。托管代码。从没有/clr选项编译的控制台应用程序项目。本机代码。您正在获得链接器错误,因为它正在寻找__cdecl函数,它实际上被编译为__clrcall函数。

这只是链接器问题,还有一个运行时问题,因为你的控制台应用程序没有加载和初始化CLR,准备执行托管代码。您需要考虑如何与托管代码进行互操作。最明显的解决方案是让你的主机应用也成为托管应用。或者使您的DLL成为非托管的DLL,您不会明显使用。net框架。或者你可以通过在本地应用程序中托管CLR (google CorBindToRuntimeEx())或将DLL转换为COM服务器来复杂化你的生活。