#Libraries in Zig

1 messages · Page 1 of 1 (latest)

wide mist
#

Hi, I'm currently making a library for Zig and I would like to know how to working with making libraries. Like does it work just like C ? Is there something I need to know ?

mighty sphinx
#

What do you mean by'library'? Are you wanting to make static/dynamic libraries that C code can call, or just Zig packages that other Zig code can use (i.e. compiled from source into another Zig project)?

wide mist
#

Zig for Zig

#

Maybe I should have said package yeah

weak belfry
#

well if you're making a zig package then it's pretty much ready for use in other zig code

#

it's mostly about configuring build script

wide mist
#

What about using it in a project ?

#

I just put the source code in src ?

weak belfry
#

No you add it through build zig

#

and then you @import it

#

check some zig libraries for examples

mighty sphinx
#

there are a few different options you could take

wide mist
#

But where do I put the library ?

#

Anywhere ?

weak belfry
#

but not higher than project root

wide mist
#

So in the project

weak belfry
#

but note that you can't do "../*"

wide mist
#

Also, isn't it better making .o files like C ?

mighty sphinx
#

Not really

weak belfry
mighty sphinx
#

Not if a zig compilation is consuming it, you wouldn't do that without a specific reason

#

(like maybe it could speed up compilation times, if that's an issue)

weak belfry
#

if your library is for zig there's no reason to write a C api if you plan to use it in zig.

weak belfry
mighty sphinx
#

oh really?

weak belfry
#

well zig caches artifacts

mighty sphinx
#

true

#

but I'm sure there would be situations where it's a win for compilation speed, though it doesn't feel ziggy

weak belfry
#

and either way, distributing .o files is a bad idea, .a exists for that reason.

mighty sphinx
#

Note that you can put the package source outside your build root (i.e. a directory that is not a child of where your root file is) by using packages in your build.zig

weak belfry
mighty sphinx
#

You can

weak belfry
#

does add package now work with outside path?

#

finally

mighty sphinx
#

You can set up your source tree like this:

project_root/
  - src/ <- we're going to build from a file in here
  - deps/ <- external zig package source files
  - build.zig

You can then have you're build.zig add packages from files in deps to compilations rooted in src

weak belfry
#

this is not what you said

#

deps is child of root

mighty sphinx
#

which is the thing that prevents you from doing @import("../*")

weak belfry
#

what?

#

ok

#

i think we confused @wide mist enough

mighty sphinx
#

probably :P

wide mist
#

Sorry I wasn't reading xD

#

Thanks for the help :D

#

Could you tell me more about build.zig too ?

mighty sphinx
#

build.zig is a file that gets compiled into an executable used for executing build steps

wide mist
#

Yeah, I mean about the Builder

mighty sphinx
#

I mean it really depends on what you want to do - the builder builds a directed graph or steps, you get to decide what those steps do. The best way to learn about it is probably going to be to look at a few articles posted online (https://zig.news/xq/zig-build-explained-part-1-59lf comes to mind, not sure if it will contain anything that has changed since it was written), read the source code (look in lib/std/build.zig and lib/std/build/*, and if you want lib/build_runner.zig which is what executes build()), or you could search for and ask in #1019652020308824145 about specific things you want to accomplish.

Zig NEWS

The Zig build system is still missing documentation and for a lot of people, this is a killer...