Skip to content

Commit 19e8f6a

Browse files
committed
zig clean & refactor
1 parent 7c4590b commit 19e8f6a

File tree

3 files changed

+26
-47
lines changed

3 files changed

+26
-47
lines changed

Diff for: examples/hello_zig/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include $(APPDIR)/Make.defs
2525
MAINSRC = hello_zig.zig
2626

2727
# Hello, Zig! built-in application info
28-
28+
ZIGFLAGS += -lc -I$(TOPDIR)/include
2929
PROGNAME = $(CONFIG_EXAMPLES_HELLO_ZIG_PROGNAME)
3030
PRIORITY = $(CONFIG_EXAMPLES_HELLO_ZIG_PRIORITY)
3131
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_ZIG_STACKSIZE)

Diff for: examples/hello_zig/hello_zig.zig

+25-6
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,36 @@
2424
const std = @import("std");
2525

2626
//****************************************************************************
27-
//* Externs
27+
//* C API - need libc linking (freestanding libc is stubs only)
2828
//****************************************************************************
29+
// nuttx namespace
30+
const nuttx = struct {
31+
pub const c = @cImport({
32+
@cInclude("nuttx/config.h");
33+
@cInclude("stdio.h");
34+
});
35+
};
2936

30-
pub extern fn printf(_format: [*:0]const u8) c_int;
37+
// or (optional) const c = std.c; // from std library (non-full libc)
3138

39+
// typedef alias
40+
const printf = nuttx.c.printf;
3241
//****************************************************************************
3342
//* hello_zig_main
3443
//****************************************************************************
35-
pub export fn main(_argc: c_int, _argv: [*]const [*]const u8) u8 {
36-
_ = _argc;
37-
_ = _argv;
38-
_ = printf("Hello, Zig!\n");
44+
comptime {
45+
// rename to hello_zig_main entry point for nuttx
46+
@export(hello_zig, .{
47+
.name = "hello_zig_main",
48+
.linkage = .weak,
49+
});
50+
}
51+
52+
fn hello_zig(_: c_int, _: ?[*]const [*]const u8) callconv(.C) c_int {
53+
print("[{s}]: Hello, Zig!\n", .{nuttx.c.CONFIG_ARCH_BOARD});
3954
return 0;
4055
}
56+
57+
fn print(comptime fmt: [*:0]const u8, args: anytype) void {
58+
_ = printf(std.fmt.comptimePrint(std.mem.span(fmt), args));
59+
}

Diff for: examples/hello_zig/hello_zig_main.zig

-40
This file was deleted.

0 commit comments

Comments
 (0)