From 84b49faada7cdc069ea2e217de64e4ca77add4c0 Mon Sep 17 00:00:00 2001 From: "pegasus.cadence@gmail.com" Date: Tue, 12 Nov 2024 00:26:47 -0800 Subject: [PATCH] anonymous import --- README.md | 35 ++++++++++++++++++++++++++--------- src/main.zig | 2 +- src/root.zig | 4 ++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2ba40a8..134bbdb 100644 --- a/README.md +++ b/README.md @@ -14,22 +14,35 @@ git submodule add -f --name rangex https://github.com/PegasusPlusUS/rangex-zig.g pub fn build(b: *std.Build) void { //... //... - // add library - const lib_while_rangex = b.addStaticLibrary(.{ - .name = "while_rangex", - .root_source_file = b.path("external/while_rangex/src/root.zig"), + //... + + // lib target + const lib = b.addStaticLibrary(.{ + .name = "my_zig_lib", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); - b.installArtifact(lib_while_rangex); + // add anonymous import to facilitate import in exe source files, if cross imported, + // both should add anonymous import + lib.root_module.addAnonymousImport("while_rangex", .{ + .root_source_file = b.path("src/external/while_rangex/src/root.zig"), + }); - // link with exe + // exe target const exe = b.addExecutable(.{ .name = "my_zig_executable", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); + // add anonymous import to facilitate import in exe source files, if cross imported, + // both should add anonymous import + exe.root_module.addAnonymousImport("while_rangex", .{ + .root_source_file = b.path("src/external/while_rangex/src/root.zig"), + }); exe.linkLibrary(lib_while_rangex); } ``` @@ -37,7 +50,9 @@ pub fn build(b: *std.Build) void { ```Zig // Usage: // in src/main.cpp -const while_rangex = @import("external/while_rangex/src/root.zig").WhileRange; +// Use anonymous import instead, can only use one way, to avoid symbol redefinition. +// const while_rangex = @import("external/while_rangex/src/root.zig").WhileRange; +const while_rangex = @import("while_rangex").WhileRange; fn main() !void { var sum:usize = 0; @@ -50,8 +65,10 @@ fn main() !void { ``` ```Zig -// lib_main() and test cases in while_rangex library: -pub fn lib_main() !void { +// Example: +// in src/external/while_rangex/src/root.zig +// main() and test cases in while_rangex library: +pub fn main() !void { // Integer range (forward) var range1 = try WhileRange(i32).init(0, 10, false, 2); std.debug.print("Forward exclusive int range [0, 10) by 2:\n", .{}); diff --git a/src/main.zig b/src/main.zig index 6daf4b9..59115bb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -7,6 +7,6 @@ pub fn main() !void { try lib_main(); } -test "test_main" { +test "test_exe_main" { try main(); } diff --git a/src/root.zig b/src/root.zig index f9c97c3..e6fe11b 100644 --- a/src/root.zig +++ b/src/root.zig @@ -178,7 +178,7 @@ pub fn IndexedWhileRange(comptime T: type) type { }; } -pub fn lib_main() !void { +pub fn main() !void { // Integer range (forward) var range1 = try WhileRange(i32).init(0, 10, false, 2); std.debug.print("Forward exclusive int range [0, 10) by 2:\n", .{}); @@ -214,7 +214,7 @@ pub fn lib_main() !void { test "test_lib_main" { std.debug.print("Running tests in src/root.zig \"main\"\n", .{}); - try lib_main(); + try main(); } test "signed/unsigned int cast" {