Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Last active November 18, 2018 05:27
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 daurnimator/c8359e540988e859eea1897b4c3d4c45 to your computer and use it in GitHub Desktop.
Save daurnimator/c8359e540988e859eea1897b4c3d4c45 to your computer and use it in GitHub Desktop.
Attempt at using zig's export to C feature
#ifndef THING_H
#define THING_H
#include <stdint.h>
#ifdef __cplusplus
#define THING_EXTERN_C extern "C"
#else
#define THING_EXTERN_C
#endif
#if defined(_WIN32)
#define THING_EXPORT THING_EXTERN_C __declspec(dllimport)
#else
#define THING_EXPORT THING_EXTERN_C __attribute__((visibility ("default")))
#endif
struct Node;
struct makeThing(u32);
THING_EXPORT void Init(struct makeThing(u32) * ptr, uint32_t x);
#endif
// Attempt at using zig's export to C feature
// Compile with `zig build-lib --output-h thing.h thing.zig`
const std = @import("std");
const List = std.LinkedList(void);
fn makeThing(comptime member:type) type {
return struct {
const Self = @This();
// intrusive LinkedList
node: List.Node,
/// initialize Thing structure
pub fn init(x:member) Self {
return Self {
.node = List.Node.init(undefined),
.x = x,
};
}
x: member,
pub fn get() member {
return x;
}
};
}
const u32Thing = makeThing(u32);
export fn InitializeThing(ptr: *u32Thing, x:u32) void {
ptr.* = u32Thing.init(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment