for added context look at: Linking a prebuilt MSVC .lib with Zig's new build system
im getting closer to my goal. after doing the changes as suggested (making changes so that the library is compiled without /GL flag).
right now my build.zig looks like:
const std = @import("std");
const targets = [_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc },
.{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .msvc },
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{
.whitelist = &targets,
.default_target = targets[0],
});
const optimize = b.standardOptimizeOption(.{});
const tsk_dep = switch (target.result.cpu.arch) {
.x86_64 => b.lazyDependency("tsk-x86_64-windows", .{}) orelse return,
.aarch64 => b.lazyDependency("tsk-aarch64-windows", .{}) orelse return,
else => unreachable,
};
const tsk_lib_path = tsk_dep.path("x64/Release_NoLibs/libtsk.lib");
const tsk_tc = b.addTranslateC(.{
.root_source_file = tsk_dep.path("include/tsk/libtsk.h"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
tsk_tc.addIncludePath(tsk_dep.path("include"));
tsk_tc.defineCMacro("WIN32_LEAN_AND_MEAN", "1");
tsk_tc.defineCMacro("NOMINMAX", "1");
tsk_tc.defineCMacro("_MSC_VER", "1950");
tsk_tc.defineCMacro("VC_EXTRALEAN", "1");
tsk_tc.defineCMacro("__INTRIN_H", null);
tsk_tc.defineCMacro("_MODE_T_", null);
tsk_tc.defineCMacro("_INC_SHELLAPI", null);
const tsk_mod = tsk_tc.createModule();
tsk_mod.addObjectFile(tsk_lib_path);
const mod = b.addModule("rere_zig", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
const exe = b.addExecutable(.{
.name = "rere_zig",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "rere_zig", .module = mod },
.{
.name = "tsk",
.module = tsk_mod,
},
},
.link_libc = true,
.link_libcpp = true,
}),
});
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
const out_dir = b.fmt("{s}-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag), @tagName(target.result.abi) });
const target_output = b.addInstallArtifact(exe, .{
.dest_dir = .{
.override = .{
.custom = out_dir,
},
},
});
b.getInstallStep().dependOn(&target_output.step);
}
but when i run zig build run i get errors, here’s a snippet
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:303:57: error: call to undeclared function '_mm_movemask_epi8'; ISO C99 and later do not support implicit function declarations
unsigned short __mask = (unsigned short)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:303:75: error: use of undeclared identifier '__v1'
unsigned short __mask = (unsigned short)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:304:21: error: use of undeclared identifier '__mask'
if (__mask != 0)
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:316:17: error: use of undeclared identifier '__m128i'
__m128i __v1 = _mm_loadu_si64(__s);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:317:17: error: use of undeclared identifier '__v1'
__v1 = _mm_cmpeq_epi16(__v1, __v2);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:303:57: note: previous definition is here
unsigned short __mask = (unsigned short)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:318:73: error: use of undeclared identifier '__v1'
unsigned char __mask = (unsigned char)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:319:21: error: use of undeclared identifier '__mask'
if (__mask != 0)
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:405:17: error: use of undeclared identifier '__m256i'
__m256i __v1 = _mm256_loadu_si256((__m256i const*)__s1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:406:17: error: use of undeclared identifier '__m256i'
__m256i __v2 = _mm256_loadu_si256((__m256i const*)__s2);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:407:17: error: use of undeclared identifier '__v1'
__v1 = _mm256_cmpeq_epi16(__v1, __v2);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:282:53: note: previous definition is here
unsigned int __mask = (unsigned int)_mm256_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:408:74: error: use of undeclared identifier '__v1'
unsigned int __mask = (unsigned int)_mm256_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:409:21: error: use of undeclared identifier '__mask'
if (__mask != 0xffffffff)
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:424:13: error: use of undeclared identifier '__m128i'
__m128i __v1 = _mm_loadu_si128((__m128i const*)__s1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:425:13: error: use of undeclared identifier '__m128i'
__m128i __v2 = _mm_loadu_si128((__m128i const*)__s2);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:426:13: error: use of undeclared identifier '__v1'
__v1 = _mm_cmpeq_epi16(__v1, __v2);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:303:57: note: previous definition is here
unsigned short __mask = (unsigned short)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:427:71: error: use of undeclared identifier '__v1'
unsigned short __mask = (unsigned short)_mm_movemask_epi8(__v1);
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\wchar.h:428:17: error: use of undeclared identifier '__mask'
if (__mask != 0xffff)
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstring.h:200:1: note: 'wcsncpy' has been explicitly marked deprecated here
__DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt.h:1974:17: note: expanded from here
_CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType __cdecl _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2);
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:368:55: note: expanded from here
#define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:358:47: note: expanded from here
#define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstring.h:178:1: note: 'wcsncat' has been explicitly marked deprecated here
__DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt.h:1974:17: note: expanded from here
_CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType __cdecl _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2);
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:368:55: note: expanded from here
#define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:358:47: note: expanded from here
#define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstring.h:220:16: note: 'wcstok' has been explicitly marked deprecated here
_Check_return_ _CRT_INSECURE_DEPRECATE(wcstok_s)
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:368:55: note: expanded from here
#define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:358:47: note: expanded from here
#define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstring.h:343:1: note: '_wcsnset' has been explicitly marked deprecated here
__DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt.h:1974:17: note: expanded from here
_CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType __cdecl _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1, _TType2 _TArg2);
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:368:55: note: expanded from here
#define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:358:47: note: expanded from here
#define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstring.h:367:1: note: '_wcsset' has been explicitly marked deprecated here
__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt.h:1933:17: note: expanded from here
_CRT_INSECURE_DEPRECATE(_SecureFuncName) _DeclSpec _ReturnType __cdecl _FuncName(_SalAttributeDst _DstType *_Dst, _TType1 _TArg1);
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:368:55: note: expanded from here
#define _CRT_INSECURE_DEPRECATE(_Replacement) _CRT_DEPRECATE_TEXT(
^
C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.50.35717\include\vcruntime.h:358:47: note: expanded from here
#define _CRT_DEPRECATE_TEXT(_Text) __declspec(deprecated(_Text))
^
D:\all\prog_elec_phil\projects\ReRecycleBin\mytry\zig\zig-pkg\N-V-__8AAJKMgECOnRHKfhYFbZ_1VEbR44LiApBSCs1slEct\include\tsk/base/tsk_base.h:114:9: error: unknown type name 'CRITICAL_SECTION'
CRITICAL_SECTION critical_section;
^
D:\all\prog_elec_phil\projects\ReRecycleBin\mytry\zig\zig-pkg\N-V-__8AAJKMgECOnRHKfhYFbZ_1VEbR44LiApBSCs1slEct\include\tsk/base/tsk_base.h:455:21: error: redefinition of 'BYTE' as different kind of symbol
typedef uint8_t BYTE;
^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winreg.h:1146:40: note: previous definition is here
_In_reads_bytes_opt_(cbData) CONST BYTE* lpData,
^
error: 12761 compilation errors
failed command: "C:\\Users\\AUTO\\terminal\\zigup-x86_64-windows\\zig\\0.17.0-dev.87+9b177a7d2\\files\\zig.exe" translate-c -lc --cache-dir .zig-cache --global-cache-dir "C:\\Users\\AUTO\\AppData\\Local\\zig" -target x86_64-windows-msvc -I "D:\\all\\prog_elec_phil\\projects\\ReRecycleBin\\mytry\\zig\\zig-pkg\\N-V-__8AAJKMgECOnRHKfhYFbZ_1VEbR44LiApBSCs1slEct\\include" -D WIN32_LEAN_AND_MEAN=1 -D NOMINMAX=1 -D _MSC_VER=1950 -D VC_EXTRALEAN=1 -D __INTRIN_H=1 -D _MODE_T_=1 -D _INC_SHELLAPI=1 "D:\\all\\prog_elec_phil\\projects\\ReRecycleBin\\mytry\\zig\\zig-pkg\\N-V-__8AAJKMgECOnRHKfhYFbZ_1VEbR44LiApBSCs1slEct\\include\\tsk\\libtsk.h" --listen=-
Build Summary: 0/6 steps succeeded (1 failed)
run transitive failure
└─ run exe rere_zig transitive failure
├─ compile exe rere_zig Debug x86_64-windows-msvc transitive failure
│ └─ translate-c 12761 errors
└─ install transitive failure
└─ install rere_zig transitive failure
└─ compile exe rere_zig Debug x86_64-windows-msvc (+1 more reused dependencies)
error: the following build command failed with exit code 1:
.zig-cache\o\0cdbc9bf414eb16515ce3805fa51d48a\build.exe C:\Users\AUTO\terminal\zigup-x86_64-windows\zig\0.17.0-dev.87+9b177a7d2\files\zig.exe C:\Users\AUTO\terminal\zigup-x86_64-windows\zig\0.17.0-dev.87+9b177a7d2\files\lib D:\all\prog_elec_phil\projects\ReRecycleBin\mytry\zig .zig-cache C:\Users\AUTO\AppData\Local\zig --seed 0xceba08ae -Z129f8133b8f9a670 run
D:\all\prog_elec_phil\projects\ReRecycleBin\mytry\zig> 1 14/06/26 00:49:0
gpt says the problem is that translate-c isnt able to parse the windows.h properly specifically the __ annotations, (MSVC SAL annotations) and these errors occur. the solution to this as gpt says is to replace msvc as abi with gnu but only for translate step in build.zig like this:
const std = @import("std");
const targets = [_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc },
.{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .msvc },
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{
.whitelist = &targets,
.default_target = targets[0],
});
const optimize = b.standardOptimizeOption(.{});
const tsk_dep = switch (target.result.cpu.arch) {
.x86_64 => b.lazyDependency("tsk-x86_64-windows", .{}) orelse return,
.aarch64 => b.lazyDependency("tsk-aarch64-windows", .{}) orelse return,
else => unreachable,
};
const tc_target = b.resolveTargetQuery(.{
.cpu_arch = target.result.cpu.arch,
.os_tag = .windows,
.abi = .gnu, // <--- Force GNU here to bypass MSVC SAL annotations
});
const tsk_lib_path = tsk_dep.path("x64/Release_NoLibs/libtsk.lib");
const tsk_tc = b.addTranslateC(.{
.root_source_file = tsk_dep.path("include/tsk/libtsk.h"),
.target = tc_target,
.optimize = optimize,
.link_libc = true,
});
tsk_tc.addIncludePath(tsk_dep.path("include"));
tsk_tc.defineCMacro("_MSC_VER", "1950");
tsk_tc.defineCMacro("NOMINMAX", null);
tsk_tc.defineCMacro("WIN32_LEAN_AND_MEAN", null);
tsk_tc.defineCMacro("VC_EXTRALEAN", null);
tsk_tc.defineCMacro("__INTRIN_H", null);
tsk_tc.defineCMacro("_MODE_T_", null);
tsk_tc.defineCMacro("_INC_SHELLAPI", null);
const tsk_mod = tsk_tc.createModule();
tsk_mod.addObjectFile(tsk_lib_path);
const mod = b.addModule("rere_zig", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
const exe = b.addExecutable(.{
.name = "rere_zig",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "rere_zig", .module = mod },
.{
.name = "tsk",
.module = tsk_mod,
},
},
.link_libc = true,
}),
});
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
const out_dir = b.fmt("{s}-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag), @tagName(target.result.abi) });
const target_output = b.addInstallArtifact(exe, .{
.dest_dir = .{
.override = .{
.custom = out_dir,
},
},
});
b.getInstallStep().dependOn(&target_output.step);
}
and it works.
this workaround feels extremely tacky and feels like this’ll explode when im halfway done with my project. is there any other solution to this?