linux C语言开发系列
Libmodbus 移植
文章目录
一、源代码下载
Two branches of libmodbus are currently maintained:
当前,libmodbus 有两个分支:
development release v3.1.6 , released on 2019-07-31. See the release notes. This development version is very stable and will be marked as stable very soon.
开发版本v3.1.6,发布于2019-07-31,请参考发行说明。这个发行版本非常稳定,并且很快将被标记为稳定的。
old release v3.0.8 . See the release notes. Almost no changes since 2013-10-06, should NOT be used on new project.
之前的版本v3.0.8,请参考发行说明。从2013-10-06以来几乎没有变化,不建议使用在新项目中。这里是引用
所以我们下载,v3.1.6
二、交叉编译
1.copy安装包到ubuntu
可以通过ftp工具,将下载好的压缩包放到ubuntu中/home/gx/linux/gx/tool中,读者可以根据自己情况放置。
2.创建安装目录
cd /home/gx/linux/gx/tool
mkdir libmodbus
3.解压安装包
tar -vxzf libmodbus-3.1.6.tar.gz
4.配置make参数
./configure --host=[交叉编译工具链前缀] --prefix=[安装路径] --enable-static
./configure --host=arm-linux-gnueabihf --prefix=/home/gx/linux/gx/tool/libmodbus --enable-static
–host 用于指定交叉编译器的前缀
–host=arm-linux-gnueabihf
–prefix 用于指定编译后的安装路径
–prefix=/home/gx/linux/gx/tool/libmodbus
–enable-static 生成静态链接库
5.编译
make
6.安装
make install
在安装目录下,会生成三个目录:include lib share
lib目录下的libmodbus.so libmodbus.so.5 libmodbus.so.5.1.0就是动态链接库文件,需要复制到开发板的/usr/lib目录下
三、测试
1.创建VS code工程
2.复制头文件
将/usr/local/modbus/install/include/modbus中的四个头文件modbus.h、modbus-rtu.h、modbus-tcp.h、modbus-version.h复制到工程目录中
3.编写C源文件
网上关于libmodbus的使用太多了,这里就不赘述了。