33My journey to understanding how Zig interacts with C and how I, someone not
44well-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
268278we use it just as you'd expect. Lastly, we have to update our ` build.zig ` file
269279to 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
278289We'll use our ` zmath ` library we wrote in C and build it into a shared libary so
279290that 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
0 commit comments