#How to cross-compile C libraries such as libcurl?

1 messages · Page 1 of 1 (latest)

tired frost
#

I'm using libcurl for HTTP POST requests, importing with:

const cURL = @cImport({
    @cInclude("curl/curl.h");
});

With this in build.zig:

    exe.linkLibC();
    exe.linkSystemLibrary("curl");

Compilation works great on my native platform (macOS), but fails when targeting other platforms such as Linux:

> zig build -Drelease-safe=true -Dtarget=x86_64-linux
/Users/nick/Play/zig/myproject/src/http.zig:3:14: error: C import failed
const cURL = @cImport({

What do I need to do to be able to cross-compile libcurl for multiple platforms on macOS?

Do I need to bundle libcurl headers with my application? If so, which files do I need from https://github.com/curl/curl ?

GitHub

A command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP...

terse peak
#

You need to cross compile the whole curl library

bitter lodge
#

^ This.
What you're currently doing is telling Zig to link with the system prebuilt binaries, IIUC.
But in order to build for other platforms, you must build the C code from source. (Or use some logic in the build.zig file to link with already-prebuilt versions of the libraries for the other platforms accordingly.)

But I would recommend building from source if you can, as that's got less nonsense to go wrong, and is simpler if the library is simpler.

How you must do this depends on how the library is set up, but the simplest is a library that just has one-or-more C files to compile.
In that case, you'd just replace the call to linkSystemLibrary with a call to exe.addCSourceFiles(&.{ "a.c", "b.c", "c.c" }, &.{}); (the second arg is a slice of strings that represent the C compilation flags to build them with.)

#

If a library uses CMake instead to build it, then while it is probably possible to invoke the CMake invocation from the build.zig and pull in the resulting objects... it's nicer in the end (and easier to work with) if you can translate what the CMake stuff is doing into Zig code in your build.zig instead.

#

It may or may not be easier to port the makefile, if it has one, rather than the cmake. Depends on the library, of course.

tired frost
#

Thanks for the tips, I'll explore some more!

bitter lodge
#

You're quite welcome! o7