Skip to content

Commit 2ffd5a7

Browse files
committed
Add callconv(.C) to enforce ABI
1 parent 4c9cf58 commit 2ffd5a7

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ For this we'll create a new Zig file, `zmath_ext.zig`
311311
```c
312312
const zmath_h = @cImport(@cInclude("zmath.h"));
313313
314-
pub extern fn add(a: c_int, b: c_int) c_int;
315-
pub extern fn sub(a: c_int, b: c_int) c_int;
314+
pub extern fn add(a: c_int, b: c_int) callconv(.C) c_int;
315+
pub extern fn sub(a: c_int, b: c_int) callconv(.C) c_int;
316316
```
317317
318-
This file is a declaration of external functions we wish to use Zig. We'll next
319-
create a `zmath.zig`, in which we will create Zig functions that will expose
320-
Zig data types through our API and cast the parameters to their corresponding C
321-
data types before calling the C functions.
318+
This file is a declaration of external functions we wish to use in Zig. We'll
319+
next create a `zmath.zig`, in which we will create Zig functions that will
320+
expose Zig data types through our API and cast the parameters to their
321+
corresponding C data types before calling the C functions.
322322
323323
[`zmath.zig`](src/zmath.zig)
324324
```c
@@ -338,7 +338,7 @@ pub fn sub(a: i32, b: i32) !i32 {
338338
```
339339
340340
As you can see, we translate the C types to Zig specific types for use in Zig
341-
applications. WE cast our input parameters to their C equivalent (`c_int`) for
341+
applications. We cast our input parameters to their C equivalent (`c_int`) for
342342
the C function's parameters. You'll also notice the return type contains `!`,
343343
meaning these functions will now return errors. This means within our
344344
application, we'll need to call the function with `try`.
@@ -457,9 +457,10 @@ pub fn build(b: *std.Build) void {
457457
- https://mtlynch.io/notes/zig-call-c-simple/
458458
What initially made me want to tackle this subject, this article is a great
459459
starting point for understanding C and Zig.
460-
461460
- [Wikipedia article for "Shared Library"](https://en.wikipedia.org/wiki/Shared_library)
462461
- [Wikipedia article for "Static Library"](https://en.wikipedia.org/wiki/Static_library)
462+
- [Discussion about this repository on Ziggit](https://ziggit.dev/t/blog-understanding-how-zig-and-c-interact/6733/7)
463+
463464
464465
465466
## Thanks

src/zmath_ext.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const zmath_h = @cImport(@cInclude("zmath.h"));
22

3-
pub extern fn add(a: c_int, b: c_int) c_int;
4-
pub extern fn sub(a: c_int, b: c_int) c_int;
3+
pub extern fn add(a: c_int, b: c_int) callconv(.C) c_int;
4+
pub extern fn sub(a: c_int, b: c_int) callconv(.C) c_int;

0 commit comments

Comments
 (0)