I have created a zig lib using the below code:
//lib/src/main.zib
const std = @import("std");
const file_paths = [_][]const u8{
"www/x/in.txt",
"www/emd.txt",
"www/index.html",
"www/e2.txt",
};
const EmbeddedFile = extern struct {
path: [*:0]const u8,
content: [*:0]const u8,
};
export const embedded_files = blk: {
var fs: [file_paths.len]EmbeddedFile = undefined;
for (file_paths) |file_path, i| {
const embedded_file = EmbeddedFile{
.path = file_path[0.. :0],
.content = @embedFile(file_path)[0.. :0],
};
fs[i] = embedded_file;
}
break :blk fs;
};
Cross compiled it as static library from Linux to Windows as below:
//lib/build.zig
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
const target = std.zig.CrossTarget{ .os_tag = .windows, .cpu_arch = .x86_64 };
const lib = b.addStaticLibrary("Zig", "src/main.zig");
lib.setOutputDir("static/");
lib.setTarget(target);
lib.setBuildMode(mode);
lib.install();
const main_tests = b.addTest("src/main.zig");
main_tests.setBuildMode(mode);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
}
Then I wrote the below file to be used as executable:
//exe/src/main.zig
const std = @import("std");
const EmbeddedFile = struct {
path: [*:0]const u8,
content: [*:0]const u8,
};
extern const embedded_files: [4]EmbeddedFile;
pub fn main() !void {
for (embedded_files) |file| {
std.debug.print("file:\t\t{s}\n", .{file.path});
std.debug.print("content:\t{s}\n", .{file.content});
}
}
And crossed compiled it also from Linux to Windows as:
//exe/build.zig
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
const target = std.zig.CrossTarget{ .os_tag = .windows, .cpu_arch = .x86_64 };
const exe = b.addExecutable("Zig", "src/main.zig");
exe.addObjectFile("../lib/static/Zig.lib");
exe.setOutputDir("shared/");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
}
And every thing done smoothly with this approach.
Now I'm trying to use the same files but by building shared lib instead of static one, so I just changed the build.zig
files to be:
- For the shared lib:
//lib/build.zig
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
const target = std.zig.CrossTarget{ .os_tag = .windows, .cpu_arch = .x86_64 };
const lib = b.addSharedLibrary("Zig", "src/main.zig", .unversioned);
lib.setOutputDir("shared/");
lib.setTarget(target);
lib.setBuildMode(mode);
lib.install();
const main_tests = b.addTest("src/main.zig");
main_tests.setBuildMode(mode);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
}
- For the executable file:
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
const target = std.zig.CrossTarget{ .os_tag = .windows, .cpu_arch = .x86_64 };
const exe = b.addExecutable("Zig", "src/main.zig");
exe.addObjectFile("../lib/shared/Zig.lib");
exe.setOutputDir("shared/");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
}
The shared library had been built, and I can see the files Zig.dll, Zig.lib, Zig.pdb, Zig.dll.obj
, but with building the executable file I got the error below:
❯ zig build
error: lld-link: undefined symbol: embedded_files
note: referenced by /home/hasany/Documents/Zig/embed-exe/zig-cache/o/2efedc7a8930ae173549e998c2b6a39a/Zig.exe.obj:(.refptr.embedded_files)
error: Zig...
error: The following command exited with error code 1:
/usr/bin/zig build-exe /home/hasany/Documents/Zig/embed-exe/src/main.zig /home/hasany/Documents/Zig/embed-lib/shared/Zig.lib --cache-dir /home/hasany/Documents/Zig/embed-exe/zig-cache --global-cache-dir /home/hasany/.cache/zig --name Zig -target x86_64-windows -mcpu x86_64 --enable-cache
error: the following build command failed with exit code 1:
/home/hasany/Documents/Zig/embed-exe/zig-cache/o/6344cfbe514e2a1e155387d0d1fd1294/build /usr/bin/zig /home/hasany/Documents/Zig/embed-exe /home/hasany/Documents/Zig/embed-exe/zig-cache /home/hasany/.cache/zig