1

I have a problem with compilation of my application, which is based on libmodbus library: https://github.com/stephane/libmodbus

I have tried to compile it using this command: cc pkg-config --cflags --libs libmodbus modbus.c -o modbus

But I get the following errors:

/usr/bin/ld: /tmp/cckdtbWy.o: in function `main':
modbus.c:(.text+0x1b): undefined reference to `modbus_new_tcp_pi'
/usr/bin/ld: modbus.c:(.text+0x5c): undefined reference to `modbus_connect'
/usr/bin/ld: modbus.c:(.text+0x8d): undefined reference to `modbus_free'
/usr/bin/ld: modbus.c:(.text+0xa3): undefined reference to `modbus_connect'
/usr/bin/ld: modbus.c:(.text+0xde): undefined reference to `modbus_read_bits'
/usr/bin/ld: modbus.c:(.text+0x11e): undefined reference to `modbus_close'
/usr/bin/ld: modbus.c:(.text+0x12a): undefined reference to `modbus_free'
collect2: error: ld returned 1 exit status

I would appreciate any help!

Best regards!

2 Answers2

1

I have worked through this problem and my solution is that:

1. Create modbus.conf file in /etc/ld.so.conf.d/.

2. Run sudo ldconfig.

3. Compile your application/program with the following command:

gcc <name>.c -o <name> -I/usr/local/include/modbus/ -lmodbus

It has worked for me, but I am not sure whether the first step is necessary.

  • You can use `#include ` instead of `#include ` and compile with `gcc .c -o -lmodbus` – Tux Jan 09 '23 at 23:51
0

Did you install the libmodbus-dev? You should try this: sudo apt-get install -y libmodbus-dev.

See v3.1.6 and v3.0.8: libmodbus docs

lostsky25
  • 85
  • 1
  • 10
  • I haven't seen that there is such an option. Documentation is not impressing and short of information. Anyway I have compiled my application using `pkg-config --libs --cflags libmodbus` , but the question is, may I use other `CFLAGS` and include other directories while compiling? Of course I will check that, but I would like to know what hides under `--cflags` parameter. – JStand Jakub Standarski Jul 29 '20 at 09:47