Skip to content

Commit 5a3583d

Browse files
committed
Update readme
1 parent 82c1eaf commit 5a3583d

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ well-versed in C, can leverage the power of third-party C libraries.
2020
- [Create a Zig application that links to the C library](#create-a-zig-application-that-links-to-the-c-library)
2121
- [Using Zig to build a C Library](#using-zig-to-build-a-c-library)
2222
- [Create a Zig wrapper around a C Function](#create-a-zig-wrapper-around-a-c-function)
23+
- [Linking to the Static/Shared Library](#linking-to-the-staticshared-library)
2324
- [Side Quests](#side-quests)
2425
- [Testing C code in Zig](#testing-c-code-in-zig)
2526
- [Resources](#resources)
@@ -296,11 +297,6 @@ Instead of `b.addStaticLibrary()`, use `b.addSharedLibrary()`.
296297
297298
_[See the difference in build.zig](build_c_shared_lib.zig)_
298299
299-
It would be wise to note that here we build our library and then import it from
300-
a path. If you build from source, use `linkLibrary(*Compile)` rather than
301-
`linkLibrary2()`, as Zig will compile faster than your OS can save the library
302-
file, causing it not to be found during build time.
303-
304300
### Create a Zig wrapper around a C Function
305301
306302
This is a simple example, so wrapping the C function in Zig may be overkill.
@@ -371,7 +367,36 @@ pub fn main() !void {
371367
}
372368
```
373369
374-
You should be able to use the same `build.zig` file as before to run this.
370+
### Linking to the Static/Shared Library
371+
372+
With this in place, and our static/shared library created, we can use `build.zig`
373+
to link our application to our library.
374+
375+
[`build.zig` for static libray](build_zig_app_static.zig)
376+
```c
377+
const std = @import("std");
378+
379+
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
380+
const exe = b.addExecutable(.{
381+
.name = "zig_app_shared",
382+
.root_source_file = b.path("src/zig_c_wrapper.zig"),
383+
.target = target,
384+
.optimize = optimize,
385+
});
386+
387+
exe.addObjectFile(b.path("zig-out/lib/zmath-shared.lib"));
388+
389+
return exe;
390+
}
391+
```
392+
393+
It would be wise to note that here we built our library and then import it from
394+
a path (using `addObjectFile()`). If you build from source, use `addObject(*Compile)`
395+
instead and pass in the proper object. This is because Zig will compile faster
396+
than your OS can save the library file, causing the build to fail because the
397+
library file could not be found during the build time of this object (at least
398+
when using `dependsOn()`, like I do in my main [`build.zig`](build.zig) file
399+
for this repo).
375400
376401
377402
## Side Quests

0 commit comments

Comments
 (0)