Zig bindings to libcurl

Hi guys, I create a libcurl bindings, check it out if you need stable HTTP access in Zig.

As always, any feedback is welcome!


Is libcurl difficult to build?

It would be great to have a zig package that builds libcurl with zig so that your bindings can be used without adding a system library dependency. This is especially cool for cross compilation.

1 Like

Bundle static libcurl is a non-trivial task.

libcurl depends on many other libs, such as libz/nghttp2/openssl, if we want to cross compile, we need to static build them all.

This is what I got by ldd basic example on a debian box (libcurl is installed by libcurl4-openssl-dev package):

root@69f7632577fd:/app# ldd zig-out/bin/basic 
	linux-vdso.so.1 (0x0000ffffa7560000)
	libcurl.so.4 => /usr/lib/aarch64-linux-gnu/libcurl.so.4 (0x0000ffffa748c000)
	libpthread.so.0 => /lib/aarch64-linux-gnu/libpthread.so.0 (0x0000ffffa745b000)
	libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffffa72e7000)
	/lib/ld-linux-aarch64.so.1 (0x0000ffffa7530000)
	libnghttp2.so.14 => /usr/lib/aarch64-linux-gnu/libnghttp2.so.14 (0x0000ffffa72ac000)
	libidn2.so.0 => /usr/lib/aarch64-linux-gnu/libidn2.so.0 (0x0000ffffa727c000)
	librtmp.so.1 => /usr/lib/aarch64-linux-gnu/librtmp.so.1 (0x0000ffffa724f000)
	libssh2.so.1 => /usr/lib/aarch64-linux-gnu/libssh2.so.1 (0x0000ffffa720d000)
	libpsl.so.5 => /usr/lib/aarch64-linux-gnu/libpsl.so.5 (0x0000ffffa71eb000)
	libssl.so.1.1 => /usr/lib/aarch64-linux-gnu/libssl.so.1.1 (0x0000ffffa714e000)
	libcrypto.so.1.1 => /usr/lib/aarch64-linux-gnu/libcrypto.so.1.1 (0x0000ffffa6ea0000)
	libgssapi_krb5.so.2 => /usr/lib/aarch64-linux-gnu/libgssapi_krb5.so.2 (0x0000ffffa6e42000)
	libldap_r-2.4.so.2 => /usr/lib/aarch64-linux-gnu/libldap_r-2.4.so.2 (0x0000ffffa6ddd000)
	liblber-2.4.so.2 => /usr/lib/aarch64-linux-gnu/liblber-2.4.so.2 (0x0000ffffa6dbe000)
	libbrotlidec.so.1 => /usr/lib/aarch64-linux-gnu/libbrotlidec.so.1 (0x0000ffffa6da3000)
	libz.so.1 => /lib/aarch64-linux-gnu/libz.so.1 (0x0000ffffa6d79000)
	libunistring.so.2 => /usr/lib/aarch64-linux-gnu/libunistring.so.2 (0x0000ffffa6bed000)
	libgnutls.so.30 => /usr/lib/aarch64-linux-gnu/libgnutls.so.30 (0x0000ffffa69d1000)
	libhogweed.so.6 => /usr/lib/aarch64-linux-gnu/libhogweed.so.6 (0x0000ffffa6979000)
	libnettle.so.8 => /usr/lib/aarch64-linux-gnu/libnettle.so.8 (0x0000ffffa6926000)
	libgmp.so.10 => /usr/lib/aarch64-linux-gnu/libgmp.so.10 (0x0000ffffa689e000)
	libgcrypt.so.20 => /usr/lib/aarch64-linux-gnu/libgcrypt.so.20 (0x0000ffffa67ce000)
	libdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000ffffa67ba000)
	libkrb5.so.3 => /usr/lib/aarch64-linux-gnu/libkrb5.so.3 (0x0000ffffa66cf000)
	libk5crypto.so.3 => /usr/lib/aarch64-linux-gnu/libk5crypto.so.3 (0x0000ffffa6691000)
	libcom_err.so.2 => /lib/aarch64-linux-gnu/libcom_err.so.2 (0x0000ffffa667d000)
	libkrb5support.so.0 => /usr/lib/aarch64-linux-gnu/libkrb5support.so.0 (0x0000ffffa6660000)
	libresolv.so.2 => /lib/aarch64-linux-gnu/libresolv.so.2 (0x0000ffffa6639000)
	libsasl2.so.2 => /usr/lib/aarch64-linux-gnu/libsasl2.so.2 (0x0000ffffa660d000)
	libbrotlicommon.so.1 => /usr/lib/aarch64-linux-gnu/libbrotlicommon.so.1 (0x0000ffffa65da000)
	libp11-kit.so.0 => /usr/lib/aarch64-linux-gnu/libp11-kit.so.0 (0x0000ffffa648b000)
	libtasn1.so.6 => /usr/lib/aarch64-linux-gnu/libtasn1.so.6 (0x0000ffffa6467000)
	libgpg-error.so.0 => /lib/aarch64-linux-gnu/libgpg-error.so.0 (0x0000ffffa6434000)
	libkeyutils.so.1 => /lib/aarch64-linux-gnu/libkeyutils.so.1 (0x0000ffffa641f000)
	libffi.so.7 => /usr/lib/aarch64-linux-gnu/libffi.so.7 (0x0000ffffa6404000)

Libcurl rust bindings support static built libcurl, but still leaves other dependencies dynamic link.

Would you mind file an issue describing what we can get by bundle static libcurl?

FYI, a certain Andrew K already zigged libz: GitHub - andrewrk/libz: zlib with the build system replaced by zig. openssl also seems zigged: GitHub - kassane/openssl-zig: TLS/SSL and crypto library (uses Zig Build).

Another option might be to use one of the wasmer builds curl/curl - wasmer,
and then use some wasm runtime to run it, either one written in zig or c.
As anyone played around with something like this?

Would be interesting to see some comparisons between wasm and native, code size, memory and cpu time.
I also wonder whether the wasmer curl build is a full curl, or maybe a somewhat simplified version,
I guess I will investigate eventually, but I am currently busy with other things.

Thanks for the links, I create an issue to track this.

1 Like

While the bits are rotten, I had put together a static libcurl for gyro: GitHub - mattnite/zig-libcurl: compile libcurl in your build.zig. Feel free to port it to the official package manager for your own project.


Here’s a PR

1 Like