Skip to content

Instantly share code, notes, and snippets.

@McSinyx
Created September 7, 2021 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McSinyx/2626241649133de7170139061702305f to your computer and use it in GitHub Desktop.
Save McSinyx/2626241649133de7170139061702305f to your computer and use it in GitHub Desktop.
Zig extern struct recognized differently from Zig and C
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const exe = b.addExecutable("test", "main.zig");
exe.addIncludeDir(".");
exe.linkSystemLibrary("c");
exe.setTarget(b.standardTargetOptions(.{}));
exe.setBuildMode(b.standardReleaseOptions());
exe.install();
}
const makeFoo = @import("shared.zig").makeFoo;
const useFoo = @cImport(@cInclude("middle.h")).useFoo;
pub fn main() void {
useFoo(makeFoo());
}
#ifndef MIDDLE_H
#define MIDDLE_H
#include "shared.h"
void useFoo(struct Foo foo) { }
#endif // MIDDLE_H
#ifndef SHARED_H
#define SHARED_H
#include <stdint.h>
struct Foo {
float bar;
int16_t baz;
};
struct Foo makeFoo(void);
#endif // SHARED_H
const Foo = extern struct {
bar: f32 = 4.20,
baz: i16 = 69,
};
pub export fn makeFoo() Foo {
return Foo{};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment