0

I am trying to use libmodbus and i have the following :

extern "C" {
#include "modbus.h"
#include "modbus-tcp.h"
}

void executeMbus() {
    modbus_t *mb;
    uint16_t tab_reg[32];
    mb = modbus_new_tcp("127.0.0.1", 1502);
    modbus_connect(mb);

    /* Read 5 registers from the address 0 */
    modbus_read_registers(mb, 0, 5, tab_reg);

    modbus_close(mb);
    modbus_free(mb);
}

I have added the path to the library in C/C++ General -> Paths and Symbols GNU C++ and i added the path to the modbus lib. However i'm getting weird undefined reference for each modbus_ method. How can i fix it ?

YSC
  • 38,212
  • 9
  • 96
  • 149
Chris Tanev
  • 208
  • 3
  • 16
  • What's the actual command line invoking the compiler? My guess is you failed to add the library objects to the object to link, only provided the search path for it. – YSC Apr 12 '17 at 11:12
  • @YSC i586-poky-linux-g++ -lmraa -fno-use-linker-plugin "--sysroot=C:\\Users\\hdta\\workspace\\libs\\iot2000-sdk-windows-2.1.2.tar\\iot2000-sdk-windows-2.1.2/sysroots/i586-nlp-32-poky-linux" -o Iot2020WithMB example.o example.o: In function `executeMbus()': after that i just get undefined reference to all modbus_ methods – Chris Tanev Apr 12 '17 at 11:13
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Apr 12 '17 at 11:21

1 Answers1

2

For the library libmodus to be linked with your program, you need to:

  1. Add the include path to the compiler settings C/C++ general -> Paths and Symbols -> Includes -> GNU C++
  2. Add the library path to the linker settings C/C++ General -> Paths and Symbols -> Library Paths
  3. Add the library object to the linker settings C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries screenshot

You did (1.) and (2.) but you also need to do (3.).

Raxvan
  • 6,257
  • 2
  • 25
  • 46
YSC
  • 38,212
  • 9
  • 96
  • 149
  • thanks for your input sir. One small problem is that in my case i use IOT2000 C++ Compiler , so i dont have the option to add libraries to the Linker(there is just General tab there). I guess i need to make another just C++ project – Chris Tanev Apr 12 '17 at 11:25
  • @ChrisTanev if your compiler is not known by Eclipse, it should provide you with the ability to provide custom command line arguments. What does the "General" tab look like? – YSC Apr 12 '17 at 22:17