LINK2019 still keeps going - visual-c++

I'm trying to resolve an unresolved external (link2019 error).There were already some posts on this issue and i try them, but I still could not figure it out.
The error is caused by my CreateFtdcMdApi function ( right?) but my understanding is that this is "resolved".
// testTraderApi.cpp : 定义控制台应用程序的入口点。
//
#include "MdSpi.h"
#include <iostream>
// UserApi对象
CThostFtdcMdApi* pUserApi = NULL;
// 配置参数
char FRONT_ADDR[] = "tcp://asp-sim2-md1.financial-trading-platform.com:26213"; // 前置地址
TThostFtdcBrokerIDType BROKER_ID = "***"; // 经纪公司代码
TThostFtdcInvestorIDType INVESTOR_ID = "0***"; // 投资者代码
TThostFtdcPasswordType PASSWORD = "*****"; // 用户密码
char *ppInstrumentID[] = { "***", "***" }; // 行情订阅列表
int iInstrumentID = 2; // 行情订阅数量
// 请求编号
int iRequestID = 0;
void main(void)
{
// 初始化UserApi
char file = 'F';
pUserApi = CThostFtdcMdApi::CreateFtdcMdApi(); // 创建UserApi
CThostFtdcMdSpi* pUserSpi = new CMdSpi();
pUserApi->RegisterSpi(pUserSpi); // 注册事件类
pUserApi->RegisterFront(FRONT_ADDR); // connect
pUserApi->Init();
pUserApi->Join();
// pUserApi->Release();
}
I truncated the code a bit, because it is verbose.
class MD_API_EXPORT CThostFtdcMdApi
{
public:
///创建MdApi
///#param pszFlowPath 存贮订阅信息文件的目录,默认为当前目录
///#return 创建出的UserApi
///modify for udp marketdata
static CThostFtdcMdApi *CreateFtdcMdApi(const char *pszFlowPath = "", const bool bIsUsingUdp=false, const bool bIsMulticast=false);
///获取API的版本信息
///#retrun 获取到的版本号
static const char *GetApiVersion();
}
Error:
1>------ Build started: Project: challenges, Configuration: Debug Win32 ------
1>challenges.obj : error LNK2019: unresolved external symbol "public: static class CThostFtdcMdApi * __cdecl CThostFtdcMdApi::CreateFtdcMdApi(char const *,bool,bool)" (?CreateFtdcMdApi#CThostFtdcMdApi##SAPAV1#PBD_N1#Z) referenced in function _main
1>d:\documents\visual studio 2013\Projects\challenges\Debug\challenges.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Related

C++ / CLI linker error because of SHAssocEnumHandlers function

To reproduce the issue,
Create a project > VC++ > CLR > Class library, name the project "ClassLibrary2" and use below two files as is.
// ClassLibrary2.h
#pragma once
#include <msclr\marshal.h>
#include <ShObjIdl.h>
using namespace System;
namespace ClassLibrary2 {
public ref class Class1
{
public : void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext);
};
}
// ClassLibrary2.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "ClassLibrary2.h"
void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext)
{
msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
PCWSTR pszStr = context->marshal_as<const wchar_t*>(ext);
IEnumAssocHandlers *pEnumHandlers = NULL;
/* below line is producing problems */
SHAssocEnumHandlers(pszStr, ASSOC_FILTER_RECOMMENDED, &pEnumHandlers);
}
Now, if I build the project, I get following errors :
Error 2 error LNK2028: unresolved token (0A0000A2) "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
Error 3 error LNK2019: unresolved external symbol "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
How to remove those linker errors ?

Windows VC++ 2010 link error _main

I was trying to poll available devices on a Windows machine by this code example from Microsoft. But there are link errors that relates to _main().
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
For some reasons, there are link errors:
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList#4 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetDeviceRegistryPropertyW#28 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiEnumDeviceInfo#12 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetClassDevsW#16 referenced in function _main
1>c:\users\visual studio 2010\Projects\usb\Debug\usb.exe : fatal error LNK1120: 4 unresolved externals
What they are about? There is no _main() at all.
First thing first, the linker errors are not about missing main, but about other functions that are referenced (called) from main.
The functions you mentioned in linker errors are from library: Setupapi.lib, and you need to include it in Linker settings (Input) of your project.
Why did it worked on VS re-open?
Probably simply because you changed the configuration (by mistake). For example from Win32 to x64, and/or from Debug to Release (or any combination) of these. The other configuration didn't have reference to this library added.

error LNK2019 and fatal error LNK1120

I encouneter a problem when compiling my projet. When I set this line:
boost::shared_ptr mySwap;
I have no problem but when I set this one:
boost::shared_ptr mySwap(new OvernightVsLiborBasisSwap(OvernightVsLiborBasisSwap::PayerOvernight,
1.0,
scheduleOis,
indexOis,
dayCountOis,
1.0,
scheduleLibor,
indexLibor,
dayCountLibor));
I have the following error message
excelFunctions.obj : error LNK2019: unresolved external symbol "public: __thiscall ModLibNY::OvernightVsLiborBasisSwap::OvernightVsLiborBasisSwap(enum ModLibNY::OvernightVsLiborBasisSwap::Type,double,class QuantLib::Schedule const &,class boost::shared_ptr const &,class QuantLib::DayCounter const &,double,class QuantLib::Schedule const &,class boost::shared_ptr const &,class QuantLib::DayCounter const &,double,double,bool,bool,class boost::optional,class boost::optional)" (??0OvernightVsLiborBasisSwap#ModLibNY##QAE#W4Type#01#NABVSchedule#QuantLib##ABV?$shared_ptr#VOvernightIndex#QuantLib###boost##ABVDayCounter#4#N1ABV?$shared_ptr#VIborIndex#QuantLib###6#3NN_N5V?$optional#W4BusinessDayConvention#QuantLib###6#6#Z) referenced in function "class xlw::NCMatrix __cdecl getOisLiborSwapCurve(class xlw::CellMatrix const &,class xlw::CellMatrix const &,class xlw::CellMatrix const &,class xlw::CellMatrix const &,double const &)" (?getOisLiborSwapCurve##YA?AVNCMatrix#xlw##ABVCellMatrix#2#000ABN#Z)
3>.\Debug\ExplainPnL.xll : fatal error LNK1120: 1 unresolved externals
The object OvernightVsLiborBasisSwap is part of my own-made static library ModLibNY which is included. The first lines of my code constain:
#include <ql/quantlib.hpp>
#include <ml/modlibny.hpp>
#include <ml/swaps/overnightvsliborbasisswap.hpp>
using namespace std;
using namespace xlw;
using namespace QuantLib;
using namespace ModLibNY;
What is very wierd is that I ve the error when I use the constructor. For information it is declared in the .hpp file as
#ifndef overnight_vs_libor_basis_swap_hpp
#define overnight_vs_libor_basis_swap_hpp
#include <ql/quantlib.hpp>
using namespace std;
using namespace QuantLib;
namespace ModLibNY {
class InterestRateIndex;
class OvernightVsLiborBasisSwap : public Swap {
public:
enum Type { ReceiverOvernight = -1, PayerOvernight = 1 };
class arguments;
class results;
class engine;
OvernightVsLiborBasisSwap(
const OvernightVsLiborBasisSwap::Type type,
const Real nominal1,
const Schedule &schedule1,
const boost::shared_ptr<OvernightIndex> &index1,
const DayCounter &dayCount1,
const Real nominal2,
const Schedule &schedule2,
const boost::shared_ptr<IborIndex> &index2,
const DayCounter &dayCount2,
const Real spread1 = 0.0,
const Real spread2 = 0.0,
const bool intermediateCapitalExchange = false,
const bool finalCapitalExchange = false,
boost::optional<BusinessDayConvention> paymentConvention1 =
boost::none,
boost::optional<BusinessDayConvention> paymentConvention2 =
boost::none);
and defined in the .cpp file:
#include "StdAfx.h"
#include "overnightvsliborbasisswap.hpp"
#include <ql/quantlib.hpp>
using namespace QuantLib;
namespace ModLibNY {
OvernightVsLiborBasisSwap::OvernightVsLiborBasisSwap(
const OvernightVsLiborBasisSwap::Type type,
const Real nominal1,
const Schedule &schedule1,
const boost::shared_ptr<OvernightIndex> &index1,
const DayCounter &dayCount1,
const Real nominal2,
const Schedule &schedule2,
const boost::shared_ptr<IborIndex> &index2,
const DayCounter &dayCount2,
const Real spread1,
const Real spread2,
const bool intermediateCapitalExchange,
const bool finalCapitalExchange,
boost::optional<BusinessDayConvention> paymentConvention1,
boost::optional<BusinessDayConvention> paymentConvention2)
: Swap(2),
type_(type),
nominal1_(std::vector<Real>(schedule1.size() - 1, nominal1)),
nominal2_(std::vector<Real>(schedule2.size() - 1, nominal2)),
schedule1_(schedule1),
schedule2_(schedule2),
index1_(index1),
index2_(index2),
dayCount1_(dayCount1),
dayCount2_(dayCount2),
intermediateCapitalExchange_(intermediateCapitalExchange),
finalCapitalExchange_(finalCapitalExchange)
{
init(paymentConvention1, paymentConvention2);
}
... If someone have an idea of the problem origin, please let me know!
Thanks

LNK2019 & LNK1120 Unresolved Externals Probably an easy fix, but I'm having real trouble

I've been getting these LNK2019s for a little while now and can't seem to find a way to get rid of them. I'm aware that there are a lot of threads on these errors already, but I've yet to find anything that's helped me so hoped someone might miss something glaringly obvious I may have missed.
I've not learnt very traditionally, so sorry if my code's a bit messy.
main:
#include "eventLoop.h"
#include <time.h>
#include <iostream>
using namespace std;
bool buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG = false;
bool KeepProgramOpen = true;
time_t timer;
time_t oldtime;
time_t dtime;
time_t looptime;
int rate;
char Note;
bool FirstLoop = true;
eventLoop MainEventLoop;
int main()
{
rate = 60;
looptime = 1000 / rate;
while(KeepProgramOpen==true)
{
time(&timer);
dtime = timer-oldtime;
if(dtime<looptime)
{
continue;
}
oldtime = timer;
MainEventLoop.FindFinalNote(buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG, FirstLoop);
FirstLoop = false;
//Closing stuff goes here
}
}
eventLoop.h:
#pragma once
class eventLoop {
public:
void FindFinalNote(bool, bool, bool, bool, bool, bool, bool, bool);
protected:
};
eventLoop.cpp:
#include "eventLoop.h"
#include "MidiOutput.h"
#include "FileIO.h"
MidiOutput MidiOutputX;
FileIO fileioX;
void eventLoop::FindFinalNote(bool A, bool B, bool C, bool D, bool E, bool F, bool G, bool firstloop)
{
if(firstloop == true)
{
for (int loopindex=0; loopindex<10; loopindex++)
{
// Note[loopindex] = Filecheck for notes
}
MidiOutputX.FindDevice(
1, /*int argc number of ports*/
60, /*char argv argument vector - strings pointed to, i don't really get it*/
);
}
char Note[10];
int KeyIndex = 0;
FileIO::key CurrentKey;
CurrentKey = fileioX.RetrieveKey(KeyIndex);
for (int x = 0; x < 10; x++)
{
Note[x] = CurrentKey.Note[x];
}
// There's a bunch of simple if statements here, nothing I need to bore you with
}
MidiOutput.h:
#pragma once
class MidiOutput {
public:
void FindDevice(int, char);
void PlayNote(unsigned char);
void EndNote();
void CloseDevice();
protected:
};
MidiOutput.cpp:
#include "MidiOutput.h"
#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
union { unsigned long word; unsigned char data[4]; } message;
int midiport;
HMIDIOUT device;
void FindDevice(int argc, char** argv)
{
if (argc < 2) {
midiport = 0;
} else {
midiport = atoi(argv[1]);
}
printf("Midi output port set to %d.\n", midiport);
midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL);
message.data[0] = 0x90; //command byte
message.data[1] = 60; //middle C
message.data[2] = 0; //volume, 0-100
message.data[3] = 0; //not used
}
void MidiOutput::PlayNote(unsigned char Note)
{
message.data[1] = Note;
message.data[2] = 100;
}
void MidiOutput::EndNote()
{
message.data[2] = 0;
}
void MidiOutput::CloseDevice()
{
midiOutReset(device);
midiOutClose(device);
}
Exact Errors:
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall MidiOutput::FindDevice(int,char)" (?FindDevice#MidiOutput##QAEXHD#Z) referenced in function "public: void __thiscall eventLoop::FindFinalNote(bool,bool,bool,bool,bool,bool,bool,bool)" (?FindFinalNote#eventLoop##QAEX_N0000000#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\eventLoop.obj GHX
Error 2 error LNK2019: unresolved external symbol _imp_midiOutOpen#20 referenced in function "void __cdecl FindDevice(int,char * *)" (?FindDevice##YAXHPAPAD#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 3 error LNK2019: unresolved external symbol _imp_midiOutClose#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 4 error LNK2019: unresolved external symbol _imp_midiOutReset#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 5 error LNK1120: 4 unresolved externals C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\Debug\GHX.exe GHX
Thanks in advance, and sorry about the wall of code, I'm not sure what's necessary.
The missing symbols, midiOutOpen, midiOutClose etc. are defined in the DLL Winmm.dll. You will need to link to Winmm.lib by either specifying it as an input to the link command or by including this in your file:
#pragma comment(lib, "Winmm.lib")
You're also getting an error about MidiOutput::FindDevice. You will need to fix up the signatures so that the .h file and .cpp match, and qualify the function definition in the .cpp file with the class name (MidiOutput::).

Matlab / Microsoft Visual C++ 2010 Express: What is "error LNK2019: unresolved external symbol"?

I am trying to create a simple C++ class and a Matlab mex file. My code is as follows:
Matlab: mexTest1.cpp
#include "mex.h"
#include "K:\\My Documents\\Visual Studio 2010\\Projects\\HelloWorld\\HelloWorld\\Class1.h"
/* Input Arguments */
#define X prhs[0]
/* Output Arguments */
#define RESULT plhs[0]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ){
/* Initialize input and output pointers
// Inputs */
double *x;
/* Outputs */
double r;
double *result;
mwSize m,n;
m = mxGetM(X);
n = mxGetN(X);
/* Create a matrix for the return argument */
RESULT = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to the various parameters */
result = mxGetPr(RESULT);
x = mxGetPr(X);
/* Do the actual computations in a subroutine */
Class1 c1(2, 15.0);
r = c1.product();
result[0] = r;
return;
}
Class1.h:
#pragma once
#include <string> // Standard string class in C++
class Class1
{
public:
int a;
double b;
public:
Class1(const int& a, const double& b);
//virtual ~Class1();
void print() const;
double product() const;
};
Class1.cpp:
#include "stdafx.h"
#include <iostream>
#include "Class1.h"
Class1::Class1(const int& a, const double& b){
Class1::a = a;
Class1::b = b;
}
void Class1::print() const{
std::cout << "a=" << Class1::a << " * b=" << Class1::b << " = " << Class1::product() << std::endl;
}
double Class1::product() const{
return a*b;
}
Running the Matlab command mex mexTest1.cpp gives the error messages:
Creating library C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.x and object C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.exp
mexTest1.obj : error LNK2019: unresolved external symbol "public: double __thiscall Class1::product(void)const " (?product#Class1##QBENXZ) referenced in function _mexFunction
mexTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(int const &,double const &)" (??0Class1##QAE#ABHABN#Z) referenced in function _mexFunction
mexTest1.mexw32 : fatal error LNK1120: 2 unresolved externals
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'mexTest1.mexw32' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
Can anyone help me fix this problem?
Thanks.
The linker is telling you that in trying to construct an executable, it wasn't supplied with an object file that contains Class1::product and Class1::Class1. That's because those functions would be supplied by compiling Class1.cpp, which your command line doesn't ask for.
You should use the syntax of mex for multiple files: mex mexTest1.cpp Class1.cpp
Your linker is unable to find deinitions(bodies) Class1 methods (constructor and product). This may be due to
You haven't provided any definition(body)
The definitions are in a lib file which you forgot to link to

Resources