Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created February 4, 2023 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/d1e6173448ab2bc350233cc20025ba56 to your computer and use it in GitHub Desktop.
Save andrewrk/d1e6173448ab2bc350233cc20025ba56 to your computer and use it in GitHub Desktop.
example of embedding a generated executable inside another one
pub export fn _start() void {}
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const bootloader = b.addExecutable(.{
.name = "bootloader",
.root_source_file = .{ .path = "bootloader.zig" },
.target = .{
.cpu_arch = .x86,
.os_tag = .freestanding,
},
.optimize = .ReleaseSmall,
});
const exe = b.addExecutable(.{
.name = "embed-generated-file",
.root_source_file = .{ .path = "main.zig" },
.target = target,
.optimize = optimize,
});
exe.addAnonymousModule("bootloader.elf", .{
.source_file = bootloader.getOutputSource(),
});
exe.install();
}
const std = @import("std");
const blah = @embedFile("bootloader.elf");
pub fn main() !void {
std.debug.print("first 3 bytes: " ++ blah[0..3] ++ "\n", .{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment