I made a feature rich C/C++ template for the Zig Build System!

While trying to learn some basic C/C++, I quickly realized part of the reason Zig is so awesome is because most C/C++ build systems kinda suck to use and learn. However, I also learned that by default they are a lot more catered to the C/C++ ecosystem than Zig’s build system is.

So in order to get some of the features and guarantees I wanted while learning C and C++, I decided to create a template that makes setting up these features effortless!

Out of the box it includes support for:

  • Sanitizers like AddressSanitizer, LeakSanitizer, ArrayBoundsSanitizer, NullSanitizer, and more in debug mode with automatic sanitizer library linking!
  • Error enabled warnings for various risky / undefined behaviours
  • Generation of compile_commands.json for integration with clangd
  • Sourcing of an ./include directory for easier header management
  • Example code for linking zig code to C/C++.
  • Support and instructions for use with Jetbrains IDEs and debuggers
  • Automatic recursive searching for C/C++ source files

The template has been tested on NixOs, Fedora, and Windows, but may have additional bugs that need to be worked out. If you use this and run into any please submit and issue!

1 Like

Thanks for sharing. My zig journey also started with its build system and compiler for a C project. I wanted to do cross platform builds hermetically and incrementally (I’m also from a world of building dependencies from source so that was a preference). Zig walked into the room with the usual candidates and it was simply automagical.

I wouldn’t go as far as saying Zig is awesome because most of the other build systems kinda suck, they’re all still impressive in their own ways and context, they have also paved ways forward for each other - software building on top of lessons and ideas from previous generations.

Zigs build system is awesome because of many reasons - two things that stand out for me is its cross platform builds and that your build script is written in zig (so fully programmable).

Hey Kurt, thanks for sharing your thoughts, I appreciate it. And I 100% agree, but for me, so far at least, the scriptable builds with zig have without a doubt been my #1 feature of it. I used to be so scared of trying to learn it, but I slowly and without much effort have learned it to a degree where this project was 80% completed for no reason other than it’s what I wanted for a basic project I was working on.

1 Like

And as a bonus to learning a bit of zig as part of your build system is you can now sprinkle some zig into your project without a lot of friction, which is exactly what happened next for me. Now I tend to just zig instead of C :slightly_smiling_face:

Great work,

I’ve been experimenting also how to use the zig toolchain to build C/C++ projects.

In these repos, I recently updated the example how to pass options to dependencies.

Perhaps It will be useful for your template to add example library as well and pass same compiler options to all the dependencies.

The repo mentions that Zig doesn’t bundle the sanitizers, but it actually does. You do get ubsan enabled by default in debug builds of C code, for example, and you can enable tsan (not sure about asan).

What issues did you encounter when building natively with Zig (instead of depending on a system provided clang)?

Thanks for the comment, I was aware of the UBsan linking but not the Tsan. The issue i ran into was with UBsan on c++ files. This admittedly is an area i need to do more testing on but that’s where the claim came from.

I have since removed the mention of UBsan from the README. Partially because I could never get it to work, adding -fsanitize=undefined causes a garbage error.

As far as I can tell, Asan is not included with Zig at all so in order for it to be used you have to link it manually.

I wish i had seen this comment about four hours ago lmao! I just updated it (per a request on reddit) with examples of how to build and link C and CPP libraries dynamically and statically (and also how to link pre-compiled ones).

Perhaps this is not the best place to mention it but in the future I would really like to see better documentation / examples of the build system from official sources. Part of my motivation when working on this fueled by a desire to improve community resources for those interested in using it.

Perhaps you could contribute this template either in the docs or in the test section of the project. I usually look at the tests to see how the different features are used.

zig/test/standalone/dependency_options at dabae3f9dc868af92e7608d897befc39e5db5c33 · ziglang/zig