Hi. I've installed the libmodbus library on my Linux PC, then I tried to compile my temp.c code. I'm new to the topic of Modbus and also programming in C, so I understand, that some errors could be caused by my own incompetence.
Then I get this response:
/usr/bin/ld: /tmp/ccTiqThz.o: in function `main':
temp.c:(.text+0x23): undefined reference to `modbus_new_rtu'
/usr/bin/ld: temp.c:(.text+0x67): undefined reference to `modbus_set_slave'
/usr/bin/ld: temp.c:(.text+0x78): undefined reference to `modbus_rtu_set_serial_mode'
/usr/bin/ld: temp.c:(.text+0x84): undefined reference to `modbus_connect'
/usr/bin/ld: temp.c:(.text+0x9c): undefined reference to `modbus_strerror'
/usr/bin/ld: temp.c:(.text+0xc4): undefined reference to `modbus_free'
/usr/bin/ld: temp.c:(.text+0xe6): undefined reference to `modbus_read_registers'
/usr/bin/ld: temp.c:(.text+0xf2): undefined reference to `modbus_close'
/usr/bin/ld: temp.c:(.text+0xfe): undefined reference to `modbus_free'
collect2: error: ld returned 1 exit statusThis is my code:
#include <stdio.h>
#include <modbus.h>
#include <cerrno>
uint16_t tab_reg[32];
int main(void) {
modbus_t *ctx;
ctx = modbus_new_rtu("/dev/ttyUSB1", 9600, 'N', 8, 2);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1;
}
modbus_set_slave(ctx, 1);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
modbus_read_registers(ctx, 0, 1, tab_reg);
modbus_close(ctx);
modbus_free(ctx);
}
Should I've attached some more libraries or elements in #include<...>, or it's caused by wrongly installing the library (I didn't get any error messages during library installation)?
Thanks in advance for any help.
Nikodem