当前位置: 首页 > news >正文

从零搭建嵌入式zig程序开发

从零搭建zig调用C

开发环境

wsl ubuntu20.04

zig 0.17.0-dev.813

目的

  1. 交叉编译程序并运行在 luckfox pico max板子上
  2. zig 调用c接口为以后在单片机上运行做准备

创建工程

mkdir helloworld cd helloworld zig init

工程结构

.├── build.zig └── src ├── c.c ├── c.h ├── main.zig └── root.zig

build.zip

const std=@import("std");pub fnbuild(b:*std.Build)void{//const target = b.standardTargetOptions(.{}); //默认配置,运行在x86-64平台上const target=b.standardTargetOptions(.{.default_target=.{.cpu_arch=.arm,.os_tag=.linux,//.abi = .gnueabihf, //这个可以编译,但不能运行在luckfox pico max上.abi=.musleabihf,},});const optimize=b.standardOptimizeOption(.{});const mod=b.addModule("foo",.{.root_source_file=b.path("src/root.zig"),.target=target,});const translate_c=b.addTranslateC(.{.root_source_file=b.path("src/c.h"),.target=target,.optimize=optimize,});const exe=b.addExecutable(.{.name="foo",.root_module=b.createModule(.{.root_source_file=b.path("src/main.zig"),.target=target,.optimize=optimize,.imports=&.{.{.name="foo",.module=mod},.{.name="c",.module=translate_c.createModule()},},.link_libc=true,}),});exe.root_module.addCSourceFile(.{.file=b.path("src/c.c"),});exe.root_module.addIncludePath(b.path("src"));b.installArtifact(exe);const run_step=b.step("run","Run the app");const run_cmd=b.addRunArtifact(exe);run_step.dependOn(&run_cmd.step);run_cmd.step.dependOn(b.getInstallStep());run_cmd.addPassthruArgs();const mod_tests=b.addTest(.{.root_module=mod,});const run_mod_tests=b.addRunArtifact(mod_tests);const exe_tests=b.addTest(.{.root_module=exe.root_module,});const run_exe_tests=b.addRunArtifact(exe_tests);const test_step=b.step("test","Run tests");test_step.dependOn(&run_mod_tests.step);test_step.dependOn(&run_exe_tests.step);}

c.c

#include"c.h"voidhello(void){printf("hello\r\n");}intadd(int x,int y){returnx+y;}

c.h

#include<stdio.h>#include<math.h>#include<stdlib.h>voidhello(void);intadd(int x,int y);

main.zig

const c=@import("c");const std=@import("std");pub fnmain()!void{_=c.printf("Hello from C: %f\n",c.sqrt(2.0));c.hello();_=std.c.printf("hello\n");_=c.printf("add test is %d\n",c.add(1,2));}

root.zig

//! By convention, root.zig is the root source file when making a package.const std=@import("std");const Io=std.Io;/// This is a documentation comment to explain the `printAnotherMessage` function below.////// Accepting an `Io.Writer` instance is a handy way to write reusable code.pub fnprintAnotherMessage(writer:*Io.Writer)Io.Writer.Error!void{trywriter.print("Run `zig build test` to run the tests.\n",.{});}pub fnadd(a:i32,b:i32)i32{returna+b;}test"basic add functionality"{trystd.testing.expect(add(3,7)==10);}

开始编译

zig build

编译后目录结构

.├── build.zig ├── src │ ├── c.c │ ├── c.h │ ├── main.zig │ └── root.zig └── zig-out └── bin └── foo

查看可执行文件信息

file zig-out/bin/foo zig-out/bin/foo:ELF32-bit LSB executable,ARM,EABI5 version1(SYSV),statically linked,with debug_info,not stripped

拷备目标板运行

chmod777foo ls-la foo-rwxrwxrwx1root root4478684Jan115:11foo./foo Hello from C:1.414214hello hello add test is3
http://www.jsqmd.com/news/1007057/

相关文章:

  • 解构黑盒:从开源项目看顶级大模型系统提示词的演进与安全边界
  • 马鞍山及周边木质包装厂家汇总,适配仓储、外贸、设备定制包装需求 - 海棠依旧大
  • 计算机Java毕设实战-面向乡镇卫生所的医用物资进销存系统(SpringBoot)设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 3种方法突破百度网盘限速:Mac版SVIP免费提速终极指南
  • 多维聚合实战:用Pandas pivot_table构建可旋转的数据立方体
  • 终极指南:5分钟为WPS Office安装Zotero插件实现高效科研写作
  • 终极XCOM 2模组管理器:AML启动器完整使用指南
  • 2026年速干不伤发!高速吹风机解锁全新居家护发方式
  • MC68HC11定时器核心解析:分频器、溢出与RTI实战指南
  • 青云考研:湖北地区 985/211 名校考研辅导的领军之选 - 辛云教育资讯
  • 河北工商注册公司真相:2026年本土财税公司大揭秘 - 互联百晓生
  • 计算机Java毕设实战-面向校园场景的二手物品置换系统设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 九章算 Joule 解读【折纸结构摩擦纳米发电机】港科广胡国标团队:让机械动作成为数字世界的“输入信号”
  • 2026邢台防水怎么彻底解决?苏易修缮教你根治漏水不复发全攻略 - 苏易修缮
  • Plain Craft Launcher 2:为什么这款免费开源启动器能让你的Minecraft体验提升3倍?
  • 唐山代理注册公司大比拼,2026年工商注册机构能力评估 - 互联百晓生
  • 5大核心功能揭秘:E-Hentai Viewer如何打造iOS端完美漫画阅读体验
  • i.MX23 BCH硬件ECC:原理、配置与DMA链实战
  • i.MX21 UART驱动开发全解析:从原理到实战避坑指南
  • 河北财务代理记账企业必看:2026年本土机构真相解析 - 互联百晓生
  • 2026石家庄防水怎么彻底解决?苏易修缮教你根治漏水不复发全攻略 - 苏易修缮
  • PyTorch核心原语认知地图:Tensor、Module、Autograd、DataLoader与Optimizer深度解析
  • DLSS Swapper终极指南:如何免费管理游戏DLSS版本,提升游戏性能体验
  • 嵌入式低功耗与引脚复用设计:时钟管理与GPIO配置实战
  • 住建部61号部令解读 | BIM强制移交入法!城建档案新规9月1日施行,全生命周期合规再升级!
  • 保定财务管理公司必看!2026年本土财税机构推荐名单 - 互联百晓生
  • 喝酒后浑身无力怎么补救缓解?
  • 北京工商注册公司必看!2026年代理记账机构大揭秘 - 互联百晓生
  • 如何快速掌握XCOM 2模组管理:AML启动器终极指南
  • Larotretinib拉罗替尼治NTRK融合实体瘤,神经系统反应多为一过性