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

| 我遇到了这三个错误,对我来说它们似乎毫无意义。如果我评论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的头文件的所有其他包括:
//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 ++ \”-> \“ Preprocessor \”并在\“ Preprocessor Definitions \”中添加一个预处理器符号(如果不包含)有一个)。即我称它为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的属性中,将对Dll项目的引用添加到Company.Pins.Bank.Win32Console \“通用属性\”-> \“框架和引用\”     
您正在尝试在使用/ clr选项编译的项目中使用函数。托管代码。从不带/ clr选项编译的控制台应用程序项目中。本机代码。由于链接器正在寻找__cdecl函数,因此实际上会被编译为__clrcall函数,因此出现链接器错误。 那只是链接器问题,还有运行时问题,因为您的控制台应用程序没有加载和初始化CLR,可以执行托管代码。您需要考虑如何与托管代码互操作。显而易见的解决方案是使控制台应用程序也成为托管应用程序。或者使您的DLL成为不受管理的DLL,您并没有明显使用.NET框架。或者,您可以通过在本地应用程序(google CorBindToRuntimeEx())中托管CLR或将DLL转换为COM服务器来使生活复杂化。     

要回复问题请先登录注册