ImportC + nuklear = success!

Chris wendlec at tcd.ie
Mon Jun 27 13:58:32 UTC 2022


On Monday, 27 June 2022 at 12:00:52 UTC, Max Samukha wrote:
> On Sunday, 26 June 2022 at 21:47:46 UTC, ryuukk_ wrote:
>
>>
>> Thanks Walter for working on ImportC, it already is super 
>> useful,
>
> Probably not until there is a reasonable way to map C files to 
> D modules.

In Zig you can do this (which is useful for cross-compilation)

"Linking a library by source

But we also have a very different way of linking libraries with 
the Zig toolchain:

We can just compile them ourselves!

This gives us the benefit that we can much much easier 
cross-compile our programs. For this, we need to convert the 
libraries build files into our build.zig. This typically requires 
a pretty good understanding of both build.zig and the build 
system your library uses. But let's assume the library is 
super-simple and just consists of a bunch of C files:

```
pub fn build(b: *std.build.Builder) void {
     const cflags = [_][]const u8{};

     const curl = b.addSharedLibrary("curl", null, .unversioned);
     curl.addCSourceFile("vendor/libcurl/src/tool_main.c", 
&cflags);
     curl.addCSourceFile("vendor/libcurl/src/tool_msgs.c", 
&cflags);
     curl.addCSourceFile("vendor/libcurl/src/tool_dirhie.c", 
&cflags);
     curl.addCSourceFile("vendor/libcurl/src/tool_doswin.c", 
&cflags);

     const exe = b.addExecutable("chapter-3", "src/main.zig");
     exe.linkLibC();
     exe.addIncludeDir("vendor/libcurl/include");
     exe.linkLibrary(curl);
     exe.install();
}
```

With this, we can use both addSharedLibrary and addStaticLibrary 
to add libraries to our LibExeObjStep.

This is especially convenient as we can use setTarget and 
setBuildMode to compile from everywhere to everywhere." [1]

[1] https://zig.news/xq/zig-build-explained-part-3-1ima


More information about the Digitalmars-d mailing list