0

I am a newbie in C... I wrote a very simple modbus1.c that includes libmodbus (whose source I downloaded, unzipped, untarred, ./configure'd, make'd and make install'd successfully).

When I try to make modbus1.c I get this:

cc -Wall -g     modbus1.c   -o modbus1
Undefined symbols for architecture x86_64:
  "_modbus_close", referenced from:
      _main in modbus1-6cd135.o
  "_modbus_connect", referenced from:
      _main in modbus1-6cd135.o
  "_modbus_free", referenced from:
      _main in modbus1-6cd135.o
  "_modbus_new_tcp_pi", referenced from:
      _main in modbus1-6cd135.o
  "_modbus_read_bits", referenced from:
      _main in modbus1-6cd135.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [modbus1] Error 1

I am running OSX snow leopard and have successfully used make to compile small programs before (tutorial level programs...) Here is the modbus1.c I am trying to compile:

#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>

int main(int argc, char *argv[]){
modbus_t *plc_client;

plc_client = modbus_new_tcp_pi("192.168.1.230","502");
if (plc_client == NULL) {
    fprintf(stderr, "Unable to allocate libmodbus context\n");
    return -1;
}
if (modbus_connect(plc_client) == -1) {
    fprintf(stderr, "Connection failed: \n");
    modbus_free(plc_client);
    return -1;
}
else if(modbus_connect(plc_client) == 0) {
    printf("MODBUS CONNECTION SUCCESSFUL\n");
}

uint8_t* catcher = malloc(sizeof(uint8_t));

if(modbus_read_bits(plc_client, 2000, 1, catcher)>0){
    printf("READ SUCCESSFUL");
}
else{
    printf("READ FAILED");
}

free(catcher);
modbus_close(plc_client);
modbus_free(plc_client);

return 0;
}

Any help will be greatly appreciated! Thanks!

-Niko

nemo
  • 593
  • 2
  • 8
  • 22
  • 2
    You need to tell the compiler (well the linker actually) to link in the modbus library, try adding `-lmodbus` to your compile line. – Etan Reisner Mar 27 '14 at 19:41
  • More or less the same question as http://stackoverflow.com/questions/10409032/why-im-getting-undefined-reference-to-sqrt-error-even-though-i-include-ma, only you are linking against modbus. – hlovdal Mar 27 '14 at 19:48
  • -lmodbus did not work. I did read something on libmodbus' git about ldconfig and making sure that /etc/ld.so.conf.d has all the required file links/associations... but my machine doesnt have ldconfig anywhere (i checked $PATH and /etc). should I look in this direction? or is this unrelated? – nemo Mar 27 '14 at 21:17

1 Answers1

1

Try this

cc -Wall -g modbus1.c -o modbus1 -L/path/to/libmodbus -lmodbus

You should replace that /path/to/libmodbus with the actual path of directory that includes the libmodbus.dylib in your system.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • I could put the path where my modbus.h file is however because im running OSX I dont believe the os maintains .so files for linking (correct me if Im wrong) – nemo Mar 31 '14 at 17:19
  • @nemo The suffix should be `.dylib`. – Lee Duhem Mar 31 '14 at 17:29
  • Thank you again! But before I read your comment I tried playing around with it and put the path to the .h, that I #include-d in the .c, instead and it worked! – nemo Mar 31 '14 at 23:54
  • @nemo Maybe Mac toolchain do different things. – Lee Duhem Apr 01 '14 at 00:10
  • ah yes im sure! I did not mean to discredit your answer, I merely wanted to point out that I maybe 'stumbled upon' another means to the same end. – nemo Apr 01 '14 at 00:29