clinuxmodbuslibmodbus

libmodbus: how to compile application including libmodbus library?


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!


Solution

  • 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.