#Help in importing C lib

1 messages · Page 1 of 1 (latest)

torpid estuary
#

I have a c lib with the file structure shown in the picture, how would I import it in zig

urban wharf
#

use the build system (run zig init to get started) and

exe.addIncludePath(b.path("path/to/include"));
exe.addLibraryPath(b.path("path/to/lib"));
exe.linkSystemLibrary("name"); // for libname.so
exe.linkLibC();

then you can do this in the code

const c = @cImport({
    @cInclude("header.h");
});
#

you can do without the build system but then you need to pass a lot of flags, -I, -L, -l

torpid estuary
#

how would I do the bin folder

#

it contains executables

urban wharf
#

what are you going to use those for

torpid estuary
#

they are a part of the lib

#

since the lib only has header files

#

no source files

urban wharf
#

what is inside lib/?

torpid estuary
urban wharf
#

these are the ones you link with, .a for static, .so for dynamic

#

idk what the bin folder has, but you don't link with executables

torpid estuary
#

these arent req then?

urban wharf
#

you wanted to call one of these inside your program?

torpid estuary
#

like these are the files we have to place in the same folder as the output executable when working with C I think

urban wharf
#

oh

torpid estuary
#

so I will just do the same here then?

#

place it in the output folder?

urban wharf
#

i guess you do this b.addInstallBinFile(b.path("path/to/bin/exename"), "exename"); for them

#

it will put those inside the bin folder of the installation prefix

torpid estuary
#

oh

#

nice

#

thanks for the help

#

I was really stressed out for the game jam

urban wharf
#

probably needs this to declare the dependency

const file = b.addInstallBinFile(b.path("path/to/bin/exename"), "exename");
b.getInstallStep().dependOn(&file.step);
lime basalt
#

you can use b.installDirectory(.{ .source_dir = b.path("path/to/bin"), .install_dir = .bin, .install_subdir = "" }); to install an entire directory

#

or b.addInstallDirectory if you don't want to add it to the default install step

torpid estuary
lime basalt
torpid estuary
#

oh

lime basalt
#

you can change the install_dir field if you want them to get copied somewhere else

torpid estuary
#

where can I find the docs for these functions

#

they

#

are not here

lime basalt
#

or b.addWriteFiles() and write_files.addCopyDirectory() if you want full control over everything

torpid estuary
#

okay

lime basalt
torpid estuary
#

thanks

torpid estuary
#

i was able to import the lib but could'nt get it to work with zig

#

is there any way I can write c code to work with the lib and just get the outputs in zig

errant canopy