# "Cannot load underlying module for WinSDK"

**URL:** <https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123>\
**Category:** Using Swift\
**Tags:** windows\
**Created:** [September 8, 2020, 6:29pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123 "2020-09-08T18:29:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![costinAndronache](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/costinandronache/32/13058_2.png) [@costinAndronache](https://forums.swift.org/u/costinAndronache)\
**Post date:** [September 8, 2020, 6:29pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/1 "2020-09-08T18:29:16Z")

</div>

I have a simple `main.swift`

```swift
import Foundation

print("imported")
```

With this batch file:

```swift
set SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
set SWIFTFLAGS=-sdk %SDKROOT% -I %SDKROOT%/usr/lib/swift -L %SDKROOT%/usr/lib/swift/windows
swiftc -v -o main.exe main.swift %SWIFTFLAGS%
```

And when I try to compile it using the 5.2.1 toolchain from the github `/swift-build` releases page it shows this error about not being able to load the underlying WinSDK module:

> E:\swiftCode\Project1\>mainExeCompile.bat  
> [compnerd.org](http://compnerd.org) Swift version 5.2.2-dev (Swift a543bfba1b)  
> Target: x86\_64-unknown-windows-msvc  
> "C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe" -frontend -c -primary-file main.swift -target x86\_64-unknown-windows-msvc -disable-objc-interop -sdk "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -color-diagnostics -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift" -autolink-library oldnames -autolink-library msvcrt -Xcc -D\_MT -Xcc -D\_DLL -module-name main -o "C:\Users\Costin\AppData\Local\Temp\main-bb5632.o"  
> e[1m:0: e[0me[0;1;31merror: e[0me[1mcannot load underlying module for 'WinSDK'  
> e[0m  
> E:\swiftCode\Project1\>

But if I try to import another say `import Swift` or `import SwiftOnoneSupport` it all compiles well:

> E:\swiftCode\Project1\>mainExeCompile.bat

E:\swiftCode\Project1\>set SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk

E:\swiftCode\Project1\>set SWIFTFLAGS=-sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows

E:\swiftCode\Project1\>swiftc -v -o main.exe main.swift -sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows  
[compnerd.org](http://compnerd.org) Swift version 5.2.2-dev (Swift a543bfba1b)  
Target: x86\_64-unknown-windows-msvc  
"C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe" -frontend -c -primary-file main.swift -target x86\_64-unknown-windows-msvc -disable-objc-interop -sdk "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -color-diagnostics -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift" -autolink-library oldnames -autolink-library msvcrt -Xcc -D\_MT -Xcc -D\_DLL -module-name main -o "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o"  
"C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\clang.exe" -target x86\_64-unknown-windows-msvc -nostartfiles -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows/x86\_64" -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift/x86\_64" "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86\_64\swiftrt.obj" "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o" -I "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk" -L "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows" -v -o main.exe  
[compnerd.org](http://compnerd.org) clang version 9.0.0 ([GitHub - apple/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. This fork is used to manage Apple’s stable releases of Clang as well as support the Swift project.](https://github.com/apple/llvm-project) c2133f2b08047708b17230b598cb789062143650) (based on LLVM 9.0.0)  
Target: x86\_64-unknown-windows-msvc  
Thread model: posix  
InstalledDir: C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin  
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\Hostx64\x64\link.exe" -out:main.exe "-libpath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\lib\x64" "-libpath:C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64" "-libpath:C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows/x86\_64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift/x86\_64" "-libpath:C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows" -nologo "C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86\_64\swiftrt.obj" "C:\Users\Costin\AppData\Local\Temp\main-d3bd06.o"  
Creating library main.lib and object main.exp  
E:\swiftCode\Project1\>

I've taken a look inside `WinSDK.swiftinterface` and noticed this strange line:

```swift
// swift-interface-format-version: 1.0
// swift-compiler-version: compnerd.org Swift version 5.2.2-dev (Swift a543bfba1b)
// swift-module-flags: -target x86_64-unknown-windows-msvc -disable-objc-interop -autolink-force-load -enable-library-evolution -module-link-name swiftWinSDK -swift-version 5 -O -enforce-exclusivity=unchecked -module-name WinSDK
import Swift
@_exported import WinSDK // <---- here 
public let HANDLE_FLAG_INHERIT: WinSDK.DWORD
```

Could this be the cause? I know this is just a generated interface file but how come is it valid as a module to import yourself?

I've attached the zipped `windows` folder in case someone wants to take a look on the import libs and swiftmodules here

> **[windows.zip](https://drive.google.com/file/d/1WELsulQ6OT0IOgw0ndbXCia8KJjJfKiW/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [September 8, 2020, 7:47pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/2 "2020-09-08T19:47:58Z")

</div>

There is a follow up step that needs to be performed manually after installation to make the Windows SDK accessible to swift.

You need to install the module maps into the Visual Studio installation. Because this modifies the Visual Studio installation, you need to run the following from an elevated ("Administrator") Native x64 Developer command prompt.

```nohighlight
set SDKROOT=%SystemDrive%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
copy "%SDKROOT%\usr\share\ucrt.modulemap" "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.modulemap" "%VCToolsInstallDir%\include\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.apinotes" "%VCToolsInstallDir%\include\visualc.apinotes"
copy "%SDKROOT%\usr\share\winsdk.modulemap" "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

```

---

<div class="post-metadata">

**Author:** ![cryo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cryo/32/13218_2.png) [@cryo](https://forums.swift.org/u/cryo)\
**Post date:** [September 23, 2020, 9:21am UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/3 "2020-09-23T09:21:06Z")

</div>

Note that the instructions on [https://www.reddit.com/r/swift/comments/ixx0bl/swift\_is\_now\_available\_for\_windows\_10/](https://www.reddit.com/r/swift/comments/ixx0bl/swift_is_now_available_for_windows_10/) are currently incorrect, since they have:

```
copy %SDKROOT%\usr\share\ucrt.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
copy %SDKROOT%\usr\share\visualc.modulemap "%VCToolsInstallDir%\include\module.modulemap"
copy %SDKROOT%\usr\share\ucrt.modulemap "%VCToolsInstallDir%\include\visualc.apinotes"
copy %SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

```

...and line 3 is wrong (has the wrong source file).

---

<div class="post-metadata">

**Author:** ![jessearmand](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jessearmand/32/5255_2.png) [@jessearmand](https://forums.swift.org/u/jessearmand)\
**Post date:** [October 17, 2020, 3:18pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/4 "2020-10-17T15:18:44Z")

</div>

There's another set of errors about `CoreFoundation` after installing the module maps:

```swift
C:\Users\jesse\Projects\swift-win32>ninja -C build SwiftWin32 UICatalog
ninja: Entering directory `build'
[1/2] Linking Swift shared library bin\SwiftWin32.dll
FAILED: bin/SwiftWin32.dll Sources/CMakeFiles/SwiftWin32.dir/Application/_TriviallyConstructible.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/Application.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/ApplicationMain.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/Information.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Application/LaunchKeyOptions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertAction.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AlertController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/AnimationCurve.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Button.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Color.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ContentContainer.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ContentSizeCategoryAdjusting.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Control.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/DatePicker.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Device.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/EdgeInsets.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Font.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/FontMetrics.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Label.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ProgressView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Responder.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Scene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneConfiguration.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneDelegate.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSession.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/SceneSizeRestrictions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Screen.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Slider.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Stepper.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Switch.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableViewCell.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TableViewDataSource.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextField.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextInputTraits.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TextView.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitCollection.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/TraitEnvironment.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/View.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewController.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinator.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/ViewControllerTransitionCoordinatorContext.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/Window.swift.obj Sources/CMakeFiles/SwiftWin32.dir/UI/WindowScene.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/AffineTransform.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Point.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Rect.swift.obj Sources/CMakeFiles/SwiftWin32.dir/CG/Size.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/BatteryMonitor.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Error.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/IndexPath+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Logging.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/Rect+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/PropertyWrappers.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/String+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowClass.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowsHandle.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WindowsHandle+UIExtensions.swift.obj Sources/CMakeFiles/SwiftWin32.dir/Support/WinSDK+Extensions.swift.obj swift/SwiftWin32.swiftmodule lib/SwiftWin32.lib
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map Sources\CMakeFiles\SwiftWin32.dir\output-file-map.json -incremental -j 12 -emit-library -o bin\SwiftWin32.dll -module-name SwiftWin32 -module-link-name SwiftWin32 -emit-module -emit-module-path swift\SwiftWin32.swiftmodule -emit-dependencies -DSwiftWin32_EXPORTS -sdk C:/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk -O -libc MD -Xcc -DNONAMELESSUNION -Xcc -DCOBJMACROS @CMakeFiles\SwiftWin32.rsp -Xlinker -implib:lib\SwiftWin32.lib && cd ."
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
..\Sources\Application\ApplicationDelegate.swift:8:14: error: missing required module 'CoreFoundation'
import class Foundation.NSNotification
             ^
ninja: build stopped: subcommand failed.

```

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [October 17, 2020, 6:45pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/5 "2020-10-17T18:45:15Z")

</div>

You missed some of the flags that you needed to pass or there is a typo in the flags. Either way, this is something that is expected for the time being. The newer snapshots actually have significantly improved things (thanks to help from @millenomi) by removing the need for the headers all together and further improvements to the installer to reduce the needed flags.

I noted that the project that you are using there is Swift/Win32, for which the current instructions are **not** compatible with 5.3 any longer - that project has moved onto the latest version of Swift development. For Swift/WIn32 specific issues though, I would recommend that you report issues to the project via GitHub as these forums are not for that project.

---

<div class="post-metadata">

**Author:** ![sspringer](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sspringer](https://forums.swift.org/u/sspringer)\
**Post date:** [May 10, 2021, 9:45pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/6 "2021-05-10T21:45:09Z")

</div>

@compnerd I am currently "suffering" from a similar error, the error as noted in [SR-14578](https://bugs.swift.org/browse/SR-14578), would be nice if this could be resolved soon... Thanks.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [May 10, 2021, 11:00pm UTC](https://forums.swift.org/t/cannot-load-underlying-module-for-winsdk/40123/7 "2021-05-10T23:00:58Z")

</div>

SR-14578 is different from this issue. This issue was due to the setup not being run through to completion. SR-14578 is due to a missing change in Foundation that should already be there in the 5.5 snapshots.
