Add Zig with C example

This commit is contained in:
Maciej Krzyżanowski 2024-08-22 23:51:43 +02:00
commit 730666e26a
5 changed files with 45 additions and 0 deletions

2
zig-with-c/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
zig-out/
.zig-cache/

27
zig-with-c/build.zig Normal file
View File

@ -0,0 +1,27 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "zig-with-c",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.use_llvm = false,
.use_lld = false,
});
exe.addCSourceFile(.{
.file = b.path("src/lib.c"),
});
exe.addIncludePath(b.path("include"));
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const run_step = b.step("run", "Run the application");
run_step.dependOn(&run_exe.step);
}

2
zig-with-c/include/lib.h Normal file
View File

@ -0,0 +1,2 @@
int add(int a,
int b);

5
zig-with-c/src/lib.c Normal file
View File

@ -0,0 +1,5 @@
int add(int a,
int b)
{
return a + b;
}

9
zig-with-c/src/main.zig Normal file
View File

@ -0,0 +1,9 @@
const std = @import("std");
const c = @cImport({
@cInclude("lib.h");
});
pub fn main() void {
std.debug.print("Hello!\n", .{});
std.debug.print("{}\n", .{c.add(3, 5)});
}