Skip to content

Instantly share code, notes, and snippets.

@sdwfrost
Created November 6, 2019 05:13
Show Gist options
  • Save sdwfrost/107ed141f841ce434839b725f1bbc7a1 to your computer and use it in GitHub Desktop.
Save sdwfrost/107ed141f841ce434839b725f1bbc7a1 to your computer and use it in GitHub Desktop.

Compiling the C file:

gcc -o test test.c -I . -I /usr/include -lc

Compiling the Zig file throws an error:

$zig build-exe ztest.zig -I . --library c
Semantic Analysis [690/699] ztest.zig:10:13: error: type '[*c].cimport:4:11.foo_t' does not support field access
    warn(f.?.x.?.y);
            ^
zig-cache/o/KTsLMnJBj_oMpD4ScU0kKunypkMOyrUqGwh01tBqUm6BmJTDGFma2NXIgWNkcfAA/cimport.zig:378:8: error: type '[*c].cimport:4:11.foo_t' does not support field access
    f.?.x = new_bar(y);
#include <stdio.h>
#include "test.h"
int main(int argc, char* argv[]){
foo_t *f = new_foo(3);
printf("%d\n",f->x->y);
}
#include <stdlib.h>
typedef struct {
int y;
} bar_t;
typedef struct {
bar_t * x;
} foo_t;
bar_t *new_bar(int y){
bar_t *b = (bar_t*)calloc(1,sizeof(bar_t));
b->y = y;
return b;
}
foo_t *new_foo(int y){
foo_t *f = (foo_t*)calloc(1,sizeof(foo_t));
f->x = new_bar(y);
return f;
}
const warn = @import("std").debug.warn;
const c = @cImport({
@cInclude("test.h");
});
pub fn main() void {
var f: [*c]c.foo_t = c.new_foo(3);
warn(f.?.x.?.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment