0

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;
}
4

2 回答 2

0

问题出在SerialPort::init(unsigned long,char const *)" (?init@SerialPort@@QAEKKPBD@Z)

正如我所料,类型LPCTSTRchar const*而不是。const char*

因此传递"COM1"参数init导致错误!

我用过LPCSTR,一切正常。我读了更多,他们应该是同样的事情......然后我尝试将我的LPCSTR( const char*) 参数转换为LPCTSTR( char const*) 并且它也有效。

如果它们应该是相同的,我仍然希望了解为什么会发生这种情况,所以我现在不会接受我自己的答案!

谢谢!

于 2012-10-31T16:24:03.953 回答
0

这看起来像 SerialHost 没有链接 SerialPort。在 VS 2008 中,您可以通过转到 Project > Project Dependencies for SerialHost 并让 SerialHost 依赖于 SerialPort 来实现此目的。

这是假设 SerialPort 项目实际上包含 SerialPort::init() 的定义。

于 2012-10-31T15:45:55.183 回答