libmodbus编译安装使用

【编译】

 ./configure --host=arm-fsl-linux-gnueabi --enable-static --prefix=[安装路径]/install/
或者
./configure --host=arm-linux --prefix=/opt/libmodbus/install

# make
# make install

其中--prefix为输出目录,
编译中如遇到undefined reference to 'rpl_malloc',删除config.h中237行的"#define malloc rpl_malloc",重新make 。

“--host=arm-linux”表示采用交叉编译,因为编译出来的程序要运行在开发板上。“--prefix=/opt/libmodbus/install”表示libmodbus的安装目录。

编译成功后,在install生成三个目录:include  lib  share;libmodbus-3.1.4/tests目录下会有测试样例。

把libs目录下的libmodbus.so文件放到开发板中


【测试】
将random-test-server.c中22行"ctx = modbus_new_tcp("127.0.0.1", 1502);" 
改成"ctx = modbus_new_tcp(NULL, 1502);",即server监听所有的ip,端口1502,然后编译:

# arm-linux-gcc random-test-server.c -o random-test-server -I /home/linux/libmodbus-3.1.4/install/include/modbus -L /home/linux/libmodbus-3.1.4/install/lib -lmodbus

将编译生成的测试程序放在开发板中。

【运行】
    将libmodbus库拷贝到开发板上,如/usr/lib目录下;运行demo程序./random-test-server;


【工程应用】
添加头文件


#include "modbus/modbus.h"
#include "modbus/modbus-rtu.h"
#include "modbus/modbus-version.h"

QtCreator:
然后在工程文件中(.pro)添加:


LIBS += -L/opt/libmodbus/install/lib -lmodbus

 

转载:http://blog.chinaunix.net/uid-31511072-id-5781791.html  作者:lunZhang

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值