1.zlib交叉编译
在虚拟机上交叉编译Arm程序,用于压缩解压日志。工作逻辑见图1所示,
图1
其中zlib交叉编译步骤如下:
sudo chmod 777 configure
sudo ./configure
此时已经生成makefile文件(用于gcc编译器linux系统的),由于我想交叉编译跑在Arm程序上,所以需要修改Makefile。
修改Makefile,需要将CC,ldshared,ranlib中的gcc全部改成带地址的自己的交叉编译工具,见图2所示。
图2
修改完成将该文件替换原来的Makefile文件,替换过程忽略。
敲入以下命令:
sudo make
sudo make install
查看没有弹出错误信息就说明编译部署成功了。
参考文章:https://www.cnblogs.com/from-zero/p/12582033.html
动态库/静态库使用参考文章:https://blog.csdn.net/wohu1104/article/details/110789570
2.zlib库部署成功验证逻辑
想在Arm上验证zlib库是否编译成功,要经过一下步骤:
1)在Arm上可以压缩日志。
2)拿到Arm压缩的日志可以在本地解析出来。
为了达到上面两项功能,首先要在本地交叉编译出Arm上跑的软件,不仅需要本地交叉编译工具(芯片提供者提供该工具),还需要
使用该交叉编译工具先将zlib在虚拟机内编译部署(该过程在1中提供),且在Arm上跑的软件程序对应的Makefile中的库引用中加上
-lz,代表引用了zlib库,语法如下:
makefile语法:https://zhuanlan.zhihu.com/p/575852387
3.以上,Arm上跑的软件编译成功,其次需要在Arm上部署zlib库,过程省略(实际用的单片机内有这个库所以没做这个操作)。
Arm上成功运行软件并生成日志,拿到该日志到本地测试是否能解压成功,解压参考如下:
使用zlib实现压缩解压:https://blog.csdn.net/u014608280/article/details/115136125
编写测试程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "errno.h"
#include "zlib.h"
int decompress(const char* src, int srcLen, char* dst, int dstLen);
/*读文件*/
int main()
{
char* str = "hello, hello";
int len = strlen(str);
int res = -1;
int err = 0;
char buf[65536];
char buf_[65536];
int fd = open("./2023-09-18_02-02-57.txt.gz", O_RDONLY);
if (fd < 0)
{
err = errno;
return -1;
}
res = read(fd, buf, 1024);
close(fd);
printf("read = %d\n", res);
for (int i = 0; i < res; i++)
{
printf("%02x", buf[i]);
}
printf("\n");
res = decompress(buf, res, buf_, sizeof(buf_));
buf_[res] = 0;
printf("decompress: %s\n", buf_);
return 0;
}
/*解压文件*/
int decompress(const char* src, int srcLen, char* dst, int dstLen)
{
z_stream strm;
strm.zalloc = NULL;
strm.zfree = NULL;
strm.opaque = NULL;
strm.avail_in = srcLen;
strm.avail_out = dstLen;
strm.next_in = (Bytef*)src;
strm.next_out = (Bytef*)dst;
int err = -1;
err = inflateInit2(&strm, MAX_WBITS + 16);
//err = inflateInit(&strm);
if (err == Z_OK)
{
err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END)
{
(void)inflateEnd(&strm);
printf("decompress succed, before decompress size is %d, after decompress size is %d\n", strm.total_in, dstLen - strm.avail_out);
return strm.total_out;
}
else
{
(void)inflateEnd(&strm);
printf("decompression failed, inflate return: \n");
return -1;
}
}
else
{
inflateEnd(&strm);
printf("decompression initialization failed, quit!\n");
return 0;
}
}