Unable to link winsdk on windows

import WinSDK.User
import Swift

public extension String {
    var LPCWSTR: [UInt16] {
        return self.withCString(encodedAs: UTF16.self) { buffer in
            Array<UInt16>(unsafeUninitializedCapacity: self.utf16.count + 1) {
                wcscpy_s($0.baseAddress, $0.count, buffer)
                $1 = $0.count
            }
        }
    }
}

MessageBoxW(UnsafeMutablePointer<HWND__>(nil), "Swift".LPCWSTR, "Swift".LPCWSTR, UINT(MB_OK));

Building the simple winsdk project above outputs the following error:

lld-link: error: undefined symbol: __declspec(dllimport) MessageBoxW
>>> referenced by C:\Users\x1398\CLionProjects\SwiftNewTest\.build\x86_64-unknown-windows-msvc\debug\SwiftNewTest.build\main.swift.o:(main)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[3/4] Linking C:\Users\x1398\CLionProjects\SwiftNewTest\.build\x86_64-unknown-windows-msvc\debug\SwiftNewTest.exe

It seems that it can't locate user32.lib.
PS: Module maps are copied as guided in the x64 native prompt.

If you tell the target in your Package.swift that it must link User32, it should work.
See example below:

.target(
	name: "foo",
	dependencies: [],
	linkerSettings: [.linkedLibrary("User32")]
),

Ah, this actually seems like something we should fix up in the module map definition, but, as @Daniel_Mullenborn points out, there is an easy fix. It is just the autolinking which is not setup properly.

Thanks a lot. @compnerd @Daniel_Mullenborn

Terms of Service

Privacy Policy

Cookie Policy