#Linking c file with zig build.

1 messages · Page 1 of 1 (latest)

tight garden
#

I am trying to include in my main.zig c code

I have added the following to my build.zig

    exe.addCSourceFile(.{ .file = b.path("csrc/cbrr.c"), .flags = &.{ "-std=c99", "-O2" } });

but when I am trying to import compiler can resolve that reference, what am I doing wrong, is it actually possible?

const cbrr = @cImport({
    @cInclude("csrc/cbrr.c");
});
#

https://mtlynch.io/notes/zig-call-c-simple/ I was trying to do smth similar to this, but example is probably irrelevant to current version of zig

outer gale
#

if you statically link the C source files, you should just cInclude the header that defines everything
Or just write the bindings yourself with extern functions

tight garden
#

so in build file I need to build static library first and then include header of my c source?

outer gale
#

Your build.zig is already right

#

You just need to cInclude a header instead of a source file

tight garden
#

'csrc/cbrr.h' file not found
#include <csrc/cbrr.h>
^

outer gale
#

i mean
Does that file exist lol

tight garden
#

it does

outer gale
#

You probably just have to add the root directory to your include path then
With exe.addIncludePath

indigo pier
#

Plus you shoulc @cImport the header not the source file

tight garden
#

exe.addIncludePath(.{ .src_path = b.path(".") });

#

?

indigo pier
#

exe.addIncludePath(b.path("./csrc"));

#

I think

#

the path should be the folder of you header file

tight garden
#

worked but error: ld.lld: undefined symbol: brr_c
note: referenced by main.zig:57
error: the following command failed with 1 compilation errors:
/home/jurip/Programs/zig/zig build-exe -I/usr/include/SDL2 -D_REENTRANT -lSDL2 -lmpg123 -lasound -ODebug -I /home/j
urip/Code/tinkerbunk/csrc -Mroot=/home/jurip/Code/tinkerbunk/src/main.zig -lc --cache-dir /home/jurip/Code/tinkerbu
nk/.zig-cache --global-cache-dir /home/jurip/.cache/zig --name tinkerbunk --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install tinkerbunk transitive failure
└─ zig build-exe tinkerbunk Debug native 1 errors
error: the following build command failed with exit code 1:
/home/jurip/Code/tinkerbunk/.zig-cache/o/62d8ae89d7a1523e9172a715159e2306/build /home/jurip/Programs/zig/zig /home/
jurip/Code/tinkerbunk /home/jurip/Code/tinkerbunk/.zig-cache /home/jurip/.cache/zig --seed 0xaf4f1ab2 -Zff2c55e69d2
52f3d

#

should I add extern declarations?

outer gale
tight garden
#

brr.c

#include "cbrr.h"
#include <alsa/asoundlib.h>
#include <mpg123.h>
#include <stdio.h>
#include <stdlib.h>

#define PCM_DEVICE "default"

void brr_c(char *filename) {

outer gale
#

Actually it doesnt look like youre compiling cbrr.c at all
According to the zig build-exe command

tight garden
#

brr.h
void brr_c(char *filename);

#

from build zig

const exe = b.addExecutable(.{
    .name = "tinkerbunk",
    .root_source_file = b.path("src/main.zig"),
    .target = target,
    .optimize = optimize,
});

// linking for linux only
// TODO: link for mac later
exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("mpg123");
exe.linkSystemLibrary("asound");
exe.linkLibC();
exe.addIncludePath(b.path("./csrc"));

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
outer gale
tight garden
#

worked thank you very much

outer gale
#

np

tight garden
#

this is freaking awesome, now If I stuck with wrapping some c code or it's complicated to wrap it I can simply implement some c adapter and use it in zig