//声明#include<stdio.h> const c = @cImport({ // See https://github.com/ziglang/zig/issues/515 @cDefine("_NO_CRT_STDIO_INLINE", "1"); @cInclude("stdio.h"); }); pub fn main() anyerror!void { std.log.info("get a char from stdin.", .{}); //调用c函数getchar var ch = c.getchar(); std.log.info("get char:{d}\n",.{ch}); }
test "basic test" { try std.testing.expectEqual(10, 3 + 7); }
2.在build.zig中加入c库
exe.linkLibC();
build.zig const std = @import("std");
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();