在C中编译包含libmodbus的程序

时间:2014-03-27 19:37:39

标签: c linux makefile modbus

我是C的新手...我写了一个非常简单的modbus1.c,包括libmodbus(我下载的源代码,解压缩,解压缩,./configure' d,make' d和make install&# 39; d成功)。

当我尝试制作modbus1.c时,我得到了这个:

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

我正在运行OSX雪豹并成功使用make来编译小程序之前(教程级程序......)这是我试图编译的modbus1.c:

#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;
}

任何帮助将不胜感激!谢谢!

-Niko

1 个答案:

答案 0 :(得分:1)

试试这个

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

您应该将/path/to/libmodbus替换为系统中包含libmodbus.dylib的目录的实际路径。