开发者

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

I get these three errors and they seem to make little sense to me. If i comment the UserInstruction1(P1, P2, P3); in the console app the errors go away. Both projects are /CLR projects.

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       
}


I assume here that all code resides in a single project (Company.Pins.Bank.Win32Console). If so you should move the <\iostream> and <\stdio.h> includes (and any other includes of headers that never/seldom change to 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)

and

//UInstruction.h

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

#include "common.h"

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

and

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

//ommitted code for brevity...

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

If UserInstruction1 resides in a Dll that is used by the Company.Pins.Bank.Win32Console project:

Make sure you define in stdafx.h for the dll and console projects:

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

Open the properties for the DLL project, go to "Configuration Properties" -> "C/C++" -> "Preprocessor" and add to "Preprocessor Definitions" a preprocessor symbol (if you don't have one). I.e. I'll call it MY_DLL. Don't forget to define it in all configurations...

Make sure you export the functions from the 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);

The cpp file for UInstruction remains the same as above...

EDIT: For completness...

//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
}

Do not forget to add a reference to the Dll project to the Company.Pins.Bank.Win32Console project from the properties of the Company.Pins.Bank.Win32Console "Common Properties" -> "Framework and References"


You are trying to use a function in a project that's compiled with the /clr option. Managed code. From a console application project that's compiled without the /clr option. Native code. You are getting the linker error because it is looking for a __cdecl function, it is actually compiled as a __clrcall function.

That's just the linker problem, there's also a runtime problem because your console app doesn't have the CLR loaded and initialized, ready to execute managed code. You need to consider how you are going to interop with managed code. The obvious solution is to make your console app a managed app as well. Or to make your DLL an unmanaged one, you are not making any obvious use the .NET framework. Or you can complicate your life by hosting the CLR in your native app (google CorBindToRuntimeEx()) or turning your DLL into a COM server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜