Zig Build System Notes

Meow KingMeow King
1 min read

Table of contents

  1. (Tested in Zig 0.13.0) Don’t use addCSourceFile to add a same C libraries that your dependent Zig libraries uses, it will produce an error like ld.lld: duplicate symbol: stbi_failure_reason. Only include the C header path using addIncludePath.

    For concrete example, when you use dvui library. Don’t create a C file like the following(and use `addCSourceFile` to include it) to use stb library.

     #define STB_IMAGE_IMPLEMENTATION
     #include "stb_image.h"
    
  2. We can directly add c libraries in build.zig.zon like the following:

         .dependencies = .{
             .stb = .{
                 .url = "git+https://github.com/nothings/stb.git?ref=master#f75e8d1cad7d90d72ef7a4661f1b994ef78b4e31",
                 .hash = "1220c4fe5a4c4ebec402f5cdef08bc264b56fb07f259107d2b01ba8d416d88624b50",
             },
         },exe.addIncludePath(stb_dep.path(""));
    

    Don’t use zig fetch since it will report an issue like there is no build.zig.zon for the added library.

    Then we can use the library like the following in build.zig.zon:

     exe.addIncludePath(stb_dep.path(""));
    

    If you create a C implementation file, then you can directly use #include clause after this.

3
Subscribe to my newsletter

Read articles from Meow King directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Meow King
Meow King