I'm having LNK2001 issues while building my solution.
The solution has 3 projects, one of them if just a main that returns 0, so I'll exclude it from the problem.
One project (SerialPort) is a .lib that implements a class (SerialPort
), while the other (SerialHost) simply creates an instance of SerialPort
.
I'm thinking the problem is somewhere in the project settings of SerialHost. Here goes the SerialHost project code:
#include "SerialPort.h"
int main (int argc, char *argv[])
{
SerialPort* serialHost = new SerialPort();
serialHost->init(PORT_HOST,"COM1");
delete serialhost;
return 0;
}
And building yields:
main.obj : error LNK2001: unresolved external symbol "public: unsigned long __thiscall SerialPort::init(unsigned long,char const *)" (?init@SerialPort@@QAEKKPBD@Z)
I've searched SO for answers and found no solution that cured my problem.
To set the project settings I followed this msnd walkthrough.
I also tried adding specifically the .lib to the Additional Dependencies in Linker-Input.
Then I read this SO thread, and setting the option "Treat wchar_t as build-in type (Properties->C/C++->Language-> ... ) to No still didn't work. I thought it would work because SerialPort::init
is declared as
DWORD init (DWORD portType, LPCTSTR portName);
- it thought the fault was with the use of the LPCTSTR type.
The SerialPort project builds OK (the init
method is implemented in the .cpp).
I'm just beginning to work with my own static libs in VS so I may be doing some basic error here.
Thank you in advance for your help!
Additional Info:
I tried commenting out the init
and using another method and it built just fine! This code:
int main (int argc, char *argv[])
{
SerialPort* serialHost = new SerialPort();
serialHost->init(PORT_HOST,"COM1");
serialHost->runTerminal();
return 0;
}
in SerialHost just yields the error in init
- if I comment init
, no linking errors appear.
Here is the implementation for SerialPort
// SerialPort.h
#pragma once
#include <windows.h>
#include <iostream>
#define READ_BUFFER_SIZE 1
#define PORT_BAUD_RATE CBR_9600
#define PORT_PARITY NOPARITY
#define PORT_BYTE_SIZE 8
#define PORT_STOP_BITS ONESTOPBIT
#define PORT_HOST 0
#define PORT_DEVICE 1
class SerialPort
{
public:
SerialPort (void);
virtual ~SerialPort (void);
DWORD init (DWORD portType, LPCTSTR portName);
DWORD runTerminal (void);
DWORD runDevice (void);
LPCTSTR getVersion (void);
protected:
// Port properties
HANDLE _hSerialComm;
// Buffers
DWORD _dwBytesRead;
DWORD _dwEvtCodeRecvd;
std::string _inputString;
DWORD _eventNum;
COMSTAT _portStats;
COMMCONFIG _portDefaultConfig;
DCB _portCfgDCB;
std::string _version;
DWORD configureSerialPort (LPCTSTR portName, DWORD portType);
DWORD resetPortCfg (LPCTSTR portName);
HANDLE openPort (LPCTSTR portName, DWORD accessFlags);
DWORD configureDCB (HANDLE hSerialPort);
DWORD configureCommMask (HANDLE hSerialPort);
DWORD configureTimeOuts (HANDLE hSerialPort);
DWORD clearPortIO (HANDLE hSerialPort);
VOID printPortConfig (DCB portCfgDCB, LPCTSTR portName);
VOID printPortErrors (DWORD portErrors, COMSTAT portStats);
DWORD checkCommErrors (HANDLE hSerialPort);
};
// SerialPort.cpp - just init method
DWORD SerialPort::init(DWORD portType,LPCTSTR portName)
{
_hSerialComm = NULL;
_dwBytesRead = 0;
_dwEvtCodeRecvd = 0;
_eventNum = 0;
memset(&_portStats,0,sizeof(_portStats));
memset(&_portDefaultConfig,0,sizeof(_portDefaultConfig));
memset(&_portCfgDCB,0,sizeof(_portCfgDCB));
// Configure Serial Port
configureSerialPort(portName,portType);
return 1;
}
This looks like SerialHost is not linking SerialPort. In VS 2008, you would achieve this by going to Project > Project Dependencies for SerialHost and make SerialHost depend on SerialPort.
That is assuming that the SerialPort project actually contains a definition of SerialPort::init().