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 では、[プロジェクト] > [SerialHost のプロジェクトの依存関係] に移動し、SerialHost を SerialPort に依存させることでこれを実現します。

これは、SerialPort プロジェクトに実際に SerialPort::init() の定義が含まれていることを前提としています。

于 2012-10-31T15:45:55.183 に答える