Skip to content

Commit 8af8cb5

Browse files
committed
Add ex5, stub in other sections
1 parent 4794bcd commit 8af8cb5

2 files changed

Lines changed: 61 additions & 17 deletions

File tree

README.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ well-versed in C, can leverage the power of third-party C libraries.
3030
This should be straight-forward. I will create a simple math library in C, with
3131
functions defined in a header file and implemented in the source file.
3232

33-
[`zmath.h`](include/zmath.h)
34-
```
33+
`zmath.h`
34+
```c
3535
int add(int a, int b);
3636
int sub(int a, int b);
3737
```
3838
39-
[`zmath.c`](src/zmath.c)
40-
```
39+
`zmath.c`
40+
```c
4141
#include "zmath.h"
4242
4343
int add(int a, int b) {
@@ -51,8 +51,8 @@ int sub(int a, int b) {
5151

5252
Next I created a simple application to utilize this library.
5353

54-
[`main.c`](src/main.c)
55-
```
54+
`main.c`
55+
```c
5656
#include <stdio.h>
5757
#include "zmath.h"
5858

@@ -72,7 +72,7 @@ int main(void) {
7272
7373
### Compiling the application using `zig cc`
7474
75-
If you're familiar with gcc, this is no problem. Here's the command to compile
75+
If you're familiar with `gcc`, this is no problem. Here's the command to compile
7676
this application:
7777
7878
```sh
@@ -92,8 +92,8 @@ Now run the resulting executable:
9292

9393
Now we can create a file called `build.zig`, which Zig will use to build our application.
9494

95-
[`build.zig`](build_c_compiled_with_zig_build.zig)
96-
```zig
95+
`build.zig`
96+
```c
9797
const std = @import("std");
9898

9999
pub fn build(b: *std.Build) void {
@@ -125,7 +125,7 @@ Things to note:
125125
- `exe.addIncludePath()` takes a `LazyPath`, and it just so happens that `std.Build` (the type of `b`) contains a function to provide a `LazyPath` object given a string.
126126
- `exe.addCSourceFiles()` takes a `struct` that includes a `files` property. This property is a pointer to an array of strings. I'll break down `&[_][]const u8 {}` real quick in plain English:
127127
128-
```zig
128+
```c
129129
&[_][]const u8 {
130130
"..",
131131
"...",
@@ -143,6 +143,7 @@ Things to note:
143143
// a reference to an automatically sized array of array of constants u8 objects.
144144
// a pointer to the address in memory where an array of arrays of u8 exists
145145
```
146+
146147
_Probably overkill of an explanation, but maybe someone will benefit from this._
147148

148149
- Next is `exe.linkLibC()`. This is the extremely convenient way that Zig links to libc. There's also `linkLibCpp()`, which could be useful to keep in mind. If you use a standard library, such as `stdio.h`, make sure to include `linkLibC()`, otherwise you'll get a compilation error when trying to build.
@@ -154,6 +155,7 @@ zig build
154155
```
155156

156157
And run the resulting executable:
158+
157159
```sh
158160
./zig-out/bin/c_compiled_with_zig_build.exe
159161
10 + 5 = 15
@@ -166,8 +168,8 @@ Same results as compiling with `zig cc`! Very cool. Let's move on to using a bit
166168

167169
Basically, I want to recreate my `main.c` in Zig. In this case, this is trivial.
168170

169-
[`main.zig`](src/zig_linked_to_c.zig)
170-
```zig
171+
`main.zig`
172+
```c
171173
const std = @import("std");
172174
const zmath = @cImport(@cInclude("zmath.h"));
173175

@@ -189,8 +191,8 @@ Fairly simple to understand. The only gotcha is using printing the output. Next
189191
we have to modify `build.zig` and point it to our `main.zig` file
190192
instead of `main.c`.
191193

192-
[`build.zig`](build_zig_linked_to_c.zig)
193-
```zig
194+
`build.zig`
195+
```c
194196
const std = @import("std");
195197

196198
pub fn build(b: *std.Build) void {
@@ -231,7 +233,7 @@ exposing them to the application code.
231233
For this I'll create a new Zig file, `zmath.zig`
232234
233235
`zmath.zig`
234-
```zig
236+
```c
235237
const zmath = @cImport(@cInclude("zmath.h"));
236238
237239
pub fn add(a: i32, b: i32) !i32 {
@@ -256,7 +258,7 @@ call the function with `try`.
256258
Let's update our `main.zig` file now
257259

258260
`main.zig`
259-
```zig
261+
```c
260262
const std = @import("std");
261263
const zmath = @import("zmath.zig");
262264

@@ -294,7 +296,7 @@ distributed code.
294296
Let's write out `build.zig` file:
295297

296298
`build.zig`
297-
```zig
299+
```c
298300
const std = @import("std");
299301

300302
pub fn build(b: *std.Build) *std.Build.Step.Compile {
@@ -325,6 +327,19 @@ Instead of `b.addStaticLibrary()`, use `b.addSharedLibrary()`.
325327
326328
### Linking a Zig application to a Pre-built C Library
327329
330+
Assuming you followed along, you'll have some library files we'll link our
331+
Zig application to directly, rather than including the source files in our build
332+
code.
333+
334+
`main.zig`
335+
```c
336+
337+
```
338+
339+
`build.zig`
340+
```c
341+
342+
```
328343

329344

330345
## Side Quests

build.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const module_ex2 = @import("build_zig_linked_to_c.zig");
55
const module_ex3 = @import("build_zig_c_wrapper.zig");
66
const module_ex4 = @import("build_c_static_library_with_zig_build.zig");
77
const module_ex5 = @import("build_c_shared_library_with_zig_build.zig");
8+
// const module_ex6 = @import("build_zig_linked_to_c_static_lib.zig");
9+
// const module_ex7 = @import("build_zig_linked_to_c_shared_lib.zig");
810

911
pub fn build(b: *std.Build) void {
1012
const target = b.standardTargetOptions(.{});
@@ -45,4 +47,31 @@ pub fn build(b: *std.Build) void {
4547

4648
const install_ex4_step = b.step("ex4", "Create a shared library file from C source code.");
4749
install_ex4_step.dependOn(&install_ex4.step);
50+
51+
// ex5
52+
const ex5 = module_ex5.build(b, target, optimize);
53+
const install_ex5 = b.addInstallArtifact(ex5, .{});
54+
55+
b.installArtifact(ex5);
56+
57+
const install_ex5_step = b.step("ex5", "Create a static library file from C source code.");
58+
install_ex5_step.dependOn(&install_ex5.step);
59+
60+
// ex6
61+
// const ex6 = module_ex6.build(b, target, optimize);
62+
// const run_ex6 = b.addInstallArtifact(ex6, .{});
63+
64+
// b.installArtifact(ex6);
65+
66+
// const run_ex6_step = b.step("ex6", "Run a Zig application that is statically linked to a C library.");
67+
// run_ex6_step.dependOn(&run_ex6.step);
68+
69+
// ex7
70+
// const ex7 = module_ex7.build(b, target, optimize);
71+
// const run_ex7 = b.addInstallArtifact(ex7, .{});
72+
73+
// b.installArtifact(ex7);
74+
75+
// const run_ex7_step = b.step("ex7", "Run a Zig application that is dynamically linked to a C library.");
76+
// run_ex7_step.dependOn(&run_ex7.step);
4877
}

0 commit comments

Comments
 (0)