Skip to content

Commit 4794bcd

Browse files
committed
Added building libraries
1 parent 196179d commit 4794bcd

5 files changed

Lines changed: 89 additions & 31 deletions

README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
My journey to understanding how Zig interacts with C and how I, someone not
44
well-versed in C, can leverage the power of third-party C libraries.
55

6-
This journey will include the following:
6+
## What This Doesn't Cover
77

8+
- Using Zig in C
9+
- Using C
10+
- C at all
11+
12+
## TOC
13+
14+
[The Journey](#the-journey)
815
- [Create a Simple C application](#create-a-simple-c-application)
916
- [Compiling the application using `zig cc`](#compiling-the-application-using-zig-cc)
1017
- [Leverage Zig's build system to build the C application](#leverage-zigs-build-system-to-build-the-c-application)
1118
- [Create a Zig application that links to the C library](#create-a-zig-application-that-links-to-the-c-library)
1219
- [Create a Zig wrapper around a C Function](#create-a-zig-wrapper-around-a-c-function)
13-
- [Create a Zig binding for the C library](#create-a-zig-binding-for-the-c-library)
20+
- [Using Zig to build a C Library](#using-zig-to-build-a-c-library)
21+
- [Linking a Zig application to a Pre-built C Library](#linking-a-zig-application-to-a-pre-built-c-library)
22+
23+
[Side Quests](#side-quests)
24+
- [Testing C code in Zig](#testing-c-code-in-zig)
1425

15-
- [Testing C code in Zig???](#)
1626

1727
## The Journey
1828
### Create a Simple C Application
@@ -268,12 +278,13 @@ You can see, we use `@import("zmath.zig")` to import our wrapper functions. Then
268278
we use it just as you'd expect. Lastly, we have to update our `build.zig` file
269279
to account for these changes.
270280

271-
Our `build.zig` file is the same as last time. We still need to link the C code,
272-
which is annoying and doesn't reflect how you might link C to Zig in "real-world"
273-
situations, such as only has a library file. We'll cover linking C library files
274-
in Zig, but first we'll build our library using Zig's build system.
281+
Our `build.zig` file is the same as last time. We link our source codes directly,
282+
but what if the situation occurs where you have a library file you intend/to use
283+
instead? Linking to a library file in Zig is simple, but first we need to create
284+
our library files. I'll next cover creating static and shared libraries, using
285+
our `zmath` C library.
275286

276-
### Using Zig to build your C Library
287+
### Using Zig to build a C Library
277288

278289
We'll use our `zmath` library we wrote in C and build it into a shared libary so
279290
that we can dynamically link to rather than needing to include the source code
@@ -284,10 +295,40 @@ Let's write out `build.zig` file:
284295

285296
`build.zig`
286297
```zig
287-
c
298+
const std = @import("std");
299+
300+
pub fn build(b: *std.Build) *std.Build.Step.Compile {
301+
const target = b.standardTargetOptions(.{});
302+
const optimize = b.standardOptimizeOption(.{});
303+
304+
const lib = b.addStaticLibrary(.{
305+
.name = "c_static_library_with_zig_build",
306+
.target = target,
307+
.optimize = optimize,
308+
});
309+
310+
lib.addIncludePath(b.path("include"));
311+
lib.addCSourceFiles(.{ .files = &[_][]const u8{"src/zmath.c"} });
312+
313+
lib.linkLibC();
314+
315+
b.installArtifact(lib);
316+
}
288317
```
289318

290-
###
319+
Again, very simple example. We add our source files, include directory, link it
320+
to libc and install it. When we run `zig build`, our library will be compiled to
321+
a static library file, `zig-out/lib/c_static_library_with_zig_build.lib`.
322+
323+
If you want to compile shared libraries instead, there's not much difference.
324+
Instead of `b.addStaticLibrary()`, use `b.addSharedLibrary()`.
325+
326+
### Linking a Zig application to a Pre-built C Library
327+
328+
329+
330+
## Side Quests
331+
332+
Some extra thoughts I have about integrating Zig and C together.
291333

292-
## Bonus!
293334
### Testing C code in Zig

build.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const std = @import("std");
33
const module_ex1 = @import("build_c_application_with_zig_build.zig");
44
const module_ex2 = @import("build_zig_linked_to_c.zig");
55
const module_ex3 = @import("build_zig_c_wrapper.zig");
6-
const module_ex4 = @import("build_c_library_with_zig_build.zig");
6+
const module_ex4 = @import("build_c_static_library_with_zig_build.zig");
7+
const module_ex5 = @import("build_c_shared_library_with_zig_build.zig");
78

89
pub fn build(b: *std.Build) void {
910
const target = b.standardTargetOptions(.{});
@@ -38,10 +39,10 @@ pub fn build(b: *std.Build) void {
3839

3940
// ex4
4041
const ex4 = module_ex4.build(b, target, optimize);
41-
const run_ex4 = b.addRunArtifact(ex4);
42+
const install_ex4 = b.addInstallArtifact(ex4, .{});
4243

4344
b.installArtifact(ex4);
4445

45-
const run_ex4_step = b.step("ex4", "Create a shared library file from C source code.");
46-
run_ex4_step.dependOn(&run_ex4.step);
46+
const install_ex4_step = b.step("ex4", "Create a shared library file from C source code.");
47+
install_ex4_step.dependOn(&install_ex4.step);
4748
}

build_c_library_with_zig_build.zig

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
4+
const lib = b.addSharedLibrary(.{
5+
.name = "c_shared_library_with_zig_build",
6+
.target = target,
7+
.optimize = optimize,
8+
});
9+
10+
lib.addIncludePath(b.path("include"));
11+
lib.addCSourceFiles(.{ .files = &[_][]const u8{"src/zmath.c"} });
12+
13+
lib.linkLibC();
14+
15+
return lib;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
4+
const lib = b.addStaticLibrary(.{
5+
.name = "c_static_library_with_zig_build",
6+
.target = target,
7+
.optimize = optimize,
8+
});
9+
10+
lib.addIncludePath(b.path("include"));
11+
lib.addCSourceFiles(.{ .files = &[_][]const u8{"src/zmath.c"} });
12+
13+
lib.linkLibC();
14+
15+
return lib;
16+
}

0 commit comments

Comments
 (0)