Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to link external library into zig_binary/library/test #146

Closed
persososo opened this issue Dec 5, 2023 · 3 comments
Closed

How to link external library into zig_binary/library/test #146

persososo opened this issue Dec 5, 2023 · 3 comments

Comments

@persososo
Copy link

persososo commented Dec 5, 2023

Is #15 necessary to write something like:

zig_binary(
    name = "my-bin",
    main = "my-bin.zig",
    deps = ["@FOO//:bar"],
)

where @FOO//:bar is an external C/C++ library ?

This seems indeed like a most welcome addition.

Is there any way to link in a C/C++ library into a zig_library/binary/test atm ?

In my trials I hit does not have mandatory providers: 'ZigPackageInfo'.

Thanks for the great work, looking forward to connecting my first toy programs with it!

@aherrmann
Copy link
Owner

Is #15 necessary to write something like:

Yes, that's correct. #15 was phrased as being about zig_binary specifically. But, by now the underlying implementation of zig_binary|library|test is the same. So, I've generalized the ticket to capture all of these.

This seems indeed like a most welcome addition.

I'd love to hear if you have some specific use-case and if you have any constraints about how this should work.

In particular, as #15 points out, Zig can link libraries in two different ways, either by filepath, or by -l flags. I.e. either some/path/to/libfoo.a, or -lfoo. Would your use-case have a preference for one or the other way of doing this?

Also, Bazel's cc_library targets capture both static and dynamic libraries at the same time and it is up to the downstream user to decide which to use. The cc_* rules offer control through a linkstatic attribute and otherwise prefer static linking over dynamic linking except for tests where it is the other way around. Now there is also cc_shared_library in Bazel.

Does your use-case have any constraints wrt a preference for static or dynamic linking?


At the moment my plan would be to start with a very simple implementation, exposing a cdeps attribute, passing libraries by filepath if possible, and preferring static linking.

zig_binary(
    name = "my-bin",
    main = "my-bin.zig",
    cdeps = ["@FOO//:bar"],
)

@persososo
Copy link
Author

Thanks for your reply, I don't have specific use cases yet beyond kicking the tires of C / zig interop under bazel; static library is the sensible thing for me to start with.

At the moment my plan would be to start with a very simple implementation, exposing a cdeps attribute, passing libraries by filepath if possible, and preferring static linking.

This would be great!

@aherrmann
Copy link
Owner

@persososo Thanks for sharing your thoughts on this! I'll close this as a duplicate of #15. Since #15 is already intended to cover this use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants