编制方案,其中包括用C libmodbus [英] compiling a program that includes libmodbus in C

查看:770
本文介绍了编制方案,其中包括用C libmodbus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C新手...我写了一个很简单的modbus1.c包括libmodbus(其源我下载,解压缩后,未解压,./configure'd,make'd并install'd成功)。

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).

当我试图让modbus1.c我得到这样的:

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

我运行OSX雪豹已经成功地使用make编译之前(教学水平,课程...)小程序,这是我想要编译modbus1.c:

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

任何帮助将大大AP preciated!谢谢!

Any help will be greatly appreciated! Thanks!

-Niko

推荐答案

试试这个

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

您应该替换 /路径/要/ libmodbus 与包括目录的实际路径 libmodbus.dylib 在您的系统。

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

这篇关于编制方案,其中包括用C libmodbus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆