# How to ensure that a function definition matches the prototype in a C header file

**URL:** <https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579>\
**Category:** Help\
**Tags:** c\
**Created:** [April 12, 2025, 1:08pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579 "2025-04-12T13:08:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpirch](https://ziggit.dev/user_avatar/ziggit.dev/dpirch/32/5321_2.png) [@dpirch](https://ziggit.dev/u/dpirch)\
**Post date:** [April 12, 2025, 1:08pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579/1 "2025-04-12T13:08:12Z")

</div>

If I write a library with a C API but implemented in Zig, with a handwritten header file (necessary with `-femit-h` not working, but it might be preferable anyways to have a user-friendly header with comments etc. instead of something automatically generated), is there a way to check that the implementation in Zig matches the function prototypes of the header file?

If I implemented the library in C, I would simply #include the header file, and the compiler produce errors if something doesn’t match.

But in Zig, for example if the header file `mathtest.h` is

```zig
double add(double a, double b);

```

and the zig implementation is

```zig
const c = @cImport(@cInclude("mathtest.h")); // doesn't do anything

// definition for c.add (but the types are wrong!)
export fn add(a: f32, b: f32) f32 {
    return a + b;
}

```

this gets compiled to a library and linked to a C program without errors, and just leads to undefined behavior at runtime.

---

<div class="post-metadata">

**Author:** ![Eisenhauer](https://ziggit.dev/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@Eisenhauer](https://ziggit.dev/u/Eisenhauer)\
**Post date:** [April 12, 2025, 2:19pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579/2 "2025-04-12T14:19:10Z")

</div>

Just a quick idea without having a compiler at hand.

In zig you should be able to compare the types of `add` and `c.add`. Place those comparisons in the first line of your exported functions and emit a `@compileError` if they do not match.

---

<div class="post-metadata">

**Author:** ![dpirch](https://ziggit.dev/user_avatar/ziggit.dev/dpirch/32/5321_2.png) [@dpirch](https://ziggit.dev/u/dpirch)\
**Post date:** [April 12, 2025, 4:02pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579/3 "2025-04-12T16:02:25Z")

</div>

A test like this seems to work too:

```zig
test "matches header" {
    try @import("std").testing.expectEqual(@TypeOf(c.add), @TypeOf(add));
}

```

This indeed finds the mismatch (“expected type fn (f64, f64) callconv(.c) f64, found type fn (f32, f32) callconv(.c) f32”).

I would have to check every function though. Maybe it is possible to loop through all functions? But `@typeInfo(@This()).@"struct".decls` contains only the names of `pub` functions, and no types.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [April 12, 2025, 6:15pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579/4 "2025-04-12T18:15:32Z")

</div>

You can use [`@field`](https://ziglang.org/documentation/master/#field) to access a declaration by name.

```zig
const decls = @typeInfo(T).@"struct".decls;
inline for (decls) |decl| {
    if (std.meta.hasFn(T, decl.name) {
        const fld = @field(T, decl.name);
        // @TypeOf(fld) works here
    }
}

```

---

<div class="post-metadata">

**Author:** ![chung-leong](https://ziggit.dev/user_avatar/ziggit.dev/chung-leong/32/10638_2.png) [@chung-leong](https://ziggit.dev/u/chung-leong)\
**Post date:** [April 12, 2025, 10:01pm UTC](https://ziggit.dev/t/how-to-ensure-that-a-function-definition-matches-the-prototype-in-a-c-header-file/9579/5 "2025-04-12T22:01:12Z")

</div>

Declare the function as public in addition to using `export`. That allows you to loop thru the functions:

```zig
const std = @import("std");

pub export fn add(a: f32, b: f32) f32 {
    return a + b;
}

test {
    inline for (comptime std.meta.declarations(@This())) |decl| {
        const NewFn = @TypeOf(@field(@This(), decl.name));
        @compileLog(NewFn);
        // ...
    }
}

```
