从零搭建嵌入式zig程序开发 从零搭建zig调用C开发环境wsl ubuntu20.04zig 0.17.0-dev.813目的交叉编译程序并运行在 luckfox pico max板子上zig 调用c接口为以后在单片机上运行做准备创建工程mkdir helloworld cd helloworld zig init工程结构.├── build.zig └── src ├── c.c ├── c.h ├── main.zig └── root.zigbuild.zipconst stdimport(std);pub fnbuild(b:*std.Build)void{//const target b.standardTargetOptions(.{}); //默认配置运行在x86-64平台上const targetb.standardTargetOptions(.{.default_target.{.cpu_arch.arm,.os_tag.linux,//.abi .gnueabihf, //这个可以编译但不能运行在luckfox pico max上.abi.musleabihf,},});const optimizeb.standardOptimizeOption(.{});const modb.addModule(foo,.{.root_source_fileb.path(src/root.zig),.targettarget,});const translate_cb.addTranslateC(.{.root_source_fileb.path(src/c.h),.targettarget,.optimizeoptimize,});const exeb.addExecutable(.{.namefoo,.root_moduleb.createModule(.{.root_source_fileb.path(src/main.zig),.targettarget,.optimizeoptimize,.imports.{.{.namefoo,.modulemod},.{.namec,.moduletranslate_c.createModule()},},.link_libctrue,}),});exe.root_module.addCSourceFile(.{.fileb.path(src/c.c),});exe.root_module.addIncludePath(b.path(src));b.installArtifact(exe);const run_stepb.step(run,Run the app);const run_cmdb.addRunArtifact(exe);run_step.dependOn(run_cmd.step);run_cmd.step.dependOn(b.getInstallStep());run_cmd.addPassthruArgs();const mod_testsb.addTest(.{.root_modulemod,});const run_mod_testsb.addRunArtifact(mod_tests);const exe_testsb.addTest(.{.root_moduleexe.root_module,});const run_exe_testsb.addRunArtifact(exe_tests);const test_stepb.step(test,Run tests);test_step.dependOn(run_mod_tests.step);test_step.dependOn(run_exe_tests.step);}c.c#includec.hvoidhello(void){printf(hello\r\n);}intadd(int x,int y){returnxy;}c.h#includestdio.h#includemath.h#includestdlib.hvoidhello(void);intadd(int x,int y);main.zigconst cimport(c);const stdimport(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 stdimport(std);const Iostd.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{returnab;}testbasic 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