Last active
February 9, 2025 13:29
-
-
Save doccaico/b259f32bfb2c6127ae89027b3f1a5c29 to your computer and use it in GitHub Desktop.
MSVC + DLL + Ziglang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn build(b: *std.Build) void { | |
const target = b.standardTargetOptions(.{}); | |
const optimize = b.standardOptimizeOption(.{}); | |
const lib_mod = b.createModule(.{ | |
.root_source_file = b.path("lib/mylib/mylib.zig"), | |
.target = target, | |
.optimize = optimize, | |
.link_libc = true, | |
}); | |
const exe_mod = b.createModule(.{ | |
.root_source_file = b.path("src/main.zig"), | |
.target = target, | |
.optimize = optimize, | |
}); | |
exe_mod.addImport("mylib", lib_mod); | |
const exe = b.addExecutable(.{ | |
.name = "using_dll_msvc", | |
.root_module = exe_mod, | |
.linkage = .dynamic, | |
}); | |
exe.addLibraryPath(b.path("./lib/mylib/windows")); | |
exe.linkSystemLibrary("mylib"); | |
// to copy the dll to the zig-out directory | |
b.installBinFile("./lib/mylib/windows/mylib.dll", "mylib.dll"); | |
b.installArtifact(exe); | |
const run_cmd = b.addRunArtifact(exe); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
if (b.args) |args| { | |
run_cmd.addArgs(args); | |
} | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.step); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const mylib = @import("mylib"); | |
pub fn main() !void { | |
std.debug.print("add(1, 2) = {d}\n", .{mylib.add(1, 2)}); | |
std.debug.print("add(10, 20) = {d}\n", .{mylib.add(10, 20)}); | |
std.debug.print("add(10, 20) = {d}\n", .{mylib.add(10, 21)}); | |
std.debug.print("add(10, 20) = {d}\n", .{mylib.add(555, 999)}); | |
} | |
// $ zig build run --summary all | |
// add(1, 2) = 3 | |
// add(10, 20) = 30 | |
// add(10, 20) = 31 | |
// add(10, 20) = 1554 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
REM create a .obj | |
cl /nologo /c mylib.c | |
REM create a .lib | |
lib /nologo /OUT:mylib.lib mylib.obj | |
REM create a .dll | |
link /nologo /DLL /OUT:mylib.dll mylib.obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment