Thread: BUG #17967: [msvc-x86] Failed to build with error LNK2019: unresolved external symbol __BitScanReverse64 referen

The following bug has been logged on the website:

Bug reference:      17967
Logged by:          Youmiu Mo
Email address:      moyoumiu@outlook.com
PostgreSQL version: 15.3
Operating system:   Windows
Description:

The file `src/bin/pg_verifybackup/pg_verifybackup.c` fails to compile using
msvc under x86, there is no `_BitScanForward64` and `_BitScanReverse64`
under **x86** for head `src/include/port/pg_bitutils.h`.

### output
```log
F:\repo\postgresql\src\include\port\pg_bitutils.h(88,13): warning C4013:
'_BitScanReverse64' undefined; assuming extern returning int (compiling
source file src/bin/pg_verifybackup/pg_verifybackup.c)
[F:\repo\postgresql\pg_verifybackup.vcxproj]
pg_verifybackup.obj : error LNK2019: unresolved external symbol
__BitScanReverse64 referenced in function _manifest_files_compute_parameters
[F:\repo\postgresql\pg_verifybackup.vcxproj]
```

### related lines

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/port/pg_bitutils.h;hb=83a611a259d41e305364b8de58f6b33a63cbda66#l88
 

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/port/pg_bitutils.h;hb=83a611a259d41e305364b8de58f6b33a63cbda66#l159
 

### references
https://learn.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64?view=msvc-170#requirements
 
https://learn.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=msvc-170#requirements
 

### steps to reproduce
```cmd
"C:\Program Files\Microsoft Visual
Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat" -host_arch=x86
-arch=x86
cd F:\repo\postgresql\src\tools\msvc
set PATH=F:\repo\tools;F:\tools\gnuwin32\bin;%PATH%
build RELEASE
cd F:\repo\postgresql
msbuild /m /p:Platform=Win32 /p:Configuration=Release
/p:PlatformToolset=v143 /p:WindowsTargetPlatformVersion=10.0.22621.0
pgsql.sln /verbosity:normal /t:Rebuild 2>&1
```


On Wed, 7 Jun 2023 at 23:26, PG Bug reporting form
<noreply@postgresql.org> wrote:
> The file `src/bin/pg_verifybackup/pg_verifybackup.c` fails to compile using
> msvc under x86, there is no `_BitScanForward64` and `_BitScanReverse64`
> under **x86** for head `src/include/port/pg_bitutils.h`.

I guess not many people are building on 32-bit MSVC these days.

I've not tested it, but I imagine the attached patch should fix it.

The ARM-64 part I included in the patch is just future-proofing.
Maybe I shouldn't bother with it.

David

Attachment
On Thu, 8 Jun 2023 at 01:00, David Rowley <dgrowleyml@gmail.com> wrote:
> I've not tested it, but I imagine the attached patch should fix it.

I've just pushed that patch to master only.

I noticed your report mentioned that this was for version 15.3. I
assumed this was a mistake as I didn't see any usage of the bit
scanning functions in v15. Commit 677319746 [1] introduced these,
which is new to v16.

Many thanks for reporting this!

David

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=677319746



Thanks for your replies and corrections, and apologies for the confusing version I mentioned.

For this commit, PostgreSQL was successfully built with msvc on both x86 and amd64.

Glad my report was helpful.

Mo


From: David Rowley <dgrowleyml@gmail.com>
Sent: Thursday, June 8, 2023 6:18
To: moyoumiu@outlook.com <moyoumiu@outlook.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #17967: [msvc-x86] Failed to build with error LNK2019: unresolved external symbol __BitScanReverse64 referen
 
On Thu, 8 Jun 2023 at 01:00, David Rowley <dgrowleyml@gmail.com> wrote:
> I've not tested it, but I imagine the attached patch should fix it.

I've just pushed that patch to master only.

I noticed your report mentioned that this was for version 15.3. I
assumed this was a mistake as I didn't see any usage of the bit
scanning functions in v15. Commit 677319746 [1] introduced these,
which is new to v16.

Many thanks for reporting this!

David

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=677319746
Hello Mo Youmiu,

On 2023-Jun-08, Mo Youmiu wrote:

> Thanks for your replies and corrections, and apologies for the confusing version I mentioned.
> 
> For this commit, PostgreSQL was successfully built with msvc on both x86 and amd64.
> 
> Glad my report was helpful.

You know what else would be extremely helpful?  A buildfarm animal that
runs with this configuration.  We should have noticed this problem as
soon as that problematic commit was introduced; a buildfarm animal would
have alerted us to it pretty much immediately rather than four months
after the fact.  Would you be in a position to running one?  It's a
moderately easy task, you just need to keep a machine (or VM) running
permanently.  You can find instructions here:
https://wiki.postgresql.org/wiki/PostgreSQL_Buildfarm_Howto

Regards

-- 
Álvaro Herrera