Thread overview
Linker errors to Windows functions
Aug 20, 2019
Vladimirs Nordholm
Aug 20, 2019
Dennis
Aug 20, 2019
Vladimirs Nordholm
August 20, 2019
Hello.

I recently reinstalled Windows 10 (build 1903), and downloaded DMD (v2.087.1) and dub (v1.16.0).

My project no longer compiles, giving the following errors:

    error LNK2019: unresolved external symbol GetSystemMetrics referenced in [...]
    error LNK2019: unresolved external symbol SetWindowPos referenced in function [...]

The function `GetSystemMetrics` and `SetWindowPos` can be found at https://github.com/dlang/druntime/blob/master/src/core/sys/windows/winuser.d , line 3874 and 4095 respectively.

In code I have `import core.sys.windows.winuser;`, but still get this error.

Any clues to why this might happen?
August 20, 2019
On Tuesday, 20 August 2019 at 17:17:01 UTC, Vladimirs Nordholm wrote:
> In code I have `import core.sys.windows.winuser;`, but still get this error.

Importing only specifies that you expect the symbols to be there, it doesn't mean the functions are linked in.

On Windows there are three targets, 32-bit OMF (old dmd default), 32-bit MSCoff and 64-bit MSCoff (dub defaults to MSCoff since not too long ago). While DMD ships with import libraries for all targets, I never know which ones get linked in by default and which ones don't.

Usually when I get Windows linking errors I either switch the target (the OMF import libraries are sometimes lacking, so then I compile with --arch=x86_mscoff or --arch=x86_64) or explicitly link the import libraries.
Just quickly Google a function like GetSystemMetrics:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
On the bottom it says "Library: User32.lib" so I add `pragma(lib, "User32.lib");` to my module using it.

August 20, 2019
On Tuesday, 20 August 2019 at 17:29:15 UTC, Dennis wrote:
> On Tuesday, 20 August 2019 at 17:17:01 UTC, Vladimirs Nordholm wrote:
>> [...]
>
> Importing only specifies that you expect the symbols to be there, it doesn't mean the functions are linked in.
>
> [...]

Thank you for the explanation Dennis.

Never done the `pragma(lib, ...)` before, but that definitely solved the problem!