A unary minus operator was applied to an unsigned type - conversion from 64 Bit to 32 Bit

Gerhard Hill hillgerhard at web.de
Fri Aug 23 16:59:56 CEST 2024


Dear ladies and gentlemen,

after importing the package MPFR 4.2 i wrote this trivial program:

#include <iostream>
#include <mpfr.h>
int main()
{
  std::cout << "Hello World!" << std::endl ;
  return 0 ;
}

When compiling it, i get the error message:

1>------ Erstellen gestartet: Projekt: MPFRTest, Konfiguration: Debug x64 ------
1>MPFRTest.cpp
1>C:\Tools\vcpkg\installed\x64-windows\include\gmp.h(1792,19): warning C4244: "return": Konvertierung von "mp_limb_t" in "unsigned long", möglicher Datenverlust
1>(Quelldatei „MPFRTest.cpp“ wird kompiliert)
1>C:\Tools\vcpkg\installed\x64-windows\include\gmp.h(2237,16): error C4146: Einem vorzeichenlosen Typ wurde ein unärer Minus-Operator zugewiesen. Das Ergebnis ist weiterhin vorzeichenlos.
1>(Quelldatei „MPFRTest.cpp“ wird kompiliert)
1>Die Erstellung des Projekts "MPFRTest.vcxproj" ist abgeschlossen -- FEHLER.
========== Build: 0 erfolgreich, 1 Fehler, 0 aktuell, 0 übersprungen ==========

To ease things, i translate the essentials of the error message to English:
WARNING: Type "mp_limb_t" will be converted to "unsigned long", loss of data (loss of precision) is possible.
ERROR: A unary minus operator was applied to an unsigned type. The result is still unsigned.

i think it is the warning that needs the attention here: Here probably "mp_limb_t" is an unsigned 64 Bit, but "unsigned long" is only 32 Bit.

i fetched the MPFR package with the command line

.\vcpkg install mpfr

from a Windows Power Shell, which to my knowledge is the recommended procedure. During the install i got this message, which might or might not be related:

...

-- Building x64-windows-rel
-- Installing x64-windows-rel
CMake Warning at scripts/cmake/vcpkg_copy_pdbs.cmake:44 (message):
  Could not find a matching pdb file for:
      C:/Tools/vcpkg/packages/mpfr_x64-windows/bin/mpfr-6.dll
      C:/Tools/vcpkg/packages/mpfr_x64-windows/debug/bin/mpfr-6.dll
Call Stack (most recent call first):
  ports/mpfr/portfile.cmake:23 (vcpkg_copy_pdbs)
  scripts/ports.cmake:192 (include)

-- Fixing pkgconfig file: C:/Tools/vcpkg/packages/mpfr_x64-windows/lib/pkgconfig/mpfr.pc
-- Fixing pkgconfig file: C:/Tools/vcpkg/packages/mpfr_x64-windows/debug/lib/pkgconfig/mpfr.pc
-- Installing: C:/Tools/vcpkg/packages/mpfr_x64-windows/share/mpfr/usage
-- Performing post-build validation
Stored binaries in 1 destinations in 314 ms.
Elapsed time to handle mpfr:x64-windows: 1.3 h
mpfr:x64-windows package ABI: fca3f09545e96d5fdd698851daf4a95abf4d97d09d23a2b66dde05176be58d52
Total install time: 4.1 h
The package mpfr can be imported via CMake FindPkgConfig module:
    find_package(PkgConfig)
    pkg_check_modules(mpfr REQUIRED IMPORTED_TARGET mpfr)
    target_link_libraries(main PRIVATE PkgConfig::mpfr)


Vincent Lefèvre from INRIA, France, one of the authors of MPFR, to whom i presented the matter before, thinks that this constitutes a bug in gmp.h

i am using Microsoft Visual Studio Community 2022 (64-Bit) - Current Version 17.11.1 (supporting C++23), under Windows 10.0.19045.4780 .


Thank you very much,
Gerhard Hill



More information about the gmp-bugs mailing list