#I'm struggling to build a project using a C library

1 messages · Page 1 of 1 (latest)

distant spear
#

I'm attempting to use zig and a statically compiled "FLECS" library, i have flecs.h and flecs.c both in my project's src folder but regardless of what i try the program 1) will not recognzie that flecs.h exists (VSCode states there is an error that its not found, doesnt get mentioned when trying to compile) and 2) errors regarding assert.h which the internet seems to say is related to an issue with compiling C code with zig. Any advice?
build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const exe = b.addExecutable(.{
        .name = "my-zig-program",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = b.standardOptimizeOption(.{}),
    });

    exe.addCSourceFile(.{
        .file = b.path("src/flecs.c"),
    });

    b.installArtifact(exe);
}```

main.zig
```c
const flecs = @cImport({
    @cInclude("flecs.h"); // Path to your flecs.h
});
const std = @import("std");

pub fn main() !void {...}```
distant sphinx
#

most likely you need a exe.addIncludePath and exe.linkLibC();

distant spear
#

ah, ill give it a try

distant spear
distant sphinx
#

addIncludePath receives a path to the folder that has any header files you need to use in cImport, linkLibC doesnt take any arguments

distant spear
#

ah, thank you!

#

thats working well now, but im still running into a long string of errors, 14 of the following

    note: referenced by C:\Users\user\Documents\Projects\ZigProjects\MainProject\src\flecs.c:23719```
distant sphinx
#

are there other c files flecs uses? you can look at their makefile for what files you need to add

#

undefined symbol errors are usually caused by either forgetting to add c files or not linking a library. depending on whether youre compiling it yourself

distant spear
#

Ah, so with FLECS you're meant to move .c and .h to your source folder, but the rest of the compiled file is elsewhere. What command would I use to define this location? I imagine this is probably what it wants

distant sphinx
distant spear
#

sorry, i have been bashing my head against these since you sent it. how exactly do you use those? I'm really struggling with finding resources and reading the source

#

This just throws errors
```c
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const exe = b.addExecutable(.{
.name = "my-zig-program",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = b.standardOptimizeOption(.{}),
});

exe.addCSourceFile(.{
    .file = b.path("src/flecs.c"),
});

exe.addLibraryPath(.{ .cwd_relative = "C:/Users/usr/Documents/flecs/flecs/Debug" });

exe.linkSystemLibrary("flecs");

b.installArtifact(exe);

}

#

I added the c library which brought me back to the 14 errors i was at before