Compile error - undefined reference to

2,738 views
Skip to first unread message

Nikodem Jędrzejczak

unread,
Sep 15, 2021, 9:02:45 AM9/15/21
to libmodbus
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.

I use this command to execute code with library attached:
g++ -I/home/nj/libmodbus-3.1.6/src temp.c

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 status

This 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

Sebastian Psm

unread,
Sep 16, 2021, 5:57:35 AM9/16/21
to libmodbus
Hey Nikodem,

the messages " undefined reference to ..." are typical linker messages. That means, the compiler successfully compiled your source file (libmodbus header files found), but after the compile step, the linker needs to glue your code together with libmodbus (not with the header declarations, but with the real machine code from libmodbus. (The linker is automatically called by the compiler at the end of the compile step.)

Unfortunately, your compile has no clue about the libmodbus machine code. Because you just told the compile where to find the libmodbus header. Do solve this do the following:

  1. Build libmodbus (see the Installation guide from https://github.com/stephane/libmodbus).
    1. Building the lib is more important then installation. So, keep your eyes on ./autogen.sh && ./configure && make && make check
  2. Tell the compiler, that you need to link against libmodbus. This can be done by passing -lmodbus as additional argument (-lmodbus is passed to the linker and the linker will search for a libmodbus.so on the system)
    1. If you have not installed libmodbus, then you can also specify a path the the build libmodbus with -L/my/path/to/library and -lmodbus

Have fun
Sebastian

Nikodem Jędrzejczak

unread,
Sep 16, 2021, 2:48:30 PM9/16/21
to libmodbus
I've made through all installation process make, make install ./configure, etc. without any errors, but when I remove the additional g++ argument as suggested in point 2, it doesn't even compile correctly.

This is what I get after using g++ temp.c command:

temp.c:2:10: fatal error: modbus.h: No such file or directory
    2 | #include <modbus.h>
      |          ^~~~~~~~~~
compilation terminated.

As I've said earlier, while I was installing the libmodbus, it didn't show me any errors.

Greetings.
Nikodem

Sebastian Psm

unread,
Sep 16, 2021, 2:57:24 PM9/16/21
to libmodbus
Hey Nikodem,

the error now is the compiler. The compiler can't find the header directory. I guess you've removed the -I argument from your run. To be clear:

g++ -I/home/nj/libmodbus-3.1.6/src -lmodbus temp.c

should work

-I (capital letter i) tells the compiler the search path for the (modbus) header
-l (lower L) tells the linker which library (libmodbus.so --> -lmodbus)

Best greetings
Sebastian

Nikodem Jędrzejczak

unread,
Sep 18, 2021, 5:09:06 AM9/18/21
to libmodbus
Oh so that's was the whole problem, I was using lower L, instead of correct capital i.
Thank you very much!
Have a great day.
Nikodem

Reply all
Reply to author
Forward
0 new messages