In my build file, I had some code that copies the generated shared library (.so, .dll, .dylib) to a particular location. Code doesn’t compile with master/0.14.0 so I made the following adjustment:
const wf = switch (@hasDecl(std.Build, "addUpdateSourceFiles")) {
true => b.addUpdateSourceFiles(),
false => b.addWriteFiles(),
};
wf.addCopyFileToSource(lib.getEmittedBin(), cfg.output_path);
wf.step.dependOn(&lib.step);
b.getInstallStep().dependOn(&wf.step);
It seems to work, but I’m kinda disturbed by that fact that I’m utilizing a function meant for updating source files. Is there another way to do this?
You don’t really need to do all of those shenanigans, simply calling b.installArtifact(my_shared_lib)
will install it in zig-out/bin
next to the executable by default for Windows.
$ zig build -Dtarget=x86_64-windows
$ tree zig-out
zig-out
├── bin
│ ├── my-executable.exe
│ ├── my-executable.pdb
│ ├── my-shared-lib.dll
│ └── my-shared-lib.pdb
└── lib
└── my-shared-lib.lib
3 directories, 5 files
On non-Windows platform, you should keep things separate and let the distro maintainer (or the person installing) choose where to put the libraries and executables, as loading libraries is not relative to the executable, but the environment’s configuration (LD_PATH, ldconfig, etc.).
If you don’t want the files in zig-out
, you can change things with the -p
or --prefix
arguments:
$ zig build -Dtarget=x86_64-windows --prefix ./my-out
$ tree my-out
my-out
├── bin
│ ├── my-executable.exe
│ ├── my-executable.pdb
│ ├── my-shared-lib.dll
│ └── my-shared-lib.pdb
└── lib
└── my-shared-lib.lib
3 directories, 5 files
You could install arbitrary shared libraries next to your executable, but only do so for Windows.
if (exe.rootModuleTarget().os.tag == .windows) {
b.installBinFile("vendor/GLFW3/lib/glfw3.dll", "glfw3.dll");
}
On Linux, macos, etc. assume that the library is available through a package manager, or link against the static version to prevent problems when distributing programs.
In an ideal situation, you should think about integrating the Zig build system for the libraries you are depending on and explicitly use them statically on all platforms.