Add Zig with C example
This commit is contained in:
commit
730666e26a
2
zig-with-c/.gitignore
vendored
Normal file
2
zig-with-c/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
zig-out/
|
||||||
|
.zig-cache/
|
27
zig-with-c/build.zig
Normal file
27
zig-with-c/build.zig
Normal 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
2
zig-with-c/include/lib.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
int add(int a,
|
||||||
|
int b);
|
5
zig-with-c/src/lib.c
Normal file
5
zig-with-c/src/lib.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
int add(int a,
|
||||||
|
int b)
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
9
zig-with-c/src/main.zig
Normal file
9
zig-with-c/src/main.zig
Normal 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)});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user