Using a zig library as a dependency in another project (C / C++)

What are the current available options for creating a shared library to be used by other projects.

It would be incredibly interesting to start experimenting with zig on a smaller scale and use Zig libraries in a current project. Instead of creating a full new project, I am wondering about the current possibilities of integrating a zig library in an existing C++ project.

More of a general thread with links or ideas on how to achieve this. While it’s very good that Zig supports both C and C++ libraries, being able to use Zig in a current project step by step would also likely increase the adoption of Zig, as we could move away parts of a codebase to it little by little.

2 Likes

Hello @nm-remarkable
Welcome to ziggit :slight_smile:

You can create from zig, static or dynamic libraries usable from C.

Add the export keyword before the fn keyword of the zig function definition, or use the @export builtin to give C linkage to a function.
Zig compiler complains if the exported function arguments and return type are unusable because they are not valid C API types.

To make a static library run zig build-lib [filename.zig]
To make a dynamic library run zig build-lib [filename.zig] -dynamic
Then reference the zig function as extern from C/C++/rust/… code and link with the produced library.

3 Likes