Search code examples
c++qttcpmodbus

undefined reference to errors


I am trying to code ModBus/TCP connection with QtCreator c++. However I could't get rid of undefined errors. Here is my code:

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

modbus.h :

#ifndef MODBUS_H
#define MODBUS_H

#include <stdio.h>
#include <stdlib.h>

#include "MbusTcpMasterProtocol.hpp"

class modbus
{
public:
    modbus();

    char *hostName;
    MbusTcpMasterProtocol *mbusProtocol;

    void openProtocol();
    void closeProtocol();
    void runPollLoop();
};

#endif // MODBUS_H

Error Messages Are :

In function `modbus::openProtocol()':
error: undefined reference to `MbusTcpMasterProtocol::MbusTcpMasterProtocol()'
error: undefined reference to `MbusTcpMasterProtocol::openProtocol(char const*)'
error: undefined reference to `getBusProtocolErrorText'

In function `modbus::runPollLoop()':
error: undefined reference to `MbusMasterFunctions::readMultipleRegisters(int, int, short*, int)'
error: undefined reference to `getBusProtocolErrorText'
collect2: ld returned 1 exit status

Could you please help to figure out this problem ?

EDIT :

Here is .pro file :

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

Solution

  • You are having a linking problem. When you use an external library, some functions may have their definition in an object file, witch you have to link against when you compile your program.

    Unfortunately I never worked with ModBus, so I can't give you precise information on how to link against it, but according to the tutorial on their website, you need to compile the source code in order to get the binaries.

    After that, you need to link to them in your project. In Qt, the way to do that is to add:

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

    To your .pro file.