对错误的未定义引用 我试图用QtC造作C++来编写Modbus/TCP连接。然而,我无法摆脱未定义的错误。这是我的密码:

对错误的未定义引用 我试图用QtC造作C++来编写Modbus/TCP连接。然而,我无法摆脱未定义的错误。这是我的密码:,c++,qt,tcp,modbus,C++,Qt,Tcp,Modbus,modbus.cpp: #include "modbus.h" modbus::modbus() { hostName = "127.0.0.1"; openProtocol(); runPollLoop(); closeProtocol(); } void modbus::openProtocol() { int result; result = mbusProtocol->openProtocol(hostName);

modbus.cpp:

#include "modbus.h"    

modbus::modbus()
{
    hostName = "127.0.0.1";
    openProtocol();

    runPollLoop();

    closeProtocol();

}


void modbus::openProtocol()
{
   int result;

   result = mbusProtocol->openProtocol(hostName);

   if (result != FTALK_SUCCESS)
   {
      fprintf(stderr, "Error opening protocol: %s!\n",
                       getBusProtocolErrorText(result));
      exit(EXIT_FAILURE);
   }
}


void modbus::closeProtocol()
{
   mbusProtocol->closeProtocol();
}


void modbus::runPollLoop()
{
   short dataArr[10];

   for (;;)
   {
      int i;
      int result;

      result = mbusProtocol->readMultipleRegisters(1, 100,
                                                  dataArr,
                                                  sizeof(dataArr) / 2);
      if (result == FTALK_SUCCESS)
         for (i = 0; i < int(sizeof(dataArr) / 2); i++)
            printf("[%d]: %hd\n", 100 + i, dataArr[i]);
      else
      {
         fprintf(stderr, "%s!\n", getBusProtocolErrorText(result));
         // Stop for fatal errors
         if (!(result & FTALK_BUS_PROTOCOL_ERROR_CLASS))
            return;
      }

#ifdef __WIN32__
      Sleep(1000);
#else
      sleep(1);
#endif
   }
}
你能帮我解决这个问题吗

编辑:

以下是.pro文件:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ModBus
TEMPLATE = app


SOURCES += main.cpp\
        dialog.cpp \
    modbus.cpp

HEADERS  += dialog.h \
    modbus.h

FORMS    += dialog.ui

您有链接问题。当您使用外部库时,某些函数可能在对象文件中有其定义,在编译程序时,您必须链接这些函数

不幸的是,我从未使用过ModBus,因此我无法提供有关如何链接ModBus的精确信息,但根据他们的教程,您需要编译源代码才能获得二进制文件

之后,您需要在项目中链接到它们。在Qt中,这样做的方法是添加:

LIBS += -L/path/to/mbus -lmbusmaster

到您的.pro文件。

看起来像是链接问题。你是如何链接到图书馆的?如果不确定这意味着什么,请向我们展示您的.profile@C请检查我的编辑。你安装了吗?如果有,那么在.pro文件中应该有一个LIBS+=-L/path/to/mbus-lmbusmaster
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ModBus
TEMPLATE = app


SOURCES += main.cpp\
        dialog.cpp \
    modbus.cpp

HEADERS  += dialog.h \
    modbus.h

FORMS    += dialog.ui
LIBS += -L/path/to/mbus -lmbusmaster