# Embedded data in C header

**URL:** <https://ziggit.dev/t/embedded-data-in-c-header/7410>\
**Category:** Help\
**Tags:** build-system\
**Created:** [December 18, 2024, 6:17pm UTC](https://ziggit.dev/t/embedded-data-in-c-header/7410 "2024-12-18T18:17:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wmiller](https://ziggit.dev/user_avatar/ziggit.dev/wmiller/32/3535_2.png) [@wmiller](https://ziggit.dev/u/wmiller)\
**Post date:** [December 18, 2024, 6:17pm UTC](https://ziggit.dev/t/embedded-data-in-c-header/7410/1 "2024-12-18T18:17:48Z")

</div>

Hello! I’m working on a C project that uses the Zig build system. I have several shader source files in this project that I’d like to embed. If I were writing Zig, I would use @embedFile(), but I’d like these in a C header. I tried cmake-style templates with addConfigHeader(), which worked, but the resulting substitution doesn’t escape newline characters, and won’t compile. My next approach is to make a small Zig lib with these files embedded and link against that in my C program, but was wondering if there is a simpler way. Thanks!

---

<div class="post-metadata">

**Author:** ![ajoino](https://ziggit.dev/user_avatar/ziggit.dev/ajoino/32/3120_2.png) [@ajoino](https://ziggit.dev/u/ajoino)\
**Post date:** [December 18, 2024, 9:01pm UTC](https://ziggit.dev/t/embedded-data-in-c-header/7410/2 "2024-12-18T21:01:15Z")

</div>

C recently (in C23) the [#embed macro](https://stackoverflow.com/questions/74621610/what-is-the-purpose-of-the-new-c23-embed-directive#74621611) which allows you to directly embed files in C code. This is what you’re looking for IIUC.

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [December 18, 2024, 10:22pm UTC](https://ziggit.dev/t/embedded-data-in-c-header/7410/3 "2024-12-18T22:22:31Z")

</div>

AFAIK `#embed` is not yet fully supported by `zig build-exe` or the build system, but there is [an open PR that adds the ability to specify the search path for files to embed](https://github.com/ziglang/zig/pull/21638).

In addition to the option of linking against a small static Zig lib that uses `@embedFile`, another option could be creating a small-ish Zig program that takes an input file and writes a simple C source file that defines a string constant (escaping characters as needed) to an output file. [The “Generating Files” section of the official build system docs](https://ziglang.org/learn/build-system/#generating-files) has some useful examples if you want an idea of how to set up `Compile` and `Run` steps that consume and produce files that can be depended on by other steps.

---

<div class="post-metadata">

**Author:** ![marler8997](https://ziggit.dev/user_avatar/ziggit.dev/marler8997/32/77_2.png) [@marler8997](https://ziggit.dev/u/marler8997)\
**Post date:** [December 18, 2024, 10:22pm UTC](https://ziggit.dev/t/embedded-data-in-c-header/7410/4 "2024-12-18T22:22:45Z")

</div>

Your last approach is the one I would take (and currently take in other projects).

Shaders.h

```zig
const char *my_cool_shader_source;
size_t my_cool_shader_source_len;

```

Shaders.zig

```zig
const my_cool_shader = @embedFile("mycoolshader.sl");
export const my_cool_shader_source: []const u8 = my_cool_shader.ptr;
export const my_cool_shader_len: usize = my_cool_shader.len;

```

P.S. castholm’s suggestion of a codegen step also sounds reasonable
