This is a MCVE of an issue between C/Zig interaction that it is driving me crazy in the last couple of days. Please, let me know it if is worth a bug report in the ziglang/zig repository or if I’m missing something obvious.
It is triggered by a specific sequence of argument types I still have not fully identified.
zigc.h
:
typedef struct {
char padding[24];
} Pad24;
typedef struct {
char padding[16];
} Pad16;
int cFunction(Pad24 a, Pad24 b, Pad24 c, Pad24 d, Pad24 e, Pad16 f, int sentinel);
zigc.c
:
#include "zigc.h"
int cFunction(Pad24 a, Pad24 b, Pad24 c, Pad24 d, Pad24 e, Pad16 f, int sentinel)
{
return sentinel;
}
zigc.zig
:
const std = @import("std");
const c = @cImport({
@cInclude("zigc.h");
});
pub fn main() !void {
const pad24 = std.mem.zeroes(c.Pad24);
const pad16 = std.mem.zeroes(c.Pad16);
const expected: c_int = 1234;
const actual = c.cFunction(pad24, pad24, pad24, pad24, pad24, pad16, expected);
// On my x86_64-pc-linux-gnu, I get "Expected: 1234, actual 0"
std.debug.print("Expected: {d}, actual {d}\n", .{ expected, actual });
}