How to correctly compile static libraries on Windows?

╰─❯ .\libc.exe E:\scoop\global\apps\zig-dev\0.13.0-dev.30\lib -includes -target x86_64-windows-msvc
Installation.find: found from KitsRoot10
windows10sdk: C:\Program Files (x86)\Windows Kits\10 version: 10.0.19041.0
dll path: C:\ProgramData\Microsoft\VisualStudio\Setup\x86\Microsoft.VisualStudio.Setup.Configuration.Native.dll
instances path from CLSID: C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances
failed to find instances dir via CLSID
trying C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances
error: unable to detect libc for target x86_64-windows.win8_1...win10_fe-msvc: LibCStdLibHeaderNotFound

Microsoft provides a tool to find the installation location of VS vswhere

╰─❯ vswhere -products "Microsoft.VisualStudio.Product.BuildTools" -latest -property "installationPath"
E:\install\Microsoft Visual Studio\2019\BuildTools

I take it C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances doesn’t exist for you?

Thanks for providing this, I was going to ask for vswhere’s output. The intention is for Zig to do the same thing as vswhere.

Previously, Zig used ISetupConfiguration via COM which is exactly what vswhere uses. The relevant PR is windows_sdk.zig: Reinstate COM ISetupEnumInstances logic by squeek502 · Pull Request #16594 · ziglang/zig · GitHub

However, using the COM interface meant a dependency on ole32.dll which incurs a startup cost, so Zig instead tries to emulate what ISetupConfiguration does, which (AFAICT) is reading <instance id>\state.json from the C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances directory. The relevant PR is Remove Zig's internal depedency on `shell32.dll` and `ole32.dll` by squeek502 · Pull Request #18091 · ziglang/zig · GitHub

Out of curiosity, could you try running this old version of libc_only.exe from PR 16594?

https://github.com/ziglang/zig/files/12206450/libc_only-20230728.zip

No need to give it any arguments, just run libc_only.exe. That version still used the COM ISetupConfiguration interface, so in theory it should detect your installation.

The tricky part now is to figure out what vswhere/ISetupConfiguration is doing that allows it to detect your installation.

2 Likes
╰─❯ .\libc_only.exe
[debug] Found version: 16.11.32929.386 (4503649030046082L)

# The directory that contains `stdlib.h`.
# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
include_dir=C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt

# The system-specific include directory. May be the same as `include_dir`.
# On Windows it's the directory that includes `vcruntime.h`.
# On POSIX it's the directory that includes `sys/errno.h`.
sys_include_dir=E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\include

# The directory that contains `crt1.o` or `crt2.o`.
# On POSIX, can be found with `cc -print-file-name=crt1.o`.
# Not needed when targeting MacOS.
crt_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64

# The directory that contains `vcruntime.lib`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\Lib\x64

# The directory that contains `kernel32.lib`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64

# The directory that contains `crtbeginS.o` and `crtendS.o`
# Only needed when targeting Haiku.
gcc_dir=
1 Like

Something that would be really helpful if you’re up for it:

  • Download Process Monitor if you don’t already have it
  • Run it and filter for Process name is libc_only.exe then Include
  • Run libc_only.exe (many events should show up in Process Monitor)
  • File → Save
    • Make sure “Events displayed using current filter” is selected
    • Choose CSV as the format
  • Open the CSV and delete the part at the top where it prints out your environment variables
  • Rename the file to .txt so this forum will accept it and include it in a post here
1 Like

Logfile.txt (72.2 KB)

1 Like

Perfect, thanks. From a quick look it looks like it’s querying the registry for HKLM\SOFTWARE\Microsoft\VisualStudio\Setup\CachePath, which contains the path E:\install\Microsoft Visual Studio\Packages, and then it’s reading state.json from E:\install\Microsoft Visual Studio\Packages\_Instances\ef61ef51\state.json.

Zig doesn’t check/use that registry key, so that’s likely the issue.

Really appreciate all the info you’ve been able to provide! I should be able to put together a PR with a fix soon.

5 Likes

PR with a potential fix created: WindowsSdk: Fix finding the _Instances directory when it's not in the default location by squeek502 · Pull Request #19926 · ziglang/zig · GitHub

@aiac if you’d like to confirm it’ll fix this for you:

https://www.ryanliptak.com/misc/libc-pr19926.zip

Run with:

.\libc.exe E:\scoop\global\apps\zig-dev\0.13.0-dev.30\lib -includes -target x86_64-windows-msvc
1 Like

Unfortunately

╰─❯ .\libc.exe E:\scoop\global\apps\zig-dev\0.13.0-dev.30\lib -includes -target x86_64-windows-msvc
error: unable to detect libc for target x86_64-windows.win8_1...win10_fe-msvc: LibCStdLibHeaderNotFound
╭╴🪟 $psh me\test-zig\miniaudio.zig via ↯ v0.13.0-dev.30+6fd09f8d2
╰─❯ cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30146 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]
╭╴🪟 $psh me\test-zig\miniaudio.zig via ↯ v0.13.0-dev.30+6fd09f8d2
╰─❯ echo $env:INCLUDE
E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\include;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt
╭╴🪟 $psh me\test-zig\miniaudio.zig via ↯ v0.13.0-dev.30+6fd09f8d2
╰─❯ echo $env:LIB
E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\lib\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64
1 Like

Could you open the Registry Editor (or run regedit.exe) and enter

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\Setup

in the top bar?

Does CachePath show as a folder within Setup or as a REG_SZ value in the right pane?

1 Like

Turns out there was a second bug that this has exposed.

Give this a shot:

https://www.ryanliptak.com/misc/libc-pr19926-take2.zip

2 Likes

Is it solved? Congratulations

╰─❯ .\libc.exe E:\scoop\global\apps\zig-dev\0.13.0-dev.30\lib -includes -target x86_64-windows-msvc
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt
E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\include
E:\install\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\atlmfc\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared
2 Likes

Looks good to me! Thanks again for all the info/testing.

3 Likes

The amount of hoops you have to jump through to find where visual Studio is installed is mind blowing.

6 Likes