pub fn main() anyerror!void { const orig_val = 33; var result_val = cfuncs.numAddTen(orig_val); std.log.info("The value return by c function is {}", .{result_val}); }
std.log.info("All your codebase are belong to us. {}", .{MyModule.my_var_val}); } 相对来说,还是rust的 impl Type {} 和 Type::associated_function() 语法更直观,容易阅读理解。 struct的成员都是公开的,但每个zig文件都是一个struct,同时zig文件的函数和类型默认都是非公开的,所以可以通过把struct内容写到一个独立zig文件实现一定程度的隐藏(成员变量无法隐藏)。 ________________________________________ 你以为只有go语言有go命令?NO,NO,NO,zig也有zig命令。 [user@linux 03_zig_cmd]$ zig -h Usage: zig [command] [options]
Commands:
build Build project from build.zig init-exe Initialize a `zig build` application in the cwd init-lib Initialize a `zig build` library in the cwd
ast-check Look for simple compile errors in any set of files build-exe Create executable from source or object files build-lib Create library from source or object files build-obj Create object from source or object files fmt Reformat Zig source into canonical form run Create executable and run immediately test Create and run a test build translate-c Convert C code to Zig code
ar Use Zig as a drop-in archiver cc Use Zig as a drop-in C compiler c++ Use Zig as a drop-in C++ compiler dlltool Use Zig as a drop-in dlltool.exe lib Use Zig as a drop-in lib.exe ranlib Use Zig as a drop-in ranlib
env Print lib path, std path, cache directory, and version help Print this help and exit libc Display native libc paths file or validate one targets List available compilation targets version Print version number and exit zen Print Zen of Zig and exit
pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options // for restricting supported target set are available. const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const mode = b.standardReleaseOptions();
[user@linux 03_zig_cmd]$ file zig-out/bin/03_zig_cmd.exe zig-out/bin/03_zig_cmd.exe: PE32+ executable (console) x86-64, for MS Windows 跟go相似,zig可以通过OS/CPU虚拟层运行交叉编译出来的程序。譬如当我安装了wine,就可以运行交叉编译出来的win32程序: [user@linux 03_zig_cmd]$ zig build run -Dtarget=x86_64-windows-gnu -fwine 0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005 0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005 0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005 0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005 0084:fixme:wineusb:query_id Unhandled ID query type 0x5. info: All your codebase are belong to us. zig支持的虚拟层包括了 [user@linux 03_zig_cmd]$ zig build -h ... -fdarling, -fno-darling Integration with system-installed Darling to execute macOS programs on Linux hosts (default: no) -fqemu, -fno-qemu Integration with system-installed QEMU to execute foreign-architecture programs on Linux hosts (default: no) --glibc-runtimes [path] Enhances QEMU integration by providing glibc built for multiple foreign architectures, allowing execution of non-native programs that link with glibc. -frosetta, -fno-rosetta Rely on Rosetta to execute x86_64 programs on ARM64 macOS hosts. (default: no) -fwasmtime, -fno-wasmtime Integration with system-installed wasmtime to execute WASI binaries. (default: no) -fwine, -fno-wine Integration with system-installed Wine to execute Windows programs on Linux hosts. (default: no) ... zig支持的交叉编译目标包括。。。太多了,从单片机到IBM大型机到WASM、renderscript,从linux、ios到UEFI、到无操作系统(即裸机开发)都全包。可以通过下面命令查看: zig targets zig默认构建Debug模式的程序,可以通过选项选择release-safe、release-fast、release-small这三种模式。开发者可以在不同开发阶段 选用适当的构建模式。 [user@linux 03_zig_cmd]$ zig build -h ... -Drelease-safe=[bool] Optimizations on and safety on -Drelease-fast=[bool] Optimizations on and safety off -Drelease-small=[bool] Size optimizations on and safety off ... ________________________________________ 你以为只有Rust有Option?NO,NO,NO,zig也有。 const std = @import("std");
pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.log.info("memory leaked: {}", .{gpa.deinit()}); // gpa.deinit()会检测内存泄漏,并打印泄漏点 var gpa_allocator = gpa.allocator();
makeLeak(gpa_allocator) catch {}; // 制造泄漏
var arena = std.heap.ArenaAllocator.init(gpa_allocator); // arena使用gpa_allocator做内存申请和释放 defer arena.deinit(); // ArenaAllocator记录内存申请,在deinit()时逐一释放 var arena_allocator = arena.allocator();
var logging_allocator = std.heap.loggingAllocator(arena_allocator); // logging_allocator记录arena_allocator的函数调用 var my_allocator = logging_allocator.allocator();