#Help in importing C lib
1 messages · Page 1 of 1 (latest)
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
what are you going to use those for
what is inside lib/?
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
these arent req then?
you wanted to call one of these inside your program?
like these are the files we have to place in the same folder as the output executable when working with C I think
oh
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
probably needs this to declare the dependency
const file = b.addInstallBinFile(b.path("path/to/bin/exename"), "exename");
b.getInstallStep().dependOn(&file.step);
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
what does installing an entire directory imply?
copying the contents of the directory into the install_dir, in this case the bin folder, so by default zig-out/bin
oh
you can change the install_dir field if you want them to get copied somewhere else
or b.addWriteFiles() and write_files.addCopyDirectory() if you want full control over everything
okay
docs are very incomplete unfortunately, gotta look at the source code for a lot of build system stuff: https://github.com/ziglang/zig/blob/bb70501060a8bfff25818cf1d80491d724f8a634/lib/std/Build.zig#L1615-L1617
thanks
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
That's a bit too vague to really help with that.