#compile C dll and include zig headers

1 messages · Page 1 of 1 (latest)

strange berry
#

I'm not sure how to compile my zig files to c headers to include in my c main file.
I have my DllMain.c, including an DllMain function. I want to call a function defined in my zig file. How do I configure build.zig to make this happen?

#

this might be a tough one cause i normally had gotten answers within minutes lol

bright blade
#

you also havent really been very clear with what you want

#

you want to call a zig function from C?

strange berry
strange berry
#

that works if what youre building from c is an executable

bright blade
#

it does, it says how to create a zig function with a C callconv

#

then you can just call that from C

#

link against the zig code

strange berry
#

How do I configure build.zig to make this happen?

bright blade
#

how do you build your C and Zig code?

strange berry
#

based off of that build.zig

#
const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));

    const exe = b.addExecutable("test", null);
    exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
    exe.linkLibrary(lib);
    exe.linkSystemLibrary("c");

    b.default_step.dependOn(&exe.step);

    const run_cmd = exe.run();

    const test_step = b.step("test", "Test the program");
    test_step.dependOn(&run_cmd.step);
}```
bright blade
#

okay then they should already be linked into the same executable, no?

#

you can just create a header file containing the right function declaration for the C code to call it

strange berry
#

is there a way to do that automatically?

bright blade
#

i think theres a way to tell it to emit headers, check the cli flags and docs

strange berry
#

when i use emit_h it tries to import zig.h

strange berry
#

it seems rather outdated

bright blade
#

its only slightly outdated, only the code samples need a couple changes

#

the rest of it seems correct

strange berry
#

even fixing it, it doesnt work

bright blade
#

doesnt work how

strange berry
#

if i dont use emit_h, the header just isnt there for the c file

bright blade
#

seems to just be broken

strange berry
#

excellent

strange berry
#

got it to work.. a lot of manual editing though

#

subpar

#

thank you for your help, though