由于minizip除了依賴于文件一些操作函數外并不依賴于其他庫,所以個人直接編譯運行;另外本次移植的是使用的xmake完成移植。
移植記錄
1、選擇合適的移植庫,xmake提供了一些可以跨平臺移植的庫,這里我選擇了minizip來進行移植。
2、編寫xmake.lua配置ToolChains并且引入minizip依賴
add_rules("mode.debug", "mode.release")
toolchain("aarch64-linux-musleabi")
set_kind("standalone")
set_sdkdir("$(projectdir)/../../tools/gnu_gcc/aarch64-linux-musleabi_for_x86_64-pc-linux-gnu")
on_load(function(toolchain)
os.setenv("PROJ_DIR", os.projectdir()) --For lua embed build script
toolchain:load_cross_toolchain()
toolchain:set("toolset", "cxx", "aarch64-linux-musleabi-g++")
toolchain:set("toolset", "cc", "aarch64-linux-musleabi-gcc")
-- add flags for aarch64
toolchain:add("cxflags", "-march=armv8-a -D__RTTHREAD__ -Wall -n --static -DHAVE_CCONFIG_H", {force = true})
toolchain:add("ldflags", "-march=armv8-a -D__RTTHREAD__ -Wall -n --static", {force = true})
toolchain:add("ldflags", "-T $(projectdir)/../../linker_scripts/aarch64/link.lds", {force = true})
if not is_config("pkg_searchdirs", "dropbear") then
toolchain:add("ldflags", "-L$(projectdir)/../../sdk/rt-thread/lib/aarch64/cortex-a -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive", {force = true})
end
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/include", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../", {force = true})
toolchain:add("includedirs", "$(projectdir)", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/dfs", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/drivers", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/finsh", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/net", {force = true})
toolchain:add("linkdirs", "$(projectdir)/../../sdk/rt-thread/lib/aarch64", {force = true})
if is_config("kind", "debug") then
toolchain:add("cxflags", "-g -gdwarf-2", {force = true})
else
toolchain:add("cxflags", "-O2", {force = true})
end
end)
toolchain_end()
add_requires("minizip")
target("minizip")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/minizip.c")
add_packages("minizip")
target("miniunz")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/miniunz.c")
add_packages("minizip")
target("minizip_test")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/main.c")
add_packages("minizip")
配置cconfig.h,這個文件如果用scons會自動生成,但是在xmake工程當中不會自動生成,所以需要自己實現
#ifndef CCONFIG_H__
#define CCONFIG_H__
/* Automatically generated file; DO NOT EDIT. /
/ compiler configure file for RT-Thread in GCC/MUSL */
#define HAVE_SYS_SIGNAL_H 1
#define HAVE_SYS_SELECT_H 1
#define HAVE_PTHREAD_H 1
#define HAVE_FDSET 1
#define HAVE_SIGACTION 1
#define HAVE_SIGEVENT 1
#define HAVE_SIGINFO 1
#define HAVE_SIGVAL 1
#endif
3、編寫src/main.c程序,這里我用minizip實現了壓縮example.txt到example.zip
/*
Copyright (c) 2006-2018, RT-Thread Development Team
SPDX-License-Identifier: GPL-2.0
Change Logs:
Date Author Notes
2023-05-17 wcx1024979076 The first version
*/
#include "stdio.h"
#include "zip.h"
int main()
{
// 文件名
const char *zipfile = "example.zip";
// 需要壓縮的文件
const char *file = "example.txt";
zipFile zf = zipOpen(zipfile, APPEND_STATUS_CREATE);
if(zf == NULL)
{
printf("Error creating %s n", zipfile);
return 1;
}
// 壓縮文件
int err = zipOpenNewFileInZip(zf, file, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION);
if(err != ZIP_OK)
{
printf("Error adding %s to %s n", file, zipfile);
return 1;
}
// 讀取文件并壓縮
FILE *f = fopen(file, "rb");
char buf[1024];
int len;
while((len = fread(buf, 1, sizeof(buf), f)) > 0)
{
zipWriteInFileInZip(zf, buf, len);
}
fclose(f);
zipCloseFileInZip(zf);
zipClose(zf, NULL);
printf("Successfully created %s n", zipfile);
return 0;
}
4、xmake編譯鏈接生成mininet可執行文件,打包進入 sd.bin
這里我使用的mcopy來實現的(用的是Codespace來寫的代碼,無root權限,不能使用mount掛載),具體命令為
mcopy -i sd.bin /path/of/the/minizip ::
5、用qemu虛擬機運行即可
運行結果:
-
虛擬機
+關注
關注
1文章
908瀏覽量
28091 -
GNU
+關注
關注
0文章
143瀏覽量
17479 -
RT-Thread
+關注
關注
31文章
1272瀏覽量
39921 -
SRC算法
+關注
關注
0文章
5瀏覽量
7423 -
for循環
+關注
關注
0文章
61瀏覽量
2493
發布評論請先 登錄
相關推薦
評論