Having error compiling a simple hello.swift on Windows 10

Installed Swift 5.7 on Windows 10 according to Swift.org - Getting Started.

Here's simple swift file:

import Foundation
print("Hello world")

Having following error compiling this:

swift hello.swift
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "AssertionReporting.h"
         ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:10: note: in file included from C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:
#include "SwiftStdint.h"
         ^
C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/SwiftStdint.h:28:10: error: 'stdint.h' file not found
#include <stdint.h>
         ^
<unknown>:0: error: could not build C module 'SwiftShims'

Swift version:

swift --version
compnerd.org Swift version 5.7.1 (swift-5.7.1-RELEASE)
Target: x86_64-unknown-windows-msvc

Any tips to solve this?

Regards,
Valeriy Van

This is expected as you are not compiling the code.

Compiling it would be:

swiftc -emit-executable -o exe.exe hello.swift

Same error running swiftc -emit-executable -o exe.exe hello.swift:

swiftc -emit-executable -o exe.exe hello.swift
e[1m<module-includes>:1:10: e[0me[0;1;30mnote: e[0me[1min file included from <module-includes>:1:
e[0m#include "AssertionReporting.h"
e[0;1;32m         ^
e[0me[1mC:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:10: e[0me[0;1;30mnote: e[0me[1min file included from C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:
e[0m#include "SwiftStdint.h"
e[0;1;32m         ^
e[0me[1mC:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/SwiftStdint.h:28:10: e[0me[0;1;31merror: e[0me[1m'stdint.h' file not found
e[0m#include <stdint.h>
e[0;1;32m         ^
e[0me[1m<unknown>:0: e[0me[0;1;31merror: e[0me[1mcould not build C module 'SwiftShims'
e[0m

That is far more interesting! Do you have more than one Visual Studio installation? If not, it sounds like the module map deployment failed.

It looks like I have one Visual Studio installed in c:\Program Files and one in c:\Program Files (x86). Not sure if one of these two is alias of another.

They are not.

This is a known limitation of the Swift distribution at the moment. In the case that you have more than one Visual Studio installation, make sure that you are running from the "x64 Developer Command Prompt" associated with the Visual Studio installation that you want to use. Otherwise, it cannot determine which version to use and will choose the none option - which ensures that the user doesn't get surprised with a random selection.

Nothing changes:

** Visual Studio 2022 Developer Command Prompt v17.4.1
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************

C:\Program Files\Microsoft Visual Studio\2022\Community>cd c:\Users\1\Documents

c:\Users\1\Documents>dir
 Volume in drive C is SYSTEM
 Volume Serial Number is E64B-DD47

 Directory of c:\Users\1\Documents

21.11.2022  15:49    <DIR>          .
21.11.2022  15:49    <DIR>          ..
14.11.2022  18:07    <DIR>          Dell
18.11.2022  12:43                45 hello.swift
18.11.2022  14:53    <DIR>          Visual Studio 2022
18.11.2022  17:22    <DIR>          Настроювані шаблони Office
               1 File(s)             45 bytes
               5 Dir(s)  67 538 096 128 bytes free

c:\Users\1\Documents>swiftc -emit-executable -o exe.exe hello.swift
e[1m<module-includes>:1:10: e[0me[0;1;30mnote: e[0me[1min file included from <module-includes>:1:
e[0m#include "AssertionReporting.h"
e[0;1;32m         ^
e[0me[1mC:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:10: e[0me[0;1;30mnote: e[0me[1min file included from C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:
e[0m#include "SwiftStdint.h"
e[0;1;32m         ^
e[0me[1mC:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/SwiftStdint.h:28:10: e[0me[0;1;31merror: e[0me[1m'stdint.h' file not found
e[0m#include <stdint.h>
e[0;1;32m         ^
e[0me[1m<unknown>:0: e[0me[0;1;31merror: e[0me[1mcould not build C module 'SwiftShims'
e[0m

@adam-fowler What is the Visual Studio Code extension for Swift doing in this regard on Windows? I would guess that mutiple Visual Studio installations would bring the same problem, as a call through the "x64 Developer Command Prompt" does not seem possible from within the extension? So for the Visual Studio Code extension it should maybe be documented (for now) that on Windows multiple installations of Visual Studio should be avoided.

You’re right. Extensions can only take the environment from the main code.exe process, which makes Visual Studio toolchain really awkward to use in spite that it’s a “1st party” stuff. In most cases (unless you call code from a developer environment with no running instance), that’s the system default environment, so none would be chosen by default.

I have proposed somewhere (in a SwiftPM issue I guess?) that we’d better integrate Visual Studio installation knowledge into SwiftPM or Swift Driver, and that’s achievable through the vswhere tool. This should make VS detection and selection easier.

Any suggestion how would I make Swift on Windows 10 working?

Then it is likely that the deployment did not work. If you updated the Visual Studio installation after installing Swift, you will need to repair the installation (you should be able to do that from the "Add or Remove Programs" dialog). Otherwise, could you please provide the install log? You should be able to get that by uninstalling Swift and re-installing it and passing /lv*x install.log to installer.exe.

The VS Code swift extension runs swift in the same manner as if it was run from a command prompt. If you installation doesn't from the command prompt it won't work from the extension either.

I'm not sure it is the job of the extension to document install issues. These should possibly be in the Windows install section of swift.org. @compnerd what do you think about adding details about these kind of limitations there.

I my case uninstalling Visual Studio and Swift and re-installing Visual Studio 2022 and Swift 5.7.1 from scratch solved the issue.

Running swift hello.swift I got error:

swift hello.swift
<unknown>:0: error: could not load the swift standard library

But running swiftc works fine:

swiftc -emit-executable -o hello.exe hello.swift
   Creating library hello.lib and object hello.exp

Generated executable works as expected:

hello
Hello world

Now I could experiment with Swift on Windows.

Thanks for helping.

Regards,
Valeriy Van