我是一个C。。。我写了一个非常简单的modbus1.c,其中包括libmodbus(我下载了它的源代码,解压,未更正,./configure'd,make'd,make install'd successfully)。
当我试着做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;
}

任何帮助都将不胜感激!谢谢!
-尼科

最佳答案

试试这个

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

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

10-06 05:21