#Import C code into zig code, then import zig code into C code

1 messages · Page 1 of 1 (latest)

strong tartan
#

For fun, I have decided to follow this guide on a pokemon gba decomp using zig 0.15.2. While I have gotten it to build and can import zig code into C, I've decided to expand out into trying to convert some existing code to zig just to see what I could do. My current goal is to replace 1 (one) function in a file with a zig function in a new file, import the dependencies in zig using cImport, then import the translated zig function back into its original file for use in the C code. (I'm handwriting the headers. It's fine.)

The problem I'm running into is importing the C code. The project doesn't have a build.zig and is just using a makefile, which you can see in the article. Any help?

Personal blog on software development, game modding

opal halo
#

either call the zig compiler from make, or replace make with a build.zig.
with the former you build either the zig or c as object(s) or library(s) and link to which ever is the executable/library.
with the latter you can do it in one compilation unit, you can do that with the former too but it'd be complicated with just the zig cli.

you just export things from zig, and in c declare the external symbols, you can do that in a header if you want. zig will in the future (and did in the past) support generating a header from zig code. but it is currently broken.

strong tartan
#

i am currently calling the zig compiler from make (replacing make sounds too hard rn it's doing too much). here's the code: ```make
$(ZIG_BUILDDIR)/%.o : $(ZIG_SUBDIR)/%.zig
$(ZIG) build-obj $< -femit-bin=$@ -target thumb-freestanding-eabi -mcpu arm7tdmi

my current issue is having difficulty importing c headers from the zig code
opal halo
#

I cant help you if you dont elaborate on 'difficulty importing c headers'

strong tartan
#

sorry gimme a sec

#

trying to recreate the issue

opal halo
#

btw, you certainly dont want to compile each zig file as a seperate object, instead compile a root file that imports the rest of the files.

strong tartan
#

ok i am building a basic example and i hope it works

strong tartan
#

ok! i could not get it to work

#

this is where my problem was

#
const c_random = @cImport({
    @cInclude("random.h");
});

const rng_value_t = c_random.Sfc32State;

pub export fn LocalRandomSeed(seed: u32) rng_value_t {
    var result: rng_value_t = undefined;
    c_random.SFC32_Seed(&result, seed, 1);
    return result;
}
#

so this is a simple function that depends on some code in a c file elsewhere, and then is compiled according to the makefile in the article

#

i have written a header file separately for it, which does work when being imported in c code

typedef struct Sfc32State rng_value_t;

extern rng_value_t LocalRandomSeed(u32 seed);
#

i think this is correct?

#

anyways i try running make, and it fails with this

zig/random.zig:1:18: error: C import failed
const c_random = @cImport({
                 ^~~~~~~~
zig/random.zig:1:18: note: libc headers not available; compilation does not link against libc
referenced by:
    rng_value_t: zig/random.zig:17:21
    LocalRandomSeed: zig/random.zig:19:42
    4 reference(s) hidden; use '-freference-trace=6' to see all references
/Users/eviefinch/.cache/zig/o/63fd5f0ce4e2bf06637b1d3ef328be04/cimport.h:1:10: error: 'random.h' file not found
#include <random.h>
opal halo
#

are you linking with libc? the flag for that is -lc

strong tartan
#

tyyyy

strong tartan
#

wooo

#

linking with libc, and using -I to import the folder for the c code and the folder for the headers have worked

#

now i'm running into a tiny little issue and that's just in the actual source code

strong tartan
#

so good news! i've gotten to this point where now it's complaining about something i can't seem to understand

#

oh wait no i think i understand what it's complaining about

#

shoot