compiling a program that includes libmodbus in C

℡╲_俬逩灬. 提交于 2020-01-06 08:31:38

问题


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


回答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.



来源:https://stackoverflow.com/questions/22697543/compiling-a-program-that-includes-libmodbus-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!