使用我的静态库时,错误lnk2001



构建解决方案时我有LNK2001问题。

该解决方案有3个项目,其中一个是返回0的主要项目,因此我将其从问题中排除。

一个项目( serialport )是一个.lib,可以实现类(SerialPort),而另一个( SerialHost )只是创建SerialPort的实例。

我认为这个问题在 SerialHost的项目设置中的某个地方。这是 SerialHost 项目代码:

#include "SerialPort.h"

int main (int argc, char *argv[])
{
    SerialPort* serialHost = new SerialPort();
    serialHost->init(PORT_HOST,"COM1");
    delete serialhost;
    return 0;
}

和建筑物产量:

main.obj:错误lnk2001:未解决的外部符号"公共:未签名 长__ thiscall serialport :: init(未签名的长,char const *)" (?init@serialport @@ qaekkpbd@z)

我已经搜索了答案,并且没有发现解决我的问题的解决方案。

要设置项目设置,我遵循了MSND演练。

我还尝试将.lib专门添加到链接器输入中的其他依赖项。

然后,我读取了这个线程,然后将选项设置为"将wchar_t视为建筑类型(properties-> c/c -> Lanaging-> ...)仍然不起作用。我认为它会工作是因为SerialPort::init被声明为

DWORD init (DWORD portType, LPCTSTR portName);
  • 它认为错误是使用LPCTSTR类型。

serialport 项目构建确定(在.cpp中实现了init方法)。

我才刚刚开始使用自己的静态液体在VS中使用,所以我可能在这里做一些基本错误。

预先感谢您的帮助!


附加信息:

我尝试评论init并使用另一种方法,并且构建正常!此代码:

int main (int argc, char *argv[])
{
    SerialPort* serialHost = new SerialPort();
    serialHost->init(PORT_HOST,"COM1");
    serialHost->runTerminal();
    return 0;
}

in SerialHost 仅在init中产生错误 - 如果我注释init,则不会出现链接错误。

这是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;
}

这看起来SerialHost没有链接SerialPort。在VS 2008中,您将通过转到serialhost的项目依赖项来实现这一目标,并使SerialHost取决于SerialPort。

假设SerialPort项目实际上包含了SerialPort :: Init()的定义。

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

我预期的是,LPCTSTR类型是char const*而不是const char*

因此,将"COM1"参数传递给init引起了错误!

我使用了LPCSTR,一切都很好。我读了更多,他们据说是同一件事...然后我尝试将我的 LPCSTRconst char*)参数施放到 LPCTSTRchar const*),并且也有效。

我仍然希望有一些光明,说明为什么会发生这种情况,所以我现在不会接受自己的答案!

谢谢!

最新更新