Skip to content

Commit

Permalink
anonymous import
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 12, 2024
1 parent 5eea128 commit 84b49fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,45 @@ 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);
}
```

```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;
Expand All @@ -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", .{});
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub fn main() !void {
try lib_main();
}

test "test_main" {
test "test_exe_main" {
try main();
}
4 changes: 2 additions & 2 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 84b49fa

Please sign in to comment.